From ea4838c7914eac1cba47b5d8a1f762728c30b0a9 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 2 Oct 2025 18:03:43 -0500 Subject: [PATCH] add DhApiChunkProcessingEvent --- coreSubProjects | 2 +- .../fabric/FabricServerProxy.java | 9 +-- .../testing/TestChunkInputReplacerEvent.java | 64 +++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 fabric/src/main/java/com/seibel/distanthorizons/fabric/testing/TestChunkInputReplacerEvent.java diff --git a/coreSubProjects b/coreSubProjects index 9690c898b..08ede3351 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 9690c898b08d6c421f557be1a68a1d4aca9d6db0 +Subproject commit 08ede3351db40c0c0185f93699277d9f81d15ebd diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/FabricServerProxy.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/FabricServerProxy.java index f932e51ff..279d182f0 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/FabricServerProxy.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/FabricServerProxy.java @@ -1,5 +1,7 @@ package com.seibel.distanthorizons.fabric; +import com.seibel.distanthorizons.api.DhApi; +import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiChunkProcessingEvent; import com.seibel.distanthorizons.api.methods.events.DhApiEventRegister; import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiLevelLoadEvent; import com.seibel.distanthorizons.common.AbstractModInitializer; @@ -7,7 +9,6 @@ import com.seibel.distanthorizons.common.wrappers.chunk.ChunkWrapper; import com.seibel.distanthorizons.common.wrappers.misc.ServerPlayerWrapper; import com.seibel.distanthorizons.common.wrappers.world.ClientLevelWrapper; import com.seibel.distanthorizons.common.wrappers.world.ServerLevelWrapper; -import com.seibel.distanthorizons.common.wrappers.worldGeneration.BatchGenerationEnvironment; import com.seibel.distanthorizons.core.api.internal.ServerApi; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.common.AbstractPluginPacketSender; @@ -15,6 +16,7 @@ import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IPluginPacketSender; import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.world.ILevelWrapper; +import com.seibel.distanthorizons.fabric.testing.TestChunkInputReplacerEvent; import com.seibel.distanthorizons.fabric.testing.TestWorldGenBindingEvent; import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents; @@ -37,8 +39,6 @@ import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import com.seibel.distanthorizons.core.network.messages.AbstractNetworkMessage; #endif -import java.util.function.Supplier; - /** * This handles all events sent to the server, * and is the starting point for most of the mod. @@ -97,10 +97,11 @@ public class FabricServerProxy implements AbstractModInitializer.IEventProxy ServerTickEvents.END_SERVER_TICK.register((server) -> SERVER_API.serverTickEvent()); - // can be enabled to test world gen overrides without having to build a separate API project + // can be enabled to test overrides/events without having to build a separate API project if (false) { DhApiEventRegister.on(DhApiLevelLoadEvent.class, new TestWorldGenBindingEvent()); + DhApi.events.bind(DhApiChunkProcessingEvent.class, new TestChunkInputReplacerEvent()); } diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/testing/TestChunkInputReplacerEvent.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/testing/TestChunkInputReplacerEvent.java new file mode 100644 index 000000000..48f3be15b --- /dev/null +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/testing/TestChunkInputReplacerEvent.java @@ -0,0 +1,64 @@ +package com.seibel.distanthorizons.fabric.testing; + +import com.seibel.distanthorizons.api.DhApi; +import com.seibel.distanthorizons.api.interfaces.block.IDhApiBlockStateWrapper; +import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiChunkProcessingEvent; +import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiEventParam; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; + +public class TestChunkInputReplacerEvent extends DhApiChunkProcessingEvent +{ + private static final Logger LOGGER = DhLoggerBuilder.getLogger(); + + private static final String REPLACEMENT_BLOCK_STATE_NAMESPACE = "minecraft:stone"; + + private IDhApiBlockStateWrapper stoneBlockWrapper = null; + private boolean initialBlockSetupComplete = false; + + + + @Override + public void blockOrBiomeChangedDuringChunkProcessing(DhApiEventParam event) + { + if (!this.initialBlockSetupComplete) + { + // this method can be called on multiple threads + synchronized (this) + { + this.initialBlockSetupComplete = true; + try + { + this.stoneBlockWrapper = DhApi.Delayed.wrapperFactory.getDefaultBlockStateWrapper(REPLACEMENT_BLOCK_STATE_NAMESPACE, event.value.levelWrapper); + } + catch (IOException e) + { + LOGGER.error("Unable to get ["+REPLACEMENT_BLOCK_STATE_NAMESPACE+"] block replacement cannot continue and is now disabled, error: ["+e.getMessage()+"].", e); + DhApi.events.unbind(DhApiChunkProcessingEvent.class, this.getClass()); + } + } + } + + // will happen if the initial setup fails until the unbind call is processed + // which likely won't happen until the current chunk has finished processing + if (this.stoneBlockWrapper == null) + { + return; + } + + + + // replace any dirt or grass block with stone + IDhApiBlockStateWrapper block = event.value.currentBlock; + if (block.getSerialString().contains("grass_block") + || block.getSerialString().contains("dirt")) + { + event.value.setBlockOverride(this.stoneBlockWrapper); + } + } + + + +}