Advertisement
Jnk1296

RPCommons - Command Base

Feb 17th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.88 KB | None | 0 0
  1. package net.risenphoenix.commons.Commands;
  2.  
  3. import net.risenphoenix.commons.Plugin;
  4. import net.risenphoenix.commons.Localization.LocalizationManager;
  5.  
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.permissions.Permission;
  8.  
  9. /**
  10.  * Base class for Plugin Commands. This class contains common shared methods which all
  11.  * plugin commands inherit.</br>
  12.  *
  13.  * </br>This class is responsible for performing checks for player
  14.  * permissions, storing and providing command specific variables such as Name, Syntax
  15.  * and Help Text, as well as providing access to Global Resources such as the Localization
  16.  * Manager.
  17.  *
  18.  * @author Jacob Keep (Jnk1296)
  19.  */
  20.  
  21. public class Command {
  22.     private final Plugin plugin;
  23.    
  24.     private String name;
  25.     private String syntax;
  26.     private String help;
  27.    
  28.     private boolean isConsoleExecutable = true;
  29.    
  30.     private String[] callArgs;
  31.     private int requiredArgs;
  32.    
  33.     private Permission[] commandPerms = null;
  34.    
  35.     private LocalizationManager LM;
  36.    
  37.     /**
  38.      * <strong>Constructor:</strong> Responisble for the main initialization of the
  39.      * Command instance. Registers the required arguments for the command to be
  40.      * identified, as well as the number of arguments needed to successfully call the
  41.      * command. The Plugin instance is required to link to the LocalizationManager.</br>
  42.      *
  43.      * </br>The number passed for <strong>requiredArgs</strong> can be any positive number
  44.      * (0+) or -1. When -1 is specified, this signals that the command is open-ended, and
  45.      * allows for a variable number of arguments. This is typically used for commands such
  46.      * as those allowing for custom messages to specified. <strong>When setting the
  47.      * number of arguments required, DO NOT INCLUDE the root command!</strong> The
  48.      * Command Manager will fail to parse input for your command if you do this.</br>
  49.      *
  50.      * <br><strong>callArgs</strong> contains the sic arguments which would be typed
  51.      * in-game to call the command. <strong>Ex: </strong><em>/ipc exempt-list ip</em>
  52.      * <strong>-></strong> { "exempt-list", "ip" }. The root command is always absent
  53.      * from the arguments array.
  54.      *
  55.      * @param plugin
  56.      * @param callArgs
  57.      * @param requiredArgs
  58.      */
  59.    
  60.     public Command(final Plugin plugin, String[] callArgs, int requiredArgs) {
  61.         this.plugin = plugin;
  62.         this.callArgs = callArgs;
  63.         this.requiredArgs = requiredArgs;
  64.         this.LM = this.plugin.getLocalizationManager();
  65.     }
  66.    
  67.     /**
  68.      * Called by the CommandManager during execution. This method is the public executor
  69.      * for all Commands, and should be called whenever you wish to execute a Command in
  70.      * your code.</br>
  71.      *
  72.      * </br>The method returns a boolean value depending on whether the CommandSender
  73.      * Object passed can call the command or not.</br>
  74.      *
  75.      * </br><b>Operation is as follows: <ul>
  76.      *
  77.      * <li></b>When called, the method
  78.      * checks the CommandSender object passed for OP Status. If the CS has OP Status, all
  79.      * further checks are skipped and <b>onExecute()</b> is called, causing the method to
  80.      * return <b>TRUE</b>.</li>
  81.      *
  82.      * <li>If the CS object does not have OP Status, the method then checks
  83.      * to see if the Command Instance has any permissions specified in <b>commandPerms</b>
  84.      * .</li>
  85.      *  
  86.      * <li>If permissions are found, they are looped through and checked against the CS
  87.      * Object one by one. Should the CS Object fail to have any of the specified
  88.      * permissions, the method returns <b>FALSE</b>.</li></ul>
  89.      */
  90.    
  91.     public final boolean onCall(CommandSender sender, String[] args) {
  92.         if (!sender.isOp()) {
  93.             if (this.commandPerms != null) {
  94.                 for (int i = 0; i < this.commandPerms.length; i++) {
  95.                     if (!sender.hasPermission(this.commandPerms[i])) return false;
  96.                 }
  97.             }
  98.         }
  99.         onExecute(sender, args);
  100.         return true;
  101.     }
  102.    
  103.     /**
  104.      * Sets the permissions required for this Command to be executed.</br>
  105.      *
  106.      * </br>The permission should be passed as an array like such:<ul>
  107.      * <li><em>new Permission[]{ new Permission("permission.one"), new Permission
  108.      * ("permission.two"), etc... };</em></li></ul>
  109.      * @param perms
  110.      */
  111.     public final void setPermissions(Permission[] perms) {
  112.         this.commandPerms = perms;
  113.     }
  114.    
  115.     /**
  116.      * Sets the Display Name for this Command.</br>
  117.      *
  118.      * </br>The name can be any string.
  119.      * @param cmdName
  120.      */
  121.     public final void setName(String cmdName) {
  122.         this.name = cmdName;
  123.     }
  124.    
  125.     /**
  126.      * Sets the syntax for this Command.</br>
  127.      *
  128.      * </br>When set, the string passed should contain the literal (sic) syntax to be used
  129.      * when calling your command, <strong>minus the root command</strong>. The syntax
  130.      * string is generally used for displaying help information and should be relatively
  131.      * easy to understand.</br>
  132.      *
  133.      * </br>Ex: <em>ban {@literal <}player{@literal >} [message]</em>
  134.      * @param cmdSyntax
  135.      */
  136.     public final void setSyntax(String cmdSyntax) {
  137.         this.syntax = cmdSyntax;
  138.     }
  139.    
  140.     /**
  141.      * Sets the Help Description text for this command.</br>
  142.      *
  143.      * </br>This text is displayed in the Help Command and is used to describe what this
  144.      * Command does, as well as any other information the end user may need to know.
  145.      * @param helpDesc
  146.      */
  147.     public final void setHelp(String helpDesc) {
  148.         this.help = helpDesc;
  149.     }
  150.    
  151.     /**
  152.      * Sets whether this Command can be called by Console or not.</br>
  153.      *
  154.      * </br>Depending on the status of this flag, you can limit this Command to being a
  155.      * Player-only Command, meaning that Console cannot execute this Command.</br>
  156.      *
  157.      * </br><strong>TRUE</strong> = Console can execute, <strong>FALSE</strong> = Console
  158.      * cannot execute.
  159.      * @param consoleCanExecute
  160.      */
  161.     public final void setConsoleExecutable(boolean consoleCanExecute) {
  162.         this.isConsoleExecutable = consoleCanExecute;
  163.     }
  164.    
  165.     /**
  166.      * The main execution method for this Command.</br>
  167.      *
  168.      * </br>The only overrideable method of this Command, this method contains the code
  169.      * that your Command will execute when onCall() is callled. The method can be set to
  170.      * return any number of Types, but doing so will break compliance with the API. Also,
  171.      * such Type specification will not work, as the result of this method (void) is
  172.      * returned to the onCall method, which also returns void.</br>
  173.      *
  174.      * </br>If you wish for this Command to return a value other than <strong>void
  175.      * </strong>, you must set the return type of both onExecute() and onCall() so that
  176.      * they both return the same Type.
  177.      */
  178.     private void onExecute(CommandSender sender, String[] args) {
  179.         throw new UnsupportedOperationException(this.getLocalString("NO_IMPLEMENT"));
  180.     }
  181.    
  182.     /**
  183.      * Returns the Name of this Command.
  184.      * @return String
  185.      */
  186.     public final String getName() {
  187.         return this.name;
  188.     }
  189.    
  190.     /**
  191.      * Returns the Syntax of this Command, minus the root.
  192.      * @return String
  193.      */
  194.     public final String getSyntax() {
  195.         return this.syntax;
  196.     }
  197.    
  198.     /**
  199.      * Returns the Help Description text for this Command.
  200.      * @return String
  201.      */
  202.     public final String getHelp() {
  203.         return this.help;
  204.     }
  205.    
  206.     /**
  207.      * Returns a Permission[] containing all the permissions this Command requires to
  208.      * be executed.
  209.      * @return Permission[]
  210.      */
  211.     public final Permission[] getPermissions() {
  212.         return this.commandPerms;
  213.     }
  214.    
  215.     /**
  216.      * Returns a boolean denoting whether this command can be executed by the Console or
  217.      * not.
  218.      * @return boolean
  219.      */
  220.     public final boolean canConsoleExecute() {
  221.         return this.isConsoleExecutable;
  222.     }
  223.    
  224.     /**
  225.      * Returns a String[] containing all the arguments required to be typed by the end
  226.      * user for this Command to be identified by the Command Manager.
  227.      * @return String[]
  228.      */
  229.     public final String[] getCallArgs() {
  230.         return this.callArgs;
  231.     }
  232.    
  233.     /**
  234.      * Returns an integer of the number of arguments required for this Command to be
  235.      * identified by the Command Manager.
  236.      * @return int
  237.      */
  238.     public final int getArgumentsRequired() {
  239.         return this.requiredArgs;
  240.     }
  241.    
  242.     /**
  243.      * This method checks the CommandSender Object passed against the permissions this
  244.      * Command requires in order to be executed. If the CS Object has all the required
  245.      * permissions, this method will return <strong>TRUE</strong>, else it will return
  246.      * <strong>FALSE</strong>.
  247.      * @param sender
  248.      * @return boolean
  249.      */
  250.     public final boolean canExecute(CommandSender sender) {
  251.         if (this.commandPerms == null) return true;
  252.        
  253.         for (int i = 0; i < this.commandPerms.length; i++) {
  254.             if (!sender.hasPermission(this.commandPerms[i])) return false;
  255.         }
  256.        
  257.         return true;
  258.     }
  259.    
  260.     /**
  261.      * This method is used to obtain localized messages from the Localization Manager. In
  262.      * order to get a Localized Message, a String <strong><em>key</em></strong> must be
  263.      * passed which corresponds to the message in the Localization Manager.
  264.      * @param key
  265.      * @return String
  266.      */
  267.     private final String getLocalString(String key) {
  268.         return this.LM.getLocalString(key);
  269.     }
  270.    
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement