Just use instanceofs

This commit is contained in:
fullwall 2012-05-18 00:06:31 +08:00
parent 841f378750
commit 08e5f8223d
6 changed files with 28 additions and 23 deletions

View File

@ -96,13 +96,6 @@ public abstract class CitizensNPC extends AbstractNPC {
} }
public void load(DataKey root) { public void load(DataKey root) {
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn()) {
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
if (spawnLoc != null)
spawn(spawnLoc);
}
Character character = CitizensAPI.getCharacterManager().getCharacter(root.getString("character")); Character character = CitizensAPI.getCharacterManager().getCharacter(root.getString("character"));
// Load the character if it exists // Load the character if it exists
@ -134,6 +127,13 @@ public abstract class CitizensNPC extends AbstractNPC {
ex.getMessage()); ex.getMessage());
} }
} }
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn()) {
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
if (spawnLoc != null)
spawn(spawnLoc);
}
} }
@Override @Override

View File

@ -17,8 +17,6 @@ public class Powered extends Trait implements Toggleable {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Creeper))
throw new NPCLoadException("NPC must be a creeper");
powered = key.getBoolean(""); powered = key.getBoolean("");
} }
@ -36,7 +34,8 @@ public class Powered extends Trait implements Toggleable {
@Override @Override
public boolean toggle() { public boolean toggle() {
powered = !powered; powered = !powered;
((Creeper) npc.getBukkitEntity()).setPowered(powered); if (npc.getBukkitEntity() instanceof Creeper)
((Creeper) npc.getBukkitEntity()).setPowered(powered);
return powered; return powered;
} }

View File

@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
public class Saddle extends Trait implements Toggleable, Listener { public class Saddle extends Trait implements Toggleable, Listener {
private final NPC npc; private final NPC npc;
private boolean saddle; private boolean saddle;
private boolean pig;
public Saddle(NPC npc) { public Saddle(NPC npc) {
this.npc = npc; this.npc = npc;
@ -21,20 +22,21 @@ public class Saddle extends Trait implements Toggleable, Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Pig))
throw new NPCLoadException("NPC must be a pig to have this trait");
saddle = key.getBoolean(""); saddle = key.getBoolean("");
} }
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Pig) if (npc.getBukkitEntity() instanceof Pig) {
((Pig) npc.getBukkitEntity()).setSaddle(saddle); ((Pig) npc.getBukkitEntity()).setSaddle(saddle);
pig = true;
} else
pig = false;
} }
@EventHandler @EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked()))) if (pig && npc.equals(CitizensAPI.getNPCRegistry().getNPC(event.getRightClicked())))
event.setCancelled(true); event.setCancelled(true);
} }
@ -46,7 +48,8 @@ public class Saddle extends Trait implements Toggleable, Listener {
@Override @Override
public boolean toggle() { public boolean toggle() {
saddle = !saddle; saddle = !saddle;
((Pig) npc.getBukkitEntity()).setSaddle(saddle); if (pig)
((Pig) npc.getBukkitEntity()).setSaddle(saddle);
return saddle; return saddle;
} }

View File

@ -21,8 +21,6 @@ public class Sheared extends Trait implements Toggleable, Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (!(npc.getBukkitEntity() instanceof Sheep))
throw new NPCLoadException("NPC must be a sheep to be sheared");
sheared = key.getBoolean(""); sheared = key.getBoolean("");
} }
@ -45,7 +43,8 @@ public class Sheared extends Trait implements Toggleable, Listener {
@Override @Override
public boolean toggle() { public boolean toggle() {
sheared = !sheared; sheared = !sheared;
((Sheep) npc.getBukkitEntity()).setSheared(sheared); if (npc.getBukkitEntity() instanceof Sheep)
((Sheep) npc.getBukkitEntity()).setSheared(sheared);
return sheared; return sheared;
} }

View File

@ -38,7 +38,8 @@ public class VillagerProfession extends Trait {
public void setProfession(Profession profession) { public void setProfession(Profession profession) {
this.profession = profession; this.profession = profession;
((Villager) npc.getBukkitEntity()).setProfession(profession); if (npc.getBukkitEntity() instanceof Villager)
((Villager) npc.getBukkitEntity()).setProfession(profession);
} }
@Override @Override

View File

@ -15,6 +15,7 @@ import org.bukkit.event.entity.SheepDyeWoolEvent;
public class WoolColor extends Trait implements Listener { public class WoolColor extends Trait implements Listener {
private DyeColor color = DyeColor.WHITE; private DyeColor color = DyeColor.WHITE;
private final NPC npc; private final NPC npc;
boolean sheep = false;
public WoolColor(NPC npc) { public WoolColor(NPC npc) {
this.npc = npc; this.npc = npc;
@ -22,8 +23,6 @@ public class WoolColor extends Trait implements Listener {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
if (npc.isSpawned() && !(npc.getBukkitEntity() instanceof Sheep))
throw new NPCLoadException("NPC must be a sheep");
try { try {
color = DyeColor.valueOf(key.getString("")); color = DyeColor.valueOf(key.getString(""));
} catch (Exception ex) { } catch (Exception ex) {
@ -33,8 +32,11 @@ public class WoolColor extends Trait implements Listener {
@Override @Override
public void onNPCSpawn() { public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Sheep) if (npc.getBukkitEntity() instanceof Sheep) {
((Sheep) npc.getBukkitEntity()).setColor(color); ((Sheep) npc.getBukkitEntity()).setColor(color);
sheep = true;
} else
sheep = false;
} }
@EventHandler @EventHandler
@ -50,7 +52,8 @@ public class WoolColor extends Trait implements Listener {
public void setColor(DyeColor color) { public void setColor(DyeColor color) {
this.color = color; this.color = color;
((Sheep) npc.getBukkitEntity()).setColor(color); if (sheep)
((Sheep) npc.getBukkitEntity()).setColor(color);
} }
@Override @Override