From 5fcd096a99eb98002d97b2ab5588d320c8320d7f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 3 Dec 2018 00:45:34 +0100 Subject: [PATCH] easy fix to a concurrency issue --- .../ellpeck/naturesaura/chunk/AuraChunk.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java index 38a59184..ef6a3236 100644 --- a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java @@ -19,7 +19,10 @@ import org.apache.commons.lang3.mutable.MutableInt; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiConsumer; import java.util.function.Supplier; @@ -27,7 +30,7 @@ public class AuraChunk implements IAuraChunk { private final Chunk chunk; private final IAuraType type; - private final Map drainSpots = new HashMap<>(); + private final Map drainSpots = new ConcurrentHashMap<>(); private final List effects = new ArrayList<>(); private boolean needsSync; @@ -66,6 +69,8 @@ public class AuraChunk implements IAuraChunk { } if (!simulate) { spot.subtract(amount); + if (spot.intValue() == 0) + this.drainSpots.remove(pos); this.markDirty(); } return amount; @@ -91,6 +96,8 @@ public class AuraChunk implements IAuraChunk { } if (!simulate) { spot.add(amount); + if (spot.intValue() == 0) + this.drainSpots.remove(pos); this.markDirty(); } return amount; @@ -144,7 +151,6 @@ public class AuraChunk implements IAuraChunk { public void update() { World world = this.chunk.getWorld(); - Set toClear = null; for (Map.Entry entry : this.drainSpots.entrySet()) { BlockPos pos = entry.getKey(); MutableInt amount = entry.getValue(); @@ -153,16 +159,6 @@ public class AuraChunk implements IAuraChunk { effect.update(world, this.chunk, this, pos, amount.intValue()); world.profiler.endSection(); } - if (amount.intValue() == 0) { - if (toClear == null) - toClear = new HashSet<>(); - toClear.add(pos); - } - } - if (toClear != null) { - for (BlockPos spot : toClear) - this.drainSpots.remove(spot); - this.markDirty(); } if (this.needsSync) {