added exceptions

This commit is contained in:
aPunch 2012-01-14 21:52:31 -06:00
parent 456c9bfceb
commit 46eb073031
4 changed files with 35 additions and 53 deletions

Binary file not shown.

View File

@ -6,11 +6,12 @@ import java.util.Set;
import net.citizensnpcs.api.Citizens;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.exception.NPCException;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.pathfinding.Navigator;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.npc.trait.LocationTrait;
import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
import net.citizensnpcs.resources.lib.CraftNPC;
import org.bukkit.Bukkit;
@ -21,6 +22,7 @@ public class CitizensNPC implements NPC {
private final Set<Trait> traits = new HashSet<Trait>();
private Character character = null;
private CraftNPC mcEntity;
private boolean spawned;
protected CitizensNPC(Character character, Trait... traits) {
this.character = character;
@ -31,15 +33,15 @@ public class CitizensNPC implements NPC {
}
@Override
public void addTrait(Trait trait) {
public void addTrait(Trait trait) throws NPCException {
if (!hasTrait(trait))
traits.add(trait);
else
throw new IllegalArgumentException("The NPC already has the trait '" + trait.getName() + "'.");
throw new NPCException("The NPC already has the trait '" + trait.getName() + "'.");
}
@Override
public void addTrait(String name) {
public void addTrait(String name) throws NPCException {
addTrait(Citizens.getTraitManager().getTrait(name));
}
@ -85,24 +87,34 @@ public class CitizensNPC implements NPC {
}
@Override
public void removeTrait(Trait trait) {
public void removeTrait(Trait trait) throws NPCException {
if (!hasTrait(trait))
throw new NPCException("The NPC does not have a trait with the name of '" + trait.getName() + ".");
traits.remove(trait);
}
@Override
public void removeTrait(String name) {
public void removeTrait(String name) throws NPCException {
removeTrait(Citizens.getTraitManager().getTrait(name));
}
@Override
public void setCharacter(Character character) {
public void setCharacter(Character character) throws NPCException {
if (this.character.equals(character))
throw new IllegalArgumentException("The NPC already has the character '" + character.getName() + "'.");
throw new NPCException("The NPC already has the character '" + character.getName() + "'.");
this.character = character;
}
@Override
public void spawn(Location loc) {
public boolean isSpawned() {
return spawned;
}
@Override
public void spawn(Location loc) throws NPCException {
if (spawned)
throw new NPCException("The NPC is already spawned.");
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc);
Bukkit.getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
@ -113,13 +125,21 @@ public class CitizensNPC implements NPC {
}
@Override
public void despawn() {
public void despawn() throws NPCException {
if (!spawned)
throw new NPCException("The NPC is already despawned.");
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
mcEntity.die();
((CitizensNPCManager) Citizens.getNPCManager()).despawn(this);
}
@Override
public void remove() {
// TODO
}
public CraftNPC getHandle() {
return mcEntity;
}

View File

@ -18,7 +18,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.npc.trait.LocationTrait;
import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
import net.citizensnpcs.resources.lib.CraftNPC;
import net.minecraft.server.ItemInWorldManager;
@ -27,6 +27,7 @@ import net.minecraft.server.WorldServer;
public class CitizensNPCManager implements NPCManager {
private Map<LivingEntity, NPC> spawned = new HashMap<LivingEntity, NPC>();
private Map<Integer, NPC> byID = new HashMap<Integer, NPC>();
@Override
public NPC createNPC() {
@ -40,7 +41,9 @@ public class CitizensNPCManager implements NPCManager {
@Override
public NPC createNPC(Character character, Trait... traits) {
return new CitizensNPC(character, traits);
CitizensNPC npc = new CitizensNPC(character, traits);
byID.put(npc.getId(), npc);
return npc;
}
@Override

View File

@ -1,41 +0,0 @@
package net.citizensnpcs.npc.trait;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import net.citizensnpcs.api.npc.trait.Trait;
public class LocationTrait implements Trait {
private Location loc;
public LocationTrait(Location loc) {
this.loc = loc;
}
@Override
public String getName() {
return "location";
}
@Override
public void load(ConfigurationSection cs) {
loc = new Location(Bukkit.getWorld(cs.getString("location.world")), cs.getDouble("location.x"),
cs.getDouble("location.y"), cs.getDouble("location.z"), (float) cs.getDouble("location.pitch"),
(float) cs.getDouble("location.yaw"));
}
@Override
public void save(ConfigurationSection cs) {
cs.set("location.world", loc.getWorld());
cs.set("location.x", loc.getX());
cs.set("location.y", loc.getY());
cs.set("location.z", loc.getZ());
cs.set("location.pitch", loc.getPitch());
cs.set("location.yaw", loc.getYaw());
}
public Location getLocation() {
return loc;
}
}