protected void checkPermission(T player, Method method) throws CommandException { if (!hasPermission(method, player)) { throw new CommandPermissionsException(); } }
/** * Register an class that contains commands (denoted by {@link Command}. * If no dependency injector is specified, then the methods of the * class will be registered to be called statically. Otherwise, new * instances will be created of the command classes and methods will * not be called statically. * * @param cls the class to register */ public void register(Class<?> cls) { registerMethods(cls, null); }
} else { throw new MissingNestedCommandException("Unknown command: " + cmdName, getNestedUsage(args, level - 1, parent, player)); checkPermission(player, method); if (argsCount == 0) { throw new MissingNestedCommandException("Sub-command required.", getNestedUsage(args, level, method, player)); } else { executeMethod(method, args, player, methodArgs, level + 1); executeMethod(parent, aCmd.value(), player, methodArgs, level); } else { Command cmd = method.getAnnotation(Command.class); throw new CommandUsageException("Too few arguments.", getUsage(args, level, cmd)); throw new CommandUsageException("Too many arguments.", getUsage(args, level, cmd)); for (char flag : context.getFlags()) { if (!newFlags.contains(flag)) { throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, level, cmd)); invokeMethod(parent, args, player, method, instance, methodArgs, argsCount);
/** * Register the methods of a class. This will automatically construct * instances as necessary. * * @param cls the class to register * @param parent the parent method * @return Commands Registered */ public List<Command> registerMethods(Class<?> cls, Method parent) { try { if (getInjector() == null) { return registerMethods(cls, parent, null); } else { Object obj = getInjector().getInstance(cls); return registerMethods(cls, parent, obj); } } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { logger.log(Level.SEVERE, "Failed to register commands", e); } return null; }
final CharSequence arguments = getArguments(cmd); for (String alias : cmd.aliases()) { final String helpMessage = "/" + alias + " " + arguments + "\n\n" + help; registerMethods(nestedCls, method); registerMethods(cls.getSuperclass(), parent, obj);
for(Map.Entry<String, Method> entry : map.entrySet()) { final String child = entry.getKey(); if(child.toLowerCase().startsWith(cmdNameLower) && hasPermission(entry.getValue(), player)) { children.add(child); } else { throw new MissingNestedCommandException("Unknown command: " + cmdName, getNestedUsage(args, level - 1, parent, player)); if(!hasPermission(method, player)) { throw new CommandPermissionsException(); if (argsCount == 0) { throw new MissingNestedCommandException("Sub-command required.", getNestedUsage(args, level, method, player)); } else { return executeMethod(method, completing, args, player, methodArgs, level + 1); return executeMethod(parent, completing, aCmd.value(), player, methodArgs, level); } else { Command cmd = method.getAnnotation(Command.class); if(completing && !supportsCompletion(method)) return null; throw new CommandUsageException("Too few arguments.", getUsage(args, level, cmd)); throw new CommandUsageException("Too many arguments.", getUsage(args, level, cmd)); for (char flag : context.getFlags()) {
final CharSequence arguments = getArguments(cmd); for (String alias : cmd.aliases()) { final String helpMessage = "/" + alias + " " + arguments + "\n\n" + help; registerMethods(nestedCls, method); registerMethods0(cls.getSuperclass(), parent, provider);
Map<String, String> helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getHelpMessages(); final String lookupName = cmd.getName().replaceAll("/", ""); if (helpText.containsKey(lookupName)) { // We have full help text for this command helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getCommands(); if (helpText.containsKey(cmd.getName())) { final String shortText = helpText.get(cmd.getName());
/** * Attempt to execute a command. * * @param args the arguments * @param player the player * @param methodArgs the arguments for the method * @throws CommandException thrown on command error */ public void execute(String[] args, T player, Object... methodArgs) throws CommandException { Object[] newMethodArgs = new Object[methodArgs.length + 1]; System.arraycopy(methodArgs, 0, newMethodArgs, 1, methodArgs.length); executeMethod(null, args, player, newMethodArgs, 0); }
public boolean registerAll(List<Command> registered) { List<CommandInfo> toRegister = new ArrayList<>(); for (Command command : registered) { List<String> permissions = null; Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]); Map<String, Method> childMethods = commands.getMethods().get(cmdMethod); if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value()); } else if (cmdMethod != null && childMethods != null && !childMethods.isEmpty()) { permissions = new ArrayList<>(); for (Method m : childMethods.values()) { if (m.isAnnotationPresent(CommandPermissions.class)) { permissions.addAll(Arrays.asList(m.getAnnotation(CommandPermissions.class).value())); } } } toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions == null ? null : permissions.toArray(new String[permissions.size()]))); } return register(toRegister); }
/** * Get the usage string for a command. * * @param args the arguments * @param level the depth of the command * @param cmd the command annotation * @return the usage string */ protected String getUsage(String[] args, int level, Command cmd) { final StringBuilder command = new StringBuilder(); command.append('/'); for (int i = 0; i <= level; ++i) { command.append(args[i]); command.append(' '); } command.append(getArguments(cmd)); final String help = cmd.help(); if (!help.isEmpty()) { command.append("\n\n"); command.append(help); } return command.toString(); }
@Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { try { Actor actor = wrapCommandSender(sender); try { commands.execute(cmd.getName(), args, actor, actor); } catch (Throwable t) { Throwable next = t; do { WorldGuard.getInstance().getExceptionConverter().convert(next); next = next.getCause(); } while (next != null); throw t; } } catch (CommandPermissionsException e) { sender.sendMessage(ChatColor.RED + "You don't have permission."); } catch (MissingNestedCommandException e) { sender.sendMessage(ChatColor.RED + e.getUsage()); } catch (CommandUsageException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); sender.sendMessage(ChatColor.RED + e.getUsage()); } catch (WrappedCommandException e) { sender.sendMessage(ChatColor.RED + e.getCause().getMessage()); } catch (CommandException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); } return true; }
Map<String, String> helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getHelpMessages(); final String lookupName = cmd.getName().replaceAll("/", ""); if (helpText.containsKey(lookupName)) { // We have full help text for this command helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getCommands(); if (helpText.containsKey(cmd.getName())) { final String shortText = helpText.get(cmd.getName());
/** * Attempt to execute a command. This version takes a separate command * name (for the root command) and then a list of following arguments. * * @param cmd command to run * @param args arguments * @param player command source * @param methodArgs method arguments * @throws CommandException thrown when the command throws an error */ public void execute(String cmd, String[] args, T player, Object... methodArgs) throws CommandException { String[] newArgs = new String[args.length + 1]; System.arraycopy(args, 0, newArgs, 1, args.length); newArgs[0] = cmd; Object[] newMethodArgs = new Object[methodArgs.length + 1]; System.arraycopy(methodArgs, 0, newMethodArgs, 1, methodArgs.length); executeMethod(null, newArgs, player, newMethodArgs, 0); }
public boolean registerAll(List<Command> registered) { List<CommandInfo> toRegister = new ArrayList<CommandInfo>(); for (Command command : registered) { String[] permissions = null; Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]); if(cmdMethod != null) { if(cmdMethod.isAnnotationPresent(CommandPermissions.class)) { permissions = cmdMethod.getAnnotation(CommandPermissions.class).value(); } } toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions)); } return register(toRegister); } }
/** * Get the usage string for a command. * * @param args the arguments * @param level the depth of the command * @param cmd the command annotation * @return the usage string */ protected String getUsage(String[] args, int level, Command cmd) { final StringBuilder command = new StringBuilder(); command.append('/'); for (int i = 0; i <= level; ++i) { command.append(args[i]); command.append(' '); } command.append(getArguments(cmd)); final String help = cmd.help(); if (!help.isEmpty()) { command.append("\n\n"); command.append(help); } return command.toString(); }
/** * Called on a command. */ @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { try { commands.execute(cmd.getName(), args, sender, sender); } catch (CommandPermissionsException e) { sender.sendMessage(ChatColor.RED + "You don't have permission."); } catch (MissingNestedCommandException e) { sender.sendMessage(ChatColor.RED + e.getUsage()); } catch (CommandUsageException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); sender.sendMessage(ChatColor.RED + e.getUsage()); } catch (WrappedCommandException e) { if (e.getCause() instanceof NumberFormatException) { sender.sendMessage(ChatColor.RED + "Number expected, string received instead."); } else { sender.sendMessage(ChatColor.RED + "An error has occurred. See console."); e.printStackTrace(); } } catch (CommandException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); } return true; }
/** * Returns whether a player has access to a command. * * @param method the method * @param player the player * @return true if permission is granted */ protected boolean hasPermission(Method method, T player) { CommandPermissions perms = method.getAnnotation(CommandPermissions.class); if (perms == null) { return true; } for (String perm : perms.value()) { if (hasPermission(player, perm)) { return true; } } return false; }
/** * Register an class that contains commands (denoted by {@link Command}. * If no dependency injector is specified, then the methods of the * class will be registered to be called statically. Otherwise, new * instances will be created of the command classes and methods will * not be called statically. A List of {@link Command} annotations from * registered commands is returned. * * @param cls the class to register * @return A List of {@link Command} annotations from registered commands, * for use in eg. a dynamic command registration system. */ public List<Command> registerAndReturn(Class<?> cls) { return registerMethods(cls, null); }
/** * Attempt to execute a command. This version takes a separate command * name (for the root command) and then a list of following arguments. * * @param cmd command to run * @param args arguments * @param player command source * @param methodArgs method arguments * @throws CommandException thrown when the command throws an error */ public void execute(String cmd, String[] args, T player, Object... methodArgs) throws CommandException { executeMethod(false, cmd, args, player, methodArgs); }