[SOLVED] Plugin Config not working

Discussion in 'Plugin Development' started by SteppingHat, Jun 26, 2012.

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

    SteppingHat

    Hey there.

    So this is my first time using config files in my plugins. The problem I'm having is that the integer value continually returns 0 instead of the integer supplied in the config file.

    Here is the code in my main class:
    Code:
    public class TempTeleport extends JavaPlugin {
       
        public static TempTeleport plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        Logger log = Logger.getLogger("Minecraft");
       
        @Override
        public void onDisable() {
            Bukkit.getServer().getScheduler().cancelTasks(this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " has been disabled!");
        }
       
        int CONF_time;
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info("--------TEMPTELEPORT by SteppingHat--------");
            this.logger.info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled!");
            log.info("-------------------------------------------");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            CONF_time = getConfig().getInt("teleporttime");
        }
    and here is the code in the config.yml file:
    Code:
    # #################### #
    # Temp Teleport Config #
    # #################### #
     
    # It is strongly recommended that you use an editor such as Notepad++ to
    # edit Bukkit config files. Windows Notepad doesn't like reading yml files.
     
    # The time that it takes before the user is teleported back (in seconds)
    teleporttime: 60
    Thanks heaps in sdvance!
     
  2. so, why getting the same logger 2 times first?
     
  3. Offline

    EnvisionRed

    Yeah, no need to define it once as "logger" and once as "log"

    At the line where you define CONF_time, try changing it to
    Code:
    CONF_time = this.getConfig().getInt("teleporttime");
     
  4. Offline

    SteppingHat

    Nup. Didn't work. I still get a 0.

    I've also removed the second logger. It was probably by accident or I wasn't thinking what I was doing :p

    Thanks for all the help!
     
  5. Yes but where are you checking it ?

    Also, if you have config.yml inside your jar and you want it to be copied when it doesn't exist, you don't need:
    Code:
    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    The getConfig() does that automatically the first time it's called.


    I also want to suggest that you remove all those printing of messages... nobody likes console spam and that information is really NOT that important for you to use 4 lines.... 3 yours and the 1 printed automatically by Bukkit when it loads your plugin, so you can just remove yours and let Bukkit print it :p
     
  6. Offline

    EnvisionRed

    Hm... I'm a little curious as to in what method you are calling the "CONF_time" int. You first defined it outside of a method, but only assigned it a value in the onEnable method as far as I can see.

    Correct me if I'm wrong, but the value you give it in the onEnable will only hold true in the onEnable method. If you're calling it in, say, the onCommand method, then it won't have the value of "getConfig.getInt("teleporttime").
    But since it's already been defined outside of the methods, the initial null value is fetchable by any method, and that's why your IDE wouldn't give you an error message, and why in another method, it would show as 0.
     
  7. No, the field will have the value of the last asignment made... onEnable() is triggered when the plugin is enabled by Bukkit, and onCommand() triggers when a command is sent, and since you can't send commands while plugins are enabling at startup, the value will most likely be the one from the config.
    You're confusing fields with local variables, which are created inside code blocks ( between "{" and "}" ) then destroyed when the code block ends.
     
    ferrybig likes this.
  8. Offline

    SteppingHat

    Done!
    I know where to put my name in it so I put it in the onEnable messages.

    Also, I can see what the variable is via a command used to check the integer and when attempting to teleport, it says you have 0 seconds before teleported back and then teleports me back straight away since the value is 0.

    I think I discovered something.

    Upon moving
    Code:
    int CONF_time;
    into the onEnable, another interger below which is half of the time (int time_half) gave me an error puting a red line under CONF_time saying CONF_time cannot be resolved to a variable.

    If gives me the option to "Create Constant CONF_time" which adds above the loggers
    Code:
    private static final int CONF_time = 0;
    and it also gives me the option to "Create Field CONF_time" which adds below the loggers
    Code:
    private int CONF_time;
    I don't know if the solution is in one of these options or not but considering that I have only been making plugins for just over a month now, my knowledge is kinda limited and I can't solve this on my own :p

    So yeah, thanks for the help guys!

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

    Sagacious_Zed Bukkit Docs

    SteppingHat
    This is a programming problem, not a plugin related on, pretty much anywhere you go you will need to keep your variables that you want to access in scope.

    You need a instance variable declared, but you also require it mutable. Therefore anything marked final is out, and you are left with one choice of declaring it.

    Also as a sanity check you can log out what was read in from the config.
    Code:
    getLogger().config("value read for teleporttime: " + getConfig().getInt("teleporttime"));
     
  10. Offline

    SteppingHat

    I've found my problem. Thanks to Sagacious_Zed for getting me to print the integer from the config to the logger.
    It came up with a 60 which is what was defined in the config.

    The problem was actually trying to define a variable in the onEnable. A simple solution was to move
    Code:
    CONF_time = getConfig().getInt("teleporttime");
    to after the onEnable and the rest worked.

    Thanks for all your help in solving this problem.

    The plugin is now available at dev.bukkit.org/server-mods/tempteleport/ if you would like to check it out.
    ~Hat
     
Thread Status:
Not open for further replies.

Share This Page