From 5c0f642842223d06eb7387363acd61a97765e806 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 30 Sep 2012 22:13:19 +0800 Subject: [PATCH] Implements traits() in requirements --- .../net/citizensnpcs/command/Command.java | 4 ---- .../citizensnpcs/command/CommandManager.java | 9 +++++++++ .../citizensnpcs/command/Requirements.java | 5 ++++- .../command/command/NPCCommands.java | 18 +++++++++++++++++- .../command/command/WaypointCommands.java | 5 ++--- .../net/citizensnpcs/npc/NPCSelector.java | 8 ++++++-- .../java/net/citizensnpcs/util/Messages.java | 1 + src/main/resources/messages_en.properties | 19 ++++++++++--------- 8 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/citizensnpcs/command/Command.java b/src/main/java/net/citizensnpcs/command/Command.java index 08d60ffad..cce64c764 100644 --- a/src/main/java/net/citizensnpcs/command/Command.java +++ b/src/main/java/net/citizensnpcs/command/Command.java @@ -3,8 +3,6 @@ package net.citizensnpcs.command; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import net.citizensnpcs.api.trait.Trait; - @Retention(RetentionPolicy.RUNTIME) public @interface Command { String[] aliases(); @@ -21,7 +19,5 @@ public @interface Command { String permission() default ""; - Class[] traits() default {}; - String usage() default ""; } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/command/CommandManager.java b/src/main/java/net/citizensnpcs/command/CommandManager.java index 56bf6d0fe..fd02c2c76 100644 --- a/src/main/java/net/citizensnpcs/command/CommandManager.java +++ b/src/main/java/net/citizensnpcs/command/CommandManager.java @@ -17,6 +17,7 @@ import java.util.logging.Logger; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.command.exception.CommandException; @@ -137,6 +138,14 @@ public class CommandManager { && !npc.getTrait(Owner.class).isOwnedBy(sender)) throw new RequirementMissingException(Messaging.tr(Messages.COMMAND_MUST_BE_OWNER)); + if (npc != null) { + for (Class clazz : cmdRequirements.traits()) { + if (!npc.hasTrait(clazz)) + throw new RequirementMissingException(Messaging.tr(Messages.COMMAND_MISSING_TRAIT, + clazz.getSimpleName())); + } + } + if (npc != null) { Set types = Sets.newEnumSet(Arrays.asList(cmdRequirements.types()), EntityType.class); diff --git a/src/main/java/net/citizensnpcs/command/Requirements.java b/src/main/java/net/citizensnpcs/command/Requirements.java index 9b78ea1c6..c34527305 100644 --- a/src/main/java/net/citizensnpcs/command/Requirements.java +++ b/src/main/java/net/citizensnpcs/command/Requirements.java @@ -3,16 +3,19 @@ package net.citizensnpcs.command; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import net.citizensnpcs.api.trait.Trait; + import org.bukkit.entity.EntityType; @Retention(RetentionPolicy.RUNTIME) public @interface Requirements { - EntityType[] excludedTypes() default { EntityType.UNKNOWN }; boolean ownership() default false; boolean selected() default false; + Class[] traits() default {}; + EntityType[] types() default { EntityType.UNKNOWN }; } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index f9a019f2c..d7346be4e 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.command.exception.NoPermissionsException; import net.citizensnpcs.command.exception.ServerCommandException; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.NPCSelector; +import net.citizensnpcs.npc.Template; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Behaviour; import net.citizensnpcs.trait.Controllable; @@ -238,7 +239,7 @@ public class NPCCommands { } if (args.hasValueFlag("trait")) { - Iterable parts = Splitter.on(",").trimResults().split(args.getFlag("trait")); + Iterable parts = Splitter.on(',').trimResults().split(args.getFlag("trait")); StringBuilder builder = new StringBuilder(); for (String tr : parts) { Trait trait = CitizensAPI.getTraitFactory().getTrait(tr); @@ -252,6 +253,21 @@ public class NPCCommands { msg += " with traits " + builder.toString(); } + if (args.hasValueFlag("template")) { + Iterable parts = Splitter.on(',').trimResults().split(args.getFlag("template")); + StringBuilder builder = new StringBuilder(); + for (String part : parts) { + Template template = Template.byName(part); + if (template == null) + continue; + template.apply(npc); + builder.append(StringHelper.wrap(part) + ", "); + } + if (builder.length() > 0) + builder.delete(builder.length() - 2, builder.length()); + msg += " with templates " + builder.toString(); + } + // Set age after entity spawns if (npc.getBukkitEntity() instanceof Ageable) npc.getTrait(Age.class).setAge(age); diff --git a/src/main/java/net/citizensnpcs/command/command/WaypointCommands.java b/src/main/java/net/citizensnpcs/command/command/WaypointCommands.java index 40f5bc00f..d2c830bf8 100644 --- a/src/main/java/net/citizensnpcs/command/command/WaypointCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/WaypointCommands.java @@ -14,7 +14,7 @@ import net.citizensnpcs.util.StringHelper; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -@Requirements(ownership = true, selected = true) +@Requirements(ownership = true, selected = true, traits = Waypoints.class) public class WaypointCommands { public WaypointCommands(Citizens plugin) { } @@ -26,8 +26,7 @@ public class WaypointCommands { modifiers = { "provider" }, min = 1, max = 2, - permission = "waypoints.provider", - traits = Waypoints.class) + permission = "waypoints.provider") public void provider(CommandContext args, CommandSender sender, NPC npc) throws CommandException { Waypoints waypoints = npc.getTrait(Waypoints.class); if (args.argsLength() == 1) { diff --git a/src/main/java/net/citizensnpcs/npc/NPCSelector.java b/src/main/java/net/citizensnpcs/npc/NPCSelector.java index 63fec7406..c248c40df 100644 --- a/src/main/java/net/citizensnpcs/npc/NPCSelector.java +++ b/src/main/java/net/citizensnpcs/npc/NPCSelector.java @@ -71,7 +71,7 @@ public class NPCSelector implements Listener { List selected = player.getMetadata("selected"); if (selected == null || selected.size() == 0 || selected.get(0).asInt() != npc.getId()) { if (Util.isSettingFulfilled(player, Setting.SELECTION_ITEM) - && (npc.getTrait(Owner.class).isOwnedBy(player))) { + && npc.getTrait(Owner.class).isOwnedBy(player)) { player.removeMetadata("selected", plugin); select(player, npc); Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), npc); @@ -83,7 +83,11 @@ public class NPCSelector implements Listener { public void select(CommandSender sender, NPC npc) { // Remove existing selection if any - List selectors = npc.data().get("selectors", Lists.newArrayList()); + List selectors = npc.data().get("selectors"); + if (selectors == null) { + selectors = Lists.newArrayList(); + npc.data().set("selectors", selectors); + } if (sender instanceof Player) { Player player = (Player) sender; if (player.hasMetadata("selected")) diff --git a/src/main/java/net/citizensnpcs/util/Messages.java b/src/main/java/net/citizensnpcs/util/Messages.java index 451b00945..d4a5876c1 100644 --- a/src/main/java/net/citizensnpcs/util/Messages.java +++ b/src/main/java/net/citizensnpcs/util/Messages.java @@ -23,6 +23,7 @@ public class Messages { public static final String COMMAND_ID_NOT_FOUND = "citizens.commands.id-not-found"; public static final String COMMAND_INVALID_MOB_TYPE = "citizens.commands.disallowed-mobtype"; public static final String COMMAND_INVALID_NUMBER = "citizens.commands.invalid-number"; + public static final String COMMAND_MISSING_TRAIT = "citizens.commands.missing-required-trait"; public static final String COMMAND_MUST_BE_INGAME = "citizens.commands.must-be-ingame"; public static final String COMMAND_MUST_BE_OWNER = "citizens.commands.must-be-owner"; public static final String COMMAND_MUST_HAVE_SELECTED = "citizens.commands.must-have-selected"; diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 3c83627a1..659ed9979 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -1,18 +1,19 @@ -citizens.changed-implementation=Citizens implementation changed=disabling plugin. +citizens.changed-implementation=Citizens implementation changed, disabling plugin. citizens.commands.console-error=Please report this error: [See console] -citizens.commands.invalid-number=That is not a valid number. -citizens.commands.must-be-ingame=You must be ingame to use that command. -citizens.commands.unknown-command=Unknown command. Did you mean: -citizens.commands.id-not-found=Couldn't find any NPC with ID {0}. citizens.commands.disallowed-mobtype=The NPC cannot be the mob type '{0}' to use that command. +citizens.commands.invalid-number=That is not a valid number. +citizens.commands.id-not-found=Couldn't find any NPC with ID {0}. +citizens.commands.missing-required-trait=Missing required trait {0}. +citizens.commands.must-be-ingame=You must be ingame to use that command. citizens.commands.must-have-selected=You must have an NPC selected to execute that command. citizens.commands.must-be-owner=You must be the owner of this NPC to execute that command. +citizens.commands.unknown-command=Unknown command. Did you mean: citizens.economy.error-loading=Unable to use economy handling. Has Vault been enabled? citizens.economy.minimum-cost-required-message=Need at least {0}. citizens.economy.money-withdrawn=Withdrew {0} for your NPC. citizens.limits.over-npc-limt=Over the NPC limit of {0}. -citizens.load-task-error=NPC load task couldn't be scheduled - disabling... -citizens.saves.load-failed=Unable to load saves=disabling... +citizens.load-task-error=NPC load task couldn't be scheduled, disabling... +citizens.saves.load-failed=Unable to load saves, disabling... citizens.sub-plugins.load=Loading {0} citizens.sub-plugins.error-on-load={0} initializing {1} citizens.settings.writing-default=Writing default setting: '{0}' @@ -27,6 +28,6 @@ citizens.notifications.npcs-loaded=Loaded {0} NPCs ({1} spawned). citizens.notifications.save-method-set=Save method set to {0}. citizens.notifications.database-connection-failed=Unable to connect to database, falling back to YAML citizens.notifications.unknown-npc-type=NPC type '{0}' was not recognized. Did you spell it correctly? +citizens.waypoints.available-providers-message=List of available providers citizens.waypoints.current-provider-message=The current waypoint provider is {0}. -citizens.waypoints.set-provider-message=Set the waypoint provider to {0}. -citizens.waypoints.available-providers-message=List of available providers \ No newline at end of file +citizens.waypoints.set-provider-message=Set the waypoint provider to {0}. \ No newline at end of file