StarShadow

MC - Command Builder API

Jul 16th, 2015 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.81 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import org.bukkit.command.Command;
  4. import org.bukkit.command.CommandExecutor;
  5. import org.bukkit.command.CommandSender;
  6. import org.bukkit.entity.Player;
  7.  
  8. public class SCmd implements CommandExecutor {
  9.    
  10.     public static enum Message {
  11.         DISABLED_PLAYERS,
  12.         DISABLED_CONSOLE,
  13.         NO_PERM,
  14.         NO_ACTION,
  15.         ARG_NOT_FOUND;
  16.     }
  17.     private static class Messages {
  18.         public static String DISABLED_PLAYERS = "That is disabled for players.";
  19.         public static String DISABLED_CONSOLE = "That is disabled for the console.";
  20.         public static String NO_PERM = "You do not have the permission to do that.";
  21.         public static String NO_ACTION = "There is no action defined for that.";
  22.         public static String ARG_NOT_FOUND = "Argument not found.";
  23.         public static void edit(Message m, String msg) {
  24.             switch(m) {
  25.             case DISABLED_PLAYERS:
  26.                 DISABLED_PLAYERS = msg;
  27.                 break;
  28.             case DISABLED_CONSOLE:
  29.                 DISABLED_CONSOLE = msg;
  30.                 break;
  31.             case NO_PERM:
  32.                 NO_PERM = msg;
  33.                 break;
  34.             case NO_ACTION:
  35.                 NO_ACTION = msg;
  36.                 break;
  37.             case ARG_NOT_FOUND:
  38.                 ARG_NOT_FOUND = msg;
  39.                 break;
  40.             default:
  41.                 break;
  42.             }
  43.         }
  44.     }
  45.    
  46.     public interface CmdAction {
  47.         public void run(CommandSender sender);
  48.     }
  49.     public interface ArgAction {
  50.         public void run(CommandSender sender, String[] args);
  51.     }
  52.    
  53.     public static class Subcommand {
  54.         private ArrayList<Subcommand> subs = new ArrayList<Subcommand>();
  55.         private ArrayList<Arguments> arglist = new ArrayList<Arguments>();
  56.         private CmdAction action;
  57.         private String cmd, perm;
  58.         private String[] help;
  59.         private boolean players = true, console = true, noac = true, endhere = false;
  60.         public Subcommand(String cmd) {
  61.             this.cmd = cmd;
  62.         }
  63.         public Subcommand(String cmd, String perm) {
  64.             this.cmd = cmd;
  65.             this.perm = perm;
  66.         }
  67.         public String getName() {
  68.             return cmd;
  69.         }
  70.         public boolean check(String input) {
  71.             return input.equalsIgnoreCase(cmd);
  72.         }
  73.         public Subcommand endHere(boolean end) {
  74.             this.endhere = end;
  75.             return this;
  76.         }
  77.         public Subcommand setHelpMSG(String[] msg) {
  78.             this.help = msg;
  79.             return this;
  80.         }
  81.         public Subcommand msgNoAction(boolean b) {
  82.             this.noac = b;
  83.             return this;
  84.         }
  85.         public Subcommand allowPlayers(boolean allow) {
  86.             players = allow;
  87.             if (!allow) console = true;
  88.             return this;
  89.         }
  90.         public Subcommand allowConsole(boolean allow) {
  91.             console = allow;
  92.             if (!allow) players = true;
  93.             return this;
  94.         }
  95.         public Subcommand addSubcommand(Subcommand sub) throws IllegalArgumentException {
  96.             for (Subcommand s : subs) {
  97.                 if (s.getName().equalsIgnoreCase(sub.getName())) throw new IllegalArgumentException("Subcommand already exists.");
  98.             }
  99.             subs.add(sub);
  100.             return this;
  101.         }
  102.         public Subcommand addArguments(Arguments args) throws IllegalArgumentException {
  103.             for (Arguments a : arglist) {
  104.                 if (a.length() == args.length()) throw new IllegalArgumentException("There are already arguments for that length.");
  105.             }
  106.             arglist.add(args);
  107.             return this;
  108.         }
  109.         public Subcommand setAction(CmdAction ca) {
  110.             action = ca;
  111.             return this;
  112.         }
  113.         public void use(CommandSender sender, String[] args, int level) {
  114.             boolean player = false;
  115.             if (sender instanceof Player) {
  116.                 player = true;
  117.                 if (!players) {
  118.                     sender.sendMessage(Messages.DISABLED_PLAYERS);
  119.                     return;
  120.                 }
  121.             }
  122.             else {
  123.                 if (!console) {
  124.                     sender.sendMessage(Messages.DISABLED_CONSOLE);
  125.                     return;
  126.                 }
  127.             }
  128.            
  129.             if (player) {
  130.                 if (perm != null && !perm.isEmpty()) {
  131.                     if (!((Player) sender).hasPermission(perm)) {
  132.                         sender.sendMessage(Messages.NO_PERM);
  133.                         return;
  134.                     }
  135.                 }
  136.             }
  137.            
  138.             if (args.length == level+1 || endhere) {
  139.                 if (action != null) {
  140.                     action.run(sender);
  141.                 }
  142.                 else {
  143.                     if (help != null && help.length > 0) {
  144.                         for (int i = 0; i < help.length; i++) {
  145.                             sender.sendMessage(help[i]);
  146.                         }
  147.                     }
  148.                     else {
  149.                         if (noac) sender.sendMessage(Messages.NO_ACTION);
  150.                     }
  151.                 }
  152.             }
  153.             else if (args.length > level+1) {
  154.                 boolean f = false;
  155.                 if (subs.size() > 0) {
  156.                     for (Subcommand sub : subs) {
  157.                         if (sub.check(args[level+1])) {
  158.                             sub.use(sender, args, level+1);
  159.                             f = true;
  160.                             break;
  161.                         }
  162.                     }
  163.                     if (!f) {
  164.                         for (Arguments a : arglist) {
  165.                             if (a.length() == (args.length-(level+1))) {
  166.                                 a.use(sender, args, level+1);
  167.                                 f = true;
  168.                                 break;
  169.                             }
  170.                         }
  171.                         if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
  172.                     }
  173.                 }
  174.                 else {
  175.                     for (Arguments a : arglist) {
  176.                         if (a.length() == (args.length-(level+1))) {
  177.                             a.use(sender, args, level+1);
  178.                             f = true;
  179.                             break;
  180.                         }
  181.                     }
  182.                     if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
  183.                 }
  184.             }
  185.         }
  186.     }
  187.    
  188.     public static class Arguments {
  189.         private ArgAction action;
  190.         private int length;
  191.         private String perm;
  192.         private String[] help;
  193.         private boolean players = true, console = true, noac = true;
  194.         public Arguments(int length) {
  195.             this.length = length;
  196.         }
  197.         public Arguments(int length, String perm) {
  198.             this.length = length;
  199.             this.perm = perm;
  200.         }
  201.         public int length() {
  202.             return length;
  203.         }
  204.         public Arguments setHelpMSG(String[] msg) {
  205.             this.help = msg;
  206.             return this;
  207.         }
  208.         public Arguments msgNoAction(boolean b) {
  209.             this.noac = b;
  210.             return this;
  211.         }
  212.         public Arguments allowPlayers(boolean allow) {
  213.             players = allow;
  214.             if (!allow) console = true;
  215.             return this;
  216.         }
  217.         public Arguments allowConsole(boolean allow) {
  218.             console = allow;
  219.             if (!allow) players = true;
  220.             return this;
  221.         }
  222.         public Arguments setAction(ArgAction aa) {
  223.             action = aa;
  224.             return this;
  225.         }
  226.         public void use(CommandSender sender, String[] args, int level) {
  227.             boolean player = false;
  228.             if (sender instanceof Player) {
  229.                 player = true;
  230.                 if (!players) {
  231.                     sender.sendMessage(Messages.DISABLED_PLAYERS);
  232.                     return;
  233.                 }
  234.             }
  235.             else {
  236.                 if (!console) {
  237.                     sender.sendMessage(Messages.DISABLED_CONSOLE);
  238.                     return;
  239.                 }
  240.             }
  241.            
  242.             if (player) {
  243.                 if (perm != null && !perm.isEmpty()) {
  244.                     if (!((Player) sender).hasPermission(perm)) {
  245.                         sender.sendMessage(Messages.NO_PERM);
  246.                         return;
  247.                     }
  248.                 }
  249.             }
  250.            
  251.             if (action != null) {
  252.                 String[] a = new String[length];
  253.                 for (int i = 0; i < length; i++) {
  254.                     a[i] = args[level+i];
  255.                 }
  256.                 action.run(sender, a);
  257.             }
  258.             else {
  259.                 if (help != null && help.length > 0) {
  260.                     for (int i = 0; i < help.length; i++) {
  261.                         sender.sendMessage(help[i]);
  262.                     }
  263.                 }
  264.                 else {
  265.                     if (noac) sender.sendMessage(Messages.NO_ACTION);
  266.                 }
  267.             }
  268.         }
  269.     }
  270.    
  271.     public SCmd() {}
  272.     public SCmd(String perm) {
  273.         this.perm = perm;
  274.     }
  275.    
  276.     private ArrayList<Subcommand> subs = new ArrayList<Subcommand>();
  277.     private ArrayList<Arguments> arglist = new ArrayList<Arguments>();
  278.     private CmdAction action;
  279.    
  280.     private String perm;
  281.     private String[] help;
  282.     private boolean players = true, console = true, noac = true, endhere = false;
  283.    
  284.     @Override
  285.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  286.        
  287.         boolean player = false;
  288.         if (sender instanceof Player) {
  289.             player = true;
  290.             if (!players) {
  291.                 sender.sendMessage(Messages.DISABLED_PLAYERS);
  292.                 return true;
  293.             }
  294.         }
  295.         else {
  296.             if (!console) {
  297.                 sender.sendMessage(Messages.DISABLED_CONSOLE);
  298.                 return true;
  299.             }
  300.         }
  301.        
  302.         if (player) {
  303.             if (perm != null && !perm.isEmpty()) {
  304.                 if (!((Player) sender).hasPermission(perm)) {
  305.                     sender.sendMessage(Messages.NO_PERM);
  306.                     return true;
  307.                 }
  308.             }
  309.         }
  310.        
  311.         if (args.length == 0 || endhere) {
  312.             if (action != null) {
  313.                 action.run(sender);
  314.             }
  315.             else {
  316.                 if (help != null && help.length > 0) {
  317.                     for (int i = 0; i < help.length; i++) {
  318.                         sender.sendMessage(help[i]);
  319.                     }
  320.                 }
  321.                 else {
  322.                     if (noac) sender.sendMessage(Messages.NO_ACTION);
  323.                 }
  324.             }
  325.         }
  326.         else if (args.length > 0) {
  327.             boolean f = false;
  328.             if (subs.size() > 0) {
  329.                 for (Subcommand sub : subs) {
  330.                     if (sub.check(args[0])) {
  331.                         sub.use(sender, args, 0);
  332.                         f = true;
  333.                         break;
  334.                     }
  335.                 }
  336.                 if (!f) {
  337.                     for (Arguments a : arglist) {
  338.                         if (a.length() == args.length) {
  339.                             a.use(sender, args, 0);
  340.                             f = true;
  341.                             break;
  342.                         }
  343.                     }
  344.                     if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
  345.                 }
  346.             }
  347.             else {
  348.                 for (Arguments a : arglist) {
  349.                     if (a.length() == args.length) {
  350.                         a.use(sender, args, 0);
  351.                         f = true;
  352.                         break;
  353.                     }
  354.                 }
  355.                 if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
  356.             }
  357.         }
  358.        
  359.         return true;
  360.     }
  361.    
  362.     public SCmd endHere(boolean end) {
  363.         endhere = end;
  364.         return this;
  365.     }
  366.    
  367.     public SCmd msgNoAction(boolean b) {
  368.         this.noac = b;
  369.         return this;
  370.     }
  371.    
  372.     public SCmd editMessage(Message m, String msg) {
  373.         Messages.edit(m, msg);
  374.         return this;
  375.     }
  376.    
  377.     public SCmd setHelpMSG(String[] msg) {
  378.         help = msg;
  379.         return this;
  380.     }
  381.    
  382.     public SCmd allowPlayers(boolean allow) {
  383.         players = allow;
  384.         if (!allow) console = true;
  385.         return this;
  386.     }
  387.    
  388.     public SCmd allowConsole(boolean allow) {
  389.         console = allow;
  390.         if (!allow) players = true;
  391.         return this;
  392.     }
  393.    
  394.     public SCmd addSubcommand(Subcommand sub) throws IllegalArgumentException {
  395.         for (Subcommand s : subs) {
  396.             if (s.getName().equalsIgnoreCase(sub.getName())) throw new IllegalArgumentException("Subcommand already exists.");
  397.         }
  398.         subs.add(sub);
  399.         return this;
  400.     }
  401.    
  402.     public SCmd addArguments(Arguments args) throws IllegalArgumentException {
  403.         for (Arguments a : arglist) {
  404.             if (a.length() == args.length()) throw new IllegalArgumentException("There are already arguments for that length.");
  405.         }
  406.         arglist.add(args);
  407.         return this;
  408.     }
  409.    
  410.     public SCmd setAction(CmdAction ca) {
  411.         action = ca;
  412.         return this;
  413.     }
  414.  
  415. }
Add Comment
Please, Sign In to add comment