From 1282821618d053fc81814df15784c0294f418b70 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Mon, 13 May 2024 09:21:59 -0500 Subject: [PATCH] Fix rare CME --- .../impl/worldgen/OreFound.java | 7 +-- .../impl/worldgen/OreGenMapSavedData.java | 44 +++++++++++-------- .../impl/worldgen/OreReplacer.java | 7 ++- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreFound.java b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreFound.java index a7f192b3..d8e650a8 100644 --- a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreFound.java +++ b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreFound.java @@ -7,17 +7,18 @@ import dev.lukebemish.excavatedvariants.api.data.Ore; import dev.lukebemish.excavatedvariants.api.data.Stone; +import org.jspecify.annotations.Nullable; public interface OreFound { - Ore excavated_variants$getOre(); + @Nullable Ore excavated_variants$getOre(); void excavated_variants$setOre(Ore o); - Stone excavated_variants$getOreStone(); + @Nullable Stone excavated_variants$getOreStone(); void excavated_variants$setOreStone(Stone o); - Stone excavated_variants$getStone(); + @Nullable Stone excavated_variants$getStone(); void excavated_variants$setStone(Stone stone); } diff --git a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreGenMapSavedData.java b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreGenMapSavedData.java index c6fa0cd2..90c4c078 100644 --- a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreGenMapSavedData.java +++ b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreGenMapSavedData.java @@ -7,7 +7,6 @@ import dev.lukebemish.excavatedvariants.impl.ExcavatedVariants; import it.unimi.dsi.fastutil.objects.Object2IntMap; -import it.unimi.dsi.fastutil.objects.Object2IntMaps; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.ServerLevelAccessor; @@ -20,32 +19,41 @@ import java.util.Set; public class OreGenMapSavedData extends SavedData { - public static final String DATA_KEY = ExcavatedVariants.MOD_ID + "_ore_replacement"; + private static final String DATA_KEY = ExcavatedVariants.MOD_ID + "_ore_replacement"; private final Object2IntMap edgeCount; - private final Set ranMap = Collections.synchronizedSet(new HashSet<>()); + private final Set ran = Collections.synchronizedSet(new HashSet<>()); public OreGenMapSavedData() { Object2IntMap edgeMap = new Object2IntOpenHashMap<>(); edgeMap.defaultReturnValue(0); - this.edgeCount = Object2IntMaps.synchronize(edgeMap); + this.edgeCount = edgeMap; } - public int getEdgeCount(ChunkKey chunkPos) { + public synchronized int getEdgeCount(ChunkKey chunkPos) { return edgeCount.getInt(chunkPos); } - public void setEdgeCount(ChunkKey chunkPos, int count) { - edgeCount.put(chunkPos, count); + public synchronized void incrEdgeCount(ChunkKey chunkPos) { + edgeCount.put(chunkPos, edgeCount.getInt(chunkPos) + 1); this.setDirty(); } - public void chunkRan(ChunkKey chunkPos) { - ranMap.add(chunkPos); + public synchronized void setEdgeCount(ChunkKey chunkPos, int count) { + if (count == 9) { + edgeCount.removeInt(chunkPos); + } else { + edgeCount.put(chunkPos, count); + this.setDirty(); + } + } + + public synchronized void chunkRan(ChunkKey chunkPos) { + ran.add(chunkPos); this.setDirty(); } - public boolean didChunkRun(ChunkKey chunkPos) { - return ranMap.contains(chunkPos); + public synchronized boolean didChunkRun(ChunkKey chunkPos) { + return ran.contains(chunkPos); } public record ChunkKey(int x, int z) {} @@ -62,7 +70,7 @@ private static OreGenMapSavedData load(CompoundTag tag) { data.edgeCount.put(new ChunkKey(edge1[i], edge2[i]), edge3[i]); } for (int i = 0; i < ran1.length; i++) { - data.ranMap.add(new ChunkKey(ran1[i], ran2[i])); + data.ran.add(new ChunkKey(ran1[i], ran2[i])); } } return data; @@ -83,18 +91,18 @@ public static OreGenMapSavedData getOrCreate(ServerLevelAccessor world) { @Override @NonNull - public CompoundTag save(@NonNull CompoundTag tag) { + public synchronized CompoundTag save(@NonNull CompoundTag tag) { ArrayList edge1 = new ArrayList<>(); ArrayList edge2 = new ArrayList<>(); ArrayList edge3 = new ArrayList<>(); ArrayList ran1 = new ArrayList<>(); ArrayList ran2 = new ArrayList<>(); - for (ChunkKey p : edgeCount.keySet()) { - edge1.add(p.x()); - edge2.add(p.z()); - edge3.add(edgeCount.getInt(p)); + for (var e : edgeCount.object2IntEntrySet()) { + edge1.add(e.getKey().x()); + edge2.add(e.getKey().z()); + edge3.add(e.getIntValue()); } - for (ChunkKey p : ranMap) { + for (ChunkKey p : ran) { ran1.add(p.x()); ran2.add(p.z()); } diff --git a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreReplacer.java b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreReplacer.java index aa67d761..c21b1c94 100644 --- a/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreReplacer.java +++ b/common/src/main/java/dev/lukebemish/excavatedvariants/impl/worldgen/OreReplacer.java @@ -19,7 +19,6 @@ import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration; import org.jspecify.annotations.NonNull; -import org.jspecify.annotations.Nullable; public class OreReplacer extends Feature { private static final int[] xs = new int[]{-1, 0, 1, 1, -1, -1, 0, 1}; @@ -48,7 +47,7 @@ public boolean modifyUnmodifiedNeighboringChunks(WorldGenLevel level, BlockPos p newPos.setX(pos.getX() + xs[i] * 16); newPos.setZ(pos.getZ() + zs[i] * 16); OreGenMapSavedData.ChunkKey chunkPos = new OreGenMapSavedData.ChunkKey(newPos.getX(), newPos.getZ()); - data.setEdgeCount(chunkPos, data.getEdgeCount(chunkPos) + 1); + data.incrEdgeCount(chunkPos); if (data.getEdgeCount(chunkPos) == 8 && data.didChunkRun(chunkPos)) { ChunkAccess chunkAccess = level.getChunk(newPos); modifyChunk(chunkAccess, minY, maxY); @@ -86,9 +85,9 @@ public void modifyChunk(ChunkAccess chunkAccess, int minY, int maxY) { if (cache[i][y & 15][j] == null) { cache[i][y & 15][j] = newState; } - @Nullable Ore ore = ((OreFound) newState.getBlock()).excavated_variants$getOre(); + Ore ore = ((OreFound) newState.getBlock()).excavated_variants$getOre(); if (ore != null) { - @Nullable Stone oreStone = ((OreFound) newState.getBlock()).excavated_variants$getOreStone(); + Stone oreStone = ((OreFound) newState.getBlock()).excavated_variants$getOreStone(); for (int c = 0; c < as.length; c++) { if (i + as[c] < 16 && i + as[c] >= 0 && j + bs[c] < 16 && j + bs[c] >= 0 && y + ys[c] >= SectionPos.sectionToBlockCoord(sectionIndex) && y + ys[c] < SectionPos.sectionToBlockCoord(sectionIndex + 1)) { BlockState thisState = cache[i + as[c]][y + ys[c] & 15][j + bs[c]];