Issues with checking config in onEnable

Discussion in 'Plugin Development' started by kunhunjon, Aug 27, 2014.

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

    kunhunjon

    So I'm trying to make a plugin where you can set the weather permanently. Don't ask why, I'm sure there are ways to do it with another plugin or even in the game.
    Code:java
    1.  
    2. public void onEnable()
    3. {
    4. //reloadConfig();
    5.  
    6. weatherState = getConfig().getString("WeatherState");
    7.  
    8. System.out.println(weatherState);
    9.  
    10. if(getConfig().getString("WeatherState").equals("sun"))
    11. {
    12. getServer().broadcastMessage("sun");
    13. getServer().getWorlds().get(0).setStorm(false);
    14. }
    15. else if(getConfig().getString("WeatherState").equals("storm"))
    16. {
    17. getServer().broadcastMessage("storm");
    18. getServer().getWorlds().get(0).setStorm(true);
    19. }
    20.  
    21. getServer().getPluginManager().registerEvents(this, this);
    22. }


    Whenever the plugin starts I want it to check the config to see if WeatherState is storm, sun, or off then set the weather to the corresponding setting. The if statement up there checking only works when I reload the server (/reload) but not when I stop and then start it. Any ideas as to why?
     
  2. Offline

    jojo1541

    The first thing i was noticing looking through your code: Why are you calling
    Code:java
    1. getConfig().getString("WeatherState")
    multiple times? You already have the String 'WeatherState', so use it. (I believe there's a third weather state called thuder btw)

    For the weather: If it works on reloading but not on stopping/starting i guess it has something to do with the world not properly loaded yet. Try to shedule it some ticks after the initial start of the server.

    Also i don't know if
    Code:java
    1. getServer().getWorlds().get(0)

    is the best way to get the current world. (Didn't need to change a world without some reference inside of it yet)


    I hope that helped a little bit.
     
  3. Offline

    mine-care

    I don't see why it possibly hAppends. Usually onenable fires each time plugin is enabled. That's weird. Try adding a delay before seting to whatever.
     
  4. Offline

    reider45

    Any errors on startup?
     
  5. Offline

    kunhunjon

    jojo1541 I was experimenting with getting the state directly from the config or from the variable. I'll try adding ticks and see if it works. mine-care I know for a fact that it is running on start up and reload, but for some reason the if statement isn't working on start up, but on reload it works great. reider45 Not any that I've seen after testing it a bigillion times.

    I also have a WeatherChangeEvent event that checks if the weather is being changed. If weatherState is set to sun it won't allow the weather to evaluate to storm and if it's set to storm it won't let the weather evaluate to sun. That isn't working either! When I stop and start the server it doesn't work but it works fine when I reload.

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

    jojo1541

    Code:java
    1. package WT;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.weather.WeatherChangeEvent;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9.  
    10.  
    11. public class WeatherTest extends JavaPlugin implements Listener {
    12.  
    13. PluginManager PM;
    14.  
    15. public void onEnable() {
    16.  
    17. PM = getServer().getPluginManager();
    18. PM.registerEvents(this, this);
    19.  
    20. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    21.  
    22. @Override
    23. public void run() {
    24.  
    25. getServer().getWorlds().get(0).setStorm(true);
    26.  
    27. }
    28.  
    29.  
    30.  
    31. }, 30L, 30L);
    32. }
    33.  
    34. public void onDisable() {
    35.  
    36. }
    37.  
    38. @EventHandler
    39. public void onWeatherChangeEvent(WeatherChangeEvent Event) {
    40.  
    41. if (Event.toWeatherState()) {
    42. Event.setCancelled(true);
    43. getServer().broadcastMessage("Weather set to sun again");
    44. }
    45.  
    46. }
    47.  
    48.  
    49. }
    50.  


    works fine for me. The Event fires everytime the weather changes. (Per command, naturally and per plugin)

    Maybe there's something wrong with your listener?
     
Thread Status:
Not open for further replies.

Share This Page