From 307b8a54a54bcca5c6b05d8b7c901f91cf13c492 Mon Sep 17 00:00:00 2001 From: The_Fireplace Date: Mon, 6 Sep 2021 12:36:41 -0500 Subject: [PATCH] Wait until after populate for initial replacement. Should fix issues with stripes & other unreliable replacements, as much as is possible with Forge. Close #16 Close #18 Close #26 Close #28 Close #29 --- changelog.txt | 1 + .../api/world/ChunkReplacementData.java | 5 ++- .../ChunkReplacedCapabilityHandler.java | 28 +++++++++++---- .../wgblockreplacer/events/ClientEvents.java | 2 +- .../wgblockreplacer/events/CommonEvents.java | 34 +++++++++++++++---- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/changelog.txt b/changelog.txt index ff95753..1020265 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,2 +1,3 @@ Improved block replacement performance Fixed multiplyChance dividing instead of multiplying +Improved replacement reliability diff --git a/src/main/java/dev/the_fireplace/wgblockreplacer/api/world/ChunkReplacementData.java b/src/main/java/dev/the_fireplace/wgblockreplacer/api/world/ChunkReplacementData.java index 7bcd0e8..640659f 100644 --- a/src/main/java/dev/the_fireplace/wgblockreplacer/api/world/ChunkReplacementData.java +++ b/src/main/java/dev/the_fireplace/wgblockreplacer/api/world/ChunkReplacementData.java @@ -9,6 +9,9 @@ static ChunkReplacementData getInstance() { return ChunkReplacedCapabilityHandler.INSTANCE; } - boolean isReplaced(Chunk chunk); + boolean needsReplacement(Chunk chunk); + + boolean needsRetrogen(Chunk chunk); + void markAsReplaced(Chunk chunk); } diff --git a/src/main/java/dev/the_fireplace/wgblockreplacer/capability/ChunkReplacedCapabilityHandler.java b/src/main/java/dev/the_fireplace/wgblockreplacer/capability/ChunkReplacedCapabilityHandler.java index 35d5385..dab422e 100644 --- a/src/main/java/dev/the_fireplace/wgblockreplacer/capability/ChunkReplacedCapabilityHandler.java +++ b/src/main/java/dev/the_fireplace/wgblockreplacer/capability/ChunkReplacedCapabilityHandler.java @@ -20,7 +20,9 @@ public final class ChunkReplacedCapabilityHandler implements ChunkReplacementData { @Deprecated public static final ChunkReplacementData INSTANCE = new ChunkReplacedCapabilityHandler(); - private ChunkReplacedCapabilityHandler() {} + + private ChunkReplacedCapabilityHandler() { + } private static final ResourceLocation BLOCKS_REPLACED_CAPABILITY_ID = new ResourceLocation(WGBlockReplacer.MODID, "blocks_replaced"); @@ -28,21 +30,33 @@ private ChunkReplacedCapabilityHandler() {} public static Capability BLOCKS_REPLACED = null; @Override - public boolean isReplaced(Chunk chunk) { - //noinspection ConstantConditions - BlockReplacedCapability cap = chunk instanceof ICapabilityProvider ? ((ICapabilityProvider) chunk).getCapability(BLOCKS_REPLACED, null) : null; - return cap != null && cap.getReplacedMarker() != null && cap.getReplacedMarker().equals(ConfigAccess.getInstance().getReplacementChunkKey()); + public boolean needsReplacement(Chunk chunk) { + BlockReplacedCapability cap = getReplacedCapability(chunk); + boolean hasReplacedMarker = cap != null + && cap.getReplacedMarker() != null; + return !hasReplacedMarker || !cap.getReplacedMarker().equals(ConfigAccess.getInstance().getReplacementChunkKey()); + } + + @Override + public boolean needsRetrogen(Chunk chunk) { + return chunk.isTerrainPopulated() && needsReplacement(chunk); } @Override public void markAsReplaced(Chunk chunk) { - //noinspection ConstantConditions - BlockReplacedCapability cap = chunk instanceof ICapabilityProvider ? ((ICapabilityProvider) chunk).getCapability(BLOCKS_REPLACED, null) : null; + BlockReplacedCapability cap = getReplacedCapability(chunk); if (cap != null) { cap.setReplacedMarker(ConfigAccess.getInstance().getReplacementChunkKey()); } } + private BlockReplacedCapability getReplacedCapability(Chunk chunk) { + //noinspection ConstantConditions + return chunk instanceof ICapabilityProvider + ? ((ICapabilityProvider) chunk).getCapability(BLOCKS_REPLACED, null) + : null; + } + @SubscribeEvent public void attachChunkCapability(AttachCapabilitiesEvent e) { assert BLOCKS_REPLACED != null; diff --git a/src/main/java/dev/the_fireplace/wgblockreplacer/events/ClientEvents.java b/src/main/java/dev/the_fireplace/wgblockreplacer/events/ClientEvents.java index d5d5f86..a11d533 100644 --- a/src/main/java/dev/the_fireplace/wgblockreplacer/events/ClientEvents.java +++ b/src/main/java/dev/the_fireplace/wgblockreplacer/events/ClientEvents.java @@ -31,7 +31,7 @@ public static void configChanged(ConfigChangedEvent event) { } @SubscribeEvent - public static void onKeyPress(ClientChatEvent event) { + public static void onChatSending(ClientChatEvent event) { if (event.getOriginalMessage().equals("$meta")) { event.setCanceled(true); RayTraceResult look = Minecraft.getMinecraft().objectMouseOver; diff --git a/src/main/java/dev/the_fireplace/wgblockreplacer/events/CommonEvents.java b/src/main/java/dev/the_fireplace/wgblockreplacer/events/CommonEvents.java index 3bcb401..28c3c24 100644 --- a/src/main/java/dev/the_fireplace/wgblockreplacer/events/CommonEvents.java +++ b/src/main/java/dev/the_fireplace/wgblockreplacer/events/CommonEvents.java @@ -6,6 +6,9 @@ import dev.the_fireplace.wgblockreplacer.api.world.ChunkReplacementData; import dev.the_fireplace.wgblockreplacer.logic.ReplacementQueue; import dev.the_fireplace.wgblockreplacer.logic.Replacer; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; +import net.minecraftforge.event.terraingen.PopulateChunkEvent; import net.minecraftforge.event.world.ChunkEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.EventPriority; @@ -15,20 +18,37 @@ @Mod.EventBusSubscriber(modid = WGBlockReplacer.MODID) public final class CommonEvents { - @SubscribeEvent(priority = EventPriority.LOWEST) - public static void onEvent(ChunkEvent.Load event) { - if (event.getWorld().isRemote + @SubscribeEvent(priority = EventPriority.LOWEST) + public static void retrogenChunk(ChunkEvent.Load event) { + if (event.getWorld().isRemote || !ConfigValidator.getInstance().isValid() - || ChunkReplacementData.getInstance().isReplaced(event.getChunk()) ) { return; } + Chunk chunk = event.getChunk(); + if (ChunkReplacementData.getInstance().needsRetrogen(chunk)) { + replaceChunk(chunk, event.getWorld()); + } + } + + @SubscribeEvent(priority = EventPriority.LOWEST) + public static void genChunk(PopulateChunkEvent.Post event) { + if (event.getWorld().isRemote || !ConfigValidator.getInstance().isValid()) { + return; + } + Chunk chunk = event.getWorld().getChunk(event.getChunkX(), event.getChunkZ()); + if (ChunkReplacementData.getInstance().needsReplacement(chunk)) { + replaceChunk(chunk, event.getWorld()); + } + } + + private static void replaceChunk(Chunk chunk, World world) { if (ConfigAccess.getInstance().getLateReplacementTicks() > 0) { - ReplacementQueue.add(event.getWorld(), event.getChunk()); + ReplacementQueue.add(world, chunk); } else { - Replacer.doReplacement(event.getWorld(), event.getChunk()); + Replacer.doReplacement(world, chunk); } - } + } @SubscribeEvent public static void onWorldTick(TickEvent.ServerTickEvent event) {