Add -r flag to /npc trat

This commit is contained in:
fullwall 2012-07-22 14:47:44 +08:00
parent f8afd032a8
commit ec3182cc01
2 changed files with 24 additions and 3 deletions

View File

@ -510,13 +510,28 @@ public class NPCCommands {
modifiers = { "trait" }, modifiers = { "trait" },
min = 2, min = 2,
max = 2, max = 2,
flags = "r",
permission = "npc.trait") permission = "npc.trait")
public void trait(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void trait(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Trait trait = CitizensAPI.getTraitFactory().getTrait(args.getString(1)); String traitName = args.getString(1);
if (!sender.hasPermission("citizens.npc.trait." + traitName))
throw new NoPermissionsException();
if (args.hasFlag('r')) {
Class<? extends Trait> clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(1));
if (clazz == null)
throw new CommandException("Trait not found.");
if (!npc.hasTrait(clazz))
throw new CommandException("The NPC doesn't have that trait.");
npc.removeTrait(clazz);
Messaging.sendF(sender, ChatColor.GREEN + "Trait %s removed successfully.",
StringHelper.wrap(traitName));
return;
}
Trait trait = CitizensAPI.getTraitFactory().getTrait(traitName);
if (trait == null) if (trait == null)
throw new CommandException("Trait not found."); throw new CommandException("Trait not found.");
npc.addTrait(trait); npc.addTrait(trait);
Messaging.sendF(sender, ChatColor.GREEN + "Trait %s added successfully.", Messaging.sendF(sender, ChatColor.GREEN + "Trait %s added successfully.",
StringHelper.wrap(trait.getName())); StringHelper.wrap(traitName));
} }
} }

View File

@ -35,7 +35,6 @@ import com.google.common.collect.Sets;
public class CitizensTraitFactory implements TraitFactory { public class CitizensTraitFactory implements TraitFactory {
private final Map<String, Class<? extends Trait>> registered = Maps.newHashMap(); private final Map<String, Class<? extends Trait>> registered = Maps.newHashMap();
// TODO: find a way to avoid naming conflicts
public CitizensTraitFactory() { public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Age.class).withName("age")); registerTrait(TraitInfo.create(Age.class).withName("age"));
registerTrait(TraitInfo.create(CurrentLocation.class).withName("location")); registerTrait(TraitInfo.create(CurrentLocation.class).withName("location"));
@ -103,9 +102,16 @@ public class CitizensTraitFactory implements TraitFactory {
return (T) create(clazz); return (T) create(clazz);
} }
@Override
public Class<? extends Trait> getTraitClass(String name) {
return registered.get(name);
}
@Override @Override
public void registerTrait(TraitInfo info) { public void registerTrait(TraitInfo info) {
Preconditions.checkNotNull(info, "info cannot be null"); Preconditions.checkNotNull(info, "info cannot be null");
if (registered.containsKey(info))
throw new IllegalArgumentException("trait name already registered");
registered.put(info.getTraitName(), info.getTraitClass()); registered.put(info.getTraitName(), info.getTraitClass());
} }