use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommands method profession.
@Command(aliases = { "npc" }, usage = "profession|prof [profession]", desc = "Set a NPC's profession", modifiers = { "profession", "prof" }, min = 2, max = 2, permission = "citizens.npc.profession")
@Requirements(selected = true, ownership = true, types = { EntityType.VILLAGER })
public void profession(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
String profession = args.getString(1);
Profession parsed = Util.matchEnum(Profession.values(), profession.toUpperCase());
if (parsed == null) {
throw new CommandException(Messages.INVALID_PROFESSION, args.getString(1), StringUtils.join(Profession.values(), ","));
}
npc.getTrait(VillagerProfession.class).setProfession(parsed);
Messaging.sendTr(sender, Messages.PROFESSION_SET, npc.getName(), profession);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommands method list.
@Command(aliases = { "npc" }, usage = "list (page) ((-a) --owner (owner) --type (type) --char (char) --registry (name))", desc = "List NPCs", flags = "a", modifiers = { "list" }, min = 1, max = 2, permission = "citizens.npc.list")
@Requirements
public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
NPCRegistry source = args.hasValueFlag("registry") ? CitizensAPI.getNamedNPCRegistry(args.getFlag("registry")) : npcRegistry;
if (source == null)
throw new CommandException();
List<NPC> npcs = new ArrayList<NPC>();
if (args.hasFlag('a')) {
for (NPC add : source.sorted()) {
npcs.add(add);
}
} else if (args.getValueFlags().size() == 0 && sender instanceof Player) {
for (NPC add : source.sorted()) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(sender)) {
npcs.add(add);
}
}
} else {
if (args.hasValueFlag("owner")) {
String name = args.getFlag("owner");
for (NPC add : source.sorted()) {
if (!npcs.contains(add) && add.getTrait(Owner.class).isOwnedBy(name)) {
npcs.add(add);
}
}
}
if (args.hasValueFlag("type")) {
EntityType type = Util.matchEntityType(args.getFlag("type"));
if (type == null)
throw new CommandException(Messages.COMMAND_INVALID_MOBTYPE, type);
for (NPC add : source) {
if (!npcs.contains(add) && add.getTrait(MobType.class).getType() == type)
npcs.add(add);
}
}
}
Paginator paginator = new Paginator().header("NPCs");
paginator.addLine("<e>Key: <a>ID <b>Name");
for (int i = 0; i < npcs.size(); i += 2) {
String line = "<a>" + npcs.get(i).getId() + "<b> " + npcs.get(i).getName();
if (npcs.size() >= i + 2)
line += " " + "<a>" + npcs.get(i + 1).getId() + "<b> " + npcs.get(i + 1).getName();
paginator.addLine(line);
}
int page = args.getInteger(1, 1);
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommands method moveto.
@Command(aliases = { "npc" }, usage = "moveto x:y:z:world | x y z world", desc = "Teleports a NPC to a given location", modifiers = "moveto", min = 1, permission = "citizens.npc.moveto")
public void moveto(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned()) {
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
}
if (!npc.isSpawned()) {
throw new CommandException("NPC could not be spawned.");
}
Location current = npc.getEntity().getLocation();
Location to;
if (args.argsLength() > 1) {
String[] parts = Iterables.toArray(Splitter.on(':').split(args.getJoinedStrings(1, ':')), String.class);
if (parts.length != 4 && parts.length != 3)
throw new CommandException(Messages.MOVETO_FORMAT);
double x = Double.parseDouble(parts[0]);
double y = Double.parseDouble(parts[1]);
double z = Double.parseDouble(parts[2]);
World world = parts.length == 4 ? Bukkit.getWorld(parts[3]) : current.getWorld();
if (world == null)
throw new CommandException(Messages.WORLD_NOT_FOUND);
to = new Location(world, x, y, z, current.getYaw(), current.getPitch());
} else {
to = current.clone();
if (args.hasValueFlag("x"))
to.setX(args.getFlagDouble("x"));
if (args.hasValueFlag("y"))
to.setY(args.getFlagDouble("y"));
if (args.hasValueFlag("z"))
to.setZ(args.getFlagDouble("z"));
if (args.hasValueFlag("yaw"))
to.setYaw((float) args.getFlagDouble("yaw"));
if (args.hasValueFlag("pitch"))
to.setPitch((float) args.getFlagDouble("pitch"));
if (args.hasValueFlag("world")) {
World world = Bukkit.getWorld(args.getFlag("world"));
if (world == null)
throw new CommandException(Messages.WORLD_NOT_FOUND);
to.setWorld(world);
}
}
npc.teleport(to, TeleportCause.COMMAND);
Messaging.sendTr(sender, Messages.MOVETO_TELEPORTED, npc.getName(), to);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class TraitCommands method configure.
@Command(aliases = { "traitc", "trc" }, usage = "[trait name] (flags)", desc = "Configures a trait", modifiers = { "*" }, min = 1, flags = "*", permission = "citizens.npc.trait-configure")
public void configure(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
String traitName = args.getString(0);
if (!sender.hasPermission("citizens.npc.trait-configure." + traitName) && !sender.hasPermission("citizens.npc.trait-configure.*"))
throw new NoPermissionsException();
Class<? extends Trait> clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(0));
if (clazz == null)
throw new CommandException(Messages.TRAIT_NOT_FOUND);
if (!CommandConfigurable.class.isAssignableFrom(clazz))
throw new CommandException(Messages.TRAIT_NOT_CONFIGURABLE);
if (!npc.hasTrait(clazz))
throw new CommandException(Messages.TRAIT_NOT_FOUND_ON_NPC);
CommandConfigurable trait = (CommandConfigurable) npc.getTrait(clazz);
trait.configure(args);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommandSelector method acceptValidatedInput.
@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
boolean found = false;
for (NPC npc : choices) {
if (input.intValue() == npc.getId()) {
found = true;
break;
}
}
CommandSender sender = (CommandSender) context.getForWhom();
if (!found) {
Messaging.sendErrorTr(sender, Messages.SELECTION_PROMPT_INVALID_CHOICE, input);
return this;
}
NPC toSelect = CitizensAPI.getNPCRegistry().getById(input.intValue());
try {
callback.run(toSelect);
} catch (ServerCommandException ex) {
Messaging.sendTr(sender, CommandMessages.MUST_BE_INGAME);
} catch (CommandUsageException ex) {
Messaging.sendError(sender, ex.getMessage());
Messaging.sendError(sender, ex.getUsage());
} catch (UnhandledCommandException ex) {
ex.printStackTrace();
} catch (WrappedCommandException ex) {
ex.getCause().printStackTrace();
} catch (CommandException ex) {
Messaging.sendError(sender, ex.getMessage());
} catch (NumberFormatException ex) {
Messaging.sendErrorTr(sender, CommandMessages.INVALID_NUMBER);
}
return null;
}
Aggregations