Multiple Commands?

Discussion in 'Plugin Development' started by ProMCKingz, Jul 22, 2014.

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

    ProMCKingz

    Hey!
    I was just wondering why this won't work?
    IT seems simple, but i just can't work out why?
    Only /heal works, and gives the user in console. However none of the other code works
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("Heal")) {
    6. if (args.length == 0) {
    7. if (sender instanceof Player){
    8. player.setHealth(20);
    9. String someone = player.getName();
    10. player.sendMessage(ChatColor.RED + "[Shields+] "
    11. + ChatColor.GREEN + "You have been healed!");
    12. getLogger().warning(someone + " Healed Themselve!");
    13. return false;
    14. }
    15. if (args.length == 1) {
    16. player.sendMessage(ChatColor.RED + "[Shields+]"
    17. + ChatColor.DARK_RED
    18. + " /heal does not require any further args");
    19. return false;
    20. }
    21.  
    22. if (cmd.getName().equalsIgnoreCase("Feed")) {
    23. if (args.length == 0) {
    24. player.setFoodLevel(20);
    25. player.sendMessage(ChatColor.RED + "Shields+] "
    26. + ChatColor.GREEN + "You have been fed!");
    27. player.addPotionEffect(new PotionEffect(
    28. PotionEffectType.HEAL, Integer.MAX_VALUE, 0));
    29. return true;
    30. }
    31. }
    32. }
    33. {
    34. }
    35. }
    36. return true;
    37. }{
    38. }{
    39. }
    40. }
     
  2. Offline

    thecrystalflame

    you have your feed command inside your heal command
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("Heal")) {
    3. if (cmd.getName().equalsIgnoreCase("Feed")) { //this will never be true
    4.  
    5. }
    6. }
     
    Skyost likes this.
  3. Offline

    Gamesareme

    After your heal command add an 'else if' statement and add your feed command there.
     
  4. Offline

    jthort

    ProMCKingz Along with Gamesareme said, use if, else if, statements rather then multiple if statements it's more efficient and looks cleaner. Why have the program check for 3 arguments when you already decided that it has two arguments

    Another suggestion, if you are planning on outputting text to the user with the plugin tag in front of it, I would recommend creating a MessageFormatter class that contains a method that takes in the message and automatically formats it for you. That way your not constantly adding that tag every time you have to send the message.

    Along with my other to suggestions, I don't see why you are blindly casting the sender to a Player, then checking if it's an instance of a player later on. Your also going to want to add an else statement after you check the sender so that the Console knows if you can't use that command without being logged in.

    Although after reading what these guys said and it works, I would highly suggest to review your code and make it as efficient as possible, because after all that's what makes plugins get to the top of the list. There is more to programming then getting it to work.

    Best of luck coding :)
     
    ProMCKingz likes this.
  5. Offline

    ProMCKingz

    jthort
    Thanks for you help, and finding other mistakes!
    I am still new to coding and have found these forums very useful and productive.
    Thanks for your time! (editing 6 times) :D

    I have done the following, and everything else works, except the feed command.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("Heal")) {
    6. if (args.length == 0) {
    7. if (sender instanceof Player){
    8. player.setHealth(20);
    9. String someone = player.getName();
    10. player.sendMessage(ChatColor.RED + "[Shields+] "
    11. + ChatColor.GREEN + "You have been healed!");
    12. getLogger().warning(someone + " Healed Themselve!");
    13. }else{
    14. getLogger().warning("You cannot perform this command from console!");
    15. return false;
    16. }}
    17. else if (args.length == 1) {
    18. player.sendMessage(ChatColor.RED + "[Shields+]"
    19. + ChatColor.DARK_RED
    20. + " /heal does not require any further args");
    21. return false;
    22. }else{
    23.  
    24. if (cmd.getName().equalsIgnoreCase("Feed")) {
    25. if (args.length == 0) {
    26. player.setFoodLevel(20);
    27. player.sendMessage(ChatColor.RED + "[Shields+] "
    28. + ChatColor.GREEN + "You have been fed!");
    29. player.addPotionEffect(new PotionEffect(
    30. PotionEffectType.HEAL, Integer.MAX_VALUE, 0));
    31. return true;
    32. }
    33. }
    34. }
    35. {
    36. }{
    37. }}
    38. return false;}}


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    jthort likes this.
  6. Offline

    Dragonphase

    ProMCKingz

    You're still checking if the command is "Feed" within the scope of the "Heal" conditional check. Line 24 will always be false.

    As well as this, what's with the bracket spam on lines 35-37?
     
  7. Offline

    AdamQpzm

    Low quality ASCII art?
     
    Dragonphase likes this.
  8. Offline

    ProMCKingz

  9. Offline

    Dragonphase

    ProMCKingz

    Check for the Feed command outside of the check for the Heal command.
     
  10. Offline

    ProMCKingz

  11. Offline

    AdamQpzm

  12. Offline

    DannyDog

    Essentially with if statements you always need to close them off if you want to have another if that's not nested.
    So like:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("Heal")) {
    2. //heal stuff
    3. }
    4. if (cmd.getName().equalsIgnoreCase("Feed")) {
    5. //feed stuff
    6. }

    Simple java syntax stuff that you should learn.
     
  13. Offline

    ProMCKingz

    So this?
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("Heal")) {
    6. if (args.length == 0) {
    7. if (sender instanceof Player){
    8. player.setHealth(20);
    9. String someone = player.getName();
    10. player.sendMessage(ChatColor.RED + "[Shields+] "
    11. + ChatColor.GREEN + "You have been healed!");
    12. getLogger().warning(someone + " Healed Themselve!");
    13. }else{
    14. getLogger().warning("You cannot perform this command from console!");
    15.  
    16. }}
    17. else if (args.length == 1) {
    18. player.sendMessage(ChatColor.RED + "[Shields+]"
    19. + ChatColor.DARK_RED
    20. + " /heal does not require any further args");
    21.  
    22. }
    23.  
    24. if (cmd.getName().equalsIgnoreCase("Feed")) {
    25. if (args.length == 0) {
    26. player.setFoodLevel(20);
    27. player.sendMessage(ChatColor.RED + "[Shields+] "
    28. + ChatColor.GREEN + "You have been fed!");
    29. player.addPotionEffect(new PotionEffect(
    30. PotionEffectType.HEAL, Integer.MAX_VALUE, 0));
    31. return true;
    32. }
    33. }
    34. }
    35. return false;
    36. }}
    37.  
     
  14. Offline

    Dragonphase

    ProMCKingz

    No... your Feed check is still inside the Heal check... Please read AdamQpzm 's post on Java Syntax.
     
  15. Offline

    ProMCKingz

    Dragonphase
    I have read it.
    However I still don't see what I am making an error on.
    Is it the "}" or what?
    Please help!
    I know this is basic, however i'm just confused!
     
  16. Offline

    AdamQpzm

    ProMCKingz The reason you're confused is because you don't understand the basics of Java: You need to learn these basics - which takes time, effort, and patience - otherwise you will encounter problems all the time and have no idea how to fix them. For this problem, I'll tell you that the check for feed needs to come after the check for heal is closed, and statements are opened and closed with { and }...

    Either way, you'll still need to learn Java before playing around with Bukkit plugins.
     
  17. Offline

    Dragonphase

    ProMCKingz

    This is what your code looks like formatted:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("Heal")) {
    6. if (args.length == 0) {
    7. if (sender instanceof Player){
    8. player.setHealth(20);
    9. String someone = player.getName();
    10. player.sendMessage(ChatColor.RED + "[Shields+] "
    11. + ChatColor.GREEN + "You have been healed!");
    12. getLogger().warning(someone + " Healed Themselve!");
    13. }else{
    14. getLogger().warning("You cannot perform this command from console!");
    15.  
    16. }
    17. }
    18. else if (args.length == 1) {
    19. player.sendMessage(ChatColor.RED + "[Shields+]"
    20. + ChatColor.DARK_RED
    21. + " /heal does not require any further args");
    22.  
    23. }
    24.  
    25. if (cmd.getName().equalsIgnoreCase("Feed")) {
    26. if (args.length == 0) {
    27. player.setFoodLevel(20);
    28. player.sendMessage(ChatColor.RED + "[Shields+] "
    29. + ChatColor.GREEN + "You have been fed!");
    30. player.addPotionEffect(new PotionEffect(
    31. PotionEffectType.HEAL, Integer.MAX_VALUE, 0));
    32. return true;
    33. }
    34. }
    35. }
    36. return false;
    37. }
    38. }


    Your conditional statement for the Feed command is still within the conditional statement for the Heal command. Cut the entire section out for the Feed command, and paste it outside the next bracket. You also have too many closing brackets, so remove the bracket from the last line.
     
Thread Status:
Not open for further replies.

Share This Page