/** * Registers a command with the given name is possible. Also uses fallbackPrefix to create a * unique name. * * @param label the name of the command, without the '/'-prefix. * @param command the command to register * @param isAlias whether the command is an alias * @param fallbackPrefix a prefix which is prepended to the command for a unique address * @return true if command was registered, false otherwise. */ private synchronized boolean register( String label, Command command, boolean isAlias, String fallbackPrefix) { knownCommands.put(fallbackPrefix + ":" + label, command); if ((command instanceof VanillaCommand || isAlias) && knownCommands.containsKey(label)) { // Request is for an alias/fallback command and it conflicts with // a existing command or previous alias ignore it // Note: This will mean it gets removed from the commands list of active aliases return false; } boolean registered = true; // If the command exists but is an alias we overwrite it, otherwise we return Command conflict = knownCommands.get(label); if (conflict != null && conflict.getLabel().equals(label)) { return false; } if (!isAlias) { command.setLabel(label); } knownCommands.put(label, command); return registered; }
/** * Get the command label (trim + lower case), include server commands [subject to change]. * * @param alias * @param strict If to return null if no command is found. * @return The command label, if possible to find, or the alias itself (+ trim + lower-case). */ public static String getCommandLabel(final String alias, final boolean strict) { final Command command = getCommand(alias); if (command == null) { return strict ? null : alias.trim().toLowerCase(); } else { return command.getLabel().trim().toLowerCase(); } }
private void fillPluginIndexes( Map<String, Set<HelpTopic>> pluginIndexes, Collection<? extends Command> commands) { for (Command command : commands) { String pluginName = getCommandPluginName(command); if (pluginName != null) { HelpTopic topic = getHelpTopic("/" + command.getLabel()); if (topic != null) { if (!pluginIndexes.containsKey(pluginName)) { pluginIndexes.put( pluginName, new TreeSet<HelpTopic>( HelpTopicComparator .helpTopicComparatorInstance())); // keep things in topic order } pluginIndexes.get(pluginName).add(topic); } } } }
public boolean onCommand( CommandSender commandSender, Command command, String label, String[] args) { HashMap<String, Integer> commandList = new HashMap<String, Integer>(); commandList.put("ec", 0); commandList.put("buycraft", 1); Boolean status = false; switch (commandList.get(command.getLabel().toLowerCase())) { case 0: status = new EnableChatCommand().process(commandSender, args); break; case 1: status = new BuycraftCommand().process(commandSender, args); break; } return status; }
@Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (cmd.getLabel().equalsIgnoreCase("ancientforge")) { if (args.length > 0) { if (AFCommand.getCommand(args[0]) != null) { AFCommand command = AFCommand.getCommand(args[0]); if (command == null) { sender.sendMessage(ChatColor.DARK_RED + "No AncientForge command called that."); return false; } if (command.getMinimumArguments() <= args.length - 1) { String[] newargs = new String[args.length - 1]; for (int i = 1; i < args.length; i++) newargs[i - 1] = args[i]; command.run(sender, newargs); } else { } } else { } } } return false; }
/** Processes all the commands registered in the server and creates help topics for them. */ public synchronized void initializeCommands() { // ** Load topics from highest to lowest priority order ** Set<String> ignoredPlugins = new HashSet<String>(yaml.getIgnoredPlugins()); // Don't load any automatic help topics if All is ignored if (ignoredPlugins.contains("All")) { return; } // Initialize help topics from the server's command map outer: for (Command command : server.getCommandMap().getCommands()) { if (commandInIgnoredPlugin(command, ignoredPlugins)) { continue; } // Register a topic for (Class c : topicFactoryMap.keySet()) { if (c.isAssignableFrom(command.getClass())) { HelpTopic t = topicFactoryMap.get(c).createTopic(command); if (t != null) addTopic(t); continue outer; } if (command instanceof PluginCommand && c.isAssignableFrom(((PluginCommand) command).getExecutor().getClass())) { HelpTopic t = topicFactoryMap.get(c).createTopic(command); if (t != null) addTopic(t); continue outer; } } addTopic(new GenericCommandHelpTopic(command)); } // Initialize command alias help topics for (Command command : server.getCommandMap().getCommands()) { if (commandInIgnoredPlugin(command, ignoredPlugins)) { continue; } for (String alias : command.getAliases()) { if (!helpTopics.containsKey("/" + alias)) { addTopic(new CommandAliasHelpTopic("/" + alias, "/" + command.getLabel(), this)); } } } // Initialize help topics from the server's fallback commands for (VanillaCommand command : server.getCommandMap().getFallbackCommands()) { if (!commandInIgnoredPlugin(command, ignoredPlugins)) { addTopic(new GenericCommandHelpTopic(command)); } } // Add alias sub-index addTopic( new IndexHelpTopic( "Aliases", "Lists command aliases", null, Collections2.filter( helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class)))); // Initialize plugin-level sub-topics Map<String, Set<HelpTopic>> pluginIndexes = new HashMap<String, Set<HelpTopic>>(); fillPluginIndexes(pluginIndexes, server.getCommandMap().getCommands()); fillPluginIndexes(pluginIndexes, server.getCommandMap().getFallbackCommands()); for (Map.Entry<String, Set<HelpTopic>> entry : pluginIndexes.entrySet()) { addTopic( new IndexHelpTopic( entry.getKey(), "All commands for " + entry.getKey(), null, entry.getValue(), "Below is a list of all " + entry.getKey() + " commands:")); } // Amend help topics from the help.yml file for (HelpTopicAmendment amendment : yaml.getTopicAmendments()) { if (helpTopics.containsKey(amendment.getTopicName())) { helpTopics .get(amendment.getTopicName()) .amendTopic(amendment.getShortText(), amendment.getFullText()); if (amendment.getPermission() != null) { helpTopics.get(amendment.getTopicName()).amendCanSee(amendment.getPermission()); } } } }
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { TreeMap<Integer, MethodWrapper> methodmap = null; /// No method to handle, show some help if ((args.length == 0 && !methods.containsKey(DEFAULT_CMD)) || (args.length > 0 && (args[0].equals("?") || args[0].equals("help")))) { showHelp(sender, command, args); return true; } final int length = args.length; final String cmd = length > 0 ? args[0].toLowerCase() : null; final String subcmd = length > 1 ? args[1].toLowerCase() : null; int startIndex = 0; /// check for subcommands if (subcmd != null && subCmdMethods.containsKey(cmd) && subCmdMethods.get(cmd).containsKey(subcmd)) { methodmap = subCmdMethods.get(cmd).get(subcmd); startIndex = 2; } if (methodmap == null && cmd != null) { // / Find our method, and verify all the annotations methodmap = methods.get(cmd); if (methodmap != null) startIndex = 1; } if (methodmap == null) { // / our last attempt methodmap = methods.get(DEFAULT_CMD); } if (methodmap == null || methodmap.isEmpty()) { return sendMessage( sender, "&cThat command does not exist!&6 /" + command.getLabel() + " help &c for help"); } MCCommand mccmd = null; List<CommandException> errs = null; boolean success = false; for (MethodWrapper mwrapper : methodmap.values()) { mccmd = mwrapper.method.getAnnotation(MCCommand.class); final boolean isOp = sender == null || sender.isOp() || sender instanceof ConsoleCommandSender; if (mccmd.op() && !isOp || mccmd.admin() && !hasAdminPerms(sender)) // / no op, no pass continue; Arguments newArgs = null; try { newArgs = verifyArgs(mwrapper, mccmd, sender, command, label, args, startIndex); Object completed = mwrapper.method.invoke(mwrapper.obj, newArgs.args); if (completed != null && completed instanceof Boolean) { success = (Boolean) completed; if (!success) { String usage = mwrapper.usage; if (usage != null && !usage.isEmpty()) { sendMessage(sender, usage); } } } else { success = true; } break; /// success on one } catch ( IllegalArgumentException e) { // / One of the arguments wasn't correct, store the message if (errs == null) errs = new ArrayList<CommandException>(); errs.add(new CommandException(e, mwrapper)); } catch (Exception e) { // / Just all around bad logInvocationError(e, mwrapper, newArgs); } } /// and handle all errors if (!success && errs != null && !errs.isEmpty()) { HashSet<String> usages = new HashSet<String>(); for (CommandException e : errs) { usages.add( ChatColor.GOLD + command.getLabel() + " " + e.mw.usage + " &c:" + e.err.getMessage()); } for (String msg : usages) { sendMessage(sender, msg); } } return true; }
@SuppressWarnings("deprecation") public boolean onCommand( CommandSender sender, Command command, String commandLabel, String[] args) { if (!(sender instanceof Player)) { log.info("You must be a player!"); return true; } Player player = (Player) sender; if (command.getLabel().equalsIgnoreCase("rubies")) { if (args.length < 1) { int x = (int) econ.getBalance(player.getName()); sender.sendMessage( this.getConfig() .getString("rubies-format") .replaceAll("<balance>", "" + x) .replaceAll("&", "\u00a7")); } if (args.length >= 1) { if (args[0].equalsIgnoreCase("reload")) { reloadConfig(); player.sendMessage(ChatColor.YELLOW + "Rubies config " + ChatColor.WHITE + "reloaded!"); } if (args[0].equalsIgnoreCase("config")) { // if (args.length == 1){ player.sendMessage(ChatColor.YELLOW + "Rubies config: "); player.sendMessage( ChatColor.GREEN + "\nrubies-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-format")); player.sendMessage( ChatColor.GREEN + "rubies-see-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-see-format")); player.sendMessage( ChatColor.GREEN + "give-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("give-rubies-permission")); player.sendMessage( ChatColor.GREEN + "rubies-recived-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-recived-format")); player.sendMessage( ChatColor.GREEN + "rubies-error-message: " + ChatColor.GOLD + this.getConfig().getString("rubies-error-message")); player.sendMessage( ChatColor.GREEN + "give-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("give-rubies-permission")); player.sendMessage( ChatColor.GREEN + "no-give-ruby-perms-message: " + ChatColor.GOLD + this.getConfig().getString("no-give-ruby-perms-message")); player.sendMessage( ChatColor.GREEN + "take-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("take-rubies-permission")); player.sendMessage( ChatColor.GREEN + "rubies-taken-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-taken-format")); player.sendMessage( ChatColor.GREEN + "no-take-ruby-perms-message: " + ChatColor.GOLD + this.getConfig().getString("no-take-ruby-perms-message")); // player.sendMessage(ChatColor.WHITE + "\nUse " + ChatColor.YELLOW + "/rubies config // <config section> <new value>" + ChatColor.WHITE + " to edit a config value"); // } /* if (args.length == 2){ player.sendMessage("\nProper command use is " + ChatColor.YELLOW + "/rubies config <config section> <new value>"); } if (args.length == 3){ if (this.getConfig().getString(args[2]) != null){ this.getConfig().set(args[2], args[3]); player.sendMessage(ChatColor.GREEN + args[2] + ChatColor.WHITE + " set to " + ChatColor.GOLD + args[3]); reloadConfig(); } if (this.getConfig().getString(args[2]) == null) { player.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + " Config section " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " does not exist!"); player.sendMessage("Type " + ChatColor.YELLOW + "/rubies config " + ChatColor.WHITE + "to see avalible options."); } }*/ } if (args[0].equalsIgnoreCase("see")) { if (args.length == 2) { if (player.getServer().getPlayer(args[1]) != null) { player.sendMessage( this.getConfig() .getString("rubies-see-format") .replaceAll("<player>", args[1]) .replaceAll("<balance>", "" + econ.getBalance(args[1])) .replaceAll("&", "\u00a7")); } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 2) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies see <player>"); } } if (args[0].equalsIgnoreCase("give")) { if (player.hasPermission(this.getConfig().getString("give-rubies-permission")) || player.isOp()) { if (args.length == 3) { if (player.getServer().getPlayer(args[1]) != null) { if (isNumeric(args[2])) { double amount = Double.parseDouble(args[2]); EconomyResponse r = econ.depositPlayer(args[1], amount); // Try to give // econ.depositPlayer(args[1], amount);//Try to give if (r.transactionSuccess()) { // Rubies given int x = (int) econ.getBalance(args[1]); player.sendMessage( this.getConfig() .getString("rubies-recived-format") .replaceAll("<given>", "" + r.amount) .replaceAll("<balance>", "" + x) .replaceAll("<player>", args[1]) .replaceAll("&", "\u00a7")); } else { // An error has occurred sender.sendMessage( this.getConfig() .getString("rubies-error-message") .replaceAll("<error>", r.errorMessage)); } } else { player.sendMessage( ChatColor.RED + "Error: " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " must be a number!"); } } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 3) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies give <player> <amount>"); } if (!player.hasPermission(this.getConfig().getString("give-rubies-permission")) || !player.isOp()) { // Player does not have permission player.sendMessage( this.getConfig() .getString("no-give-ruby-perms-message") .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } } } if (args[0].equalsIgnoreCase("take")) { if (player.hasPermission(this.getConfig().getString("take-rubies-permission")) || player.isOp()) { if (args.length == 3) { if (player.getServer().getPlayer(args[1]) != null) { if (isNumeric(args[2])) { double amount = Double.parseDouble(args[2]); EconomyResponse r = econ.withdrawPlayer(args[1], amount); // Try to take // econ.withdrawPlayer(args[1], amount);//Try to take if (r.transactionSuccess()) { // Rubies taken player.sendMessage( this.getConfig() .getString("rubies-taken-format") .replaceAll("<player>", args[1]) .replaceAll("<taken>", "" + r.amount) .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } else { // An error has occurred sender.sendMessage( this.getConfig() .getString("rubies-error-message") .replaceAll("<error>", r.errorMessage)); } } else { player.sendMessage( ChatColor.RED + "Error: " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " must be a number!"); } } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 3) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies take <player> <amount>"); } } if (!player.hasPermission(this.getConfig().getString("take-rubies-permission")) || !player.isOp()) { // Player does not have permission player.sendMessage( this.getConfig() .getString("no-take-ruby-perms-message") .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } } if (!args[0].equalsIgnoreCase("reload") && !args[0].equalsIgnoreCase("see") && !args[0].equalsIgnoreCase("give") && !args[0].equalsIgnoreCase("take") && !args[0].equalsIgnoreCase("config")) { player.sendMessage( "Rubies Commands: " + ChatColor.YELLOW + "\n/rubies " + ChatColor.GRAY + ": See how many rubies you have."); player.sendMessage( ChatColor.YELLOW + "/rubies see <player> " + ChatColor.GRAY + ": See how many rubies a player has."); player.sendMessage( ChatColor.YELLOW + "/rubies give <player> <amount> " + ChatColor.GRAY + ": Gives rubies to a player."); player.sendMessage( ChatColor.YELLOW + "/rubies take <player> <amount> " + ChatColor.GRAY + ": Take rubies from a player."); player.sendMessage( ChatColor.YELLOW + "/rubies config " + ChatColor.GRAY + ": Rubies config options."); player.sendMessage( ChatColor.YELLOW + "/rubies reload " + ChatColor.GRAY + ": Reloads the rubies config file."); } } return true; } return false; }