From 51184dd209907952dbee0ecefaa7969a20d313f9 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 2 Nov 2012 00:35:35 +0100 Subject: [PATCH] Ensure getEntityModifier is thread safe. FIXES Ticket-6. Don't use the world's getPlayers-method, as it's enumerating a collection that's not thread safe. This is important as getEntityModifier may be called by a client packet listener (which is async). --- .../protocol/events/PacketContainer.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java index 920611b5..5ab389c2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -26,6 +26,8 @@ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import org.bukkit.Bukkit; +import org.bukkit.Server; import org.bukkit.World; import org.bukkit.WorldType; import org.bukkit.craftbukkit.CraftWorld; @@ -268,8 +270,7 @@ public class PacketContainer implements Serializable { final Object worldServer = ((CraftWorld) world).getHandle(); final Class nmsEntityClass = net.minecraft.server.Entity.class; - final World worldCopy = world; - + if (getEntity == null) getEntity = FuzzyReflection.fromObject(worldServer).getMethodByParameters( "getEntity", nmsEntityClass, new Class[] { int.class }); @@ -296,10 +297,14 @@ public class PacketContainer implements Serializable { if (nmsEntity != null) { return nmsEntity.getBukkitEntity(); } else { - // Maybe it's a player that's just logged in? Try a search - for (Player player : worldCopy.getPlayers()) { - if (player.getEntityId() == id) { - return player; + Server server = Bukkit.getServer(); + + // Maybe it's a player that has just logged in? Try a search + if (server != null) { + for (Player player : server.getOnlinePlayers()) { + if (player.getEntityId() == id) { + return player; + } } }