I need help with Multiple Commands (New to java)

Discussion in 'Plugin Development' started by ARNGCollins, Aug 6, 2012.

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

    ARNGCollins

    I've been slowly learning java over the past couple of days as I wait to leave for bootcamp and i started to watch tutorial videos on how to do the commands but they didnt really explain how to add Multiple Commands. I changed a lot myself and received help with my previous problem's.
    Here's what i have now. But For some reason I am having issue's getting All 3 commands to work without any errors.

    Code:
    package me.ARNGCollins.NationalGuard;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class NationalGuard extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static NationalGuard plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("NGVersion")){
                player.sendMessage(ChatColor.GOLD + "Version 1");
            }
            if(cmd.getName().equalsIgnoreCase("NGHeal")){
                if(args.length == 0){
                    //heal = 0 args1 /heal PLAYER = 1 args1
                    player.setHealth(20);
                    player.setFireTicks(0);
                    player.setFoodLevel(20);
                    player.sendMessage(ChatColor.AQUA + "You've been healed");
                }else if(args.length == 1){
                    if(player.getServer().getPlayer(args[0]) !=null){
                    Player targetPlayer1 = player.getServer().getPlayer(args [0]);
                    targetPlayer1.setHealth(20);
                    targetPlayer1.setFireTicks(0);
                    targetPlayer1.setFoodLevel(20);
                    player.sendMessage(ChatColor.AQUA + "You've been healed");
                }else{
                    player.sendMessage(ChatColor.RED + "Player Not Online");
            }
            if(cmd.getName().equalsIgnoreCase("NGTele")){
                if(args.length == 0)
                    player.sendMessage(ChatColor.GREEN + "Too little agruments");
            }else if(args.length == 1){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Location targetPlayerLocation = targetPlayer.getLocation();
                player.teleport(targetPlayerLocation);
            }else if(args.length == 2){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                Location targetPlayer1Location = targetPlayer1.getLocation();
                targetPlayer1.teleport(targetPlayer1Location);
                targetPlayer1.sendMessage("You've been teleported to " + player.getDisplayName());
            }
            }
        }
            return false;
    }
    }
    Here's my Plugin.yml if this helps

    Code:
    name: NationalGuard
    main: me.ARNGCollins.NationalGuard.NationalGuard
    version: 1
    description: >
                ARNG Plugin.
    commands:
      NGHeal:
        description: Heals the Player
        aliases: [h]
      NGTele:
        description: whatever
        aliases: [tp]
      NGVersion:
        description: Shows the Version of NG
        aliases: [ng]
     
  2. Offline

    Courier

    Edit: The reason yours isn't working is a brace mismatch. Count your braces, you will see that your if statements do not end where you expected them to.

    You should return after you've processed the command though, so you don't need to check for others.
    Either like this:
    Code:java
    1. if(name.equalsIgnoreCase("command1"))
    2. {
    3. //command1
    4. return;
    5. }
    6. if(name.equalsIgnoreCase("command2"))
    7. {
    8. //command2
    9. return;
    10. }
    11. ...

    Or like this
    Code:java
    1. if(name.equalsIgnoreCase("command1"))
    2. {
    3. //command1
    4. }
    5. else if(name.equalsIgnoreCase("command2"))
    6. {
    7. //command2
    8. }
    9. ...

    If you had a LOT of commands and are very concerned about efficiency, you could do something more complicated with a HashMap, or if you are using Java 7 you can use a switch statement:
    Code:java
    1. //you cannot switch strings before Java 7
    2. switch(name)
    3. {
    4. case "command1":
    5. //command1
    6. break;
    7. case "command2":
    8. //command2
    9. break;
    10. ...
    11. default: return false;
    12. }
    If you plan on distributing your plugin to others, it is probably best not to use Java 7, as many servers only have a JRE for Java 6, or even 5.
     
  3. Offline

    ARNGCollins

    Alright i got Version to Work and healing but there's a small bug in healing. When i use the heal command on another player it TP's me to that player. And when i try to add the "return;" it doesn't work.
    Here's the code i have right now.

    Code:
    package me.ARNGCollins.NationalGuard;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class NationalGuard extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static NationalGuard plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("NGVersion")){
                player.sendMessage(ChatColor.GOLD + "Version 1");
        }
            if(cmd.getName().equalsIgnoreCase("NGHeal")){
                if(args.length == 0){
                    //heal = 0 args1 /heal PLAYER = 1 args1
                    player.setHealth(20);
                    player.setFireTicks(0);
                    player.setFoodLevel(20);
                    player.sendMessage(ChatColor.AQUA + "You've been healed");
            }else if(args.length == 1){
                    if(player.getServer().getPlayer(args[0]) !=null){
                    Player targetPlayer1 = player.getServer().getPlayer(args [0]);
                    targetPlayer1.setHealth(20);
                    targetPlayer1.setFireTicks(0);
                    targetPlayer1.setFoodLevel(20);
                    player.sendMessage(ChatColor.AQUA + "You've been healed");
            }else{
                    player.sendMessage(ChatColor.RED + "Player Not Online");
        }
            if(cmd.getName().equalsIgnoreCase("NGTele")){
                if(args.length == 0)
                    player.sendMessage(ChatColor.GREEN + "Too little agruments");
            }else if(args.length == 1){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Location targetPlayerLocation = targetPlayer.getLocation();
                player.teleport(targetPlayerLocation);
            }else if(args.length == 2){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                Location targetPlayer1Location = targetPlayer1.getLocation();
                targetPlayer.teleport(targetPlayer1Location);
                targetPlayer.sendMessage("You've been teleported to " + player.getDisplayName());
          }   
        }
    }
            return false;
    }
    }
     
  4. Offline

    azyhd

    I think your targetplayer isnt good, try using:
    Player tplayer = getServer().getPlayer(args[0];

    And your "You've been healed" is wrong use:
    tplayer.sendMessage(ChatColor.AQUA + "You've been healed by" + player.getName());
     
  5. Offline

    Courier

    Your problem is still the braces. You need to move the two closing braces which are before "return false;" to just before "if(cmd.getName().equalsIgnore("NGTele"))". Your code should look like this:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd,
    2. String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4. if (cmd.getName().equalsIgnoreCase("NGVersion")) {
    5. player.sendMessage(ChatColor.GOLD + "Version 1");
    6. return true;
    7. }
    8. if (cmd.getName().equalsIgnoreCase("NGHeal")) {
    9. if (args.length == 0) {
    10. // heal = 0 args1 /heal PLAYER = 1 args1
    11. player.setHealth(20);
    12. player.setFireTicks(0);
    13. player.setFoodLevel(20);
    14. player.sendMessage(ChatColor.AQUA + "You've been healed");
    15. } else if (args.length == 1) {
    16. if (player.getServer().getPlayer(args[0]) != null) {
    17. Player targetPlayer1 = player.getServer()
    18. .getPlayer(args[0]);
    19. targetPlayer1.setHealth(20);
    20. targetPlayer1.setFireTicks(0);
    21. targetPlayer1.setFoodLevel(20);
    22. player.sendMessage(ChatColor.AQUA + "You've been healed");
    23. } else {
    24. player.sendMessage(ChatColor.RED + "Player Not Online");
    25. }
    26. }
    27. return true;
    28. }
    29. if (cmd.getName().equalsIgnoreCase("NGTele")) {
    30. if (args.length == 0)
    31. player.sendMessage(ChatColor.GREEN
    32. + "Too little agruments");
    33. } else if (args.length == 1) {
    34. Player targetPlayer = player.getServer().getPlayer(args[0]);
    35. Location targetPlayerLocation = targetPlayer.getLocation();
    36. player.teleport(targetPlayerLocation);
    37. } else if (args.length == 2) {
    38. Player targetPlayer = player.getServer().getPlayer(args[0]);
    39. Player targetPlayer1 = player.getServer()
    40. .getPlayer(args[1]);
    41. Location targetPlayer1Location = targetPlayer1
    42. .getLocation();
    43. targetPlayer.teleport(targetPlayer1Location);
    44. targetPlayer.sendMessage("You've been teleported to "
    45. + player.getDisplayName());
    46. return true;
    47. }
    48. return false;
    49. }

    The reason you didn't see it is that your indentation does not match your braces. In Eclipse press Ctrl+F or select your code and press Ctrl+I to automatically format your indentations according to your braces. That will make the problem apparent.
     
  6. Offline

    ARNGCollins

    Thank you for the fix for targetplayer.sendMessage. But, I still ran into a problem when i heal the targeted player it also Tele's me to them. Any idea how to fix that?
     
  7. Offline

    azyhd

    Maybe replace "cmd.getName().equalsIgnoreCase("NGheal");" to "commandLabel.equalsIgnoreCase("NGheal");
     
  8. Offline

    ARNGCollins

    After i did that i started up the server and now i cant heal other players.

    EDIT: Alright i fixed my Teleport bug when i heal the targeted player but my NGTele command (/tp) still doesnt work

    Alright i fixed my Teleport bug when i heal the targeted player but my NGTele command (/tp) still doesnt work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    Courier

    Does healing still not work?
    At first glance, the teleport code looks fine. You have to use /ngtele not /tp
     
  10. Offline

    azyhd

    if(args.length== 1){
    player.performCommand("tp " + player.getName() + args[0]);
    } else if(agrs.length == 2){
    player.performCommand("tp " + args[0] + args[1]);
    }

    only thing i can come with right now
     
  11. Offline

    ARNGCollins

    I fixed healing that works just fine now. And i have aliases set for /tp but ngtele doesnt work either.
     
  12. Offline

    Courier

    I found the problem.
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("NGTele")) {
    2. if (args.length == 0)
    3. player.sendMessage(ChatColor.GREEN
    4. + "Too little agruments");
    5. } else if (args.length == 1) ...

    You need to have
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("NGTele")) {
    2. if (args.length == 0)
    3. {
    4. player.sendMessage(ChatColor.GREEN
    5. + "Too little agruments");
    6. } else if (args.length == 1) {
    7. Player targetPlayer = player.getServer().getPlayer(args[0]);
    8. Location targetPlayerLocation = targetPlayer.getLocation();
    9. player.teleport(targetPlayerLocation);
    10. } else if (args.length == 2) {
    11. Player targetPlayer = player.getServer().getPlayer(args[0]);
    12. Player targetPlayer1 = player.getServer()
    13. .getPlayer(args[1]);
    14. Location targetPlayer1Location = targetPlayer1
    15. .getLocation();
    16. targetPlayer.teleport(targetPlayer1Location);
    17. targetPlayer.sendMessage("You've been teleported to "
    18. + player.getDisplayName());
    19. }
    20. return true;
    21. }
     
  13. Offline

    zeeveener

    He will probably want an else at the end incase there are too many arguments. That way he doesn't run into any errors, but that's what I would do as well.
     
  14. Offline

    Courier

    He won't have any errors, nothing will happen. It returns true so the command will have no feedback (which is still bad, but not broken).
     
  15. Offline

    ARNGCollins

    The Teleport command works now but it doesn't show the message.
    Here's the current code i have for Teleport

    Code:
            if(cmd.getName().equalsIgnoreCase("NGTele")){
                if(args.length == 0)
                {
                    player.sendMessage(ChatColor.GREEN + "Too little agruments");
            }else if(args.length == 1){
                if(player.getServer().getPlayer(args[0]) !=null){
                    Player targetPlayer = player.getServer().getPlayer(args[0]);
                    Location targetPlayerLocation = targetPlayer.getLocation();
                    player.teleport(targetPlayerLocation);
                }else if(args.length == 2){
                    if(player.getServer().getPlayer(args[0]) !=null){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                        Location targetPlayer1Location = targetPlayer1.getLocation();
                        targetPlayer.teleport(targetPlayer1Location);
                        targetPlayer.sendMessage("You've been teleported to " + player.getDisplayName());   
                    }else{
                        player.sendMessage(ChatColor.RED + "Player Not Online");
                    }
                    return true;
                }
            }
        }
     
  16. Offline

    Courier

    Well there is no message for the 1 argument code, since it only teleports the sender. The other ones aren't showing anybody any messages at all?
     
  17. Offline

    zeeveener

    I didn't mean to degrade what you posted. I just meant that isn't it better coding to have an else statement to close the method?
     
  18. Offline

    ARNGCollins

    The "Too Little Agruments" message shows up for all users. But The "Player Not Online" or "You've been ..." Message doesn't work
     
  19. Offline

    Courier

    I know :d

    Usually for this sort of thing it's best to use a switch statement:
    Code:java
    1. switch(args.length){
    2. case 0: //do stuff
    3. return true;
    4. case 1: //do stuff
    5. return true;
    6. case 2: //do stuff
    7. default: return false;
    8. }
     
  20. Offline

    zeeveener

    Code:java
    1. }else if(args.length == 1){
    2. if(player.getServer().getPlayer(args[0]) !=null){
    3. Player targetPlayer = player.getServer().getPlayer(args[0]);
    4. Location targetPlayerLocation = targetPlayer.getLocation();
    5. player.teleport(targetPlayerLocation);
    6. }else if(args.length == 2){
    7. if(player.getServer().getPlayer(args[0]) !=null){
    8. Player targetPlayer = player.getServer().getPlayer(args[0]);
    9. Player targetPlayer1 = player.getServer().getPlayer(args[1]);
    10. Location targetPlayer1Location = targetPlayer1.getLocation();
    11. targetPlayer.teleport(targetPlayer1Location);
    12. targetPlayer.sendMessage("You've been teleported to " + player.getDisplayName());
    13. }else{
    14. player.sendMessage(ChatColor.RED + "Player Not Online");
    15. }
    16. return true;
    17. }
    18. }


    This code has the test for if there are 2 arguments within the test for if there is only 1 argument. It isn't sending the message, because there will only ever be one argument inside that if block.

    Move it outside and you might see some differences.
     
  21. Offline

    Courier

    Yet again, a problem with braces. Select the code in Eclipse and press Ctrl+I so you can spot the problem.
     
  22. Offline

    ARNGCollins

    Did that and it still doesn't show the messages


    Not quite sure what you mean to do. Sorry still trying to learn this stuff

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  23. Offline

    zeeveener

    Ok let me show you.
    Change this code:
    Code:java
    1. }else if(args.length == 1){
    2. if(player.getServer().getPlayer(args[0]) !=null){
    3. Player targetPlayer = player.getServer().getPlayer(args[0]);
    4. Location targetPlayerLocation = targetPlayer.getLocation();
    5. player.teleport(targetPlayerLocation);
    6. }else if(args.length == 2){
    7. if(player.getServer().getPlayer(args[0]) !=null){
    8. Player targetPlayer = player.getServer().getPlayer(args[0]);
    9. Player targetPlayer1 = player.getServer().getPlayer(args[1]);
    10. Location targetPlayer1Location = targetPlayer1.getLocation();
    11. targetPlayer.teleport(targetPlayer1Location);
    12. targetPlayer.sendMessage("You've been teleported to " + player.getDisplayName());
    13. }else{
    14. player.sendMessage(ChatColor.RED + "Player Not Online");
    15. }
    16. return true;
    17. }
    18. }

    To this code:
    Code:java
    1. }else if(args.length == 1){
    2. if(player.getServer().getPlayer(args[0]) !=null){
    3. Player targetPlayer = player.getServer().getPlayer(args[0]);
    4. Location targetPlayerLocation = targetPlayer.getLocation();
    5. player.teleport(targetPlayerLocation);
    6. }
    7. }else if(args.length == 2){
    8. if(player.getServer().getPlayer(args[0]) !=null){
    9. Player targetPlayer = player.getServer().getPlayer(args[0]);
    10. Player targetPlayer1 = player.getServer().getPlayer(args[1]);
    11. Location targetPlayer1Location = targetPlayer1.getLocation();
    12. targetPlayer.teleport(targetPlayer1Location);
    13. targetPlayer.sendMessage("You've been teleported to " + player.getDisplayName());
    14. }else{
    15. player.sendMessage(ChatColor.RED + "Player Not Online");
    16. }
    17. return true;
    18. }


    Like I said. Take the test for if there are 2 arguments and move it outside of the test for one argument.
     
Thread Status:
Not open for further replies.

Share This Page