From 845e8fb76a9737b2866941650dc38a05a9f6fdaa Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 27 May 2024 06:15:14 -0700 Subject: [PATCH] Clean up some mixins by using mixinextras @Local Allowing us to capture local variables removes the need for hacks in @Redirect working around lack of local variable capture. --- .../HolderCompletableFuture.java | 11 --- .../moonrise/common/util/MoonriseCommon.java | 4 +- .../chunk_system/ChunkGeneratorMixin.java | 27 ++------ .../chunk_system/EntityTickListMixin.java | 23 ++----- .../NoiseBasedChunkGeneratorMixin.java | 69 +++---------------- 5 files changed, 21 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/ca/spottedleaf/moonrise/common/real_dumb_shit/HolderCompletableFuture.java diff --git a/src/main/java/ca/spottedleaf/moonrise/common/real_dumb_shit/HolderCompletableFuture.java b/src/main/java/ca/spottedleaf/moonrise/common/real_dumb_shit/HolderCompletableFuture.java deleted file mode 100644 index aa423bf5..00000000 --- a/src/main/java/ca/spottedleaf/moonrise/common/real_dumb_shit/HolderCompletableFuture.java +++ /dev/null @@ -1,11 +0,0 @@ -package ca.spottedleaf.moonrise.common.real_dumb_shit; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -public class HolderCompletableFuture extends CompletableFuture { - - public final List toExecute = new ArrayList<>(); - -} diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java index 6f03c2c9..e5bee7b1 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/MoonriseCommon.java @@ -30,9 +30,9 @@ public final class MoonriseCommon { } WORKER_POOL = new PrioritisedThreadPool( - "Moonrise Chunk System Worker Pool", workerThreads, + "Moonrise Worker Pool", workerThreads, (final Thread thread, final Integer id) -> { - thread.setName("Moonrise Chunk System Worker #" + id.intValue()); + thread.setName("Moonrise Common Worker #" + id.intValue()); thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread thread, final Throwable throwable) { diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ChunkGeneratorMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ChunkGeneratorMixin.java index 21a234bf..0261c355 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ChunkGeneratorMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ChunkGeneratorMixin.java @@ -1,6 +1,7 @@ package ca.spottedleaf.moonrise.mixin.chunk_system; import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevelReader; +import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.StructureManager; import net.minecraft.world.level.chunk.ChunkAccess; @@ -21,7 +22,7 @@ public abstract class ChunkGeneratorMixin { /** - * @reason Pass the supplier to the mixin below so that we can change the executor to the parameter provided + * @reason Use the provided executor, chunk system sets this to something specific * @author Spottedleaf */ @Redirect( @@ -31,27 +32,9 @@ public abstract class ChunkGeneratorMixin { target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" ) ) - private CompletableFuture passSupplier(Supplier supplier, Executor executor) { - return (CompletableFuture)CompletableFuture.completedFuture(supplier); - } - - /** - * @reason Retrieve the supplier from the mixin above so that we can change the executor to the parameter provided - * @author Spottedleaf - */ - @Inject( - method = "createBiomes", - cancellable = true, - at = @At( - value = "RETURN" - ) - ) - private void unpackSupplier(Executor executor, RandomState randomState, Blender blender, - StructureManager structureManager, ChunkAccess chunkAccess, - CallbackInfoReturnable> cir) { - cir.setReturnValue( - CompletableFuture.supplyAsync(((CompletableFuture>)(CompletableFuture)cir.getReturnValue()).join(), executor) - ); + private CompletableFuture redirectBiomesExecutor(final Supplier supplier, final Executor badExecutor, + @Local(ordinal = 0, argsOnly = true) final Executor executor) { + return CompletableFuture.supplyAsync(supplier, executor); } /** diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/EntityTickListMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/EntityTickListMixin.java index 4a0aa625..621f13b3 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/EntityTickListMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/EntityTickListMixin.java @@ -2,6 +2,7 @@ import ca.spottedleaf.moonrise.common.list.IteratorSafeOrderedReferenceSet; import ca.spottedleaf.moonrise.common.util.TickThread; +import com.llamalad7.mixinextras.sugar.Local; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.entity.EntityTickList; @@ -68,25 +69,9 @@ private V hookAdd(final Int2ObjectMap instance, final int key, final V va return null; } - /** - * @reason Route to new entity list - * @author Spottedleaf - */ - @Inject( - method = "remove", - at = @At( - value = "INVOKE", - target = "Lit/unimi/dsi/fastutil/ints/Int2ObjectMap;remove(I)Ljava/lang/Object;", - remap = false - ) - ) - private void hookRemove(final Entity entity, final CallbackInfo ci) { - this.entities.remove(entity); - } - /** - * @reason Avoid NPE on accessing old state + * @reason Route to new entity list * @author Spottedleaf */ @Redirect( @@ -97,7 +82,9 @@ private void hookRemove(final Entity entity, final CallbackInfo ci) { remap = false ) ) - private V hookRemoveAvoidNPE(final Int2ObjectMap instance, final int key) { + private V hookRemoveAvoidNPE(final Int2ObjectMap instance, final int key, + final @Local(ordinal = 0, argsOnly = true) Entity entity) { + this.entities.remove(entity); return null; } diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/NoiseBasedChunkGeneratorMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/NoiseBasedChunkGeneratorMixin.java index 5126b38a..5297c1d5 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/NoiseBasedChunkGeneratorMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/NoiseBasedChunkGeneratorMixin.java @@ -1,6 +1,6 @@ package ca.spottedleaf.moonrise.mixin.chunk_system; -import ca.spottedleaf.moonrise.common.real_dumb_shit.HolderCompletableFuture; +import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.world.level.StructureManager; import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator; @@ -20,7 +20,7 @@ public abstract class NoiseBasedChunkGeneratorMixin { /** - * @reason Pass the supplier to the mixin below so that we can change the executor to the parameter provided + * @reason Use the provided executor, chunk system sets this to something specific * @author Spottedleaf */ @Redirect( @@ -30,32 +30,13 @@ public abstract class NoiseBasedChunkGeneratorMixin { target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" ) ) - private CompletableFuture passSupplierBiomes(Supplier supplier, Executor executor) { - return (CompletableFuture)CompletableFuture.completedFuture(supplier); + private CompletableFuture redirectBiomesExecutor(final Supplier supplier, final Executor badExecutor, + @Local(ordinal = 0, argsOnly = true) final Executor executor) { + return CompletableFuture.supplyAsync(supplier, executor); } /** - * @reason Retrieve the supplier from the mixin above so that we can change the executor to the parameter provided - * @author Spottedleaf - */ - @Inject( - method = "createBiomes", - cancellable = true, - at = @At( - value = "RETURN" - ) - ) - private void unpackSupplierBiomes(Executor executor, RandomState randomState, Blender blender, - StructureManager structureManager, ChunkAccess chunkAccess, - CallbackInfoReturnable> cir) { - cir.setReturnValue( - CompletableFuture.supplyAsync(((CompletableFuture>)(CompletableFuture)cir.getReturnValue()).join(), executor) - ); - } - - - /** - * @reason Pass the executor tasks to the mixin below so that we can change the executor to the parameter provided + * @reason Use the provided executor, chunk system sets this to something specific * @author Spottedleaf */ @Redirect( @@ -65,40 +46,8 @@ private void unpackSupplierBiomes(Executor executor, RandomState randomState, Bl target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" ) ) - private CompletableFuture passSupplierNoise(Supplier supplier, Executor executor) { - final HolderCompletableFuture ret = new HolderCompletableFuture<>(); - - ret.toExecute.add(() -> { - try { - ret.complete(supplier.get()); - } catch (final Throwable throwable) { - ret.completeExceptionally(throwable); - } - }); - - return ret; - } - - /** - * @reason Retrieve the executor tasks from the mixin above so that we can change the executor to the parameter provided - * @author Spottedleaf - */ - @Redirect( - method = "fillFromNoise", - at = @At( - value = "INVOKE", - target = "Ljava/util/concurrent/CompletableFuture;whenCompleteAsync(Ljava/util/function/BiConsumer;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" - ) - ) - private CompletableFuture unpackSupplierNoise(final CompletableFuture instance, final BiConsumer action, - final Executor executor) { - final HolderCompletableFuture casted = (HolderCompletableFuture)instance; - - for (final Runnable run : casted.toExecute) { - executor.execute(run); - } - - // note: executor is the parameter we want - return instance.whenCompleteAsync(action, executor); + private CompletableFuture redirectNoiseExecutor(final Supplier supplier, final Executor badExecutor, + @Local(ordinal = 0, argsOnly = true) final Executor executor) { + return CompletableFuture.supplyAsync(supplier, executor); } }