This commit is contained in:
aPunch 2012-01-20 07:15:14 -06:00
parent a240b71573
commit 204023728c
4 changed files with 43 additions and 9 deletions

Binary file not shown.

View File

@ -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;
}

View File

@ -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<Entity, NPC> spawned = new HashMap<Entity, NPC>();
private Map<Integer, NPC> byID = new HashMap<Integer, NPC>();
private Map<Entity, NPC> spawned = new ConcurrentHashMap<Entity, NPC>();
private Map<Integer, NPC> byID = new ConcurrentHashMap<Integer, NPC>();
@Override
public NPC createNPC(String name) {
@ -56,13 +56,13 @@ public class CitizensNPCManager implements NPCManager {
@Override
public Collection<NPC> getNPCs() {
return spawned.values();
return byID.values();
}
@Override
public Collection<NPC> getNPCs(Class<? extends Trait> trait) {
Set<NPC> npcs = new HashSet<NPC>();
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;

View File

@ -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<String, CraftPlayer> players = (Map<String, CraftPlayer>) f.get(null);
if (players != null)
players.remove(name);
} catch (Exception ex) {
Messaging.log("Unable to fetch player map from CraftEntity: " + ex.getMessage());
}
}
}