From 114c83ecdd30dac922fcb6b8fe83ba9e9e04cf6d Mon Sep 17 00:00:00 2001 From: aPunch Date: Sun, 18 Mar 2012 16:14:17 -0500 Subject: [PATCH] Added ability to modify NPC age. This addresses CITIZENS-10. --- .../command/command/AdminCommands.java | 22 +-- .../command/command/EditorCommands.java | 27 +-- .../command/command/HelpCommands.java | 18 +- .../command/command/NPCCommands.java | 184 +++++++----------- .../npc/CitizensTraitManager.java | 2 + src/main/java/net/citizensnpcs/trait/Age.java | 64 ++++++ src/main/resources/plugin.yml | 1 + 7 files changed, 146 insertions(+), 172 deletions(-) create mode 100644 src/main/java/net/citizensnpcs/trait/Age.java diff --git a/src/main/java/net/citizensnpcs/command/command/AdminCommands.java b/src/main/java/net/citizensnpcs/command/command/AdminCommands.java index 4e1a68c61..191f92412 100644 --- a/src/main/java/net/citizensnpcs/command/command/AdminCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/AdminCommands.java @@ -24,21 +24,14 @@ public class AdminCommands { @Command(aliases = { "citizens" }, desc = "Show basic plugin information", max = 0, permission = "admin") public void citizens(CommandContext args, Player player, NPC npc) { - Messaging.send(player, " " - + StringHelper.wrapHeader("Citizens v" + plugin.getDescription().getVersion())); + Messaging.send(player, + " " + StringHelper.wrapHeader("Citizens v" + plugin.getDescription().getVersion())); Messaging.send(player, " <7>-- Written by fullwall and aPunch"); Messaging.send(player, " <7>-- Source: http://github.com/CitizensDev"); Messaging.send(player, " <7>-- Website: " + plugin.getDescription().getWebsite()); } - @Command( - aliases = { "citizens" }, - usage = "reload", - desc = "Reload Citizens", - modifiers = { "reload" }, - min = 1, - max = 1, - permission = "admin") + @Command(aliases = { "citizens" }, usage = "reload", desc = "Reload Citizens", modifiers = { "reload" }, min = 1, max = 1, permission = "admin") @ServerCommand public void reload(CommandContext args, CommandSender sender, NPC npc) throws CommandException { Messaging.send(sender, "Reloading Citizens..."); @@ -51,14 +44,7 @@ public class AdminCommands { } } - @Command( - aliases = { "citizens" }, - usage = "save", - desc = "Save NPCs", - modifiers = { "save" }, - min = 1, - max = 1, - permission = "admin") + @Command(aliases = { "citizens" }, usage = "save", desc = "Save NPCs", modifiers = { "save" }, min = 1, max = 1, permission = "admin") @ServerCommand public void save(CommandContext args, CommandSender sender, NPC npc) { Messaging.send(sender, "Saving Citizens..."); diff --git a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java index d3d71d7a4..58934f0ce 100644 --- a/src/main/java/net/citizensnpcs/command/command/EditorCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/EditorCommands.java @@ -15,40 +15,19 @@ import org.bukkit.entity.Player; @Requirements(selected = true, ownership = true) public class EditorCommands { - @Command( - aliases = { "npc" }, - usage = "equip", - desc = "Toggle the equipment editor", - modifiers = { "equip" }, - min = 1, - max = 1, - permission = "npc.edit.equip") + @Command(aliases = { "npc" }, usage = "equip", desc = "Toggle the equipment editor", modifiers = { "equip" }, min = 1, max = 1, permission = "npc.edit.equip") @Requirements(selected = true, ownership = true, types = { EntityType.ENDERMAN, EntityType.PLAYER, EntityType.PIG, EntityType.SHEEP }) public void equip(CommandContext args, Player player, NPC npc) { Editor.enterOrLeave(player, new EquipmentEditor(player, npc)); } - @Command( - aliases = { "npc" }, - usage = "path", - desc = "Toggle the waypoint editor", - modifiers = { "path" }, - min = 1, - max = 1, - permission = "npc.edit.path") + @Command(aliases = { "npc" }, usage = "path", desc = "Toggle the waypoint editor", modifiers = { "path" }, min = 1, max = 1, permission = "npc.edit.path") public void path(CommandContext args, Player player, NPC npc) { Editor.enterOrLeave(player, npc.getTrait(Waypoints.class).getEditor(player)); } - @Command( - aliases = { "npc" }, - usage = "text", - desc = "Toggle the text editor", - modifiers = { "text" }, - min = 1, - max = 1, - permission = "npc.edit.text") + @Command(aliases = { "npc" }, usage = "text", desc = "Toggle the text editor", modifiers = { "text" }, min = 1, max = 1, permission = "npc.edit.text") public void text(CommandContext args, Player player, NPC npc) { Editor.enterOrLeave(player, npc.getTrait(Text.class).getEditor(player)); } diff --git a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java index e656bf0d9..3213a3455 100644 --- a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java @@ -25,14 +25,7 @@ public class HelpCommands { cmdManager = plugin.getCommandManager(); } - @Command( - aliases = { "citizens" }, - usage = "help (page)", - desc = "Citizens help menu", - modifiers = { "help" }, - min = 1, - max = 2, - permission = "help") + @Command(aliases = { "citizens" }, usage = "help (page)", desc = "Citizens help menu", modifiers = { "help" }, min = 1, max = 2, permission = "help") @Requirements @ServerCommand public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { @@ -45,14 +38,7 @@ public class HelpCommands { throw new CommandException("The page '" + page + "' does not exist."); } - @Command( - aliases = { "npc" }, - usage = "help (page)", - desc = "NPC help menu", - modifiers = { "help" }, - min = 1, - max = 2, - permission = "npc.help") + @Command(aliases = { "npc" }, usage = "help (page)", desc = "NPC help menu", modifiers = { "help" }, min = 1, max = 2, permission = "npc.help") @Requirements @ServerCommand public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 5bc186e8e..a28033322 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -19,6 +19,7 @@ import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensTraitManager; +import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; @@ -29,6 +30,7 @@ import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.StringHelper; import org.bukkit.ChatColor; +import org.bukkit.entity.Animals; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.Villager.Profession; @@ -44,13 +46,7 @@ public class NPCCommands { npcManager = plugin.getNPCManager(); } - @Command( - aliases = { "npc" }, - usage = "character [character]", - desc = "Set the character of a NPC", - modifiers = { "character" }, - min = 2, - max = 2) + @Command(aliases = { "npc" }, usage = "character [character]", desc = "Set the character of a NPC", modifiers = { "character" }, min = 2, max = 2) public void character(CommandContext args, Player player, NPC npc) throws CommandException { String name = args.getString(1).toLowerCase(); Character character = characterManager.getCharacter(name); @@ -73,14 +69,7 @@ public class NPCCommands { npc.setCharacter(character); } - @Command( - aliases = { "npc" }, - usage = "create [name] (--type (type) --char (char))", - desc = "Create a new NPC", - modifiers = { "create" }, - min = 2, - max = 5, - permission = "npc.create") + @Command(aliases = { "npc" }, usage = "create [name] (--type (type) --char (char))", desc = "Create a new NPC", flags = "b", modifiers = { "create" }, min = 2, max = 5, permission = "npc.create") @Requirements public void create(CommandContext args, Player player, NPC npc) { String name = args.getString(1); @@ -115,7 +104,21 @@ public class NPCCommands { msg += " with the character " + StringHelper.wrap(character); } } - msg += " at your location."; + msg += " at your location"; + + int age = 0; + if (args.hasFlag('b')) { + // TODO: Use Ageable when it is implemented + if (!Animals.class.isAssignableFrom(type.getEntityClass())) + Messaging.sendError(player, "The mob type '" + type.name().toLowerCase().replace("_", "-") + + "' cannot be aged."); + else { + age = -24000; + msg += " as a baby"; + } + } + + msg += "."; // Initialize necessary traits npc.getTrait(Owner.class).setOwner(player.getName()); @@ -124,33 +127,22 @@ public class NPCCommands { npc.addTrait(traitManager.getTrait(Text.class, npc)); npc.spawn(player.getLocation()); + + // Set age after entity spawns + npc.getTrait(Age.class).setAge(age); + npcManager.selectNPC(player, npc); Messaging.send(player, msg); } - @Command( - aliases = { "npc" }, - usage = "despawn", - desc = "Despawn a NPC", - modifiers = { "despawn" }, - min = 1, - max = 1, - permission = "npc.despawn") + @Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn a NPC", modifiers = { "despawn" }, min = 1, max = 1, permission = "npc.despawn") public void despawn(CommandContext args, Player player, NPC npc) { npc.getTrait(Spawned.class).setSpawned(false); npc.despawn(); Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + "."); } - @Command( - aliases = { "npc" }, - usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))", - desc = "List NPCs", - flags = "a", - modifiers = { "list" }, - min = 1, - max = 2, - permission = "npc.list") + @Command(aliases = { "npc" }, usage = "list (page) ((-a) --owner (owner) --type (type) --char (char))", desc = "List NPCs", flags = "a", modifiers = { "list" }, min = 1, max = 2, permission = "npc.list") @Requirements public void list(CommandContext args, Player player, NPC npc) throws CommandException { List npcs = new ArrayList(); @@ -214,14 +206,8 @@ public class NPCCommands { throw new CommandException("The page '" + page + "' does not exist."); } - @Command( - aliases = { "npc" }, - usage = "lookclose", - desc = "Toggle whether a NPC will look when a player is near", - modifiers = { "lookclose", "look", "rotate" }, - min = 1, - max = 1, - permission = "npc.lookclose") + @Command(aliases = { "npc" }, usage = "lookclose", desc = "Toggle whether a NPC will look when a player is near", modifiers = { + "lookclose", "look", "rotate" }, min = 1, max = 1, permission = "npc.lookclose") public void lookClose(CommandContext args, Player player, NPC npc) { String msg = StringHelper.wrap(npc.getName()) + " will " + (npc.getTrait(LookClose.class).toggle() ? "now rotate" : "no longer rotate"); @@ -237,14 +223,7 @@ public class NPCCommands { Messaging.send(player, " Type: " + npc.getTrait(MobType.class).getType()); } - @Command( - aliases = { "npc" }, - usage = "owner [name]", - desc = "Set the owner of an NPC", - modifiers = { "owner" }, - min = 2, - max = 2, - permission = "npc.owner") + @Command(aliases = { "npc" }, usage = "owner [name]", desc = "Set the owner of an NPC", modifiers = { "owner" }, min = 2, max = 2, permission = "npc.owner") public void owner(CommandContext args, Player player, NPC npc) throws CommandException { String name = args.getString(1); if (npc.getTrait(Owner.class).getOwner().equals(name)) @@ -254,13 +233,7 @@ public class NPCCommands { + "."); } - @Command( - aliases = { "npc" }, - usage = "remove (all)", - desc = "Remove a NPC", - modifiers = { "remove" }, - min = 1, - max = 2) + @Command(aliases = { "npc" }, usage = "remove (all)", desc = "Remove a NPC", modifiers = { "remove" }, min = 1, max = 2) @Requirements public void remove(CommandContext args, Player player, NPC npc) throws CommandException { if (args.argsLength() == 2) { @@ -282,14 +255,7 @@ public class NPCCommands { Messaging.send(player, "You permanently removed " + StringHelper.wrap(npc.getName()) + "."); } - @Command( - aliases = { "npc" }, - usage = "rename [name]", - desc = "Rename a NPC", - modifiers = { "rename" }, - min = 2, - max = 2, - permission = "npc.rename") + @Command(aliases = { "npc" }, usage = "rename [name]", desc = "Rename a NPC", modifiers = { "rename" }, min = 2, max = 2, permission = "npc.rename") public void rename(CommandContext args, Player player, NPC npc) { String oldName = npc.getName(); String newName = args.getString(1); @@ -303,14 +269,7 @@ public class NPCCommands { + "."); } - @Command( - aliases = { "npc" }, - usage = "select [id]", - desc = "Select a NPC with the given ID", - modifiers = { "select" }, - min = 2, - max = 2, - permission = "npc.select") + @Command(aliases = { "npc" }, usage = "select [id]", desc = "Select a NPC with the given ID", modifiers = { "select" }, min = 2, max = 2, permission = "npc.select") @Requirements(ownership = true) public void select(CommandContext args, Player player, NPC npc) throws CommandException { NPC toSelect = npcManager.getNPC(args.getInteger(1)); @@ -322,14 +281,7 @@ public class NPCCommands { Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect); } - @Command( - aliases = { "npc" }, - usage = "spawn [id]", - desc = "Spawn an existing NPC", - modifiers = { "spawn" }, - min = 2, - max = 2, - permission = "npc.spawn") + @Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" }, min = 2, max = 2, permission = "npc.spawn") @Requirements public void spawn(CommandContext args, Player player, NPC npc) throws CommandException { NPC respawn = npcManager.getNPC(args.getInteger(1)); @@ -348,14 +300,7 @@ public class NPCCommands { + " Use '/npc tphere' to teleport the NPC to your location."); } - @Command( - aliases = { "npc" }, - usage = "tp", - desc = "Teleport to a NPC", - modifiers = { "tp", "teleport" }, - min = 1, - max = 1, - permission = "npc.tp") + @Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to a NPC", modifiers = { "tp", "teleport" }, min = 1, max = 1, permission = "npc.tp") public void tp(CommandContext args, Player player, NPC npc) { // Spawn the NPC if it isn't spawned to prevent NPEs if (!npc.isSpawned()) @@ -364,14 +309,7 @@ public class NPCCommands { Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + "."); } - @Command( - aliases = { "npc" }, - usage = "tphere", - desc = "Teleport a NPC to your location", - modifiers = { "tphere" }, - min = 1, - max = 1, - permission = "npc.tphere") + @Command(aliases = { "npc" }, usage = "tphere", desc = "Teleport a NPC to your location", modifiers = { "tphere" }, min = 1, max = 1, permission = "npc.tphere") public void tphere(CommandContext args, Player player, NPC npc) { // Spawn the NPC if it isn't spawned to prevent NPEs if (!npc.isSpawned()) @@ -380,14 +318,7 @@ public class NPCCommands { 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") + @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 " @@ -395,23 +326,48 @@ public class NPCCommands { Messaging.send(player, msg += " be powered."); } - @Command( - aliases = { "npc" }, - usage = "profession [profession]", - desc = "Set a NPC's profession", - modifiers = { "profession" }, - min = 2, - max = 2, - permission = "npc.profession") + @Command(aliases = { "npc" }, usage = "profession [profession]", desc = "Set a NPC's profession", modifiers = { "profession" }, min = 2, max = 2, permission = "npc.profession") @Requirements(selected = true, ownership = true, types = { EntityType.VILLAGER }) public void profession(CommandContext args, Player player, NPC npc) throws CommandException { String profession = args.getString(1); try { npc.getTrait(VillagerProfession.class).setProfession(Profession.valueOf(profession.toUpperCase())); - Messaging.send(player, StringHelper.wrap(npc.getName()) + " is now the profession " - + StringHelper.wrap(profession.toUpperCase()) + "."); + Messaging.send( + player, + StringHelper.wrap(npc.getName()) + " is now the profession " + + StringHelper.wrap(profession.toUpperCase()) + "."); } catch (IllegalArgumentException ex) { throw new CommandException("'" + profession + "' is not a valid profession."); } } + + @Command(aliases = { "npc" }, usage = "age [age] (-l)", desc = "Set the age of a NPC", flags = "l", modifiers = { "age" }, min = 1, max = 2, permission = "npc.age") + @Requirements(selected = true, ownership = true, types = { EntityType.CHICKEN, EntityType.COW, EntityType.OCELOT, + EntityType.PIG, EntityType.SHEEP, EntityType.VILLAGER, EntityType.WOLF }) + public void age(CommandContext args, Player player, NPC npc) throws CommandException { + Age trait = npc.getTrait(Age.class); + + if (args.argsLength() > 1) { + int age = 0; + String ageStr = "an adult"; + try { + age = args.getInteger(1); + if (age < -24000 || age > 0) + throw new CommandException("Invalid age. Valid: adult, baby, number between -24000 and 0"); + ageStr = "age " + age; + } catch (NumberFormatException ex) { + if (args.getString(1).equalsIgnoreCase("baby")) { + age = -24000; + ageStr = "a baby"; + } else if (!args.getString(1).equalsIgnoreCase("adult")) + throw new CommandException("Invalid age. Valid: adult, baby, number between -24000 and 0"); + } + + trait.setAge(age); + Messaging.send(player, StringHelper.wrap(npc.getName()) + " is now " + ageStr + "."); + } + + if (args.hasFlag('l')) + Messaging.send(player, "Age " + (trait.toggle() ? "locked" : "unlocked") + "."); + } } \ 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 913a2559e..b9f43492e 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -14,6 +14,7 @@ import net.citizensnpcs.api.trait.trait.Inventory; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.api.trait.trait.Spawned; +import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; @@ -29,6 +30,7 @@ public class CitizensTraitManager implements TraitManager { public CitizensTraitManager() { // Register Citizens traits + registerTrait(new TraitFactory(Age.class).withName("age")); registerTrait(new TraitFactory(CurrentLocation.class).withName("location")); registerTrait(new TraitFactory(Equipment.class).withName("equipment")); registerTrait(new TraitFactory(Inventory.class).withName("inventory")); diff --git a/src/main/java/net/citizensnpcs/trait/Age.java b/src/main/java/net/citizensnpcs/trait/Age.java new file mode 100644 index 000000000..e06a947b1 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/Age.java @@ -0,0 +1,64 @@ +package net.citizensnpcs.trait; + +import org.bukkit.entity.Animals; + +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 Age extends Trait implements Runnable, Toggleable { + private int age = 0; + private boolean locked = true; + private final NPC npc; + + public Age(NPC npc) { + this.npc = npc; + } + + @Override + public void load(DataKey key) throws NPCLoadException { + age = key.getInt("age"); + locked = key.getBoolean("locked"); + } + + @Override + public void save(DataKey key) { + key.setInt("age", age); + key.setBoolean("locked", locked); + } + + @Override + public void onNPCSpawn() { + // TODO: Switch to use Ageable when that is implemented + if (npc.getBukkitEntity() instanceof Animals) { + Animals animal = (Animals) npc.getBukkitEntity(); + animal.setAge(age); + animal.setAgeLock(locked); + } + } + + @Override + public void run() { + if (!locked) + age = ((Animals) npc.getBukkitEntity()).getAge(); + } + + @Override + public boolean toggle() { + locked = !locked; + ((Animals) npc.getBukkitEntity()).setAgeLock(locked); + return locked; + } + + public void setAge(int age) { + this.age = age; + if (npc.getBukkitEntity() instanceof Animals) + ((Animals) npc.getBukkitEntity()).setAge(age); + } + + @Override + public String toString() { + return "Age{age=" + age + ",locked=" + locked + "}"; + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 862b20509..c274fa9f7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -16,6 +16,7 @@ permissions: citizens.help: true citizens.npc.*: children: + citizens.npc.age: true citizens.npc.character.*: true citizens.npc.create: true citizens.npc.despawn: true