Custom Config Not Adding Information

Discussion in 'Plugin Development' started by BurnerDiamond, Jul 28, 2015.

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

    BurnerDiamond

    Code:
    public class KeyCommand implements CommandExecutor {
    
        private Core core;
    
        public KeyCommand(Core core) {
            this.core = core;
            this.keyManager = core.getKeyManager();
            this.keyConfig = core.getKeysFile().getConfig();
        }
    
        KeyManager keyManager = new KeyManager(core);
        List<Key> keyList = keyManager.getKeyList();
        Configuration keyConfig;
    
        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
    
            if (command.getName().equalsIgnoreCase("key")) {
    
                if (strings[0].equalsIgnoreCase("create")) {
    
                    if (strings.length >= 3) {
    
                        if (!keyExists(strings[1])) {
    
                            commandSender.sendMessage("It works");
    
                            if (isNumeric(strings[2])) {
    
                                commandSender.sendMessage("It works 1");
    
                                if(Material.getMaterial(Integer.parseInt(strings[2])) != null) {
                                    String name = strings[1];
                                    keyConfig.set(name + ".material", strings[2]);
                                    keyConfig.set(name + ".display", strings[1]);
                                    saveKeys();
                                    keyManager.loadKeys();
                                    commandSender.sendMessage(Language.KEY_CREATE.getConfigValue(new String[] {strings[1]}));
    
                                }
                                else {
    
                                    commandSender.sendMessage(Language.KEY_ITEM_NOT_EXIST.getConfigValue(new String[] {strings[2]}));
                                }
                            }
                            else {
    
                                commandSender.sendMessage("It works 2");
    
                                String materialTUC = strings[2].toUpperCase();
                                if(Material.getMaterial(materialTUC) != null) {
                                    commandSender.sendMessage("it works 3");
                                    String name = strings[1].replaceAll("_", " ");
                                    keyConfig.set(name + ".material", strings[2]);
                                    keyConfig.set(name + ".display", strings[1]);
                                    saveKeys();
                                    keyManager.loadKeys();
    
                                }
                                else {
                                    commandSender.sendMessage(Language.KEY_ITEM_NOT_EXIST.getConfigValue(new String[] {strings[2]}));
                                }
                            }
                        }
                        else {
                            commandSender.sendMessage(Language.KEY_EXISTS.getConfigValue(new String[] {strings[1]}));
                        }
                    }
                    else {
                        commandSender.sendMessage(Language.NOT_ENOUGH_ARGUMENTS.getConfigValue(null));
                    }
    
                }
            }
            return false;
        }
    It never adds anything to the config.

    Core class:

    Code:
    public class Core extends JavaPlugin {
    
        public KeyManager getKeyManager() {
            return keyManager;
        }
    
        private KeyManager keyManager;
    
        private ConfigurationWrapper languageFile = new ConfigurationWrapper(this, "", "language.yml");
    
        private ConfigurationWrapper keysFile = new ConfigurationWrapper(this, "", "keys.yml");
        private ConfigurationWrapper cratesFile = new ConfigurationWrapper(this, "", "crates.yml");
    
        public ConfigurationWrapper getKeysFile() {return keysFile;}
        public ConfigurationWrapper getCratesFile() {return cratesFile;}
    
        @Override
        public void onEnable() {
    
            KeyManager keyManager = new KeyManager(this);
            this.keyManager = keyManager;
    
            getCommand("key").setExecutor(new KeyCommand(this));
    
            languageFile.createNewFile("Loading Crates V2 language config", "Crates V2 Language Config");
            loadLanguage();
    
            keysFile.createNewFile("Loading Crates V2 key config", "Crates V2 Key Config");
            loadKeys();
    
            cratesFile.createNewFile("Loading Crates V2 crate config", "Crates V2 Crate Config");
            loadCrates();
        }
    
        @Override
        public void onDisable() {
    
            getConfig().options().copyDefaults(true);
            saveConfig();
    
            languageFile.getConfig().options().copyDefaults(true);
            languageFile.saveConfig();
    
            keysFile.getConfig().options().copyDefaults(true);
            keysFile.saveConfig();
    
        }
    
        private void loadLanguage() {
            Language.setFile(languageFile.getConfig());
    
            for (final Language value : Language.values()) {
                languageFile.getConfig().addDefault(value.getPath(),
                        value.getDefault());
            }
    
            languageFile.getConfig().options().copyDefaults(true);
            languageFile.saveConfig();
        }
    
        private void loadKeys() {
    
            keysFile.getConfig().options().copyDefaults(true);
            keysFile.saveConfig();
        }
    
        private void loadCrates() {
    
            cratesFile.getConfig().options().copyDefaults(true);
            cratesFile.saveConfig();
        }
    }
     
  2. Offline

    Ruptur

  3. Offline

    BurnerDiamond

    Code:
    public class ConfigurationWrapper {
    
    
        private final JavaPlugin plugin;
        private FileConfiguration config;
        private File configFile;
        private final String folderName, fileName;
    
        public ConfigurationWrapper(final JavaPlugin instance, final String folderName, final String fileName) {
            this.plugin = instance;
            this.folderName = folderName;
            this.fileName = fileName;
        }
    
        public void createNewFile(final String message, final String header) {
            reloadConfig();
            saveConfig();
            loadConfig(header);
    
            if (message != null) {
                plugin.getLogger().info(message);
            }
        }
    
        public FileConfiguration getConfig() {
            if (config == null) {
                reloadConfig();
            }
            return config;
        }
    
        public void loadConfig(final String header) {
            config.options().header(header);
            config.options().copyDefaults(true);
            saveConfig();
        }
    
        public void reloadConfig() {
            if (configFile == null) {
                configFile = new File(plugin.getDataFolder() + folderName, fileName);
            }
            config = YamlConfiguration.loadConfiguration(configFile);
        }
    
        public void saveConfig() {
            if (config == null || configFile == null) {
                return;
            }
            try {
                getConfig().save(configFile);
            } catch (final IOException ex) {
                plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
            }
        }
    }
    
     
  4. Offline

    Ruptur

    @BurnerDiamond
    Your file doesnt ever get created thats why
    Code:
    configFile = new File(plugin.getDataFolder() + folderName, fileName);
    
    When you intialise a new File object it doesnt actually create a physical file
    You have to create the file yourself with file.getCreateNewFile(). This method throw an IoException
    An example:
    Code:
    File file = new File("location.extention");
    
    try {
        file.createNewFile();
    catch(IoException ignored) {
        getLogger().log(Level.SEVERE, "Unable to create file at location: " + xxx)
    }
    
     
  5. Offline

    BurnerDiamond

    All created:

    http://imgur.com/R8FmSBi

    keysFile.createNewFile("Loading Crates V2 key config", "Crates V2 Key Config");

    is in the onEnable method.
     
    Last edited: Jul 28, 2015
  6. Offline

    BurnerDiamond

    I tested it

    BUMP!
     
  7. Offline

    Tecno_Wizard

    @BurnerDiamond, have you checked to make sure you are not saving clone data?
     
Thread Status:
Not open for further replies.

Share This Page