From d12618af0fceb84da4d76e4d21b74c9aae9817ac Mon Sep 17 00:00:00 2001 From: Eoghanmc22 Date: Thu, 13 Aug 2020 18:50:57 -0400 Subject: [PATCH] Fix concurrent modify exception. --- .../net/minestom/server/thread/PerGroupChunkProvider.java | 6 ++---- src/main/java/net/minestom/server/utils/MathUtils.java | 8 ++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java index eba0a7696..ab53b1c6b 100644 --- a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java +++ b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java @@ -109,9 +109,7 @@ public class PerGroupChunkProvider extends ThreadProvider { ArrayList> futures = new ArrayList<>(); - instanceInstanceMap.entrySet().forEach(entry -> { - final Instance instance = entry.getKey(); - final Map instanceMap = entry.getValue(); + instanceInstanceMap.forEach((instance, instanceMap) -> { // True if the instance ended its tick call AtomicBoolean instanceUpdated = new AtomicBoolean(false); @@ -157,7 +155,7 @@ public class PerGroupChunkProvider extends ThreadProvider { } private Map getInstanceMap(Instance instance) { - return instanceInstanceMap.computeIfAbsent(instance, inst -> new HashMap<>()); + return instanceInstanceMap.computeIfAbsent(instance, inst -> new ConcurrentHashMap<>()); } } diff --git a/src/main/java/net/minestom/server/utils/MathUtils.java b/src/main/java/net/minestom/server/utils/MathUtils.java index fc00ee700..6d83fb761 100644 --- a/src/main/java/net/minestom/server/utils/MathUtils.java +++ b/src/main/java/net/minestom/server/utils/MathUtils.java @@ -70,4 +70,12 @@ public final class MathUtils { public static float setBetween(float number, float min, float max) { return number > max ? max : number < min ? min : number; } + + public static int clamp(int value, int min, int max) { + if (value < min) { + return min; + } else { + return Math.min(value, max); + } + } }