This commit is contained in:
aPunch 2012-01-14 22:23:58 -06:00
parent cf1bb89509
commit ac7f58b089
3 changed files with 46 additions and 28 deletions

Binary file not shown.

View File

@ -6,7 +6,6 @@ import java.util.Set;
import net.citizensnpcs.api.Citizens; import net.citizensnpcs.api.Citizens;
import net.citizensnpcs.api.event.NPCDespawnEvent; import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent; import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.exception.NPCException;
import net.citizensnpcs.api.npc.trait.Character; import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.pathfinding.Navigator; import net.citizensnpcs.api.npc.pathfinding.Navigator;
@ -23,26 +22,28 @@ public class CitizensNPC implements NPC {
private Character character = null; private Character character = null;
private CraftNPC mcEntity; private CraftNPC mcEntity;
private boolean spawned; private boolean spawned;
private final CitizensNPCManager manager;
protected CitizensNPC(Character character, Trait... traits) { protected CitizensNPC(Character character, Trait... traits) {
this.character = character; this.character = character;
for (Trait trait : traits) { for (Trait trait : traits) {
this.traits.add(trait); this.traits.add(trait);
} }
this.id = ((CitizensNPCManager) Citizens.getNPCManager()).getUniqueID(); manager = (CitizensNPCManager) Citizens.getNPCManager();
id = manager.getUniqueID();
} }
@Override @Override
public void addTrait(Trait trait) throws NPCException { public void addTrait(Trait trait) {
if (!hasTrait(trait)) if (!hasTrait(trait))
traits.add(trait); traits.add(trait);
else else
throw new NPCException("The NPC already has the trait '" + trait.getName() + "'."); System.out.println("The NPC already has the trait '" + trait.getName() + "'.");
} }
@Override @Override
public void addTrait(String name) throws NPCException { public void addTrait(String name) {
addTrait(Citizens.getTraitManager().getTrait(name)); addTrait(getTrait(name));
} }
@Override @Override
@ -83,25 +84,29 @@ public class CitizensNPC implements NPC {
@Override @Override
public boolean hasTrait(String name) { public boolean hasTrait(String name) {
return hasTrait(Citizens.getTraitManager().getTrait(name)); return hasTrait(getTrait(name));
} }
@Override @Override
public void removeTrait(Trait trait) throws NPCException { public void removeTrait(Trait trait) {
if (!hasTrait(trait)) if (!hasTrait(trait)) {
throw new NPCException("The NPC does not have a trait with the name of '" + trait.getName() + "."); System.out.println("The NPC does not have a trait with the name of '" + trait.getName() + ".");
return;
}
traits.remove(trait); traits.remove(trait);
} }
@Override @Override
public void removeTrait(String name) throws NPCException { public void removeTrait(String name) {
removeTrait(Citizens.getTraitManager().getTrait(name)); removeTrait(getTrait(name));
} }
@Override @Override
public void setCharacter(Character character) throws NPCException { public void setCharacter(Character character) {
if (this.character.equals(character)) if (this.character.equals(character)) {
throw new NPCException("The NPC already has the character '" + character.getName() + "'."); System.out.println("The NPC already has the character '" + character.getName() + "'.");
return;
}
this.character = character; this.character = character;
} }
@ -111,9 +116,11 @@ public class CitizensNPC implements NPC {
} }
@Override @Override
public void spawn(Location loc) throws NPCException { public void spawn(Location loc) {
if (spawned) if (spawned) {
throw new NPCException("The NPC is already spawned."); System.out.println("The NPC is already spawned.");
return;
}
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc); NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc);
Bukkit.getPluginManager().callEvent(spawnEvent); Bukkit.getPluginManager().callEvent(spawnEvent);
@ -121,23 +128,28 @@ public class CitizensNPC implements NPC {
return; return;
} }
addTrait(new LocationTrait(loc)); addTrait(new LocationTrait(loc));
mcEntity = ((CitizensNPCManager) Citizens.getNPCManager()).spawn(this); mcEntity = manager.spawn(this);
} }
@Override @Override
public void despawn() throws NPCException { public void despawn() {
if (!spawned) if (!spawned) {
throw new NPCException("The NPC is already despawned."); System.out.println("The NPC is already despawned.");
return;
}
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this)); Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
mcEntity.die(); mcEntity.die();
((CitizensNPCManager) Citizens.getNPCManager()).despawn(this); manager.despawn(this);
} }
@Override @Override
public void remove() { public void remove() {
// TODO if (spawned) {
despawn();
}
manager.remove(this);
} }
public CraftNPC getHandle() { public CraftNPC getHandle() {

View File

@ -13,7 +13,6 @@ import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import net.citizensnpcs.api.Citizens;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager; import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.trait.Character; import net.citizensnpcs.api.npc.trait.Character;
@ -74,7 +73,13 @@ public class CitizensNPCManager implements NPCManager {
@Override @Override
public Collection<NPC> getNPCs(String name) { public Collection<NPC> getNPCs(String name) {
return getNPCs(Citizens.getTraitManager().getTrait(name)); Set<NPC> npcs = new HashSet<NPC>();
for (NPC npc : spawned.values()) {
if (npc.hasTrait(name)) {
npcs.add(npc);
}
}
return npcs;
} }
@Override @Override
@ -111,14 +116,15 @@ public class CitizensNPCManager implements NPCManager {
} }
public void remove(NPC npc) { public void remove(NPC npc) {
despawn(npc);
byID.remove(npc.getId()); byID.remove(npc.getId());
} }
private WorldServer getWorldServer(World world) { public WorldServer getWorldServer(World world) {
return ((CraftWorld) world).getHandle(); return ((CraftWorld) world).getHandle();
} }
private MinecraftServer getMinecraftServer(Server server) { public MinecraftServer getMinecraftServer(Server server) {
return ((CraftServer) server).getServer(); return ((CraftServer) server).getServer();
} }
} }