From 8efeb3fe25915ec4d65f1f5b858716dfe01ce5e9 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Fri, 27 Dec 2019 03:03:12 -0900 Subject: [PATCH] Fix exception when removing all entities --- .../network/session/cache/EntityCache.java | 11 ++++------- .../geysermc/connector/utils/DimensionUtils.java | 13 ++++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java index fd0bd7c5a..a248327ae 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java @@ -25,10 +25,7 @@ package org.geysermc.connector.network.session.cache; -import it.unimi.dsi.fastutil.longs.Long2LongMap; -import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.longs.*; import lombok.Getter; import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.PlayerEntity; @@ -45,9 +42,9 @@ public class EntityCache { private GeyserSession session; @Getter - private Long2ObjectMap entities = new Long2ObjectOpenHashMap<>(); - private Long2LongMap entityIdTranslations = new Long2LongOpenHashMap(); - private Map playerEntities = new HashMap<>(); + private Long2ObjectMap entities = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>()); + private Long2LongMap entityIdTranslations = Long2LongMaps.synchronize(new Long2LongOpenHashMap()); + private Map playerEntities = Collections.synchronizedMap(new HashMap<>()); private Map bossbars = new HashMap<>(); @Getter diff --git a/connector/src/main/java/org/geysermc/connector/utils/DimensionUtils.java b/connector/src/main/java/org/geysermc/connector/utils/DimensionUtils.java index 2f559da36..970532718 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/DimensionUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/DimensionUtils.java @@ -6,6 +6,9 @@ import org.geysermc.connector.entity.Entity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.TranslatorsInit; +import java.util.ArrayList; +import java.util.List; + public class DimensionUtils { public static void switchDimension(GeyserSession session, int javaDimension, boolean fake) { int bedrockDimension = javaToBedrock(javaDimension); @@ -15,13 +18,9 @@ public class DimensionUtils { Vector3i pos = Vector3i.from(0, 32767, 0); - //FIXME: chance of exception while iterating - try { - for (Entity entity : session.getEntityCache().getEntities().values()) { - session.getEntityCache().removeEntity(entity, false); - } - } catch (Exception e) { - e.printStackTrace(); + List entities = new ArrayList<>(session.getEntityCache().getEntities().values()); + for (Entity entity : entities) { + session.getEntityCache().removeEntity(entity, false); } ChangeDimensionPacket changeDimensionPacket = new ChangeDimensionPacket();