Skip to content

Commit

Permalink
Wait until after populate for initial replacement. Should fix issues …
Browse files Browse the repository at this point in the history
…with stripes & other unreliable replacements, as much as is possible with Forge.

Close #16
Close #18
Close #26
Close #28
Close #29
  • Loading branch information
The-Fireplace committed Sep 6, 2021
1 parent a9ad525 commit 307b8a5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Improved block replacement performance
Fixed multiplyChance dividing instead of multiplying
Improved replacement reliability
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,43 @@
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");

@CapabilityInject(BlockReplacedCapability.class)
public static Capability<BlockReplacedCapability> 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<Chunk> e) {
assert BLOCKS_REPLACED != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 307b8a5

Please sign in to comment.