Solved Multi YAML Files

Discussion in 'Plugin Development' started by jafacakes2011, May 22, 2013.

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

    jafacakes2011

    Hello, I am having an issue with my plugin, I am wanting to have multiple .yml files so that i can store player data separate to my plugin config.yml

    I have looked at many posts on doing this and i am not 100% on it.
    I must say I am not the best at Java and i am just beginning to learn.

    My Main Class
    Show Spoiler

    Code:
    package com.domain.plugin;
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class currency extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static currency plugin;
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            getLogger().info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has Been Enabled!");
            myNewClass.customConfig.set("one.two.three", "HELLO");
        }
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            getLogger().info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has Been Disabled!");
            myNewClass.saveCustomConfig();
        }
    }

    myNewClass
    Show Spoiler

    Code:
    package com.domain.plugin;
     
    import java.io.File;
    import java.io.InputStream;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    public class myNewClass {
        public static currency plugin;
     
        public static FileConfiguration customConfig = null;
        public static File customConfigFile = null;
     
        public static void reloadCustomConfig() {
            if (customConfigFile == null) {
            customConfigFile = new File(plugin.getDataFolder(), "customConfig.yml");
            }
            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
     
            // Look for defaults in the jar
            InputStream defConfigStream = plugin.getResource("customConfig.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                customConfig.setDefaults(defConfig);
            }
        }
        public static FileConfiguration getCustomConfig() {
            if (customConfig == null) {
                reloadCustomConfig();
            }
            return customConfig;
        }
        public static void saveCustomConfig() {
            if (customConfigFile == null) {
                customConfigFile = new File(plugin.getDataFolder(), "customConfig.yml");
            }
        }
    }

    Any help would be much appreciated, thanks in advance.
    jafacakes2011
     
  2. Offline

    NoLiver92

    search this forum, you are reinventing the wheel. there are excellent tutorials on here and most of them contain a class file which has this code already done for you.

    This would be the best place to go as this bukkit tutorial shows how to set up to make a custom yml file which can be used like a normal config file: link

    If you want to do more yml files then 1 all you need to do is set the variable customconfigFile to a new file.yml then you can make as many as you want. when you want to read them back then you just set the variable to the file you want, load it then you can read and write to it as if it was a standard config file. Life made easy as you are using bukkits own code (makes your plugins lighter)
     
  3. Offline

    caseif

    You've just violated two of the unwritten rules of this subforum...
     
  4. Offline

    Junrall

    AngryNerd
    :rolleyes:
     
Thread Status:
Not open for further replies.

Share This Page