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.
This commit is contained in:
Spottedleaf
2024-05-27 06:15:14 -07:00
parent 0fa6d0710f
commit 845e8fb76a
5 changed files with 21 additions and 113 deletions

View File

@@ -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<T> extends CompletableFuture<T> {
public final List<Runnable> toExecute = new ArrayList<>();
}

View File

@@ -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) {

View File

@@ -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 @@ import java.util.function.Supplier;
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 <U> CompletableFuture<U> passSupplier(Supplier<U> supplier, Executor executor) {
return (CompletableFuture<U>)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<CompletableFuture<ChunkAccess>> cir) {
cir.setReturnValue(
CompletableFuture.supplyAsync(((CompletableFuture<Supplier<ChunkAccess>>)(CompletableFuture)cir.getReturnValue()).join(), executor)
);
private <U> CompletableFuture<U> redirectBiomesExecutor(final Supplier<U> supplier, final Executor badExecutor,
@Local(ordinal = 0, argsOnly = true) final Executor executor) {
return CompletableFuture.supplyAsync(supplier, executor);
}
/**

View File

@@ -2,6 +2,7 @@ package ca.spottedleaf.moonrise.mixin.chunk_system;
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,27 +69,11 @@ public abstract class EntityTickListMixin {
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
* @author Spottedleaf
*/
@Redirect(
method = "remove",
at = @At(
@@ -97,7 +82,9 @@ public abstract class EntityTickListMixin {
remap = false
)
)
private <V> V hookRemoveAvoidNPE(final Int2ObjectMap<V> instance, final int key) {
private <V> V hookRemoveAvoidNPE(final Int2ObjectMap<V> instance, final int key,
final @Local(ordinal = 0, argsOnly = true) Entity entity) {
this.entities.remove(entity);
return null;
}

View File

@@ -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 @@ import java.util.function.Supplier;
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 <U> CompletableFuture<U> passSupplierBiomes(Supplier<U> supplier, Executor executor) {
return (CompletableFuture<U>)CompletableFuture.completedFuture(supplier);
private <U> CompletableFuture<U> redirectBiomesExecutor(final Supplier<U> 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<CompletableFuture<ChunkAccess>> cir) {
cir.setReturnValue(
CompletableFuture.supplyAsync(((CompletableFuture<Supplier<ChunkAccess>>)(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 @@ public abstract class NoiseBasedChunkGeneratorMixin {
target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"
)
)
private <U> CompletableFuture<U> passSupplierNoise(Supplier<U> supplier, Executor executor) {
final HolderCompletableFuture<U> 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 <T> CompletableFuture<T> unpackSupplierNoise(final CompletableFuture<T> instance, final BiConsumer<? super T, ? super Throwable> action,
final Executor executor) {
final HolderCompletableFuture<T> casted = (HolderCompletableFuture<T>)instance;
for (final Runnable run : casted.toExecute) {
executor.execute(run);
}
// note: executor is the parameter we want
return instance.whenCompleteAsync(action, executor);
private <U> CompletableFuture<U> redirectNoiseExecutor(final Supplier<U> supplier, final Executor badExecutor,
@Local(ordinal = 0, argsOnly = true) final Executor executor) {
return CompletableFuture.supplyAsync(supplier, executor);
}
}