Resource Custom YamlConfig - Code

Discussion in 'Spigot Plugin Development' started by alexhackers, Jun 5, 2017.

Thread Status:
Not open for further replies.
  1. removed
     
    #1 alexhackers, Jun 5, 2017
    Last edited: Oct 16, 2017
  2. PLEASE do not do that :eek:

    That is a perfect example for the opposite of "lightweight".
     
    • Agree Agree x 2
  3. Just fix the imports, sorry, thanks for remember
     
  4. This is what I'd use, and do use occasionally.
    Code (Text):
    public class FileHandler {
        private FileConfiguration data;
        private File dfile;
        private String pluginName;

        private Server server = Bukkit.getServer();
        private ConsoleCommandSender console = server.getConsoleSender();

        private void c(String s) {
            console.sendMessage(s);
        }

        public FileHandler(String pluginName, String fileName) {
            this.pluginName = pluginName;
            String title = fileName.toLowerCase();
            this.dfile = new File("plugins/" + pluginName, title + ".yml");
            if (!this.dfile.exists()) {
                try {
                    this.dfile.createNewFile();
                } catch (IOException e) {
                    c(ChatColor.RED + pluginName + " >> Setup file exception!");
                }
            }
            c(ChatColor.GREEN + pluginName + " >> Loading " + title + ".yml!");
            this.data = YamlConfiguration.loadConfiguration(this.dfile);
            c(ChatColor.GREEN + pluginName + " >> " + title + ".yml loaded!");
            saveData();
        }

        public FileConfiguration getData() {
            return this.data;
        }

        public void saveData() {
            try {
                if (this.dfile == null || this.data == null) {
                    return;
                }
                this.data.save(this.dfile);
            } catch (IOException e) {
                c(ChatColor.RED + pluginName + " >> Save file exception!");
            }
        }

        public void reloadData() {
            this.data = YamlConfiguration.loadConfiguration(this.dfile);
        }
     
     
  5. Over complicated.

    Code (Text):
    public class MyConfig extends YamlCo figuration {
        private final File file;

        public MyConfig(JavaPlugin plugin, String filename) {
            this.file = new File(plugin.getDataFolder(), filename);
            reload();
        }

        public void reload() {
            try {
                load(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void save() {
            try {
                save(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    Example usage:
    Code (Text):
    public void onEnable() {
        MyConfig config = new MyConfig(this, "somefile.yml");
        String str = config.getString("somepath");
        str = "blah";
        config.set("somepath", str);
        config.save();
    }
     
    • Winner Winner x 2
    • Agree Agree x 1
  6. Completely agree, but I like to print messages to console. lol
     
  7. What's wrong with the default spigot one? Why would we need a custom yaml code that seems to have the same methods?
     
  8. Making custom configs makes you not have to deal with

    [​IMG]

    every single time.
     
  9. Every single time? Don't you only need one of those for every instance of a YAML file you want?
     
Thread Status:
Not open for further replies.