Easy CommandsEverything that you need for commands Source You can create a CommandHandler that will handle all the command stuff for you.Included Features are:- A Helpscreen for all commands (or only the ones you are permitted to use) - You can customize how it looks (header, colors, and texts)- Tab completion for all commands (You can add support for players too)- Build in permission system with custom messages (default or per command permission message)- You can add variables in commands that can be accessed by their name Customize the Helpscreen:- You can use CommandHandler.setDefaultHelpFormat(String head, String seperator, String noCmd, ChatColor c1, ChatColor c2) - head: The header of your helpscreen (e.g. "<MyPlugin> Help Page <page> of <maxpage>") - seperator: The string that will split the commandname from the description - noCmd: The message that should be displayed if no commands are found - c1 & c2: The colors (colors for each line of the helpscreen will swap) Create Commands:- You need a class that extends CommandListener - Pass your commandformat to the constructor - e.g. "/cmd <?/help>" or "/cmd say <...>" or "/cmd search <var=what>" - Listen to your command in the handleCommand method - You can get a Variable ("<var=NAME>") by using getVar("NAME", args) - You can get the rest of the command (if you have "<...>" in your command) with getVar("<...>", args)- You can override other methods (you don't have to) - hasPermission(Permissible p) - sendPermissionMessage(CommandSender s) - getDescription() - tabCompleteArgument(ArrayList<String> add, CommandSender s, Command c, String label, String[] args, String argName, String current) - add: You have to add the possible results here - argName: The name of the current variable ("<var=NAME>") - current: The current value of the variable - public void tabCompleteEnd(ArrayList<String> add, CommandSender s, Command c, String label, String[] args, String current, int endoffs) - add: You have to add the possible results for the last word here - current: The current value of the last word - endoffs: How many words are in the end (counting only the ones for "<...>") Screenshots (Move your mouse to reveal the content) Screenshots (open) Screenshots (close) Helpscreen (with permissions) Helpscreen (without permissions) /test print &cHallo das ist ein test Example Usage: Code:java import java.util.ArrayList;import java.util.Collection;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.permissions.Permissible;import org.bukkit.plugin.java.JavaPlugin; public class TestPlugin extends JavaPlugin { private CommandHandler handler; @Override public void onEnable() { Collection<CommandListener> commands = new ArrayList<CommandListener>(); commands.add(new CommandListener("/test <?/help> <var=page>") { @Override public void handleCommand(CommandHandler h, CommandSender sender, Command command, String label, String[] args) { int page = 1; String pageArg = getVar("page", args); if (pageArg != null) try { page = Integer.valueOf(getVar("page", args)); } catch (Exception e) { sender.sendMessage("'" + pageArg + "' is not a number"); return; } sender.sendMessage(h.getHelp(sender, page, 5)); } }); commands.add(new CommandListener("/test print <...>") { @Override public void handleCommand(CommandHandler h, CommandSender sender, Command command, String label, String[] args) { String print = getVar("<...>", args); if(print != null) Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', print)); } @Override public boolean hasPermission(Permissible p) { return p.hasPermission("test.print"); } }); commands.add(new CommandListener("/test heal <var=player>") { @Override public void handleCommand(CommandHandler h, CommandSender sender, Command command, String label, String[] args) { String player = getVar("player", args); Player p = null; if(player != null) { p = Bukkit.getPlayer(player); if(p == null) { sender.sendMessage(ChatColor.RED + "Player '" + player + "' not found"); return; } } if(p != null) { p.setHealth(p.getMaxHealth()); } else if(sender instanceof Player) { p = (Player) sender; p.setHealth(p.getMaxHealth()); } else { sender.sendMessage("You have to be a player"); } } @Override public void tabCompleteArgument(ArrayList<String> add, CommandSender s, Command c, String label, String[] args, String argName, String current) { if(argName.equalsIgnoreCase("player")) { for(Player p : Bukkit.getOnlinePlayers()) if(p.getName().toLowerCase().startsWith(current.toLowerCase())) add.add(p.getName()); } } @Override public String getDescription() { return "You will get healed!!"; } @Override public boolean hasPermission(Permissible p) { return p.hasPermission("test.heal"); } @Override public void sendPermissionMessage(CommandSender p) { p.sendMessage(ChatColor.RED + "You are not allowed to heal yourself..."); } }); handler = new CommandHandler(ChatColor.BLUE + "Command not found!\nUse '/test ?' to see all commands", commands); handler.setDefaultHelpFormat( ChatColor.LIGHT_PURPLE + "<TestPlugin> Help Page <page> of <maxpage>:", " -> ", ChatColor.RED + "No commands found", ChatColor.GREEN, ChatColor.BLUE); } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { return handler.handleCommand(sender, command, label, args); } @Override public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { return handler.handleTabComplete(sender, command, alias, args); } } You can make your commands in another class to: Code:java import java.util.ArrayList; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.permissions.Permissible; public class HealCommand extends CommandListener{ public HealCommand() { super("/test heal <var=player>"); // TODO Auto-generated constructor stub } @Override public void handleCommand(CommandHandler h, CommandSender sender, Command command, String label, String[] args) { String player = getVar("player", args); Player p = null; if(player != null) { p = Bukkit.getPlayer(player); if(p == null) { sender.sendMessage(ChatColor.RED + "Player '" + player + "' not found"); return; } } if(p != null) { p.setHealth(p.getMaxHealth()); } else if(sender instanceof Player) { p = (Player) sender; p.setHealth(p.getMaxHealth()); } else { sender.sendMessage("You have to be a player"); } } @Override public void tabCompleteArgument(ArrayList<String> add, CommandSender s, Command c, String label, String[] args, String argName, String current) { if(argName.equalsIgnoreCase("player")) { for(Player p : Bukkit.getOnlinePlayers()) if(p.getName().toLowerCase().startsWith(current.toLowerCase())) add.add(p.getName()); } } @Override public String getDescription() { return "You will get healed!!"; } @Override public boolean hasPermission(Permissible p) { return p.hasPermission("test.heal"); } @Override public void sendPermissionMessage(CommandSender p) { p.sendMessage(ChatColor.RED + "You are not allowed to heal yourself..."); } }
Ummm.. To me this has no use because the regular bukkit API can easily do this, So its better off as a plugin
ChipDev RenegadeEagle I use this code in all my plugins, so I dont have to worry about the helpscreen. I usually have only 1 command in my plugin, and the executor for it is a total mess, so this script invokes 1 listener for every subcommand, without me needing to worry about the distribution on other classes. Also the tab complete, will autocomplete arguments when they are given in the command constructor.