Solved A few commands are not working in my Plugin!

Discussion in 'Plugin Development' started by FTC_WTFire7, Aug 18, 2013.

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

    FTC_WTFire7

    Hello. I am making a plugin. It has the following Commands so far:

    /heal
    /feed
    /survival
    /creative
    /time [day,night]

    Now, /Survival is not working,
    and /Time isn't working at all. I am not a professional in java. But here is my main class (have no others)

    Code:java
    1. package me.chasemostat.legendarycommands;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.World;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Legendarycommands extends JavaPlugin {
    18. public static Legendarycommands plugin;
    19. public final Logger logger = Logger.getLogger("Minecraft");;
    20.  
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " is now disabled!");
    24. }
    25.  
    26. public void onEnable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " is now enabled!");
    29. }
    30. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    31. Player player = (Player) sender;
    32. World world = player.getWorld();
    33. if (sender.hasPermission("LC.heal"))
    34. if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
    35. if(args.length == 0){
    36. //heal = 0 args /heal Player = 1 args
    37. player.setHealth(20);
    38. player.sendMessage(ChatColor.GREEN + "Healed");
    39. }else if(args.length == 1){
    40. Player targetPlayer = player.getServer().getPlayer(args [0]);
    41. targetPlayer.setHealth(20);
    42. player.sendMessage(ChatColor.GREEN + "Healed");
    43. return false;
    44. }
    45. }else if(commandLabel.equalsIgnoreCase("feed")){
    46. if (sender.hasPermission("LC.feed"))
    47. if(args.length == 0){
    48. //feed = 0 args /feed Player = 1 args
    49. player.setFoodLevel(20);
    50. player.sendMessage(ChatColor.GOLD + "Your hunger has been filled!");
    51. }else if(args.length == 1){
    52. Player targetPlayer = player.getServer().getPlayer(args [0]);
    53. targetPlayer.setFoodLevel(20);
    54. player.sendMessage(ChatColor.GOLD + "Player has been fed!");
    55. return false;
    56. }
    57. }else if(commandLabel.equalsIgnoreCase("creative")){
    58. if (sender.hasPermission("LC.gamemode"))
    59. if(args.length == 0){
    60. //creative = 0 args /creative Player = 1 args
    61. player.setGameMode(GameMode.CREATIVE);
    62. player.sendMessage("ChatColor.GOLD + Gamemode set to Creative");
    63. if(args.length == 1){
    64. player.sendMessage("ChatColor.RED + Sorry, you cannot change other people gamemode yet!");
    65. return false;
    66. }
    67. }else if(commandLabel.equalsIgnoreCase("survival")){
    68. if (sender.hasPermission("LC.survival"))
    69. if(args.length == 0){
    70. player.setGameMode(GameMode.SURVIVAL);
    71. player.sendMessage("ChatColor.GOLD + Gamemode set to Survival");
    72. if(args.length == 1){
    73. Player targetPlayer = player.getServer().getPlayer(args [0]);
    74. targetPlayer.setGameMode(GameMode.SURVIVAL);
    75. return false;
    76. }
    77. }else if(commandLabel.equalsIgnoreCase("time")){
    78. if(args[0].equalsIgnoreCase("day")){
    79. world.setTime(0);
    80. Bukkit.broadcastMessage("Time set to Day!");
    81. }else if(args[0].equalsIgnoreCase("night")){
    82. world.setTime(14000);
    83. Bukkit.broadcastMessage("Time set to Night!");
    84. }else if(args[0].equalsIgnoreCase("morning")){
    85. world.setTime(1000);
    86. Bukkit.broadcastMessage("Time set to Morning");
    87. return false;
    88. }
    89.  
    90. }
    91. }
    92.  
    93. }
    94. return false;
    95.  
    96. } {
    97.  
    98.  
    99. }
    100. }


    Please Help, If you can. Thanks!
     
  2. Offline

    Polaris29

    FTC_WTFire7 Bad indentation/curly brackets. Your time and survival command else ifs are inside the creative else if body
     
  3. Offline

    FTC_WTFire7

    What do you mean by Bad indentation?

    And how many brackets should be there after return false;?
    Polaris29
     
  4. Offline

    Polaris29

    That means nothing

    Basically, this:
    Code:java
    1.  
    2. }else if(commandLabel.equalsIgnoreCase("survival")){
    3. if (sender.hasPermission("LC.survival"))
    4. if(args.length == 0){
    5. player.setGameMode(GameMode.SURVIVAL);
    6. player.sendMessage("ChatColor.GOLD + Gamemode set to Survival");
    7. if(args.length == 1){
    8. Player targetPlayer = player.getServer().getPlayer(args [0]);
    9. targetPlayer.setGameMode(GameMode.SURVIVAL);
    10. return false;
    11. }
    12. }else if(commandLabel.equalsIgnoreCase("time")){
    13. if(args[0].equalsIgnoreCase("day")){
    14. world.setTime(0);
    15. Bukkit.broadcastMessage("Time set to Day!");
    16. }else if(args[0].equalsIgnoreCase("night")){
    17. world.setTime(14000);
    18. Bukkit.broadcastMessage("Time set to Night!");
    19. }else if(args[0].equalsIgnoreCase("morning")){
    20. world.setTime(1000);
    21. Bukkit.broadcastMessage("Time set to Morning");
    22. return false;
    23. }
    24.  
    25. }
    26. }
    27.  


    Is inside this:

    Code:java
    1.  
    2. } else if(commandLabel.equalsIgnoreCase("creative")){
    3. }
    4.  


    Your time and survival commands are not working because of the way your code is right now, the command label has to both "survival" and "creative" for the survival command to work, and the command label has to be both "time", "survival" and "creative" for the time command to work
     
  5. Offline

    FTC_WTFire7

    Sorry, I do not understand (noob in java)

    " the command label has to be both "time" and "creative" for the time command to work, " What do you mean by both?
     
  6. Offline

    Polaris29

    FTC_WTFire7 If you were to take out some of the content, part of your code would look like this:

    Code:java
    1.  
    2. }else if(commandLabel.equalsIgnoreCase("creative")){
    3. if (sender.hasPermission("LC.gamemode"))
    4. if(args.length == 0){
    5.  
    6. } else if(commandLabel.equalsIgnoreCase("survival")){
    7. if (sender.hasPermission("LC.survival"))
    8. if(args.length == 0){
    9. } else if(commandLabel.equalsIgnoreCase("time")){
    10.  
    11. }
    12. }
    13.  
    14. }
    15.  


    The code that checks if the commandLabel is "survival" is inside the body of the else if statement that checks if the command label is "creative"
    And the code that checks if the commandLabel is "time" is inside the body of the else if statement that checks if the command label is "survival"
     
  7. Offline

    FTC_WTFire7

    So where am I missing the bracket?
     
  8. Offline

    Polaris29

    FTC_WTFire7 You're not missing a bracket, by "bad", I meant kinda misplaced, not missing

    Just change this:
    Show Spoiler

    }else if(commandLabel.equalsIgnoreCase("creative")){
    if (sender.hasPermission("LC.gamemode"))
    if(args.length == 0){
    //creative = 0 args /creative Player = 1 args
    player.setGameMode(GameMode.CREATIVE);
    player.sendMessage("ChatColor.GOLD + Gamemode set to Creative");
    if(args.length == 1){
    player.sendMessage("ChatColor.RED + Sorry, you cannot change other people gamemode yet!");
    return false;
    }
    }else if(commandLabel.equalsIgnoreCase("survival")){
    if (sender.hasPermission("LC.survival"))
    if(args.length == 0){
    player.setGameMode(GameMode.SURVIVAL);
    player.sendMessage("ChatColor.GOLD + Gamemode set to Survival");
    if(args.length == 1){
    Player targetPlayer = player.getServer().getPlayer(args [0]);
    targetPlayer.setGameMode(GameMode.SURVIVAL);
    return false;
    }
    }else if(commandLabel.equalsIgnoreCase("time")){
    if(args[0].equalsIgnoreCase("day")){
    world.setTime(0);
    Bukkit.broadcastMessage("Time set to Day!");
    }else if(args[0].equalsIgnoreCase("night")){
    world.setTime(14000);
    Bukkit.broadcastMessage("Time set to Night!");
    }else if(args[0].equalsIgnoreCase("morning")){
    world.setTime(1000);
    Bukkit.broadcastMessage("Time set to Morning");
    return false;
    }

    }
    }

    }

    to this:
    Show Spoiler

    }else if(commandLabel.equalsIgnoreCase("creative")){
    if (sender.hasPermission("LC.gamemode"))
    if(args.length == 0){
    //creative = 0 args /creative Player = 1 args
    player.setGameMode(GameMode.CREATIVE);
    player.sendMessage("ChatColor.GOLD + Gamemode set to Creative");
    if(args.length == 1){
    player.sendMessage("ChatColor.RED + Sorry, you cannot change other people gamemode yet!");
    return false;
    }
    }

    } else if(commandLabel.equalsIgnoreCase("survival")){
    if (sender.hasPermission("LC.survival"))
    if(args.length == 0){
    player.setGameMode(GameMode.SURVIVAL);
    player.sendMessage("ChatColor.GOLD + Gamemode set to Survival");
    if(args.length == 1){
    Player targetPlayer = player.getServer().getPlayer(args [0]);
    targetPlayer.setGameMode(GameMode.SURVIVAL);
    return false;
    }
    }
    }else if(commandLabel.equalsIgnoreCase("time")){
    if(args[0].equalsIgnoreCase("day")){
    world.setTime(0);
    Bukkit.broadcastMessage("Time set to Day!");
    }else if(args[0].equalsIgnoreCase("night")){
    world.setTime(14000);
    Bukkit.broadcastMessage("Time set to Night!");
    }else if(args[0].equalsIgnoreCase("morning")){
    world.setTime(1000);
    Bukkit.broadcastMessage("Time set to Morning");
    return false;
    }
    }

    And that should fix it

    Edit: The forums messed up the indentation...
     
  9. If you're using Eclipse a cool trick to fix indentation is by pressing "Ctrl+Shift+F" while click in a Code window.
     
  10. Offline

    FTC_WTFire7

    DescriptionResourcePathLocationType
    Syntax error, insert "else Statement" to complete IfStatementLegendarycommands.java/LegendaryCommands/src/me/chasemostat/legendarycommandsline 92Java Problem
    Syntax error, insert "}" to complete MethodBodyLegendarycommands.java/LegendaryCommands/src/me/chasemostat/legendarycommandsline 92Java Problem
    Syntax error, insert "else Statement" to complete IfStatementLegendarycommands.java/LegendaryCommands/src/me/chasemostat/legendarycommandsline 92Java Problem
    Syntax error, insert "}" to complete ClassBodyLegendarycommands.java/LegendaryCommands/src/me/chasemostat/legendarycommandsline 29Java Problem
    I get all of these errors
     
  11. Offline

    Polaris29

    FTC_WTFire7 Can you post the class file you have now?
     
  12. Offline

    FTC_WTFire7

    Polaris29

    Code:java
    1. package me.chasemostat.legendarycommands;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.World;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Legendarycommands extends JavaPlugin {
    18. public static Legendarycommands plugin;
    19. public final Logger logger = Logger.getLogger("Minecraft");;
    20.  
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " is now disabled!");
    24. }
    25.  
    26. public void onEnable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " is now enabled!");
    29. }
    30. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    31. Player player = (Player) sender;
    32. World world = player.getWorld();
    33. if (sender.hasPermission("LC.heal"))
    34. if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
    35. if(args.length == 0){
    36. //heal = 0 args /heal Player = 1 args
    37. player.setHealth(20);
    38. player.sendMessage(ChatColor.GREEN + "Healed");
    39. }else if(args.length == 1){
    40. Player targetPlayer = player.getServer().getPlayer(args [0]);
    41. targetPlayer.setHealth(20);
    42. player.sendMessage(ChatColor.GREEN + "Healed");
    43. return false;
    44. }
    45. } else if(commandLabel.equalsIgnoreCase("feed")){
    46. if (sender.hasPermission("LC.feed"))
    47. if(args.length == 0){
    48. //feed = 0 args /feed Player = 1 args
    49. player.setFoodLevel(20);
    50. player.sendMessage(ChatColor.GOLD + "Your hunger has been filled!");
    51. }else if(args.length == 1){
    52. Player targetPlayer = player.getServer().getPlayer(args [0]);
    53. targetPlayer.setFoodLevel(20);
    54. player.sendMessage(ChatColor.GOLD + "Player has been fed!");
    55. return false;
    56. }
    57. }else if(commandLabel.equalsIgnoreCase("creative")){
    58. if (sender.hasPermission("LC.gamemode"))
    59. if(args.length == 0){
    60. //creative = 0 args /creative Player = 1 args
    61. player.setGameMode(GameMode.CREATIVE);
    62. player.sendMessage("ChatColor.GOLD + Gamemode set to Creative");
    63. if(args.length == 1){
    64. player.sendMessage("ChatColor.RED + Sorry, you cannot change other people gamemode yet!");
    65. return false;
    66. }
    67. }
    68.  
    69. } else if(commandLabel.equalsIgnoreCase("survival")){
    70. if (sender.hasPermission("LC.survival"))
    71. if(args.length == 0){
    72. player.setGameMode(GameMode.SURVIVAL);
    73. player.sendMessage("ChatColor.GOLD + Gamemode set to Survival");
    74. if(args.length == 1){
    75. Player targetPlayer = player.getServer().getPlayer(args [0]);
    76. targetPlayer.setGameMode(GameMode.SURVIVAL);
    77. return false;
    78. }
    79. }
    80. }else if(commandLabel.equalsIgnoreCase("time")){
    81. if(args[0].equalsIgnoreCase("day")){
    82. world.setTime(0);
    83. Bukkit.broadcastMessage("Time set to Day!");
    84. }else if(args[0].equalsIgnoreCase("night")){
    85. world.setTime(14000);
    86. Bukkit.broadcastMessage("Time set to Night!");
    87. }else if(args[0].equalsIgnoreCase("morning")){
    88. world.setTime(1000);
    89. Bukkit.broadcastMessage("Time set to Morning");
    90. return false;
    91. }
    92. }
     
  13. Offline

    Polaris29

  14. Offline

    FTC_WTFire7

    Thanks So Much for your help. I think I know what I am doing now. Thanks So Much!!
     
Thread Status:
Not open for further replies.

Share This Page