Search in sources :

Example 1 with NoPermissionsException

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

the class NPCCommands method controllable.

@Command(aliases = { "npc" }, usage = "controllable|control (-m(ount),-y,-n,-o)", desc = "Toggles whether the NPC can be ridden and controlled", modifiers = { "controllable", "control" }, min = 1, max = 1, flags = "myno")
public void controllable(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if ((npc.isSpawned() && !sender.hasPermission("citizens.npc.controllable." + npc.getEntity().getType().name().toLowerCase().replace("_", ""))) || !sender.hasPermission("citizens.npc.controllable"))
        throw new NoPermissionsException();
    if (!npc.hasTrait(Controllable.class)) {
        npc.addTrait(new Controllable(false));
    }
    Controllable trait = npc.getTrait(Controllable.class);
    boolean enabled = trait.toggle();
    if (args.hasFlag('y')) {
        enabled = trait.setEnabled(true);
    } else if (args.hasFlag('n')) {
        enabled = trait.setEnabled(false);
    }
    trait.setOwnerRequired(args.hasFlag('o'));
    String key = enabled ? Messages.CONTROLLABLE_SET : Messages.CONTROLLABLE_REMOVED;
    Messaging.sendTr(sender, key, npc.getName());
    if (enabled && args.hasFlag('m') && sender instanceof Player) {
        trait.mount((Player) sender);
    }
}
Also used : Player(org.bukkit.entity.Player) Controllable(net.citizensnpcs.trait.Controllable) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) Command(net.citizensnpcs.api.command.Command)

Example 2 with NoPermissionsException

use of net.citizensnpcs.api.command.exception.NoPermissionsException 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)

Example 3 with NoPermissionsException

use of net.citizensnpcs.api.command.exception.NoPermissionsException 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 4 with NoPermissionsException

use of net.citizensnpcs.api.command.exception.NoPermissionsException in project CitizensAPI by CitizensDev.

the class CommandManager method executeHelp.

private void executeHelp(String[] args, CommandSender sender) throws CommandException {
    if (!sender.hasPermission("citizens." + args[0] + ".help"))
        throw new NoPermissionsException();
    int page = 1;
    try {
        page = args.length == 3 ? Integer.parseInt(args[2]) : page;
    } catch (NumberFormatException e) {
        sendSpecificHelp(sender, args[0], args[2]);
        return;
    }
    sendHelp(sender, args[0], page);
}
Also used : NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException)

Example 5 with NoPermissionsException

use of net.citizensnpcs.api.command.exception.NoPermissionsException in project CitizensAPI by CitizensDev.

the class CommandManager method executeMethod.

// Attempt to execute a command.
private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException {
    String cmdName = args[0].toLowerCase();
    String modifier = args.length > 1 ? args[1] : "";
    boolean help = modifier.toLowerCase().equals("help");
    Method method = commands.get(cmdName + " " + modifier.toLowerCase());
    if (method == null && !help) {
        method = commands.get(cmdName + " *");
    }
    if (method == null && help) {
        executeHelp(args, sender);
        return;
    }
    if (method == null)
        throw new UnhandledCommandException();
    if (!serverCommands.contains(method) && sender instanceof ConsoleCommandSender)
        throw new ServerCommandException();
    if (!hasPermission(method, sender))
        throw new NoPermissionsException();
    Command cmd = method.getAnnotation(Command.class);
    CommandContext context = new CommandContext(sender, args);
    if (cmd.requiresFlags() && !context.hasAnyFlags())
        throw new CommandUsageException("", getUsage(args, cmd));
    if (context.argsLength() < cmd.min())
        throw new CommandUsageException(CommandMessages.TOO_FEW_ARGUMENTS, getUsage(args, cmd));
    if (cmd.max() != -1 && context.argsLength() > cmd.max())
        throw new CommandUsageException(CommandMessages.TOO_MANY_ARGUMENTS, getUsage(args, cmd));
    if (!cmd.flags().contains("*")) {
        for (char flag : context.getFlags()) if (cmd.flags().indexOf(String.valueOf(flag)) == -1)
            throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, cmd));
    }
    methodArgs[0] = context;
    for (Annotation annotation : registeredAnnotations.get(method)) {
        CommandAnnotationProcessor processor = annotationProcessors.get(annotation.annotationType());
        processor.process(sender, context, annotation, methodArgs);
    }
    Object instance = instances.get(method);
    try {
        method.invoke(instance, methodArgs);
    } catch (IllegalArgumentException e) {
        logger.log(Level.SEVERE, "Failed to execute command", e);
    } catch (IllegalAccessException e) {
        logger.log(Level.SEVERE, "Failed to execute command", e);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof CommandException) {
            if (e.getCause() instanceof CommandUsageException && ((CommandUsageException) e.getCause()).getUsage() == null) {
                ((CommandUsageException) e.getCause()).setUsage(getUsage(args, cmd));
            }
            throw (CommandException) e.getCause();
        }
        throw new WrappedCommandException(e.getCause());
    }
}
Also used : WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) Method(java.lang.reflect.Method) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) CommandUsageException(net.citizensnpcs.api.command.exception.CommandUsageException) UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException)

Aggregations

NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)6 Command (net.citizensnpcs.api.command.Command)4 CommandException (net.citizensnpcs.api.command.exception.CommandException)4 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)3 Requirements (net.citizensnpcs.api.command.Requirements)2 Owner (net.citizensnpcs.api.trait.trait.Owner)2 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)2 Player (org.bukkit.entity.Player)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CommandConfigurable (net.citizensnpcs.api.command.CommandConfigurable)1 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)1 UnhandledCommandException (net.citizensnpcs.api.command.exception.UnhandledCommandException)1 WrappedCommandException (net.citizensnpcs.api.command.exception.WrappedCommandException)1 CommandSenderCreateNPCEvent (net.citizensnpcs.api.event.CommandSenderCreateNPCEvent)1 PlayerCreateNPCEvent (net.citizensnpcs.api.event.PlayerCreateNPCEvent)1 NPC (net.citizensnpcs.api.npc.NPC)1 Trait (net.citizensnpcs.api.trait.Trait)1 MobType (net.citizensnpcs.api.trait.trait.MobType)1