|
2 | 2 |
|
3 | 3 | import com.ishland.c2me.base.mixin.access.IThreadedAnvilChunkStorage; |
4 | 4 | import com.ishland.c2me.rewrites.chunksystem.common.ducks.IChunkSystemAccess; |
| 5 | +import com.ishland.c2me.rewrites.chunksystem.common.structs.ChunkSystemExecutors; |
5 | 6 | import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
6 | 7 | import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
7 | 8 | import net.minecraft.server.world.ChunkHolder; |
| 9 | +import net.minecraft.server.world.ChunkLevelManager; |
8 | 10 | import net.minecraft.server.world.OptionalChunk; |
9 | 11 | import net.minecraft.server.world.ServerChunkLoadingManager; |
10 | 12 | import net.minecraft.server.world.ServerChunkManager; |
|
24 | 26 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
25 | 27 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
26 | 28 |
|
| 29 | +import java.util.ArrayDeque; |
| 30 | +import java.util.Queue; |
27 | 31 | import java.util.concurrent.CompletableFuture; |
28 | 32 |
|
29 | 33 | @Mixin(ServerChunkManager.class) |
@@ -85,4 +89,32 @@ public String getDebugString() { |
85 | 89 | return Integer.toString(((IChunkSystemAccess) this.chunkLoadingManager).c2me$getTheChunkSystem().itemCount()) + ", " + Integer.toString(this.getLoadedChunkCount()); |
86 | 90 | } |
87 | 91 |
|
| 92 | + @WrapOperation(method = "updateChunks", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ChunkLevelManager;update(Lnet/minecraft/server/world/ServerChunkLoadingManager;)Z")) |
| 93 | + private boolean consolidateSchedules(ChunkLevelManager instance, ServerChunkLoadingManager chunkLoadingManager, Operation<Boolean> original) { |
| 94 | + Queue<Runnable> runnables = ChunkSystemExecutors.CONSOLIDATING_QUEUE.get(); |
| 95 | + if (runnables != null) { |
| 96 | + new Throwable("CONSOLIDATING_QUEUE leak").printStackTrace(); |
| 97 | + return original.call(instance, chunkLoadingManager); |
| 98 | + } |
| 99 | + |
| 100 | + ChunkSystemExecutors.CONSOLIDATING_QUEUE.set(runnables = new ArrayDeque<>()); |
| 101 | + try { |
| 102 | + return original.call(instance, chunkLoadingManager); |
| 103 | + } finally { |
| 104 | + Queue<Runnable> finalRunnables = runnables; |
| 105 | + if (!finalRunnables.isEmpty()) { |
| 106 | + ChunkSystemExecutors.backingBackgroundExecutor.execute(() -> { |
| 107 | + while (!finalRunnables.isEmpty()) { |
| 108 | + try { |
| 109 | + finalRunnables.remove().run(); |
| 110 | + } catch (Throwable t) { |
| 111 | + t.printStackTrace(); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + ChunkSystemExecutors.CONSOLIDATING_QUEUE.remove(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
88 | 120 | } |
0 commit comments