diff --git a/lib/CitizensAPI.jar b/lib/CitizensAPI.jar index c98b32c78..5f3305c28 100644 Binary files a/lib/CitizensAPI.jar and b/lib/CitizensAPI.jar differ diff --git a/src/net/citizensnpcs/npc/CitizensNPC.java b/src/net/citizensnpcs/npc/CitizensNPC.java index da9cf6bf7..931e251e8 100644 --- a/src/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/net/citizensnpcs/npc/CitizensNPC.java @@ -17,6 +17,7 @@ import net.citizensnpcs.util.Messaging; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.entity.Entity; public class CitizensNPC implements NPC { private final int id; @@ -87,10 +88,7 @@ public class CitizensNPC implements NPC { if (t != null) { return trait.cast(t); } - - T create = factory.create(trait); - addTrait(trait); - return create; + return null; } @Override @@ -173,6 +171,11 @@ public class CitizensNPC implements NPC { manager.remove(this); } + @Override + public Entity getBukkitEntity() { + return mcEntity.getBukkitEntity(); + } + public CraftNPC getHandle() { return mcEntity; } diff --git a/src/net/citizensnpcs/npc/CitizensNPCManager.java b/src/net/citizensnpcs/npc/CitizensNPCManager.java index 30012fe00..8358418ce 100644 --- a/src/net/citizensnpcs/npc/CitizensNPCManager.java +++ b/src/net/citizensnpcs/npc/CitizensNPCManager.java @@ -1,10 +1,10 @@ package net.citizensnpcs.npc; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -29,8 +29,8 @@ import net.minecraft.server.Packet29DestroyEntity; import net.minecraft.server.WorldServer; public class CitizensNPCManager implements NPCManager { - private Map spawned = new HashMap(); - private Map byID = new HashMap(); + private Map spawned = new ConcurrentHashMap(); + private Map byID = new ConcurrentHashMap(); @Override public NPC createNPC(String name) { @@ -56,13 +56,13 @@ public class CitizensNPCManager implements NPCManager { @Override public Collection getNPCs() { - return spawned.values(); + return byID.values(); } @Override public Collection getNPCs(Class trait) { Set npcs = new HashSet(); - for (NPC npc : spawned.values()) { + for (NPC npc : byID.values()) { if (npc.hasTrait(trait)) npcs.add(npc); } @@ -88,9 +88,11 @@ public class CitizensNPCManager implements NPCManager { WorldServer ws = getWorldServer(loc.getWorld()); CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, npc.getFullName(), new ItemInWorldManager(ws)); + mcEntity.removeFromPlayerMap(npc.getFullName()); mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); ws.addEntity(mcEntity); ws.players.remove(mcEntity); + // mcEntity.removeFromPlayerMap(npc.getFullName()); spawned.put(mcEntity.getPlayer(), npc); return mcEntity; diff --git a/src/net/citizensnpcs/resources/lib/CraftNPC.java b/src/net/citizensnpcs/resources/lib/CraftNPC.java index 539dd91fe..c25d63487 100644 --- a/src/net/citizensnpcs/resources/lib/CraftNPC.java +++ b/src/net/citizensnpcs/resources/lib/CraftNPC.java @@ -1,7 +1,14 @@ package net.citizensnpcs.resources.lib; import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Map; +import org.bukkit.craftbukkit.entity.CraftEntity; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Entity; + +import net.citizensnpcs.util.Messaging; import net.minecraft.server.EntityPlayer; import net.minecraft.server.ItemInWorldManager; import net.minecraft.server.MinecraftServer; @@ -32,4 +39,26 @@ public class CraftNPC extends EntityPlayer { e.printStackTrace(); } } + + @Override + public Entity getBukkitEntity() { + if (this.bukkitEntity == null) { + super.getBukkitEntity(); + removeFromPlayerMap(name); + } + return super.getBukkitEntity(); + } + + @SuppressWarnings("unchecked") + public void removeFromPlayerMap(String name) { + try { + Field f = CraftEntity.class.getDeclaredField("players"); + f.setAccessible(true); + Map players = (Map) f.get(null); + if (players != null) + players.remove(name); + } catch (Exception ex) { + Messaging.log("Unable to fetch player map from CraftEntity: " + ex.getMessage()); + } + } } \ No newline at end of file