From 91da0bf252e5f5c375197806c7f7b7dad602f8d2 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 22 Nov 2025 09:25:55 -0600 Subject: [PATCH] replace and simplify WorldGenThreadCheck --- .../common/wrappers/DependencySetup.java | 1 - .../common/wrappers/WorldGenThreadCheck.java | 39 ------------ .../BatchGenerationEnvironment.java | 60 +++++++++---------- .../worldGeneration/GenerationEvent.java | 2 +- .../worldGeneration/GlobalWorldGenParams.java | 30 +++++++--- .../worldGeneration/ThreadWorldGenParams.java | 38 ++++++++---- coreSubProjects | 2 +- .../fabric/mixins/server/MixinLevelTicks.java | 11 +--- .../mixins/server/MixinTracingExecutor.java | 9 +-- .../server/MixinUtilBackgroundThread.java | 17 +++--- .../server/MixinUtilBackgroundThread.java | 26 ++++---- .../neoforge/NeoforgeServerProxy.java | 5 +- .../mixins/server/MixinLevelTicks.java | 10 +--- .../mixins/server/MixinTracingExecutor.java | 9 +-- .../server/MixinUtilBackgroundThread.java | 12 ++-- 15 files changed, 118 insertions(+), 153 deletions(-) delete mode 100644 common/src/main/java/com/seibel/distanthorizons/common/wrappers/WorldGenThreadCheck.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java index 9260196fd..b8fb6a99f 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java @@ -56,7 +56,6 @@ public class DependencySetup SingletonInjector.INSTANCE.bind(IVersionConstants.class, VersionConstants.INSTANCE); SingletonInjector.INSTANCE.bind(IWrapperFactory.class, WrapperFactory.INSTANCE); SingletonInjector.INSTANCE.bind(IKeyedClientLevelManager.class, KeyedClientLevelManager.INSTANCE); - WorldGenThreadCheck.isSetup = true; } public static void createServerBindings() diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/WorldGenThreadCheck.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/WorldGenThreadCheck.java deleted file mode 100644 index 224ed8c69..000000000 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/WorldGenThreadCheck.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the Distant Horizons mod - * licensed under the GNU LGPL v3 License. - * - * Copyright (C) 2020 James Seibel - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.seibel.distanthorizons.common.wrappers; - -import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; - -import java.util.function.Supplier; - -public class WorldGenThreadCheck -{ - public static boolean isSetup = false; - - /** - * This is used so we can override some MC logic when running - * in DH's world generator. - * Specifically so we can redirect threads to run on DH threads instead - * of MC threads. - */ - //public static Supplier isCurrentThreadDhWorldGenThread = (() -> { return false; }); - public static Supplier isCurrentThreadDhWorldGenThread = BatchGenerationEnvironment::isCurrentThreadDistantGeneratorThread; // TODO can we just do this? - -} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java index 19a3c9d65..b14868c2e 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java @@ -58,7 +58,6 @@ import java.util.stream.StreamSupport; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; import com.seibel.distanthorizons.common.wrappers.worldGeneration.step.StepBiomes; import com.seibel.distanthorizons.common.wrappers.worldGeneration.step.StepFeatures; import com.seibel.distanthorizons.common.wrappers.worldGeneration.step.StepNoise; @@ -126,6 +125,7 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm public static final int MAX_WORLD_GEN_CHUNK_BORDER_NEEDED; + private final IDhServerLevel serverLevel; /** @@ -137,20 +137,19 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm - //=================Generation Step=================== - public final LinkedBlockingQueue generationEventList = new LinkedBlockingQueue<>(); public final GlobalWorldGenParams params; + public final StepStructureStart stepStructureStart = new StepStructureStart(this); public final StepStructureReference stepStructureReference = new StepStructureReference(this); public final StepBiomes stepBiomes = new StepBiomes(this); public final StepNoise stepNoise = new StepNoise(this); public final StepSurface stepSurface = new StepSurface(this); public final StepFeatures stepFeatures = new StepFeatures(this); + public boolean unsafeThreadingRecorded = false; public static final long EXCEPTION_TIMER_RESET_TIME = TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS); public static final int EXCEPTION_COUNTER_TRIGGER = 20; - public static final int RANGE_TO_RANGE_EMPTY_EXTENSION = 1; public int unknownExceptionCount = 0; public long lastExceptionTriggerTime = 0; @@ -170,7 +169,7 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm } public static ThreadLocal isDhWorldGenThreadRef = new ThreadLocal<>(); - public static boolean isCurrentThreadDistantGeneratorThread() { return (isDhWorldGenThreadRef.get() != null); } + public static boolean isThisDhWorldGenThread() { return (isDhWorldGenThreadRef.get() != null); } @@ -180,9 +179,6 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm static { - // TODO can this be handled directly? - WorldGenThreadCheck.isCurrentThreadDhWorldGenThread = BatchGenerationEnvironment::isCurrentThreadDistantGeneratorThread; - boolean isTerraFirmaCraftPresent = false; try { @@ -218,7 +214,7 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm { this.serverLevel = serverLevel; - LOGGER.info("================WORLD_GEN_STEP_INITING============="); + LOGGER.info("Creating Batch Generator"); serverLevel.getServerLevelWrapper().getDimensionType(); @@ -345,8 +341,6 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm /** @throws RejectedExecutionException if the given {@link Executor} is cancelled. */ public CompletableFuture generateLodFromListAsync(GenerationEvent genEvent, Executor executor) throws RejectedExecutionException, InterruptedException { - LOGGER.debug("Lod Generate Event: " + genEvent.minPos); - // Minecraft's generation events expect odd chunk width areas (3x3, 7x7, or 11x11), // but DH submits square generation events (4x4). // We handle this later, although that handling would need to change if the gen size ever changes. @@ -561,13 +555,13 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm * otherwise this will return an empty chunk. */ private CompletableFuture createEmptyOrPreExistingChunkAsync( - int x, int z, + int chunkX, int chunkZ, Map chunkSkyLightingByDhPos, Map chunkBlockLightingByDhPos, Map generatedChunkByDhPos) { - ChunkPos chunkPos = new ChunkPos(x, z); - DhChunkPos dhChunkPos = new DhChunkPos(x, z); + ChunkPos chunkPos = new ChunkPos(chunkX, chunkZ); + DhChunkPos dhChunkPos = new DhChunkPos(chunkX, chunkZ); if (generatedChunkByDhPos.containsKey(dhChunkPos)) { @@ -1110,9 +1104,29 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm } private static ArrayGridList GetCutoutFrom(ArrayGridList total, int border) { return new ArrayGridList<>(total, border, total.gridSize - border); } private static ArrayGridList GetCutoutFrom(ArrayGridList total, EDhApiWorldGenerationStep step) { return GetCutoutFrom(total, WORLD_GEN_CHUNK_BORDER_NEEDED_BY_GEN_STEP.get(step)); } - //private static ArrayGridList GetCutoutFrom(ArrayGridList total, EDhApiWorldGenerationStep step) { return GetCutoutFrom(total, 0); } + + @Override + public CompletableFuture generateChunks( + int minX, int minZ, int genSize, + EDhApiDistantGeneratorMode generatorMode, EDhApiWorldGenerationStep targetStep, + ExecutorService worldGeneratorThreadPool, Consumer resultConsumer) + { + //System.out.println("GenerationEvent: "+genSize+"@"+minX+","+minZ+" "+targetStep); + + // TODO: Check event overlap via e.tooClose() + GenerationEvent genEvent = GenerationEvent.startEvent(new DhChunkPos(minX, minZ), genSize, this, generatorMode, targetStep, resultConsumer, worldGeneratorThreadPool); + this.generationEventList.add(genEvent); + return genEvent.future; + } + + + + //================// + // base overrides // + //================// + @Override public void close() { @@ -1145,20 +1159,6 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm LOGGER.info(BatchGenerationEnvironment.class.getSimpleName() + " shutdown complete."); } - @Override - public CompletableFuture generateChunks( - int minX, int minZ, int genSize, - EDhApiDistantGeneratorMode generatorMode, EDhApiWorldGenerationStep targetStep, - ExecutorService worldGeneratorThreadPool, Consumer resultConsumer) - { - //System.out.println("GenerationEvent: "+genSize+"@"+minX+","+minZ+" "+targetStep); - - // TODO: Check event overlap via e.tooClose() - GenerationEvent genEvent = GenerationEvent.startEvent(new DhChunkPos(minX, minZ), genSize, this, generatorMode, targetStep, resultConsumer, worldGeneratorThreadPool); - this.generationEventList.add(genEvent); - return genEvent.future; - } - //================// @@ -1174,7 +1174,7 @@ public final class BatchGenerationEnvironment implements IBatchGeneratorEnvironm { if (Thread.interrupted()) { - throw new InterruptedException(BatchGenerationEnvironment.class.getSimpleName() + " task interrupted."); + throw new InterruptedException("["+BatchGenerationEnvironment.class.getSimpleName()+"] task interrupted."); } } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GenerationEvent.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GenerationEvent.java index 3189919ad..c05945921 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GenerationEvent.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GenerationEvent.java @@ -102,7 +102,7 @@ public final class GenerationEvent worldGeneratorThreadPool.execute(() -> { // TODO why not just always set this each time? - boolean alreadyMarked = BatchGenerationEnvironment.isCurrentThreadDistantGeneratorThread(); + boolean alreadyMarked = BatchGenerationEnvironment.isThisDhWorldGenThread(); if (!alreadyMarked) { BatchGenerationEnvironment.isDhWorldGenThreadRef.set(true); diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GlobalWorldGenParams.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GlobalWorldGenParams.java index c4dc9dcf9..c640685f6 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GlobalWorldGenParams.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/GlobalWorldGenParams.java @@ -36,15 +36,29 @@ import net.minecraft.world.level.chunk.storage.ChunkScanAccess; #if MC_VER < MC_1_19_2 import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager; #else -import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; -import net.minecraft.world.level.levelgen.RandomState; -#if MC_VER >= MC_1_19_4 -import net.minecraft.world.level.levelgen.WorldOptions; -import net.minecraft.core.registries.Registries; -#endif #endif import net.minecraft.world.level.storage.WorldData; +#if MC_VER < MC_1_19_4 +#elif MC_VER < MC_1_21_3 +import net.minecraft.core.registries.Registries; +#else +import net.minecraft.core.registries.Registries; +#endif + +#if MC_VER < MC_1_19_4 +import net.minecraft.world.level.levelgen.WorldGenSettings; +#else +import net.minecraft.world.level.levelgen.WorldOptions; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; +import net.minecraft.world.level.levelgen.RandomState; +#endif + +/** + * Handles parameters that are relevant for the entire MC world. + * + * @see ThreadWorldGenParams + */ public final class GlobalWorldGenParams { public final ChunkGenerator generator; @@ -87,7 +101,7 @@ public final class GlobalWorldGenParams MinecraftServer server = this.level.getServer(); WorldData worldData = server.getWorldData(); this.registry = server.registryAccess(); - + #if MC_VER < MC_1_19_4 this.worldGenSettings = worldData.worldGenSettings(); this.biomes = registry.registryOrThrow(Registry.BIOME_REGISTRY); @@ -116,4 +130,6 @@ public final class GlobalWorldGenParams #endif } + + } \ No newline at end of file diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/ThreadWorldGenParams.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/ThreadWorldGenParams.java index c34d02687..23b783247 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/ThreadWorldGenParams.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/ThreadWorldGenParams.java @@ -31,17 +31,20 @@ import net.minecraft.world.level.levelgen.structure.StructureCheck; public final class ThreadWorldGenParams { - private static final ThreadLocal LOCAL_PARAM = new ThreadLocal<>(); + private static final ThreadLocal LOCAL_PARAM_REF = new ThreadLocal<>(); final ServerLevel level; public WorldGenStructFeatManager structFeat = null; + #if MC_VER >= MC_1_18_2 public StructureCheck structCheck; #endif + boolean isValid = true; public final PerfCalculator perf = new PerfCalculator(); + // used for some older MC versions private static GlobalWorldGenParams previousGlobalWorldGenParams = null; @@ -52,7 +55,7 @@ public final class ThreadWorldGenParams public static ThreadWorldGenParams getOrMake(GlobalWorldGenParams globalParams) { - ThreadWorldGenParams threadParam = LOCAL_PARAM.get(); + ThreadWorldGenParams threadParam = LOCAL_PARAM_REF.get(); if (threadParam != null && threadParam.isValid && threadParam.level == globalParams.level) @@ -61,7 +64,7 @@ public final class ThreadWorldGenParams } threadParam = new ThreadWorldGenParams(globalParams); - LOCAL_PARAM.set(threadParam); + LOCAL_PARAM_REF.set(threadParam); return threadParam; } @@ -70,45 +73,54 @@ public final class ThreadWorldGenParams previousGlobalWorldGenParams = param; this.level = param.level; + #if MC_VER < MC_1_18_2 - this.structFeat = new WorldGenStructFeatManager(param.worldGenSettings, level); + this.structFeat = new WorldGenStructFeatManager(param.worldGenSettings, this.level); #elif MC_VER < MC_1_19_2 this.structCheck = this.createStructureCheck(param); #else this.structCheck = new StructureCheck(param.chunkScanner, param.registry, param.structures, - param.level.dimension(), param.generator, param.randomState, level, param.generator.getBiomeSource(), param.worldSeed, + param.level.dimension(), param.generator, param.randomState, this.level, param.generator.getBiomeSource(), param.worldSeed, param.dataFixer); #endif } + //==========// + // builders // + //==========// + public void makeStructFeat(WorldGenLevel genLevel, GlobalWorldGenParams param) { - #if MC_VER < MC_1_19_4 - this.structFeat = new WorldGenStructFeatManager(param.worldGenSettings, genLevel #if MC_VER >= MC_1_18_2 , this.structCheck #endif ); + #if MC_VER < MC_1_18_2 + this.structFeat = new WorldGenStructFeatManager(param.worldGenSettings, genLevel); + #elif MC_VER < MC_1_19_4 + this.structFeat = new WorldGenStructFeatManager(param.worldGenSettings, genLevel, this.structCheck); #else this.structFeat = new WorldGenStructFeatManager(param.worldOptions, genLevel, this.structCheck); #endif } - - #if MC_VER >= MC_1_18_2 && MC_VER < MC_1_19_2 + #if MC_VER < MC_1_18_2 + #elif MC_VER < MC_1_19_2 public void recreateStructureCheck() { - if (previousGlobalParameters != null) + if (previousGlobalWorldGenParams != null) { - this.structCheck = createStructureCheck(previousGlobalParameters); + this.structCheck = this.createStructureCheck(previousGlobalWorldGenParams); } } - private StructureCheck createStructureCheck(GlobalParameters param) + private StructureCheck createStructureCheck(GlobalWorldGenParams param) { return new StructureCheck(param.chunkScanner, param.registry, param.structures, param.level.dimension(), param.generator, this.level, param.generator.getBiomeSource(), param.worldSeed, - param.fixerUpper); + param.dataFixer); } #else public void recreateStructureCheck() { /* do nothing */ } #endif + + } \ No newline at end of file diff --git a/coreSubProjects b/coreSubProjects index 2537c4a25..1b4f9e894 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 2537c4a25925781219567a01eb05acc592a84572 +Subproject commit 1b4f9e89428f06a222925137906352f7484c61a7 diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinLevelTicks.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinLevelTicks.java index 8a40f05bb..4df7a270f 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinLevelTicks.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinLevelTicks.java @@ -13,8 +13,7 @@ public class MixinLevelTicks #else -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; - +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import net.minecraft.world.ticks.LevelTicks; import net.minecraft.world.ticks.ScheduledTick; @@ -26,18 +25,12 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(LevelTicks.class) // available in 1.18.2+, but only needed in 1.21.4+ public class MixinLevelTicks { - // TODO put in a common location - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - - - @Inject(method = "schedule", at = @At(value = "HEAD"), cancellable = true) private void onChunkSave(ScheduledTick tick, CallbackInfo ci) { // In MC 1.21.4 an error check was added to log attempting to schedule ticks for unloaded chunks // this caused a lot of unnecessary errors when generating sand (FallingBlock.class). - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { ci.cancel(); } diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinTracingExecutor.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinTracingExecutor.java index dd10c7588..a35e4e759 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinTracingExecutor.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinTracingExecutor.java @@ -19,6 +19,7 @@ package com.seibel.distanthorizons.fabric.mixins.server; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import org.spongepowered.asm.mixin.Mixin; #if MC_VER < MC_1_21_3 @@ -35,7 +36,6 @@ public class MixinTracingExecutor } #else -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; import com.seibel.distanthorizons.core.util.objects.RunOnThisThreadExecutorService; import net.minecraft.TracingExecutor; import org.spongepowered.asm.mixin.injection.At; @@ -54,16 +54,11 @@ import java.util.concurrent.Executor; @Mixin(TracingExecutor.class) public class MixinTracingExecutor { - // TODO put in a common location - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - - // replaced with TracingExecutor in MC 1.21.3+ @Inject(method = "forName(Ljava/lang/String;)Ljava/util/concurrent/Executor;", at = @At("HEAD"), cancellable = true) private void forName(String executorName, CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { // run this task on the current DH thread instead of a new MC thread ci.setReturnValue(new RunOnThisThreadExecutorService()); diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinUtilBackgroundThread.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinUtilBackgroundThread.java index 3e297fb4b..80ea7f5df 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinUtilBackgroundThread.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/server/MixinUtilBackgroundThread.java @@ -19,14 +19,19 @@ package com.seibel.distanthorizons.fabric.mixins.server; -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import com.seibel.distanthorizons.core.util.objects.RunOnThisThreadExecutorService; import org.spongepowered.asm.mixin.Mixin; import net.minecraft.Util; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + #if MC_VER < MC_1_16_5 #elif MC_VER < MC_1_21_3 +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; #else #endif @@ -41,15 +46,11 @@ import java.util.function.Supplier; @Mixin(Util.class) public class MixinUtilBackgroundThread { - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - - #if MC_VER < MC_1_21_3 @Inject(method = "backgroundExecutor", at = @At("HEAD"), cancellable = true) private static void overrideUtil$backgroundExecutor(CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { // run this task on the current DH thread instead of a new MC thread ci.setReturnValue(new RunOnThisThreadExecutorService()); @@ -65,7 +66,7 @@ public class MixinUtilBackgroundThread at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskName(String string, Runnable r, CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Runnable) triggered"); ci.setReturnValue(r); @@ -81,7 +82,7 @@ public class MixinUtilBackgroundThread at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskNameForSupplier(String string, Supplier r, CallbackInfoReturnable> ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Supplier) triggered"); ci.setReturnValue(r); diff --git a/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/server/MixinUtilBackgroundThread.java b/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/server/MixinUtilBackgroundThread.java index 194114ad0..dfaf7de41 100644 --- a/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/server/MixinUtilBackgroundThread.java +++ b/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/server/MixinUtilBackgroundThread.java @@ -22,12 +22,15 @@ package com.seibel.distanthorizons.forge.mixins.server; import java.util.concurrent.ExecutorService; import java.util.function.Supplier; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import com.seibel.distanthorizons.common.wrappers.DependencySetupDoneCheck; import com.seibel.distanthorizons.core.util.objects.RunOnThisThreadExecutorService; import net.minecraft.Util; @@ -35,17 +38,17 @@ import net.minecraft.Util; @Mixin(Util.class) public class MixinUtilBackgroundThread { - private static boolean shouldApplyOverride() - { - return DependencySetupDoneCheck.isDone && DependencySetupDoneCheck.getIsCurrentThreadDistantGeneratorThread.get(); - } + @Unique + private static final DhLogger LOGGER = new DhLoggerBuilder().name("MixinUtilBackgroundThread").build(); + + @Inject(method = "backgroundExecutor", at = @At("HEAD"), cancellable = true) private static void overrideUtil$backgroundExecutor(CallbackInfoReturnable ci) { - if (shouldApplyOverride()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { - //ApiShared.LOGGER.info("util backgroundExecutor triggered"); + //LOGGER.info("util backgroundExecutor triggered"); ci.setReturnValue(new RunOnThisThreadExecutorService()); } } @@ -55,21 +58,22 @@ public class MixinUtilBackgroundThread at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskName(String string, Runnable r, CallbackInfoReturnable ci) { - if (shouldApplyOverride()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { - //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Runnable) triggered"); + //LOGGER.info("util wrapThreadWithTaskName(Runnable) triggered"); ci.setReturnValue(r); } } #endif + #if MC_VER >= MC_1_18_2 @Inject(method = "wrapThreadWithTaskName(Ljava/lang/String;Ljava/util/function/Supplier;)Ljava/util/function/Supplier;", at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskNameForSupplier(String string, Supplier r, CallbackInfoReturnable> ci) { - if (shouldApplyOverride()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { - //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Supplier) triggered"); + //LOGGER.info("util wrapThreadWithTaskName(Supplier) triggered"); ci.setReturnValue(r); } } diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeServerProxy.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeServerProxy.java index da9fbf0b8..a91dcc863 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeServerProxy.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/NeoforgeServerProxy.java @@ -30,8 +30,7 @@ import java.util.function.Supplier; #if MC_VER < MC_1_20_6 import net.neoforged.neoforge.event.TickEvent; -#else -import net.neoforged.neoforge.event.tick.ServerTickEvent; +#else #endif @@ -52,7 +51,7 @@ public class NeoforgeServerProxy implements AbstractModInitializer.IEventProxy public NeoforgeServerProxy(boolean isDedicated) { this.isDedicated = isDedicated; - isGenerationThreadChecker = BatchGenerationEnvironment::isCurrentThreadDistantGeneratorThread; + isGenerationThreadChecker = BatchGenerationEnvironment::isThisDhWorldGenThread; } @Override diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinLevelTicks.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinLevelTicks.java index 8eb434908..15c5c8423 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinLevelTicks.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinLevelTicks.java @@ -13,7 +13,7 @@ public class MixinLevelTicks #else -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import net.minecraft.world.ticks.LevelTicks; import net.minecraft.world.ticks.ScheduledTick; import org.spongepowered.asm.mixin.Mixin; @@ -24,18 +24,12 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(LevelTicks.class) // available in 1.18.2+, but only needed in 1.21.4+ public class MixinLevelTicks { - // TODO put in a common location - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - - - @Inject(method = "schedule", at = @At(value = "HEAD"), cancellable = true) private void onChunkSave(ScheduledTick tick, CallbackInfo ci) { // In MC 1.21.4 an error check was added to log attempting to schedule ticks for unloaded chunks // this caused a lot of unnecessary errors when generating sand (FallingBlock.class). - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { ci.cancel(); } diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinTracingExecutor.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinTracingExecutor.java index f7f1eea53..51abf0a5f 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinTracingExecutor.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinTracingExecutor.java @@ -35,7 +35,7 @@ public class MixinTracingExecutor } #else -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import com.seibel.distanthorizons.core.util.objects.RunOnThisThreadExecutorService; import net.minecraft.TracingExecutor; import org.spongepowered.asm.mixin.Mixin; @@ -55,11 +55,6 @@ import java.util.concurrent.Executor; @Mixin(TracingExecutor.class) public class MixinTracingExecutor { - // TODO put in a common location - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - - // Util.backgroundExecutor().forName("init_biomes") // needed for world gen @@ -67,7 +62,7 @@ public class MixinTracingExecutor @Inject(method = "forName(Ljava/lang/String;)Ljava/util/concurrent/Executor;", at = @At("HEAD"), cancellable = true) private void forName(String executorName, CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { // run this task on the current DH thread instead of a new MC thread ci.setReturnValue(new RunOnThisThreadExecutorService()); diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinUtilBackgroundThread.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinUtilBackgroundThread.java index 91d08f79a..33e38e235 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinUtilBackgroundThread.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/server/MixinUtilBackgroundThread.java @@ -19,11 +19,10 @@ package com.seibel.distanthorizons.neoforge.mixins.server; +import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import com.seibel.distanthorizons.core.util.objects.RunOnThisThreadExecutorService; import org.spongepowered.asm.mixin.Mixin; -import com.seibel.distanthorizons.common.wrappers.WorldGenThreadCheck; - import net.minecraft.Util; /** @@ -36,16 +35,13 @@ import net.minecraft.Util; @Mixin(Util.class) public class MixinUtilBackgroundThread { - private static boolean isWorldGenThread() - { return WorldGenThreadCheck.isSetup && WorldGenThreadCheck.isCurrentThreadDhWorldGenThread.get(); } - #if MC_VER < MC_1_21_3 @Inject(method = "backgroundExecutor", at = @At("HEAD"), cancellable = true) private static void overrideUtil$backgroundExecutor(CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { // run this task on the current DH thread instead of a new MC thread ci.setReturnValue(new RunOnThisThreadExecutorService()); @@ -61,7 +57,7 @@ public class MixinUtilBackgroundThread at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskName(String string, Runnable r, CallbackInfoReturnable ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Runnable) triggered"); ci.setReturnValue(r); @@ -77,7 +73,7 @@ public class MixinUtilBackgroundThread at = @At("HEAD"), cancellable = true) private static void overrideUtil$wrapThreadWithTaskNameForSupplier(String string, Supplier r, CallbackInfoReturnable> ci) { - if (isWorldGenThread()) + if (BatchGenerationEnvironment.isThisDhWorldGenThread()) { //ApiShared.LOGGER.info("util wrapThreadWithTaskName(Supplier) triggered"); ci.setReturnValue(r);