Language.yml config returns null!

Discussion in 'Plugin Development' started by BurnerDiamond, Apr 29, 2015.

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

    BurnerDiamond

    I have this class which creates a language.yml

    Code:
    /*#######################################################################################################################*/
    
        public void createLanguageConfig() throws IOException {
    
            FileConfiguration fileConfiguration;
            File file = new File(Core.getInstance().getDataFolder() + File.separator + "language.yml");
    
            if (!file.exists()) {
                file.createNewFile();
                fileConfiguration = YamlConfiguration.loadConfiguration(file);
                fileConfiguration.set("chestName", "Chest");
                fileConfiguration.save(file);
    
            }
        }
        /*#######################################################################################################################*/
    
    
        public static FileConfiguration getLanguageConfig() {
    
            File plugins = Core.getInstance().getDataFolder().getParentFile();
            File folder = new File(plugins.getPath() + File.separator + "language.yml");
            FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(folder);
    
            return fileConfiguration;
    
        }
    
    
        /*#######################################################################################################################*/
    
    I have only one thing in the yml which is this:

    chestName: Chest

    The problem is when I use it in this context:

    Code:
     Inventory inventory = Bukkit.createInventory(p, 9, "§4§l" + Configs.getLanguageConfig().getString("chestName"));
    It sets the chest name to null.

    Any help?
     
  2. Offline

    mine-care

    @BurnerDiamond I think this is caused because the string with the specified path does not exist.
     
  3. Offline

    BurnerDiamond

    Not possible, the config and the path are exactly the same.

    The language.yml only contains this:

    chestName: Chest

    See anything wrong?

    Could it be possible that the string has not been initalized?

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

    HungerCraftNL

    I always use create a setup method, that's called by the onEnable(), which gives a FileConfiguration variable the right value. Than I create a method to get the messages or the configuration file. ( Example )
     
  5. Offline

    SuperOriginal

    @HungerCraftNL Why not use a constructor..?


    @BurnerDiamond Don't create a new File and YamlConfiguration object every time you get it, instantiate it through the constructor, then use that instance. Also that method shouldn't be static either.
     
    Last edited: Apr 30, 2015
    mine-care likes this.
  6. Code:
    File plugins = Core.getInstance().getDataFolder().getParentFile();
            File folder = new File(plugins.getPath() + File.separator + "language.yml");
    This code is loading the file at <serverfolder>/<plugins>/language.yml, but you are saving it to <serverfolder>/<plugins>/<plugin>/language.yml, just change it to:
    Code:
    File folder = new File(Core.getInstance().getDataFolder(), "language.yml");
     
  7. Offline

    BurnerDiamond

    How do I only make one instance of it?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    BurnerDiamond

    Could you please show me what you mean?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @BurnerDiamond
    class Something{
    private File plugins;

    Then put a value in there in the constructor
     
  11. Offline

    SuperOriginal

    @BurnerDiamond
    Code:
    public class MyClass{
        //private fields:
        private File file;
        private YamlConfiguration config;
    
        //instantiate them in the constructor, this way they only have to be instantiated once.
        public MyClass(){
            file = /*...*/
            config = YamlConfiguration.loadConfiguration(file);
        }
    }
     
    Last edited: Apr 30, 2015
Thread Status:
Not open for further replies.

Share This Page