easy fix to a concurrency issue

This commit is contained in:
Ellpeck 2018-12-03 00:45:34 +01:00
parent 031bf3fbc3
commit 5fcd096a99

View file

@ -19,7 +19,10 @@ import org.apache.commons.lang3.mutable.MutableInt;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; 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.BiConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -27,7 +30,7 @@ public class AuraChunk implements IAuraChunk {
private final Chunk chunk; private final Chunk chunk;
private final IAuraType type; private final IAuraType type;
private final Map<BlockPos, MutableInt> drainSpots = new HashMap<>(); private final Map<BlockPos, MutableInt> drainSpots = new ConcurrentHashMap<>();
private final List<IDrainSpotEffect> effects = new ArrayList<>(); private final List<IDrainSpotEffect> effects = new ArrayList<>();
private boolean needsSync; private boolean needsSync;
@ -66,6 +69,8 @@ public class AuraChunk implements IAuraChunk {
} }
if (!simulate) { if (!simulate) {
spot.subtract(amount); spot.subtract(amount);
if (spot.intValue() == 0)
this.drainSpots.remove(pos);
this.markDirty(); this.markDirty();
} }
return amount; return amount;
@ -91,6 +96,8 @@ public class AuraChunk implements IAuraChunk {
} }
if (!simulate) { if (!simulate) {
spot.add(amount); spot.add(amount);
if (spot.intValue() == 0)
this.drainSpots.remove(pos);
this.markDirty(); this.markDirty();
} }
return amount; return amount;
@ -144,7 +151,6 @@ public class AuraChunk implements IAuraChunk {
public void update() { public void update() {
World world = this.chunk.getWorld(); World world = this.chunk.getWorld();
Set<BlockPos> toClear = null;
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) { for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos pos = entry.getKey(); BlockPos pos = entry.getKey();
MutableInt amount = entry.getValue(); MutableInt amount = entry.getValue();
@ -153,16 +159,6 @@ public class AuraChunk implements IAuraChunk {
effect.update(world, this.chunk, this, pos, amount.intValue()); effect.update(world, this.chunk, this, pos, amount.intValue());
world.profiler.endSection(); 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) { if (this.needsSync) {