From d042ff8b3b3003977565ac7e774158ce7a5754c2 Mon Sep 17 00:00:00 2001 From: aPunch Date: Sun, 18 Mar 2012 18:11:13 -0500 Subject: [PATCH] Added ability to equip Pig NPCs with saddles. This addresses CITIZENS-6. --- .../command/command/NPCCommands.java | 2 + .../npc/CitizensTraitManager.java | 2 + .../npc/entity/CitizensPigNPC.java | 28 +++++++++- .../java/net/citizensnpcs/trait/Saddle.java | 56 +++++++++++++++++++ .../trait/VillagerProfession.java | 1 + 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/citizensnpcs/trait/Saddle.java diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index a28033322..e74409e4a 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -23,6 +23,7 @@ import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; +import net.citizensnpcs.trait.Saddle; import net.citizensnpcs.trait.VillagerProfession; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; @@ -125,6 +126,7 @@ public class NPCCommands { npc.getTrait(MobType.class).setType(type.toString()); npc.addTrait(traitManager.getTrait(LookClose.class, npc)); npc.addTrait(traitManager.getTrait(Text.class, npc)); + npc.addTrait(traitManager.getTrait(Saddle.class, npc)); npc.spawn(player.getLocation()); diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java index b9f43492e..de663ba27 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -18,6 +18,7 @@ import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; +import net.citizensnpcs.trait.Saddle; import net.citizensnpcs.trait.Sheared; import net.citizensnpcs.trait.VillagerProfession; import net.citizensnpcs.trait.WoolColor; @@ -38,6 +39,7 @@ public class CitizensTraitManager implements TraitManager { registerTrait(new TraitFactory(MobType.class).withName("type")); registerTrait(new TraitFactory(Owner.class).withName("owner")); registerTrait(new TraitFactory(Powered.class).withName("powered")); + registerTrait(new TraitFactory(Saddle.class).withName("saddle")); registerTrait(new TraitFactory(Sheared.class).withName("sheared")); registerTrait(new TraitFactory(Spawned.class).withName("spawned")); registerTrait(new TraitFactory(Text.class).withName("text")); diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java index 24b714384..9289404d1 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensPigNPC.java @@ -1,17 +1,24 @@ package net.citizensnpcs.npc.entity; import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.editor.Equipable; import net.citizensnpcs.npc.CitizensMobNPC; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.ai.NPCHandle; +import net.citizensnpcs.trait.Saddle; +import net.citizensnpcs.util.Messaging; +import net.citizensnpcs.util.StringHelper; import net.minecraft.server.EntityPig; import net.minecraft.server.EntityWeatherLighting; import net.minecraft.server.PathfinderGoalSelector; import net.minecraft.server.World; +import org.bukkit.Material; import org.bukkit.entity.Pig; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; -public class CitizensPigNPC extends CitizensMobNPC { +public class CitizensPigNPC extends CitizensMobNPC implements Equipable { public CitizensPigNPC(CitizensNPCManager manager, int id, String name) { super(manager, id, name, EntityPigNPC.class); @@ -22,6 +29,25 @@ public class CitizensPigNPC extends CitizensMobNPC { return (Pig) getHandle().getBukkitEntity(); } + @Override + public void equip(Player equipper) { + ItemStack hand = equipper.getItemInHand(); + if (hand.getType() == Material.SADDLE) { + if (!getBukkitEntity().hasSaddle()) { + getTrait(Saddle.class).toggle(); + equipper.setItemInHand(null); + Messaging.send(equipper, StringHelper.wrap(getName()) + " is now saddled."); + } + } else { + if (getBukkitEntity().hasSaddle()) { + equipper.getWorld().dropItemNaturally(getBukkitEntity().getLocation(), + new ItemStack(Material.SADDLE, 1)); + getTrait(Saddle.class).toggle(); + Messaging.send(equipper, StringHelper.wrap(getName()) + " is no longer saddled."); + } + } + } + public static class EntityPigNPC extends EntityPig implements NPCHandle { private final NPC npc; diff --git a/src/main/java/net/citizensnpcs/trait/Saddle.java b/src/main/java/net/citizensnpcs/trait/Saddle.java new file mode 100644 index 000000000..9487fcce7 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/Saddle.java @@ -0,0 +1,56 @@ +package net.citizensnpcs.trait; + +import org.bukkit.entity.Pig; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.exception.NPCLoadException; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.util.DataKey; + +public class Saddle extends Trait implements Toggleable, Listener { + private boolean saddle; + private final NPC npc; + + public Saddle(NPC npc) { + this.npc = npc; + } + + @Override + public void load(DataKey key) throws NPCLoadException { + saddle = key.getBoolean(""); + } + + @Override + public void save(DataKey key) { + key.setBoolean("", saddle); + } + + @Override + public void onNPCSpawn() { + if (npc.getBukkitEntity() instanceof Pig) + ((Pig) npc.getBukkitEntity()).setSaddle(saddle); + } + + @Override + public boolean toggle() { + saddle = !saddle; + ((Pig) npc.getBukkitEntity()).setSaddle(saddle); + return saddle; + } + + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + // TODO: Complete trait/plugin system + if (CitizensAPI.getNPCManager().isNPC(event.getRightClicked())) + event.setCancelled(true); + } + + @Override + public String toString() { + return "Saddle{" + saddle + "}"; + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/VillagerProfession.java b/src/main/java/net/citizensnpcs/trait/VillagerProfession.java index 6375f8ea8..a65a15fd5 100644 --- a/src/main/java/net/citizensnpcs/trait/VillagerProfession.java +++ b/src/main/java/net/citizensnpcs/trait/VillagerProfession.java @@ -38,6 +38,7 @@ public class VillagerProfession extends Trait { public void setProfession(Profession profession) { this.profession = profession; + ((Villager) npc.getBukkitEntity()).setProfession(profession); } @Override