Create a .yml File on another class

Discussion in 'Spigot Plugin Development' started by Peng1104, May 18, 2017.

  1. Do anyone has a ideia how a possible to make a class that creats a yml file whiout been your main class?
     
    #1 Peng1104, May 18, 2017
    Last edited: May 20, 2017
  2. Explain what your asking.... Kinda do not understand what your asking.
     
  3. Yes, you can use a separate class file to create another YAML file. Assuming the code you offered above actually works, you should just be able to instantiate the File class from your Main class, and it'll work. If your code is not doing what you want, here is an article from the Spigot Wiki describing how to make addition YAML files: https://www.spigotmc.org/wiki/config-files/
     
  4. i have see this link but it only allow you tu create YAML files on the same class.... this artical in this case is useless...
     
    #4 Peng1104, May 18, 2017
    Last edited: May 18, 2017
  5. I have create a class that extends Main and add the java code to create a new file and the respective methods for use it
    Code;
    Code (Text):
    public class KillFile extends Main{
       
        public KillFile() {}
       
        private FileConfiguration KillConfig = null;
        public File KillConfigFile = null;
       
        public void reloadKillConfigFile() {
           
            if (KillConfigFile == null) {
                KillConfigFile = new File(getDataFolder() + File.separator + "Comandos Minecraft" , "kill.yml");
            }
            KillConfig = YamlConfiguration.loadConfiguration(KillConfigFile);
           
            try {
                Reader defCorConfigStream = new InputStreamReader(getResource("kill.yml"), "UTF8");
                if (defCorConfigStream != null) {
                    YamlConfiguration defCorConfig = YamlConfiguration.loadConfiguration(defCorConfigStream);
                    KillConfig.setDefaults(defCorConfig);
                }
            }
            catch (UnsupportedEncodingException ex) {
            }
        }
        public FileConfiguration getKillConfigFile() {
            if (KillConfig == null) {
                reloadKillConfigFile();
            }
            return KillConfig;
        }
        public void saveKillConfigFile() {
            if (KillConfig == null || KillConfigFile == null) {
                return;
            }
            try {
                getKillConfigFile().save(KillConfigFile);
            }
            catch (IOException ex) {
                getLogger().log(Level.SEVERE, getConfig().getString("IMPOSSIVEL") + getConfig().getString("ARQUIVO") + KillConfigFile.getName(), ex);
            }
        }
    }

    And add this to the onEnable()
    Code (Text):
    KillFile killfile = new KillFile();
            killfile.getKillConfigFile().options().copyDefaults(true);
            killfile.saveKillConfigFile();
            getLogger().info("The File" + killfile.KillConfigFile.getName() +  "has been loaded!");

    I have this Ideia but when i start the plugin it says that it is already enable...
     
  6. No, it is not useless. The code in there can just be put in another class file if you want; it doesn't have to be in your Main class. Your problem is you're extending the Main class, don't do that. Your code should look something like this:

    Code (Text):
    public class FileManager {
        File file;
        FileConfiguration fc;
        public FileManager(Main pl) {

        }

        private void makeFile(){
            //code to make a file if it doesn't exist, see the wiki page for more info.
        }

        public FileConfiguration getFileConfig(){
            return fc;
        }
        public File getFile(){
            return file;
        }
    }
    Code (Text):
    public class Main extends JavaPlugin {
        FileManager fm;
        public void onEnable() {
            fm = new FileManager(this);
        }
    }
    Then, everytime you want to "reload" the file in your Main class, just call getFile and getFileConfig then replace all your old values with the new ones.
     
  7. Your method is not very efficient

    Create a FileManager class
    Code (Text):
     public FileManager {
       FileManager() { }
      static FileManager instance = new FileManager();

      public static FileManager getInstance() {
        return instance;
      }

    }
    Add a setup method which you will call in your onEnable
    Code (Text):

    //Import org.bukkit.plugin.Plugin for plugin p;

    File file;
    FileConfiguration config;
    public void setup(Plugin p) {
     
     
      if (! p.getDataFolder().exists()) {
        p.getDataFolder.mkdirs();
      }

      file = new File(p.getDataFolder(), "file.yml");
     
    //Checks if File exists or not
      if (! file.createNewFile()) {
           
      }

      config = YamlConfiguration.load(file);
    }

    //Add reload and save here
    public void reloadConfig() {
        config = YamlConfiguration.load(file);
    }

    public void saveConfig() {
      config.save(file);
    }

    public FileConfiguration getConfig() {
      return config;
    }
     
    This way you only need 1 instance and you can do

    Code (Text):
     public void onEnable() {
      FileManager.getInstance().setup(this);
    }

    //If you want to access the config you can just do

    FileManager.getInstance().getConfig();
     
     
  8. I have make this code on the class File:
    Code (Text):
    public class SayFile {
     
        SayFile() {}
     
        static SayFile instance = new SayFile();
     
        public static SayFile getInstance() {
            return instance;
        }
        public void setup(Plugin p) {
            if (!p.getDataFolder().exists()) {
                p.getDataFolder().mkdirs();
            }
        }
     
        private Plugin p;
        private Main pl;
     
         private FileConfiguration SayConfig;
        public File SayConfigFile;
        public FileConfiguration getSayConfigFile() {
            return this.SayConfig;
        }
        private void criarArquivos() {
            SayConfigFile = new File(p.getDataFolder() + File.separator + "Comandos Minecraft" + File.separator + "Say", "sayyml");
         
            if (!SayConfigFile.exists()) {
                SayConfigFile.getParentFile().mkdirs();
                p.saveResource("say.yml", false);
            }
        }
     
     
        public void reloadSayConfigFile() {
            if (SayConfigFile == null) {
                criarArquivos();
            }
            SayConfig = YamlConfiguration.loadConfiguration(SayConfigFile);
         
            try {
                Reader defCorConfigStream = new InputStreamReader(p.getResource("say.yml"), "UTF8");
                if (defCorConfigStream != null) {
                    YamlConfiguration defCorConfig = YamlConfiguration.loadConfiguration(defCorConfigStream);
                    SayConfig.setDefaults(defCorConfig);
                }
            }
            catch (UnsupportedEncodingException ex) {
            }
        }
        public void saveSayConfigFile() {
            if (SayConfig == null || SayConfigFile == null) {
                return;
            }
            try {
                getSayConfigFile().save(SayConfigFile);
            }
            catch (IOException ex) {
                p.getLogger().log(Level.SEVERE, pl.getConfig().getString("IMPOSSIVEL") + pl.getConfig().getString("ARQUIVO") +SayConfigFile.getName(), ex);
            }
        }
    }
    and add this to the onEnable:
    Code (Text):
    private SayFile sayfile;

    final public void onEnable() {
            sayfile.getSayConfigFile().options().copyDefaults(true);
            sayfile.saveSayConfigFile();
            logger.info(config.getString("ARQUIVO") + sayfile.SayConfigFile.getName() + config.getString("SUCESSO"));
    }
    and this error hapends:
    [​IMG]
    Url: https://prnt.sc/fa9cls
     
  9. Your setup method isn't complete. You need to create the file there as well.

    2. You cant just create an empty instance in your main class. You need to do SayFile.getInstance().//method you are using
     
  10. i have change but i dont undersant one thig

    why do the p.getDataFolder() dont work and make the plugin crash?

    My Code:
    on the onEnable():
    Code (Text):
    SayFile sayfile = SayFile.getInstance();
            sayfile.getSayConfigFile().options().copyDefaults(true);
            sayfile.saveSayConfigFile();
            logger.info(config.getString("ARQUIVO") + sayfile.SayConfigFile.getName() + config.getString("SUCESSO"));
    On the file.class:
    Code (Text):
    public class SayFile {
       
        SayFile() {}
       
        static SayFile instance = new SayFile();
       
        public static SayFile getInstance() {
            return instance;
        }
        private Plugin p;
        private Main pl;
       
         private FileConfiguration SayConfig;
        public File SayConfigFile;
        public void reloadSayConfigFile() {
            if (SayConfigFile == null) {
                if (!p.getDataFolder().exists()) {
                    p.getDataFolder().mkdirs();
                }
                SayConfigFile = new File(p.getDataFolder() + File.separator + "Minecraft" + File.separator + "Say", "say.yml");
            }
            SayConfig = YamlConfiguration.loadConfiguration(SayConfigFile);
           
            try {
                Reader defCorConfigStream = new InputStreamReader(p.getResource("say.yml"), "UTF8");
                if (defCorConfigStream != null) {
                    YamlConfiguration defCorConfig = YamlConfiguration.loadConfiguration(defCorConfigStream);
                    SayConfig.setDefaults(defCorConfig);
                }
            }
            catch (UnsupportedEncodingException ex) {
            }
        }
        public FileConfiguration getSayConfigFile() {
            if (SayConfig == null) {
                reloadSayConfigFile();
            }
            return this.SayConfig;
        }
        public void saveSayConfigFile() {
            if (SayConfig == null || SayConfigFile == null) {
                return;
            }
            try {
                getSayConfigFile().save(SayConfigFile);
            }
            catch (IOException ex) {
                p.getLogger().log(Level.SEVERE, pl.getConfig().getString("IMPOSSIVEL") + pl.getConfig().getString("ARQUIVO") + SayConfigFile.getName(), ex);
            }
        }
    }

    Erro:
    [​IMG]
     
  11. Code (Text):


      private FileConfiguration config;


      public ConfigManagerClassExample(JavaPlugin pl)
      {
        File configFile = new File(pl.getDataFolder(), "configfile.yml");
        if (!configFile.exists()) pl.saveResource("configfile.yml", false);
        config = YamlConfiguration.loadConfiguration(configFile);
      }
     
    This is how I do things. I really hate the default configuration api setup, and I think this is far easier..
     
    • Like Like x 1
  12. i know that the error is on the line 33 and if i delet this segment of the if clause the error go to the line
    SayConfigFile = new File(p.getDataFolder() + File.separator + "Minecraft" + File.separator + "Say", "say.yml");

    and the error is from qye p.getDataFolder() this is the only thick that i find out....