Search in sources :

Example 21 with Command

use of net.citizensnpcs.api.command.Command 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 22 with Command

use of net.citizensnpcs.api.command.Command 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 23 with Command

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

the class NPCCommands method script.

@Command(aliases = { "npc" }, usage = "script --add [files] --remove [files]", desc = "Controls an NPC's scripts", modifiers = { "script" }, min = 1, max = 1, permission = "citizens.npc.script")
public void script(CommandContext args, CommandSender sender, NPC npc) {
    ScriptTrait trait = npc.getTrait(ScriptTrait.class);
    if (args.hasValueFlag("add")) {
        List<String> files = new ArrayList<String>();
        for (String file : args.getFlag("add").split(",")) {
            if (!trait.validateFile(file)) {
                Messaging.sendErrorTr(sender, Messages.INVALID_SCRIPT_FILE, file);
                return;
            }
            files.add(file);
        }
        trait.addScripts(files);
    }
    if (args.hasValueFlag("remove")) {
        trait.removeScripts(Arrays.asList(args.getFlag("remove").split(",")));
    }
    Messaging.sendTr(sender, Messages.CURRENT_SCRIPTS, npc.getName(), Joiner.on("]],[[ ").join(trait.getScripts()));
}
Also used : ScriptTrait(net.citizensnpcs.trait.ScriptTrait) ArrayList(java.util.ArrayList) Command(net.citizensnpcs.api.command.Command)

Example 24 with Command

use of net.citizensnpcs.api.command.Command 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 25 with Command

use of net.citizensnpcs.api.command.Command 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

Command (net.citizensnpcs.api.command.Command)60 CommandException (net.citizensnpcs.api.command.exception.CommandException)40 Requirements (net.citizensnpcs.api.command.Requirements)39 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)27 Player (org.bukkit.entity.Player)11 Location (org.bukkit.Location)10 NPC (net.citizensnpcs.api.npc.NPC)9 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)9 DyeColor (org.bukkit.DyeColor)8 BarColor (org.bukkit.boss.BarColor)5 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)4 Owner (net.citizensnpcs.api.trait.trait.Owner)4 Anchors (net.citizensnpcs.trait.Anchors)4 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)3 MobType (net.citizensnpcs.api.trait.trait.MobType)3 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)3 ScriptTrait (net.citizensnpcs.trait.ScriptTrait)3 SheepTrait (net.citizensnpcs.trait.SheepTrait)3 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 ArrayList (java.util.ArrayList)2