I have a few questions about generating config files. Is it possible to create more than one yml file? If so, how can I create a yml file which has its name defined in a String var? Thanks, also, I am quite a noob with this, only started a week ago but I'm learning fast
File configFile = new File(getDataFolder(), "fileName.yml"); FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
Thank-you! What about creating a file with a name stored in a var? EDIT: Didn't seem to work for me. I put that exact code into a command and ran it. No file was made. I must be missing something obvious...
Put that in your main class: Code: private static final String CONFIG_NAME = "myconfig.yml"; private FileConfiguration config; private File configFile; public void reloadConfig() { if(configFile == null) { configFile = new File(getDataFolder(), CONFIG_NAME); } config = YamlConfiguration.loadConfiguration(configFile); } public FileConfiguration getConfig() { if(config == null) { this.reloadConfig(); } return config; } public void saveConfig() { if(config == null || configFile == null) { return; } try { getConfig().save(configFile); } catch(IOException e) { getLogger().log(Level.SEVERE, "Could not save config to " + configFile, e); } } Now you can use the custom config as the normal config with getConfig(), reloadConfig() and saveConfig()
YES! Thank-you so much! Also, how can I setup the default stuff. The tutorials all say to do this: Code:java if(!.exists()) { cfg.addDefault("config.baserep", 1); cfg.options().copyDefaults(true); saveConfig(); } But your method is a bit different?
Ok, I did this and also changed the cfg to config. But the only error left is on .exists - its asking me if I want to make a method named exists() But if I removed the whole if line its fine...
Brilliant. Any idea how to check if a file with that players name already exists and cancel the file generation? Code:java if file exists { cancel creation tellplayer(You are already activated)}
Assuming the file name is the name of the player.... Player p = // Your player; for (File f : getDataFolder().listFiles()) { if (f.getName().equalsIgnoreCase(p.getName()) { p.sendMessage("You are activated!"); } }
Hmm, fine in terms of java... internal error in bukkit. I'm sure I must have implemented it wrong. Code:java String name = sender.getName(); sender.sendMessage(ChatColor.RED + "You are set-up for reputation!"); String fileName = name + ".yml"; Player p = null; for (File f : getDataFolder().listFiles()) { if (f.getName().equalsIgnoreCase(p.getName())) { p.sendMessage("You are already activated with reputation!"); } } File configFile = new File(getDataFolder(), fileName); FileConfiguration config = YamlConfiguration.loadConfiguration(configFile); try { if(configFile.exists()) { config.addDefault("config.rep", 0); config.options().copyDefaults(true); config.save(configFile); } config.save(configFile); } catch (IOException e) { e.printStackTrace(); }
Because p is null and you are trying to execute a method on a null object, thus you got an NPE. Assign Player p to sender, after running an instanceof Player and casting it properly.
Ok. More errors. First Run of command: Fine, config file made but nothing in it! Get message: You are set-up for reputation! As expected. Second run of command: The contents is added to the config file. Get message: You are set-up for reputation! Third run of command: Nothing changes. Get message: You are set-up for reputation!
Here is a few points that need to be understood. 1. The object in memory is a copy of the file on disk when the object was created. 2. Changes to the object in memory are not reflected in the file unless saved. 3. Saves are manual.