From a02521b5cfed63a75efb384962e2e27514aaf245 Mon Sep 17 00:00:00 2001 From: aPunch Date: Sun, 11 Mar 2012 17:39:18 -0500 Subject: [PATCH] Added ability to change if a creeper NPC is powered. This address CITIZENS-16. --- .../command/command/NPCCommands.java | 16 +++++++ .../npc/CitizensTraitManager.java | 2 + .../java/net/citizensnpcs/trait/Powered.java | 45 +++++++++++++++++++ src/main/resources/plugin.yml | 1 + 4 files changed, 64 insertions(+) create mode 100644 src/main/java/net/citizensnpcs/trait/Powered.java diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 3a6d162cd..37f381be8 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -20,6 +20,7 @@ import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.Powered; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.StringHelper; @@ -367,4 +368,19 @@ public class NPCCommands { npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation()); Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location."); } + + @Command( + aliases = { "npc" }, + usage = "power", + desc = "Toggle a creeper NPC as powered", + modifiers = { "power" }, + min = 1, + max = 1, + permission = "npc.power") + @Requirements(selected = true, ownership = true, types = { EntityType.CREEPER }) + public void power(CommandContext args, Player player, NPC npc) { + String msg = StringHelper.wrap(npc.getName()) + " will " + + (npc.getTrait(Powered.class).toggle() ? "now" : "no longer"); + Messaging.send(player, msg += " be powered."); + } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java index 5bab8e6cc..37e0a5993 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -16,6 +16,7 @@ import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.api.trait.trait.SpawnLocation; import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.Powered; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.waypoint.Waypoints; @@ -29,6 +30,7 @@ public class CitizensTraitManager implements TraitManager { registerTrait(new TraitFactory(LookClose.class).withName("look-close")); registerTrait(new TraitFactory(MobType.class).withName("type")); registerTrait(new TraitFactory(Owner.class).withName("owner")); + registerTrait(new TraitFactory(Powered.class).withName("powered")); registerTrait(new TraitFactory(Spawned.class).withName("spawned")); registerTrait(new TraitFactory(SpawnLocation.class).withName("location")); registerTrait(new TraitFactory(Text.class).withName("text")); diff --git a/src/main/java/net/citizensnpcs/trait/Powered.java b/src/main/java/net/citizensnpcs/trait/Powered.java new file mode 100644 index 000000000..3ce053d8f --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/Powered.java @@ -0,0 +1,45 @@ +package net.citizensnpcs.trait; + +import org.bukkit.entity.Creeper; + +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.util.DataKey; + +public class Powered extends Trait implements Toggleable { + private boolean powered; + private final NPC npc; + + public Powered(NPC npc) { + this.npc = npc; + } + + @Override + public void load(DataKey key) { + powered = key.getBoolean(""); + } + + @Override + public void save(DataKey key) { + key.setBoolean("", powered); + } + + @Override + public void onNPCSpawn() { + if (npc.getBukkitEntity() instanceof Creeper) + ((Creeper) npc.getBukkitEntity()).setPowered(powered); + } + + @Override + public boolean toggle() { + powered = !powered; + if (npc.getBukkitEntity() instanceof Creeper) + ((Creeper) npc.getBukkitEntity()).setPowered(powered); + return powered; + } + + @Override + public String toString() { + return "Powered{" + powered + "}"; + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d85ff1eb9..862b20509 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -27,6 +27,7 @@ permissions: citizens.npc.help: true citizens.npc.list: true citizens.npc.owner: true + citizens.npc.power: true citizens.npc.remove: true citizens.npc.remove.all: true citizens.npc.rename: true