Skip to content

Commit

Permalink
Fix rare CME
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed May 13, 2024
1 parent 40e6208 commit 1282821
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ChunkKey> edgeCount;
private final Set<ChunkKey> ranMap = Collections.synchronizedSet(new HashSet<>());
private final Set<ChunkKey> ran = Collections.synchronizedSet(new HashSet<>());

public OreGenMapSavedData() {
Object2IntMap<ChunkKey> 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) {}
Expand All @@ -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;
Expand All @@ -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<Integer> edge1 = new ArrayList<>();
ArrayList<Integer> edge2 = new ArrayList<>();
ArrayList<Integer> edge3 = new ArrayList<>();
ArrayList<Integer> ran1 = new ArrayList<>();
ArrayList<Integer> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoneFeatureConfiguration> {
private static final int[] xs = new int[]{-1, 0, 1, 1, -1, -1, 0, 1};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]];
Expand Down

0 comments on commit 1282821

Please sign in to comment.