Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. public class OrenCommandHandler implements CommandExecutor {
  2.  
  3.     /**
  4.      * All the commands registered and past on
  5.      */
  6.     private ArrayList<OrenCommand> commands = new ArrayList<>();
  7.  
  8.     @Override
  9.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  10.         if (args.length == 0) {
  11.             sender.sendMessage("§8§lOrenMC Commands:");
  12.             sender.sendMessage(help(0));
  13.         } else if (NumberUtils.isNumber(args[0])) {
  14.             sender.sendMessage("§8§lOrenMC Commands:");
  15.             sender.sendMessage(help(Integer.parseInt(args[0])));
  16.         } else {
  17.             OrenCommand oc = getCommand(args[0]);
  18.             if (oc == null) {
  19.                 sender.sendMessage("§8§lOrenMC Commands:" + help(0));
  20.                 sender.sendMessage(help(0));
  21.             } else {
  22.                 oc.getExecutor().onCommand(sender, cmd, args[0], args.length >= 2 ? Arrays.copyOfRange(args, 1, args.length) : new String[0]);
  23.             }
  24.         }
  25.         return true;
  26.     }
  27.  
  28.     /**
  29.      * Get a string of all the commands registered
  30.      *
  31.      * @return The commands separated by a comma
  32.      */
  33.     private String help(int start) {
  34.         StringBuilder sb = new StringBuilder();
  35.  
  36.         OrenCommand oc;
  37.         for (int i = start * 9; i < commands.size() && i < (start + 1) * 9; i++) {
  38.             oc = commands.get(i);
  39.             sb.append("§a" + StringUtils.capitalize(oc.getName()) + ": §7" + oc.getDescription() + "\n");
  40.         }
  41.         return sb.toString().trim();
  42.     }
  43.  
  44.     /**
  45.      * Add a command to be handled by OrenCore.
  46.      *
  47.      * @param name
  48.      *            The name of the command
  49.      * @param executor
  50.      *            The executor to handle the command
  51.      * @param aliases
  52.      *            Any aliases of the command
  53.      */
  54.     public void hookCommand(String name, CommandExecutor executor, String description, String... aliases) {
  55.         commands.add(new OrenCommand(name, executor, description, aliases));
  56.     }
  57.  
  58.     /**
  59.      * Get the command based on the given alias or label
  60.      *
  61.      * @param label
  62.      *            The alias or label
  63.      * @return the corresponding command
  64.      */
  65.     private OrenCommand getCommand(String label) {
  66.         for (OrenCommand oc : commands) {
  67.             if (oc.isAlias(label)) {
  68.                 return oc;
  69.             }
  70.         }
  71.         return null;
  72.     }
  73.  
  74.     private class OrenCommand {
  75.  
  76.         private String name;
  77.         private CommandExecutor executor;
  78.         private String description;
  79.         private String[] aliases;
  80.  
  81.         public OrenCommand(String name, CommandExecutor executor, String description, String... aliases) {
  82.             this.name = name;
  83.             this.executor = executor;
  84.             this.description = description;
  85.             this.aliases = aliases;
  86.         }
  87.  
  88.         public CommandExecutor getExecutor() {
  89.             return executor;
  90.         }
  91.  
  92.         public String getName() {
  93.             return name;
  94.         }
  95.  
  96.         public String getDescription() {
  97.             return description;
  98.         }
  99.  
  100.         public boolean isAlias(String label) {
  101.             if (label.equalsIgnoreCase(name)) {
  102.                 return true;
  103.             }
  104.             for (String alias : aliases) {
  105.                 if (alias.equalsIgnoreCase(label)) {
  106.                     return true;
  107.                 }
  108.             }
  109.             return false;
  110.         }
  111.  
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement