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) {
// 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"));
// Load the character if it exists
@ -134,6 +127,13 @@ public abstract class CitizensNPC extends AbstractNPC {
ex.getMessage());
}
}
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn()) {
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
if (spawnLoc != null)
spawn(spawnLoc);
}
}
@Override

View File

@ -17,8 +17,6 @@ public class Powered extends Trait implements Toggleable {
@Override
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("");
}
@ -36,7 +34,8 @@ public class Powered extends Trait implements Toggleable {
@Override
public boolean toggle() {
powered = !powered;
((Creeper) npc.getBukkitEntity()).setPowered(powered);
if (npc.getBukkitEntity() instanceof Creeper)
((Creeper) npc.getBukkitEntity()).setPowered(powered);
return powered;
}

View File

@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
public class Saddle extends Trait implements Toggleable, Listener {
private final NPC npc;
private boolean saddle;
private boolean pig;
public Saddle(NPC npc) {
this.npc = npc;
@ -21,20 +22,21 @@ public class Saddle extends Trait implements Toggleable, Listener {
@Override
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("");
}
@Override
public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Pig)
if (npc.getBukkitEntity() instanceof Pig) {
((Pig) npc.getBukkitEntity()).setSaddle(saddle);
pig = true;
} else
pig = false;
}
@EventHandler
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);
}
@ -46,7 +48,8 @@ public class Saddle extends Trait implements Toggleable, Listener {
@Override
public boolean toggle() {
saddle = !saddle;
((Pig) npc.getBukkitEntity()).setSaddle(saddle);
if (pig)
((Pig) npc.getBukkitEntity()).setSaddle(saddle);
return saddle;
}

View File

@ -21,8 +21,6 @@ public class Sheared extends Trait implements Toggleable, Listener {
@Override
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("");
}
@ -45,7 +43,8 @@ public class Sheared extends Trait implements Toggleable, Listener {
@Override
public boolean toggle() {
sheared = !sheared;
((Sheep) npc.getBukkitEntity()).setSheared(sheared);
if (npc.getBukkitEntity() instanceof Sheep)
((Sheep) npc.getBukkitEntity()).setSheared(sheared);
return sheared;
}

View File

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

View File

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