Search in sources :

Example 21 with CommandException

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);
}
Also used : Profession(org.bukkit.entity.Villager.Profession) VillagerProfession(net.citizensnpcs.trait.VillagerProfession) VillagerProfession(net.citizensnpcs.trait.VillagerProfession) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 22 with CommandException

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);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) EntityType(org.bukkit.entity.EntityType) Player(org.bukkit.entity.Player) Owner(net.citizensnpcs.api.trait.trait.Owner) ArrayList(java.util.ArrayList) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Paginator(net.citizensnpcs.api.util.Paginator) NPCRegistry(net.citizensnpcs.api.npc.NPCRegistry) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 23 with CommandException

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);
}
Also used : ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) World(org.bukkit.World) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command)

Example 24 with CommandException

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);
}
Also used : NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) CommandException(net.citizensnpcs.api.command.exception.CommandException) CommandConfigurable(net.citizensnpcs.api.command.CommandConfigurable) Command(net.citizensnpcs.api.command.Command)

Example 25 with CommandException

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;
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) CommandUsageException(net.citizensnpcs.api.command.exception.CommandUsageException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) CommandSender(org.bukkit.command.CommandSender) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException)

Aggregations

CommandException (net.citizensnpcs.api.command.exception.CommandException)49 Command (net.citizensnpcs.api.command.Command)40 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)30 Requirements (net.citizensnpcs.api.command.Requirements)25 NPC (net.citizensnpcs.api.npc.NPC)9 Player (org.bukkit.entity.Player)8 Paginator (net.citizensnpcs.api.util.Paginator)7 Location (org.bukkit.Location)7 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)6 DyeColor (org.bukkit.DyeColor)5 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)4 Owner (net.citizensnpcs.api.trait.trait.Owner)4 UnhandledCommandException (net.citizensnpcs.api.command.exception.UnhandledCommandException)3 WrappedCommandException (net.citizensnpcs.api.command.exception.WrappedCommandException)3 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)3 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)2 CommandSenderCreateNPCEvent (net.citizensnpcs.api.event.CommandSenderCreateNPCEvent)2 PlayerCreateNPCEvent (net.citizensnpcs.api.event.PlayerCreateNPCEvent)2 Template (net.citizensnpcs.npc.Template)2 Age (net.citizensnpcs.trait.Age)2