From 4fa02d12962db8191eee9e54a428cf6fdc2d8d93 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 15 Nov 2020 12:08:58 +0100 Subject: [PATCH] Fix CME --- .../spectatesystem/elements/REntity.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index 7225343..ff5dedc 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -41,7 +41,9 @@ public abstract class REntity { protected REntity(int internalId, Entity entity){ this.internalId = internalId; this.entity = entity; - entities.put(internalId, this); + synchronized(entities){ + entities.put(internalId, this); + } for(Player player : Bukkit.getOnlinePlayers()){ sendToPlayer(player); @@ -49,17 +51,15 @@ public abstract class REntity { } public static void playerJoins(Player player){ - for(REntity entity : entities.values()){ - entity.sendToPlayer(player); - entity.sendLocation(player); + synchronized(entities){ + for(REntity entity : entities.values()){ + entity.sendToPlayer(player); + entity.sendLocation(player); + } } } protected static EntityLiving getShooter(){ - for(REntity entity : entities.values()){ - if(entity.entity instanceof EntityLiving) - return (EntityLiving) entity.entity; - } return ((CraftPlayer)Bukkit.getOnlinePlayers().iterator().next()).getHandle(); } @@ -69,10 +69,12 @@ public abstract class REntity { public static void removeAll(){ int entity_counter = 0; - while(!entities.isEmpty()){ - entity_counter++; - REntity entity = entities.values().iterator().next(); - entity.remove(); + synchronized (entities){ + while(!entities.isEmpty()){ + entity_counter++; + REntity entity = entities.values().iterator().next(); + entity.remove(); + } } System.out.println("Has to remove manually " + entity_counter + " entities!"); } @@ -96,7 +98,9 @@ public abstract class REntity { connection.sendPacket(packet); } Bukkit.getScheduler().runTask(SpectateSystem.get(), entity::die); - entities.remove(internalId); + synchronized (entities){ + entities.remove(internalId); + } } public void sneak(boolean sneaking) {