Solved Is anything wrong with my code?

Discussion in 'Plugin Development' started by Keithman3, Aug 18, 2014.

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

    Keithman3

    Hello everyone, I'm new to Java and Bukkit and I'm created my first plugin for a server. Despite all the changes the owner has told me to make the plugin still won't load in the console.

    The owner thinks there is a problem in the code, but he can't see it and nor can I.

    If my code has many mistakes please forgive me, I am a beginner at coding.
    My plugin is supposed to control the weather.

    Thank you for reading and your help is greatly appreciated!

    Since I can't get the code to load I will paste it into here.

    **EDIT: I've arranged the code into a nicer format, sorry about that.
    **EDIT: My current plugin.yml is at the bottom.
    **EDIT: plugin.yml edited.
    **EDIT: Fixed the code a bit.

    ==========================




    Code:java
    1. package weather.Keithman3;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.World;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12.  
    13. public class Main extends JavaPlugin {
    14.  
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public void onEnable() {
    18. logger.info("Welcome to GalaxyWeather BETA by Keithman3!");
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    22. Player player = (Player) sender;
    23. if (commandLabel.equalsIgnoreCase("weather")) {
    24. if (args[0].equalsIgnoreCase("sun")) {
    25. World world = player.getWorld();
    26. world.setStorm(false);
    27. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "sun");
    28. }
    29. }
    30. else if (args[0].equalsIgnoreCase("rain")) {
    31. World world = player.getWorld();
    32. world.setThundering(true);
    33. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "rain");
    34. }
    35. else if (args[0].equalsIgnoreCase("storm")) {
    36. World world = player.getWorld();
    37. world.setThundering(true);
    38. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "storm");
    39. }
    40. return true;
    41. }
    42.  
    43. }


    ===========
    URGH! Apparently it won't allow the .yml extention somehow, so I've decided to paste the plugin.yml text in the post.
    Code:
        name: Weatherify
        version: 1.0
        description: Weatherify allows the user to control the weather.
        author: Keithman3
        authors: DamnHippo
        main: weather.Keithman3.Main
     
        commands:
          weather:
            description: Sets weather to sun in server.
            permission: galaxy.weather
            permission-message: Sorry, you cannot set the weather!
            usage: Syntax error! Please type /weather (sun, storm, rain)
     
  2. Offline

    Forseth11

    1. Get a test server, test it, and give us the stack trace.
    2. Next time put the code in this format so we can read it:
    Code:java
    1. package weather.Keithman3;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.World;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12.  
    13. public class Main extends JavaPlugin {
    14. public static void main(String[] arguments) {
    15.  
    16. }
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18.  
    19. public void onEnable() {
    20. logger.info("Welcome to GalaxyWeather 3.0 by Keithman3!");
    21. }
    22. public void onDisable() {
    23. logger.info("GalaxyWeather was disabled.");
    24.  
    25. }
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27. Player player = (Player) sender;
    28. if (commandLabel.equalsIgnoreCase("weather")) {
    29. if (args[0].equalsIgnoreCase("sun")) {
    30. World world = player.getWorld();
    31. world.setStorm(false);
    32. }
    33. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "sun");
    34. }
    35. if (commandLabel.equalsIgnoreCase("weather")) {
    36. if (args[0].equalsIgnoreCase("rain"));
    37. World world = player.getWorld();
    38. world.setThundering(true);
    39. }
    40. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "rain");
    41. if (commandLabel.equalsIgnoreCase("weather")) {
    42. if (args[0].equalsIgnoreCase("storm")) {
    43. World world = player.getWorld();
    44. world.setThundering(true);
    45. }
    46. player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "storm");
    47. }
    48. return true;
    49. }
    50.  
    51. }

    EDIT: 3. Show us your plugin.yml
     
  3. Offline

    Dubehh

    Keithman3
    Notes:
    - 'public static void main(String[] args)' is not needed in a minecraft plugin. onEnable does the trick.
    - You don't need to check two times if the commandLabel is wheater, since it doesn't changes, you can have just one
    - Be aware you get erros once this command is activated through the console, since you cast the sender immediately to a player without checking if it is actually a player
    - Show the plugin.yml + the stacktrace like Forseth11 said.
    - Make sure every bracket '{' is positioned good

    Just some things ;)
     
  4. Offline

    Forseth11

    lol Dubehh that is one bugged up code... I did not even bother to read it since it was not posted nicely.
     
  5. Offline

    Dubehh

    Forseth11
    Excuse me? I just 'tahged' you because you gave some (good) advice he should follow too?
     
  6. Offline

    Geekhellmc

    Code:
    public class Main extends JavaPlugin {
     
    public final Logger logger = Logger.getLogger("Minecraft");
     
    public void onEnable() {
    logger.info("Welcome to GalaxyWeather 3.0 by Keithman3!");
    }
    public void onDisable() {
    logger.info("GalaxyWeather was disabled.");
     
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    Player player = (Player) sender;
    if (commandLabel.equalsIgnoreCase("weather")) {
    if (args[0].equalsIgnoreCase("sun")) {
    World world = player.getWorld();
    world.setStorm(false);
    player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "sun");
    }
    }
    if (args[0].equalsIgnoreCase("rain"));
    World world = player.getWorld();
    world.setThundering(true);
    player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "rain");
    }
    if (args[0].equalsIgnoreCase("storm")) {
    World world = player.getWorld();
    world.setThundering(true);
    player.sendMessage(ChatColor.AQUA + "You turned the weather to: " + ChatColor.GOLD + "storm");
    }
    return true;
    }
     
    }
    That should work (Untested)
     
  7. Offline

    Lactem

    1. You shouldn't cast before checking.
    2. You should use else ifs or a switch instead of a bunch of ifs, although the program will work the same either way.
    3. You should return true inside those ifs.
     
  8. Offline

    fireblast709

    Do note that both Minecraft and Bukkit are compiled in Java 6, and Strings in switches is a Java 7 feature (so you are secretly supposed to compile in Java 6 for maximum compatibility)
    Geekhellmc & Keithman3 to continue the above list
    1. Use cmd.getName() instead of commandLabel for alias support
    2. No need to log the disable message as it adds nothing in respect to Bukkit's disable message
    3. Do not use Logger.getLogger("Minecraft") nor Bukkit.getLogger(). Use the Logger that JavaPlugin provides through it's getLogger() method.
     
  9. Offline

    Lactem

    fireblast709 I know this, but it really doesn't matter at all. You're not actually editing minecraft or Bukkit and then compiling. If your program is built with Java 7+ and the server uses Java 7+ (which they all should, even though not all do :( ), then everything will be fine.
     
  10. Offline

    AronTheGamer

    Code:java
    1. public boolean onCommand( CommandSender sender, Command cmd, String commaneLabel, String[] args )
    2. {
    3. if( cmd.getName().equalsIgnoreCase( "weather" ) )
    4. {
    5. if( args.length > 0 )
    6. {
    7. if( args.length > 1 )
    8. {
    9. World w = getServer().getWorld( args[1] );
    10. }
    11. else if( sender instanceof Player )
    12. {
    13. World w = ( (Player) sender ).getLocation().getWorld()(
    14. }
    15. else
    16. {
    17. sender.sendMessage( "As console you should provide the world to change the weather." );
    18. return true;
    19. }
    20. switch( args[0].toLowerCase() )
    21. {
    22. case "rain":
    23. w.setThundering( false );
    24. w.setStorming( true );
    25. break;
    26. case "sun":
    27. w.setThundering( false );
    28. w.setStorming( false );
    29. break;
    30. case "storm":
    31. w.setThundering( true );
    32. w.setStorming( true );
    33. break;
    34. }
    35. else
    36. {
    37. sender.sendMessage( "Missing weather argument." );
    38. }
    39. }
    40. }


    Not tested, wrote on ma phone.
     
  11. Offline

    Keithman3

    I fixed it to make it look nicer. Thanks for the advice.

    I think you made a mistake with "if(args[0].equalsIgnoreCase("rain"));", I think there's supposed to be "{" after "("rain"))", everything else was good, thanks for the advice, I still have yet to test it.

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

    Geekhellmc

    Ok, no problem.
     
  13. Offline

    Rocoty

    Keithman3 Just take a good look at the plugin.yml. then take a good look at the main class' package and name. It should come to you...
     
  14. Offline

    ClassyInvader69

    Rocoty LOL. You were the only one to spot the error.
     
  15. Offline

    Keithman3

    Oh my gosh, you were right! That was such a stupid error... I'll fix it.
     
  16. Offline

    xize

    Keithman3

    maybe also try removing these public static void main(String[] arguments) { I think its not your intention to run it appart from bukkit like a executable?:p

    ofc thats may not the issue though, can you post any error/exceptions if they still persists?
     
  17. Offline

    Keithman3

    Fixed.
     
  18. Offline

    Keithman3

    It worked! Thank you to everyone for your help.

    Code:java
    1. System.out.println("Thank you very much!");


    (I think that was right... :p)
     
Thread Status:
Not open for further replies.

Share This Page