Only certain commands are working

Discussion in 'Plugin Development' started by Monkey_Swag, Jan 1, 2014.

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

    Monkey_Swag

    Hello everyone, so I was making a simple potions command where you type (for example) /strength, and it will give you OR someone else strength, poison, night vision, etc. However, only two of my commands are working. /speed and /slowness. Some friends told me that I was missing some closing brackets, but every time I tried adding brackets I would get loads and loads of errors. If someone could help me it would be great, thanks.

    Code:
    Code:java
    1. package me.Monkey_Swag.Potions;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.potion.PotionEffect;
    9. import org.bukkit.potion.PotionEffectType;
    10.  
    11. public class Potions extends JavaPlugin{
    12.  
    13. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    14. Player player = (Player) sender;
    15.  
    16. // speed, slowness, haste, strength, instant_health, Instant_damage, Jump_Boost,
    17. //Nausea, regeneration, resistance, fire_resistance, water_Breathing, Invisibility, Blindness,
    18. //Night_Vision, Hunger, Weakness, Poison, Wither, Health_Boost, Absorption, Saturation
    19.  
    20.  
    21. //SPEED
    22. if(cmd.getName().equalsIgnoreCase("speed")) {
    23. if(args.length == 0){
    24. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SPEED" + ChatColor.GREEN + " buff!");
    25. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 1));
    26. }else if(args.length == 1){
    27. Player targetPlayer = player.getServer().getPlayer(args[0]);
    28. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SPEED" + ChatColor.GREEN + " buff!");
    29. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 1));
    30. }
    31. //SLOWNESS
    32. }else if(cmd.getName().equalsIgnoreCase("slowness"))
    33. if(args.length == 0) {
    34. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    35. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    36. }else if(args.length == 1){
    37. Player targetPlayer = player.getServer().getPlayer(args[0]);
    38. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    39. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    40.  
    41. //HASTE
    42. }else if(cmd.getName().equalsIgnoreCase("haste"))
    43. if(args.length == 0) {
    44. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "HASTE" + ChatColor.GREEN + " buff!");
    45. player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 500, 1));
    46. }else if(args.length == 1){
    47. Player targetPlayer = player.getServer().getPlayer(args[0]);
    48. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "HASTE" + ChatColor.GREEN + " buff!");
    49. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 500, 1));
    50.  
    51. //STRENGTH
    52. }else if(cmd.getName().equalsIgnoreCase("strength"))
    53. if(args.length == 0) {
    54. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "STRENGTH" + ChatColor.GREEN + " buff!");
    55. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 500, 1));
    56. }else if(args.length == 1){
    57. Player targetPlayer = player.getServer().getPlayer(args[0]);
    58. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "STRENGTH" + ChatColor.GREEN + " buff!");
    59. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 500, 1));
    60.  
    61. //INSTANT HEALTH
    62. }else if(cmd.getName().equalsIgnoreCase("health"))
    63. if(args.length == 0) {
    64. player.sendMessage(ChatColor.GREEN + "You now have an " + ChatColor.WHITE + "INSTANT HEALTH" + ChatColor.GREEN + " buff!");
    65. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 500, 1));
    66. }else if(args.length == 1){
    67. Player targetPlayer = player.getServer().getPlayer(args[0]);
    68. targetPlayer.sendMessage(ChatColor.GREEN + "You now have an " + ChatColor.WHITE + "INSTANT HEALTH" + ChatColor.GREEN + " buff!");
    69. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 500, 1));
    70.  
    71. //INSTANT DAMAGE
    72. }else if(cmd.getName().equalsIgnoreCase("damage"))
    73. if(args.length == 0) {
    74. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "DAMAGE" + ChatColor.GREEN + " debuff!");
    75. player.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 500, 1));
    76. }else if(args.length == 1){
    77. Player targetPlayer = player.getServer().getPlayer(args[0]);
    78. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "DAMAGE" + ChatColor.GREEN + " debuff!");
    79. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 500, 1));
    80.  
    81. //JUMP BOOST
    82. }else if(cmd.getName().equalsIgnoreCase("jump"))
    83. if(args.length == 0) {
    84. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "JUMP BOOST" + ChatColor.GREEN + " buff!");
    85. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 500, 1));
    86. }else if(args.length == 1){
    87. Player targetPlayer = player.getServer().getPlayer(args[0]);
    88. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "JUMP BOOST" + ChatColor.GREEN + " buff!");
    89. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 500, 1));
    90.  
    91. //NAUSEA
    92. }else if(cmd.getName().equalsIgnoreCase("nausea"))
    93. if(args.length == 0) {
    94. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "NAUSEA" + ChatColor.GREEN + " debuff!");
    95. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 500, 1));
    96. }else if(args.length == 1){
    97. Player targetPlayer = player.getServer().getPlayer(args[0]);
    98. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "NAUSEA" + ChatColor.GREEN + " debuff!");
    99. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 500, 1));
    100.  
    101. //REGENERATION
    102. }else if(cmd.getName().equalsIgnoreCase("regeneration") || cmd.getName().equalsIgnoreCase("regen"))
    103. if(args.length == 0) {
    104. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "REGENERATION" + ChatColor.GREEN + " buff!");
    105. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 500, 1));
    106. }else if(args.length == 1){
    107. Player targetPlayer = player.getServer().getPlayer(args[0]);
    108. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "REGENERATION" + ChatColor.GREEN + " buff!");
    109. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 500, 1));
    110.  
    111.  
    112. //RESISTANCE
    113. }else if(cmd.getName().equalsIgnoreCase("resistance"))
    114. if(args.length == 0) {
    115. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "RESISTANCE" + ChatColor.GREEN + " debuff!");
    116. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 500, 1));
    117. }else if(args.length == 1){
    118. Player targetPlayer = player.getServer().getPlayer(args[0]);
    119. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "RESISTANCE" + ChatColor.GREEN + " buff!");
    120. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 500, 1));
    121. }
    122.  
    123.  
    124. return false;
    125. }
    126. }
    127.  


    plugin.yml:
    Code:
    name: Potions
    main: me.Monkey_Swag.Potions.Potions
    version: 1.0
    description: >
                Add potion effects to yourself or to other players!
    commands:
        speed:
          description: give yourself or someone else a speed boost!
        slowness:
          description: give yourself or someone else a slowness debuff!
        haste:
          description: give yourself or someone else a haste buff!
        strength:
          description: give yourself or someone else a strength buff!
        health:
          description: give yourself or someone else am instant health buff!
        damage:
          description: give yourself or someone else a damage debuff!
        jump:
          description: give yourself or someone else a jump buff!
        nausea:
          description: give yourself or someone else a nausea debuff!
        regeneration:
          description: give yourself or someone else a regeneration buff!
        regen:
          description: give yourself or someone else a regeneration buff!
        resistance:
          description: give yourself or someone else a resistance buff!      
     
  2. Offline

    Ronbo

    Dude... You have to tab your if/elses correctly or things will get messed up very quickly. For example, you need another } just above //STRENGTH because you aren't closing the Haste command checking part.
     
  3. Offline

    Wizehh

    Code:java
    1. }else if(cmd.getName().equalsIgnoreCase("slowness"))

    Should be:
    Code:java
    1. }else if(cmd.getName().equalsIgnoreCase("slowness")) {

    You can figure out the rest yourself, I hope.

    Make sure you're properly indenting your code, otherwise things like this will happen -.-
     
  4. Offline

    ISHLCMinecraft

    first of all, in the slowness command you check the arguments, and then "else if(...)"
    and easy way to debug the problems with the brackets if to hit CTRL + SHIFT + F
    it will orgnize your code and the problems will jump to your eyes.

    Wizehh
    it dousn't have to be with the brackets at the end becouse there is only one line after it.
    java counts "if" blocks as 1 line.
     
  5. Offline

    BillyGalbreath

    Organize your formatting. Indentations arent there just to be pretty. They let us developers see structure in the code.

    If you had organized it you would have seen you missed a LOT of { } brackets.

    Code:java
    1.  
    2. package me.Monkey_Swag.Potions;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.potion.PotionEffect;
    10. import org.bukkit.potion.PotionEffectType;
    11.  
    12. public class Potions extends JavaPlugin {
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    15. Player player = (Player) sender;
    16.  
    17. // speed, slowness, haste, strength, instant_health, Instant_damage, Jump_Boost,
    18. // Nausea, regeneration, resistance, fire_resistance, water_Breathing, Invisibility, Blindness,
    19. // Night_Vision, Hunger, Weakness, Poison, Wither, Health_Boost, Absorption, Saturation
    20.  
    21. // SPEED
    22. if (cmd.getName().equalsIgnoreCase("speed")) {
    23. if (args.length == 0) {
    24. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SPEED" + ChatColor.GREEN + " buff!");
    25. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 1));
    26. } else if (args.length == 1) {
    27. Player targetPlayer = player.getServer().getPlayer(args[0]);
    28. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SPEED" + ChatColor.GREEN + " buff!");
    29. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 1));
    30. }
    31.  
    32. // SLOWNESS
    33. } else if (cmd.getName().equalsIgnoreCase("slowness")) {
    34. if (args.length == 0) {
    35. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    36. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    37. } else if (args.length == 1) {
    38. Player targetPlayer = player.getServer().getPlayer(args[0]);
    39. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    40. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    41. }
    42.  
    43. // HASTE
    44. } else if (cmd.getName().equalsIgnoreCase("haste")) {
    45. if (args.length == 0) {
    46. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "HASTE" + ChatColor.GREEN + " buff!");
    47. player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 500, 1));
    48. } else if (args.length == 1) {
    49. Player targetPlayer = player.getServer().getPlayer(args[0]);
    50. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "HASTE" + ChatColor.GREEN + " buff!");
    51. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 500, 1));
    52. }
    53.  
    54. // STRENGTH
    55. } else if (cmd.getName().equalsIgnoreCase("strength")) {
    56. if (args.length == 0) {
    57. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "STRENGTH" + ChatColor.GREEN + " buff!");
    58. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 500, 1));
    59. } else if (args.length == 1) {
    60. Player targetPlayer = player.getServer().getPlayer(args[0]);
    61. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "STRENGTH" + ChatColor.GREEN + " buff!");
    62. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 500, 1));
    63. }
    64.  
    65. // INSTANT HEALTH
    66. } else if (cmd.getName().equalsIgnoreCase("health")) {
    67. if (args.length == 0) {
    68. player.sendMessage(ChatColor.GREEN + "You now have an " + ChatColor.WHITE + "INSTANT HEALTH" + ChatColor.GREEN + " buff!");
    69. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 500, 1));
    70. } else if (args.length == 1) {
    71. Player targetPlayer = player.getServer().getPlayer(args[0]);
    72. targetPlayer.sendMessage(ChatColor.GREEN + "You now have an " + ChatColor.WHITE + "INSTANT HEALTH" + ChatColor.GREEN + " buff!");
    73. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 500, 1));
    74. }
    75.  
    76. // INSTANT DAMAGE
    77. } else if (cmd.getName().equalsIgnoreCase("damage")) {
    78. if (args.length == 0) {
    79. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "DAMAGE" + ChatColor.GREEN + " debuff!");
    80. player.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 500, 1));
    81. } else if (args.length == 1) {
    82. Player targetPlayer = player.getServer().getPlayer(args[0]);
    83. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "DAMAGE" + ChatColor.GREEN + " debuff!");
    84. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 500, 1));
    85. }
    86.  
    87. // JUMP BOOST
    88. } else if (cmd.getName().equalsIgnoreCase("jump")) {
    89. if (args.length == 0) {
    90. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "JUMP BOOST" + ChatColor.GREEN + " buff!");
    91. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 500, 1));
    92. } else if (args.length == 1) {
    93. Player targetPlayer = player.getServer().getPlayer(args[0]);
    94. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "JUMP BOOST" + ChatColor.GREEN + " buff!");
    95. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 500, 1));
    96. }
    97.  
    98. // NAUSEA
    99. } else if (cmd.getName().equalsIgnoreCase("nausea")) {
    100. if (args.length == 0) {
    101. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "NAUSEA" + ChatColor.GREEN + " debuff!");
    102. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 500, 1));
    103. } else if (args.length == 1) {
    104. Player targetPlayer = player.getServer().getPlayer(args[0]);
    105. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "NAUSEA" + ChatColor.GREEN + " debuff!");
    106. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 500, 1));
    107. }
    108.  
    109. // REGENERATION
    110. } else if (cmd.getName().equalsIgnoreCase("regeneration") || cmd.getName().equalsIgnoreCase("regen")) {
    111. if (args.length == 0) {
    112. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "REGENERATION" + ChatColor.GREEN + " buff!");
    113. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 500, 1));
    114. } else if (args.length == 1) {
    115. Player targetPlayer = player.getServer().getPlayer(args[0]);
    116. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "REGENERATION" + ChatColor.GREEN + " buff!");
    117. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 500, 1));
    118. }
    119.  
    120. // RESISTANCE
    121. } else if (cmd.getName().equalsIgnoreCase("resistance")) {
    122. if (args.length == 0) {
    123. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "RESISTANCE" + ChatColor.GREEN + " debuff!");
    124. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 500, 1));
    125. } else if (args.length == 1) {
    126. Player targetPlayer = player.getServer().getPlayer(args[0]);
    127. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "RESISTANCE" + ChatColor.GREEN + " buff!");
    128. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 500, 1));
    129. }
    130. }
    131.  
    132. return false;
    133. }
    134. }
    135.  
     
  6. Offline

    Wizehh

    Isn't it CTRL + SHIFT + F?
     
  7. Offline

    ISHLCMinecraft

    you right! my fault :\
     
  8. Offline

    BillyGalbreath

    It is CTRL+SHIFT+F. But it will not auto-organize this code because of all the missing brackets.
     
  9. Offline

    Wizehh

    ?
     
  10. Offline

    BillyGalbreath

    What?

    Code:java
    1.  
    2. // SLOWNESS
    3. } else if (cmd.getName().equalsIgnoreCase("slowness")) {
    4. if (args.length == 0) {
    5. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    6. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    7. } else if (args.length == 1) {
    8. Player targetPlayer = player.getServer().getPlayer(args[0]);
    9. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    10. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    11. }
    12.  
    13. // HASTE
    14. } else if (cmd.getName().equalsIgnoreCase("haste")) {
    15.  


    is NOT the same as

    Code:java
    1.  
    2. // SLOWNESS
    3. } else if (cmd.getName().equalsIgnoreCase("slowness"))
    4. if (args.length == 0) {
    5. player.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    6. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    7. } else if (args.length == 1) {
    8. Player targetPlayer = player.getServer().getPlayer(args[0]);
    9. targetPlayer.sendMessage(ChatColor.GREEN + "You now have a " + ChatColor.WHITE + "SLOWNESS" + ChatColor.GREEN + " debuff!");
    10. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 500, 1));
    11.  
    12. // HASTE
    13. } else if (cmd.getName().equalsIgnoreCase("haste")) {
    14.  


    Here's the problem:

    Code:java
    1.  
    2. } else if (cmd.getName().equalsIgnoreCase("slowness"))
    3.  

    Closing bracket doesnt belong to the correct if/else statement. It belongs to the nested if/else. Thats why trying to auto-format this code fails. The syntax is correct, but the logic is a fail.

    Every if/else statement other than the first one is open-ended, each nesting further into itself.

    Use proper brackets, even for 1-liners like this (since they are pretty big 1-liners) just to make life easier to spot these kinds of errors.
     
  11. Offline

    Monkey_Swag

Thread Status:
Not open for further replies.

Share This Page