Implements traits() in requirements

This commit is contained in:
fullwall 2012-09-30 22:13:19 +08:00
parent 4134982674
commit 14af55efa5
8 changed files with 49 additions and 20 deletions

View File

@ -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<? extends Trait>[] traits() default {};
String usage() default "";
}

View File

@ -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<? extends Trait> clazz : cmdRequirements.traits()) {
if (!npc.hasTrait(clazz))
throw new RequirementMissingException(Messaging.tr(Messages.COMMAND_MISSING_TRAIT,
clazz.getSimpleName()));
}
}
if (npc != null) {
Set<EntityType> types = Sets.newEnumSet(Arrays.asList(cmdRequirements.types()),
EntityType.class);

View File

@ -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<? extends Trait>[] traits() default {};
EntityType[] types() default { EntityType.UNKNOWN };
}

View File

@ -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<String> parts = Splitter.on(",").trimResults().split(args.getFlag("trait"));
Iterable<String> 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<String> 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);

View File

@ -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) {

View File

@ -71,7 +71,7 @@ public class NPCSelector implements Listener {
List<MetadataValue> 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<Object> selectors = npc.data().get("selectors", Lists.newArrayList());
List<String> 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"))

View File

@ -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";

View File

@ -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
citizens.waypoints.set-provider-message=Set the waypoint provider to {0}.