YAMLConfiguration.get() returning null?

Discussion in 'Plugin Development' started by jcgurango, Oct 12, 2011.

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

    jcgurango

    This is a very odd problem I'm having. Here's a class I made to bring back the old functionality of the Configuration class:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package org.jcgurango.IT;
    import java.io.File;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    /**
     *
     * @author JC
     */
    public class Configuration {
        private YamlConfiguration yc = null;
        private File yamlFile =  null;
    
        public Configuration(File f)
        {
            yamlFile = f;
            yc = new YamlConfiguration();
        }
    
        public Object getProperty(String path)
        {
            return yc.get(path);
        }
    
        public void setProperty(String path, Object value)
        {
            yc.set(path, value);
        }
    
        public void save()
        {
            try
            {
                yc.save(yamlFile);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public void load()
        {
            try
            {
                yc.load(yamlFile);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    And here's my code to load a configuration file:
    Code:
                Configuration configFile = new Configuration(new File("plugins/ItemTrades/config.yml"));
                configFile.load();
                for(int i = 0; i < prices.length; i++)
                {
                    prices[i] = (Integer)configFile.getProperty("config.prices." + i);
                }
                moneyID = (Integer)configFile.getProperty("config.moneyID");
    
    Now, apparently, the problem lies in the getProperty method. It's returning a null value, and I don't see why. Does anybody else see it?

    EDIT: Also, this is some code I used to generate a blank configuration file, which works completely fine. The configuration file gets saved, and I checked it and all the values are correct:
    Code:
                Configuration configFile = new Configuration(new File("plugins/ItemTrades/config.yml"));
                configFile.setProperty("config.moneyID", 0);
                for(int i = 0; i < prices.length; i++)
                {
                    configFile.setProperty("config.prices." + i, 0);
                }
                print("Saving empty config file...");
                configFile.save();
    
     
  2. Offline

    desht

    Well, I'd have to wonder why you're using getProperty() and casting it to Integer, when there's already a perfectly good getInt() call in the API. That approach doesn't do any favours for robustness.

    Seriously, what does this wrapper buy you? Would it not be easier in the long run to bite the bullet and update your plugin code to use the new API?
     
  3. Offline

    RROD

    Why would you want to bring back the old, config? The new one works fine, if not better. The developers put the new one in for a reason. If you're not used to it yet, you need to be. This is a bad way of coding. =/
     
  4. Offline

    jcgurango

    This wrapper buys me a lot of time, considering I have tons of things using configuration files. I'd have to go in and change them all. I don't see what the new API added, though. Maybe it's faster, maybe it's something else, I don't know. But that's not what I'm asking. The .get() method is returning null when it shouldn't be.

    Read what I said to the other poster. It's not that I'm not used to it. The fact that I made a class that properly utilizes it should show very well that I know how to use it correctly. My plugin was developed before they put out the new API, and I'd rather not go through the 15 or so areas the old Configuration class was utilized changing them all to use this new one.

    Also, did you guys really just post here to tell me that this is bad? Not even gonna answer my question?
     
  5. Offline

    desht

    Are you using CB 1317? That build has a few bugs with the new API, which were pretty much fixed in by 1323, I believe. In particular a bug that caused lookup failures with paths containing 3 or more elements (e.g. config.prices.X)

    I will stand by my previous comments though - it's better to invest a little extra effort in the short term to save potential long-term pain.
     
  6. Offline

    jcgurango

    I wasn't aware of said bug, and yes I actually was using 1317. Thanks.
     
  7. Offline

    Taien

    I
    I'm not the OP, but I'd love to do what you suggest - I just can't find ANY information on this new system. Can you link me PLEASE to something that explains it? All my plugins are broken in this build, so I can't update my server to it.
     
  8. Offline

    desht

    http://forums.bukkit.org/threads/he...configureable-by-the-users.40981/#post-740663
    http://forums.bukkit.org/threads/configuration.41248/#post-744768
    http://wiki.bukkit.org/User:Sagacious_Zed/Introduction_to_the_New_Configuration
    http://forums.bukkit.org/threads/recommended-build-1317.40937/page-3#post-745168

    But something important to understand - the config API update has broken exactly zero plugins, because the old API still works.
     
  9. Offline

    Taien

    When I tried to update my server running plugins using the Configuration API and not FileConfiguration, it basically cleared out the config files of all my plugins. Has something to do with the new way the config files are named. (config.yml). It's no biggie, I managed to get them all updated now after reading some of those topics. Thanks :)

    It's so odd though...I searched the wiki and I didn't find that page, and I specifically searched for "Configuration". Argh.
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    @Taien
    Sorry about that, I was writing it as a user page,just to get things blocked out, so when you search, it only searches the main namespace and thus was not finding it. I have moved the page out of my User page. It will redirect you for now.

    So if anyone want to link to that page, get rid of the UserPage reference. This way we can finally get rid of the redirect
     
  11. Offline

    Taien

    Thanks Zed :)

    Is there a good thread somewhere with a debate on the best way to read string lists now? I found this bit of code:
    Code:
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public <T> List<T> getList(FileConfiguration config, String path, List<T> def)
        {
            List list = config.getList(path);
            if (list != null && list.getClass().equals(def.getClass()))
            {
                return (List<T>) list;
            }
            else
            {
                return def;
            }
        }
    ...but this seems to have trouble reading lists of strings. I use a List<String> to store messages that are cycled with one of my plugins, and for some reason when I load the config data using this snippet above it returns an empty list (probably because it's returning the default, which is new LinkedList<String>). Any suggestions?
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    Actually that if statement does not do what you think it does. Think this
    Code:
    ArrayList<Integer> li = new ArrayList<Integer>();
    ArrayList<Float> lf = new ArrayList<Float>();
    if (li.getClass() == lf.getClass()) // evaluates to true
      System.out.println("Equal");
    This segment of code outputs Equal

    Dinnerbone suggested this
    Code:
    List<String> listofstrings = config.getList("path.to.list");
     
  13. Offline

    Taien

    The last bit I've seen in the suggestions, but when I try to put that in it throws warnings and such. Should I just ignore the warnings?

    Edit: Ok I just went ahead and ignored the warnings and it seems to work fine. Thanks :)
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    Funny enough netbeans does not warn me about this unchecked cast. And if you really wanted to. you can write your own method, to cast from Object to String....
     
  15. Offline

    AlexDGr8r

    That really frustrates me. I spent about 2 hours looking for an error in my code because it was returning the default value when there was a value stored in the file already. I really hope they fix this. I'll have to bring my plugin back to the old configuration in order to fix this. :mad:
     
  16. Offline

    Sagacious_Zed Bukkit Docs

    It has already been fixed....
     
  17. Offline

    AlexDGr8r

    Not for the recommended build which is what I am working on.
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    That particular recommended build will forever be the same.
     
Thread Status:
Not open for further replies.

Share This Page