Creating a Lang file issues

Discussion in 'Spigot Plugin Development' started by Cosmicluck, May 28, 2016.

  1. So I am trying to make a language file for a plugin of mine, but I am having an issue. The file is being created, but...

    Basically, nothing is getting added to the config, no paths or strings. I can't see what i'm doing wrong and was hoping someone could help me out!

    Creating the language config:
    Code (Text):
    package com.cosmicluck.inventorytools;

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;

    public class LangConfig {

        private final Plugin PLUGIN;
        private final String FILENAME;
        private final File FOLDER;
        private FileConfiguration config;
        private File configFile;

        public LangConfig(String filename, Main instance) {
            if (!filename.endsWith(".yml")) {
                filename += ".yml";
            }
            FILENAME = filename;
            PLUGIN = instance;
            FOLDER = PLUGIN.getDataFolder();
            config = null;
            configFile = null;
            reload();
        }

        public FileConfiguration getConfig() {
            if (config == null) {
                reload();
            }
            return config;
        }

        public void reload() {
            if (!this.FOLDER.exists()) {
                try {
                    if (FOLDER.mkdir()) {
                        PLUGIN.getLogger().log(Level.INFO, "Folder " + FOLDER.getName() + " created.");
                    } else {
                        PLUGIN.getLogger().log(Level.WARNING, "Unable to create folder " + FOLDER.getName() + ".");
                    }
                } catch (Exception e) {

                }
            }
            configFile = new File(FOLDER, FILENAME);
            if (!configFile.exists()) {
                try {
                    configFile.createNewFile();
                } catch (IOException e) {

                }
            }
            config = YamlConfiguration.loadConfiguration(configFile);
        }

        public void saveDefaultConfig() {
            if (configFile == null) {
                configFile = new File(PLUGIN.getDataFolder(), FILENAME);
            }
            if (!configFile.exists()) {
                PLUGIN.saveResource(FILENAME, false);
            }
        }

        public void makeConfig() {
            Lang.setFile(config);
            for (Lang item : Lang.values()) {
                if (config.getString(item.getPath()) == null) {
                    config.set(item.getPath(), item.getDefault());
                }
            }
        }

        public void save() {
            if (config == null || configFile == null) {
                return;
            }
            try {
                getConfig().save(configFile);
            } catch (IOException ex) {
                PLUGIN.getLogger().log(Level.WARNING, "Could not save config to " + configFile.getName(), ex);
            }
        }

        public void set(String path, Object o) {
            getConfig().set(path, o);
        }
    }
     
    Language enum:
    Code (Text):
    package com.cosmicluck.inventorytools;

    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;

    public enum Lang {
        TITLE("path-one", "&3This is my title"),
        TWO("path-two", "&cTest two");

        private String path;
        private String def;
        private static FileConfiguration LANG;
        Lang(String path, String start) {
            this.path = path;
            this.def = start;
        }
        public static void setFile(FileConfiguration config) {
            LANG = config;
        }
        @Override
        public String toString() {
            if (this == TITLE)
                return ChatColor.translateAlternateColorCodes('&', LANG.getString(path, def)) + " ";
            return ChatColor.translateAlternateColorCodes('&', LANG.getString(path, def));
        }
        public String getDefault() {
            return this.def;
        }
        public String getPath() {
            return this.path;
        }
    }
     
    NOTE: I have replaced the enums with fake ones or the sake of publicly showing code.

    If you need any more information, let me know!
     
  2. Ehm
    saveResource("Lang.yml",true)?
    Or you could as I would, and Essentials and such does, properties/XML in classpath?