From 308070c1595f4cdaa31bd20d8b1ab1221eb5fd3c Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Tue, 20 Jun 2023 23:56:37 +0200 Subject: [PATCH] Hopefully fix concurrent modification exception --- .../com/alttd/datalock/EventListener.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/alttd/datalock/EventListener.java b/src/main/java/com/alttd/datalock/EventListener.java index dd18477..c5b2e8e 100644 --- a/src/main/java/com/alttd/datalock/EventListener.java +++ b/src/main/java/com/alttd/datalock/EventListener.java @@ -19,10 +19,19 @@ public class EventListener { private EventListener() {} -// private final Idempotency idempotencyStorage = new Idempotency(); + // private final Idempotency idempotencyStorage = new Idempotency(); private final HashMap> queuedLocks = new HashMap<>(); private final HashMap> channelLockMap = new HashMap<>(); + + private synchronized void putDataInChannelLockMap(ChannelIdentifier identifier, HashSet set) { + channelLockMap.put(identifier, set); + } + + private synchronized HashSet getSetFromChannelLockMap(ChannelIdentifier identifier) { + return channelLockMap.getOrDefault(identifier, new HashSet<>()); + } private final List channelIdentifierList = new ArrayList<>(); + private static EventListener instance = null; public static EventListener getInstance() { @@ -54,7 +63,7 @@ public class EventListener { } } - public void clearServer(int hashCode) { + public synchronized void clearServer(int hashCode) { channelLockMap.keySet().forEach(key -> { HashSet temp = new HashSet<>(); HashSet locks = channelLockMap.get(key); @@ -68,11 +77,11 @@ public class EventListener { if (Config.DEBUG) Logger.info("Clearing % from % due to clear server being called for the server that lock is on", lock.getData(), key.getId()); } - channelLockMap.put(key, locks); + putDataInChannelLockMap(key, locks); }); } - private String formatLockMap(HashMap> map) { + private synchronized String formatLockMap(HashMap> map) { StringBuilder stringBuilder = new StringBuilder(); for (ChannelIdentifier plugin : map.keySet()) { stringBuilder @@ -113,7 +122,7 @@ public class EventListener { return; } - HashSet hashLock = channelLockMap.getOrDefault(identifier, new HashSet<>()); + HashSet hashLock = getSetFromChannelLockMap(identifier); ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); String channel; try { @@ -221,7 +230,7 @@ public class EventListener { //Lock the data lockSet.add(lock); - channelLockMap.put(identifier, lockSet); + putDataInChannelLockMap(identifier, lockSet); sendPluginMessage(channel, true, lock.getData(), idempotency, serverConnection, identifier); } @@ -249,7 +258,7 @@ public class EventListener { { lockSet.remove(lock); queueNextLock(lockSet, lock, identifier, idempotency); - channelLockMap.put(identifier, lockSet); + putDataInChannelLockMap(identifier, lockSet); sendPluginMessage(channel, true, lock.getData(), idempotency, serverConnection, identifier); return; }