[Tutorial] How to: Create a Player & Console command executor.

Discussion in 'Resources' started by Jake6177, Nov 24, 2013.

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

    Jake6177

    This is how I structure commands in almost all of my plugins and I thought that it might be of use to someone or a few someones :) Check it out:

    Main class:
    Code:java
    1.  
    2. package something;
    3.  
    4. // not bothering with imports
    5.  
    6. public class something extends JavaPlugin {
    7. this.playerExecutor = new PlayerExecutor();
    8. this.consoleExecutor = new ConsoleExecutor();
    9.  
    10. @Override
    11. public boolean onCommand(CommandSender sender, Command string, String label, String[] args) {
    12. if (sender instanceof Player) {
    13. Player player = (Player) sender;
    14. return this.playerExecutor.exec(player, label, args);
    15. }
    16. return this.consoleExecutor.exec(sender, label, args);
    17. }
    18. }
    19.  


    Alright, so let's break this down..

    First, we check if the sender is an instance of Player (is a Player) and if they are, ..wait..WHAT?

    Let me explain, if the sender is a player, we'll call an object of a PlayerExecutor class. On that object, we'll call it's respective method "exec". This method receives the player, the command label, and the command arguments. From here, we can distinguish what the command is and then execute that command.

    Let's take a look at the PlayerExecutor class:
    Code:java
    1.  
    2. package something.commands.player;
    3.  
    4. // not bothering with imports
    5.  
    6. public class PlayerExecutor {
    7. private Kill kill = new Kill();
    8.  
    9. public boolean exec(Player p, String l, String a[]) {
    10. if (l.equalsIgnoreCase("kill") {
    11. return this.kill.kill(p);
    12. }
    13. return true;
    14. }
    15. }
    16.  


    Okay, now I bet you're real confused. Let's break this one down now..

    First, we define a new object of a Kill class (which we haven't coded yet) and then we set it in the PlayerExecutor constructor. In our exec method, we check if the label (which we got from our onCommand()) is equal to "kill", ignoring the case. If so, then we return a method of the Kill object, kill (which is another boolean), passing in the player as the only argument.

    Now, let's take a look at the Kill class:
    Code:java
    1.  
    2. package something.commands.player.kill;
    3.  
    4. // not bothering with imports
    5.  
    6. public class Kill {
    7. public boolean kill(Player p) {
    8. p.setHealth(0.0);
    9. p.sendMessage(ChatColor.GREEN + "You have committed suicide. Congratulations!");
    10. return true;
    11. }
    12. }
    13.  


    Now we're done!

    Let's break this all down:

    First, in our onCommand(), we check if the CommandSender is an instance of (is a) Player. If it is, then we call our PlayerExecutor, passing in the Player, the Label, and the Arguments.

    With those, in our PlayerExecutor, we have a method "exec". That method takes our Player, Label, and Arguments, and checks the label for the command that was typed. When it finds one that matches, in this case: "kill", it will call the Kill object's "kill" method. In that method, we set the Player's health to 0.0, which will kill them.

    Thoroughly confused? Good.

    Think of it like this: onCommand() -> proper Executor class -> proper Command class -> return true/false -> return true/false -> return true/false

    It's a bit of extra work, but it really helps with organization and flow.

    Hope you liked the tutorial, see ya 'round. :)
     
Thread Status:
Not open for further replies.

Share This Page