Need help with commands!

Discussion in 'Plugin Development' started by DaddyDBJ21, Sep 14, 2013.

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

    DaddyDBJ21

    So I have an onCommand method with a command. When someone executes the command, it needs to do one thing. When they execute it again, it needs to do something else. How do I do this? Here's my current code. However, when I type the command, all three iterations of the command all execute at the same time. Help!

    Code:java
    1. int commandCount = 0;
    2. public boolean onCommand(CommandSender sender, Command comamnd, String Commandlabel, String[] args){
    3. if(Commandlabel.equalsIgnoreCase("command")){ // the command that will be executed
    4. if(sender.isOp()){ // the player that is sending the command
    5. if (args.length != 1) {
    6. Player player = (Player) sender;
    7. player.sendMessage("Usage: ");
    8. }
    9. if (commandCount == 0) {
    10. stuff();
    11. commandCount++;
    12. }
    13. if (commandCount == 1) {
    14. stuff2();
    15. commandCount++;
    16. }
    17. if (commandCount == 2) {
    18. stuff3();
    19.  
    20. commandCount = 0;
    21. }
    22. }
    23. else{
    24. Player player = (Player) sender;
    25. player.sendMessage(ChatColor.RED + "You don't have permission to perform this command!");
    26. // insert code here on what to do if the player is not an op
    27. }
    28.  
    29. }
    30. return false;
    31. }
     
  2. DaddyDBJ21
    You have to break the method or return something to prevent it from continuing.
     
  3. Offline

    chasechocolate

    Use else-ifs. Also, save the command counts in a HashMap<String, Integer>.
     
    DaddyDBJ21 likes this.
  4. Offline

    DaddyDBJ21

Thread Status:
Not open for further replies.

Share This Page