[SOLVED] Problem with Configuration, always returning def instead of actual value

Discussion in 'Plugin Development' started by NuxlyStardust, Apr 16, 2011.

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

    NuxlyStardust

    -- Edit --
    Okay forget it, I found what was wrong. I forgot to call config.load(); before starting to read values.
    -- /Edit --


    Hello there
    I hope someone will be able to help me with this cause I've been struggling to fix it for a hour now and I'm about to give up.
    It's a really simple code but I really can't figure out what's wrong with it.
    Explanations below.


    Code:
    package net.nuxcom.minecraft.nxcoords;
    
    import java.io.File;
    
    import org.bukkit.util.config.Configuration;
    
    public final class PlgConfiguration {
    
        private Configuration config;
    
        private float maxN, maxS, maxE, maxW;
    
        public PlgConfiguration(String filename) {
    
            File configFile = new File(filename);
            config = new Configuration(configFile);
    
            if(!configFile.exists()) {
    
                // Sample values
                setBounds(2000, 2000, 2000, 2000);
                save();
    
            } else {
    
                load();
    
            }
    
        }
    
        public void save() {
    
            config.setProperty("teleport.max_north", (int)maxN);
            config.setProperty("teleport.max_south", (int)maxS);
            config.setProperty("teleport.max_east", (int)maxE);
            config.setProperty("teleport.max_west", (int)maxW);
    
            config.save();
    
        }
    
        public void load() {
    
            // Problem: getInt always returns 2000 no matter what the values are in the file.
            setBounds((float)config.getInt("teleport.max_north", 2000),
                    (float)config.getInt("teleport.max_south", 2000),
                    (float)config.getInt("teleport.max_east", 2000),
                    (float)config.getInt("teleport.max_west", 2000));
    
        }
    
        public void setBounds(float maxN, float maxS, float maxE, float maxW) {
    
            this.maxN = maxN;
            this.maxS = maxS;
            this.maxE = maxE;
            this.maxW = maxW;
    
        }
    
        public float getMaxN() {
    
            return maxN;
    
        }
    
        public float getMaxS() {
    
            return maxS;
    
        }
    
        public float getMaxE() {
    
            return maxE;
    
        }
    
        public float getMaxW() {
    
            return maxW;
    
        }
    
    }
    


    This class loads the plugin configuration, stores values in variables so I can retrieve them anywhere else in the code. It is constructed like this: new PlgConfiguration(getDataFolder() + "\\config.yml");
    If the file doesn't exist, it creates default values and save them.
    The config file as it is now only contains the max distance for a teleport command (North, South, East, West).
    setBounds(...) changes those values in the variables, save() dumps them into the config file, load() loads them from the config file.

    My problem is in the load method: no matter what I put in the config file, it will always find "2000" (the default value I specified in the code).

    I did this test in the code:
    Setting "teleport.max_north" to 10, saving, then trying to read "teleport.max_north" like this: config.getInt("teleport.max_north", 2000)
    It returns 2000 instead of 10.
    Even if I don't cast the values to floats, the problem is the same.

    Here's the config file for reference:
    Code:
    teleport:
        max_west: 5000
        max_east: 5000
        max_south: 5000
        max_north: 5000
    
    It always returns 2000 instead of 5000 for each of these values.

    So, what am I doing wrong?

    Thanks in advance :)


    PS- I'm using the latest recommended build for both the server and the API. (677)
     
Thread Status:
Not open for further replies.

Share This Page