[Lib] Easy Commands

Discussion in 'Resources' started by CoderCloud, Aug 17, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    CoderCloud

    Easy Commands
    Everything that you need for commands
    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 (open)

    Helpscreen (with permissions)
    2014-08-17_11.25.14.png
    Helpscreen (without permissions)
    2014-08-17_11.25.37.png
    /test print &cHallo das ist ein test
    2014-08-17_11.26.44.png

    Example Usage:
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.Collection;
    3. import java.util.List;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.permissions.Permissible;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class TestPlugin extends JavaPlugin {
    14.  
    15. private CommandHandler handler;
    16.  
    17. @Override
    18. public void onEnable() {
    19. Collection<CommandListener> commands = new ArrayList<CommandListener>();
    20.  
    21. commands.add(new CommandListener("/test <?/help> <var=page>") {
    22.  
    23. @Override
    24. public void handleCommand(CommandHandler h, CommandSender sender,
    25. Command command, String label, String[] args) {
    26. int page = 1;
    27. String pageArg = getVar("page", args);
    28. if (pageArg != null)
    29. try {
    30. page = Integer.valueOf(getVar("page", args));
    31. } catch (Exception e) {
    32. sender.sendMessage("'" + pageArg + "' is not a number");
    33. return;
    34. }
    35. sender.sendMessage(h.getHelp(sender, page, 5));
    36. }
    37.  
    38. });
    39.  
    40. commands.add(new CommandListener("/test print <...>") {
    41.  
    42. @Override
    43. public void handleCommand(CommandHandler h, CommandSender sender,
    44. Command command, String label, String[] args) {
    45. String print = getVar("<...>", args);
    46.  
    47. if(print != null)
    48. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', print));
    49.  
    50. }
    51.  
    52. @Override
    53. public boolean hasPermission(Permissible p) {
    54. return p.hasPermission("test.print");
    55. }
    56.  
    57. });
    58.  
    59. commands.add(new CommandListener("/test heal <var=player>") {
    60.  
    61. @Override
    62. public void handleCommand(CommandHandler h, CommandSender sender,
    63. Command command, String label, String[] args) {
    64. String player = getVar("player", args);
    65.  
    66. Player p = null;
    67.  
    68. if(player != null) {
    69. p = Bukkit.getPlayer(player);
    70. if(p == null) {
    71. sender.sendMessage(ChatColor.RED + "Player '" + player + "' not found");
    72. return;
    73. }
    74. }
    75.  
    76. if(p != null) {
    77. p.setHealth(p.getMaxHealth());
    78. } else if(sender instanceof Player) {
    79. p = (Player) sender;
    80. p.setHealth(p.getMaxHealth());
    81. } else {
    82. sender.sendMessage("You have to be a player");
    83. }
    84. }
    85.  
    86. @Override
    87. public void tabCompleteArgument(ArrayList<String> add,
    88. CommandSender s, Command c, String label, String[] args,
    89. String argName, String current) {
    90. if(argName.equalsIgnoreCase("player")) {
    91. for(Player p : Bukkit.getOnlinePlayers())
    92. if(p.getName().toLowerCase().startsWith(current.toLowerCase()))
    93. add.add(p.getName());
    94. }
    95. }
    96.  
    97. @Override
    98. public String getDescription() {
    99. return "You will get healed!!";
    100. }
    101.  
    102. @Override
    103. public boolean hasPermission(Permissible p) {
    104. return p.hasPermission("test.heal");
    105. }
    106.  
    107. @Override
    108. public void sendPermissionMessage(CommandSender p) {
    109. p.sendMessage(ChatColor.RED + "You are not allowed to heal yourself...");
    110. }
    111.  
    112. });
    113.  
    114. handler = new CommandHandler(ChatColor.BLUE + "Command not found!\nUse '/test ?' to see all commands", commands);
    115. handler.setDefaultHelpFormat(
    116. ChatColor.LIGHT_PURPLE + "<TestPlugin> Help Page <page> of <maxpage>:",
    117. " -> ",
    118. ChatColor.RED + "No commands found",
    119. ChatColor.GREEN, ChatColor.BLUE);
    120. }
    121.  
    122. @Override
    123. public boolean onCommand(CommandSender sender, Command command,
    124. String label, String[] args) {
    125. return handler.handleCommand(sender, command, label, args);
    126. }
    127.  
    128. @Override
    129. public List<String> onTabComplete(CommandSender sender, Command command,
    130. String alias, String[] args) {
    131. return handler.handleTabComplete(sender, command, alias, args);
    132. }
    133.  
    134. }


    You can make your commands in another class to:

    Code:java
    1. import java.util.ArrayList;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.permissions.Permissible;
    9.  
    10. public class HealCommand extends CommandListener{
    11.  
    12. public HealCommand() {
    13. super("/test heal <var=player>");
    14. // TODO Auto-generated constructor stub
    15. }
    16.  
    17. @Override
    18. public void handleCommand(CommandHandler h, CommandSender sender,
    19. Command command, String label, String[] args) {
    20. String player = getVar("player", args);
    21.  
    22. Player p = null;
    23.  
    24. if(player != null) {
    25. p = Bukkit.getPlayer(player);
    26. if(p == null) {
    27. sender.sendMessage(ChatColor.RED + "Player '" + player + "' not found");
    28. return;
    29. }
    30. }
    31.  
    32. if(p != null) {
    33. p.setHealth(p.getMaxHealth());
    34. } else if(sender instanceof Player) {
    35. p = (Player) sender;
    36. p.setHealth(p.getMaxHealth());
    37. } else {
    38. sender.sendMessage("You have to be a player");
    39. }
    40. }
    41.  
    42. @Override
    43. public void tabCompleteArgument(ArrayList<String> add,
    44. CommandSender s, Command c, String label, String[] args,
    45. String argName, String current) {
    46. if(argName.equalsIgnoreCase("player")) {
    47. for(Player p : Bukkit.getOnlinePlayers())
    48. if(p.getName().toLowerCase().startsWith(current.toLowerCase()))
    49. add.add(p.getName());
    50. }
    51. }
    52.  
    53. @Override
    54. public String getDescription() {
    55. return "You will get healed!!";
    56. }
    57.  
    58. @Override
    59. public boolean hasPermission(Permissible p) {
    60. return p.hasPermission("test.heal");
    61. }
    62.  
    63. @Override
    64. public void sendPermissionMessage(CommandSender p) {
    65. p.sendMessage(ChatColor.RED + "You are not allowed to heal yourself...");
    66. }
    67.  
    68. }

     
  2. Offline

    ChipDev

    Ummm.. To me this has no use because the regular bukkit API can easily do this, So its better off as a plugin
     
  3. Why do I feel like the regular system would be easier...
    No offence, but semi-useless resource.
     
  4. Offline

    CoderCloud

    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.
     
Thread Status:
Not open for further replies.

Share This Page