Search in sources :

Example 16 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.

the class NPCCommands method ocelot.

@Command(aliases = { "npc" }, usage = "ocelot (--type type) (-s(itting), -n(ot sitting))", desc = "Set the ocelot type of an NPC and whether it is sitting", modifiers = { "ocelot" }, min = 1, max = 1, requiresFlags = true, flags = "sn", permission = "citizens.npc.ocelot")
@Requirements(selected = true, ownership = true, types = { EntityType.OCELOT })
public void ocelot(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    OcelotModifiers trait = npc.getTrait(OcelotModifiers.class);
    if (args.hasFlag('s')) {
        trait.setSitting(true);
    } else if (args.hasFlag('n')) {
        trait.setSitting(false);
    }
    if (args.hasValueFlag("type")) {
        Ocelot.Type type = Util.matchEnum(Ocelot.Type.values(), args.getFlag("type"));
        if (type == null) {
            String valid = Util.listValuesPretty(Ocelot.Type.values());
            throw new CommandException(Messages.INVALID_OCELOT_TYPE, valid);
        }
        trait.setType(type);
    }
}
Also used : Ocelot(org.bukkit.entity.Ocelot) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) OcelotModifiers(net.citizensnpcs.trait.OcelotModifiers) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 17 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.

the class NPCCommands method pathfindingOptions.

@Command(aliases = { "npc" }, usage = "pathopt --avoid-water|aw [true|false] --stationary-ticks [ticks] --attack-range [range] --distance-margin [margin]", desc = "Sets an NPC's pathfinding options", modifiers = { "pathopt", "po", "patho" }, min = 1, max = 1, permission = "citizens.npc.pathfindingoptions")
public void pathfindingOptions(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    boolean found = false;
    if (args.hasValueFlag("avoid-water") || args.hasValueFlag("aw")) {
        String raw = args.getFlag("avoid-water", args.getFlag("aw"));
        boolean avoid = Boolean.parseBoolean(raw);
        npc.getNavigator().getDefaultParameters().avoidWater(avoid);
        Messaging.sendTr(sender, avoid ? Messages.PATHFINDING_OPTIONS_AVOID_WATER_SET : Messages.PATHFINDING_OPTIONS_AVOID_WATER_UNSET, npc.getName());
        found = true;
    }
    if (args.hasValueFlag("stationary-ticks")) {
        int ticks = Integer.parseInt(args.getFlag("stationary-ticks"));
        if (ticks < 0)
            throw new CommandException();
        npc.getNavigator().getDefaultParameters().stationaryTicks(ticks);
        Messaging.sendTr(sender, Messages.PATHFINDING_OPTIONS_STATIONARY_TICKS_SET, npc.getName(), ticks);
        found = true;
    }
    if (args.hasValueFlag("distance-margin")) {
        double distance = Double.parseDouble(args.getFlag("distance-margin"));
        if (distance < 0)
            throw new CommandException();
        npc.getNavigator().getDefaultParameters().distanceMargin(Math.pow(distance, 2));
        Messaging.sendTr(sender, Messages.PATHFINDING_OPTIONS_DISTANCE_MARGIN_SET, npc.getName(), distance);
        found = true;
    }
    if (args.hasValueFlag("attack-range")) {
        double range = Double.parseDouble(args.getFlag("attack-range"));
        if (range < 0)
            throw new CommandException();
        npc.getNavigator().getDefaultParameters().attackRange(range);
        Messaging.sendTr(sender, Messages.PATHFINDING_OPTIONS_ATTACK_RANGE_SET, npc.getName(), range);
        found = true;
    }
    if (!found) {
        throw new CommandException();
    }
}
Also used : ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command)

Example 18 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.

the class NPCCommands method wolf.

@Command(aliases = { "npc" }, usage = "wolf (-s(itting) a(ngry) t(amed) i(nfo)) --collar [hex rgb color|name]", desc = "Sets wolf modifiers", modifiers = { "wolf" }, min = 1, max = 1, requiresFlags = true, flags = "sati", permission = "citizens.npc.wolf")
@Requirements(selected = true, ownership = true, types = EntityType.WOLF)
public void wolf(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    WolfModifiers trait = npc.getTrait(WolfModifiers.class);
    if (args.hasFlag('a')) {
        trait.setAngry(!trait.isAngry());
    }
    if (args.hasFlag('s')) {
        trait.setSitting(!trait.isSitting());
    }
    if (args.hasFlag('t')) {
        trait.setTamed(!trait.isTamed());
    }
    if (args.hasValueFlag("collar")) {
        String unparsed = args.getFlag("collar");
        DyeColor color = null;
        try {
            color = DyeColor.valueOf(unparsed.toUpperCase().replace(' ', '_'));
        } catch (IllegalArgumentException e) {
            try {
                int rgb = Integer.parseInt(unparsed.replace("#", ""), 16);
                color = DyeColor.getByColor(org.bukkit.Color.fromRGB(rgb));
            } catch (NumberFormatException ex) {
                throw new CommandException(Messages.COLLAR_COLOUR_NOT_RECOGNISED, unparsed);
            }
        }
        if (color == null)
            throw new CommandException(Messages.COLLAR_COLOUR_NOT_SUPPORTED, unparsed);
        trait.setCollarColor(color);
    }
    Messaging.sendTr(sender, Messages.WOLF_TRAIT_UPDATED, npc.getName(), trait.isAngry(), trait.isSitting(), trait.isTamed(), trait.getCollarColor().name());
}
Also used : WolfModifiers(net.citizensnpcs.trait.WolfModifiers) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) DyeColor(org.bukkit.DyeColor) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 19 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.

the class NPCCommands method select.

@Command(aliases = { "npc" }, usage = "select|sel [id|name] (--r range)", desc = "Select a NPC with the given ID or name", modifiers = { "select", "sel" }, min = 1, max = 2, permission = "citizens.npc.select")
@Requirements
public void select(CommandContext args, final CommandSender sender, final NPC npc) throws CommandException {
    NPCCommandSelector.Callback callback = new NPCCommandSelector.Callback() {

        @Override
        public void run(NPC toSelect) throws CommandException {
            if (toSelect == null)
                throw new CommandException(Messages.NPC_NOT_FOUND);
            if (npc != null && toSelect.getId() == npc.getId())
                throw new CommandException(Messages.NPC_ALREADY_SELECTED);
            selector.select(sender, toSelect);
            Messaging.sendWithNPC(sender, Setting.SELECTION_MESSAGE.asString(), toSelect);
        }
    };
    if (args.argsLength() <= 1) {
        if (!(sender instanceof Player))
            throw new ServerCommandException();
        double range = Math.abs(args.getFlagDouble("r", 10));
        Entity player = (Player) sender;
        final Location location = args.getSenderLocation();
        List<Entity> search = player.getNearbyEntities(range, range, range);
        Collections.sort(search, new Comparator<Entity>() {

            @Override
            public int compare(Entity o1, Entity o2) {
                double d = o1.getLocation().distanceSquared(location) - o2.getLocation().distanceSquared(location);
                return d > 0 ? 1 : d < 0 ? -1 : 0;
            }
        });
        for (Entity possibleNPC : search) {
            NPC test = npcRegistry.getNPC(possibleNPC);
            if (test == null)
                continue;
            callback.run(test);
            break;
        }
    } else {
        NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1));
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) Player(org.bukkit.entity.Player) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 20 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.

the class NPCCommands method remove.

@Command(aliases = { "npc" }, usage = "remove|rem (all|id|name|--owner [owner])", desc = "Remove a NPC", modifiers = { "remove", "rem" }, min = 1, max = 2)
@Requirements
public void remove(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException {
    if (args.hasValueFlag("owner")) {
        String owner = args.getFlag("owner");
        Collection<NPC> npcs = Lists.newArrayList(npcRegistry);
        for (NPC o : npcs) {
            if (o.getTrait(Owner.class).isOwnedBy(owner)) {
                o.destroy();
            }
        }
        Messaging.sendTr(sender, Messages.NPCS_REMOVED);
        return;
    }
    if (args.argsLength() == 2) {
        if (args.getString(1).equalsIgnoreCase("all")) {
            if (!sender.hasPermission("citizens.admin.remove.all") && !sender.hasPermission("citizens.admin"))
                throw new NoPermissionsException();
            npcRegistry.deregisterAll();
            Messaging.sendTr(sender, Messages.REMOVED_ALL_NPCS);
            return;
        } else {
            NPCCommandSelector.Callback callback = new NPCCommandSelector.Callback() {

                @Override
                public void run(NPC npc) throws CommandException {
                    if (npc == null)
                        throw new CommandException(Messages.COMMAND_MUST_HAVE_SELECTED);
                    if (!(sender instanceof ConsoleCommandSender) && !npc.getTrait(Owner.class).isOwnedBy(sender))
                        throw new CommandException(Messages.COMMAND_MUST_BE_OWNER);
                    if (!sender.hasPermission("citizens.npc.remove") && !sender.hasPermission("citizens.admin"))
                        throw new NoPermissionsException();
                    npc.destroy();
                    Messaging.sendTr(sender, Messages.NPC_REMOVED, npc.getName());
                }
            };
            NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1));
            return;
        }
    }
    if (npc == null)
        throw new CommandException(Messages.COMMAND_MUST_HAVE_SELECTED);
    if (!(sender instanceof ConsoleCommandSender) && !npc.getTrait(Owner.class).isOwnedBy(sender))
        throw new CommandException(Messages.COMMAND_MUST_BE_OWNER);
    if (!sender.hasPermission("citizens.npc.remove") && !sender.hasPermission("citizens.admin"))
        throw new NoPermissionsException();
    npc.destroy();
    Messaging.sendTr(sender, Messages.NPC_REMOVED, npc.getName());
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Owner(net.citizensnpcs.api.trait.trait.Owner) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

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