Many people make custom config files. To be honest though, I don't think anything is as efficient as this: Featuring: - Auto hash (#) headers! (using shortcut function)- Enhanced Reload!- Shortcut function for saving- You wondering why you're reading this resource! Config.java (Move your mouse to reveal the content) Config.java (open) Config.java (close) Code: package your.package.here; import java.io.File; import java.util.Map; import org.bukkit.util.config.Configuration; public class Config { private final Configuration config; /* Setup Configuration Instances * Either: * - new Config("path"); // Creates file for config * - new Config("path", "name"); // Creates file for config * - new Config(file); // Pass the file for config directly * - new Config(config); // Pass the config directly */ public Config(String path) { config = new Configuration(new File(path)); config.load(); } public Config(String path, String name) { config = new Configuration(new File(path, name)); config.load(); } public Config(File file) { config = new Configuration(file); config.load(); } public Config (Configuration config) { this.config = config; this.config.load(); } // Use getConfig to edit config public Configuration getConfig() { return config; } // Header Functions (Adds in Comment Hashes) public void setHeader(String header) { config.setHeader((!header.startsWith("#")?"# ":"")+header); } public void setHeader(String... headerlines) { StringBuilder header = new StringBuilder(); for(String line : headerlines) header.append(!line.startsWith("#")?"# ":"").append(line).append(header.length()>0?"\r\n":""); setHeader(header.toString()); } // Reload/Save functions public void reload() { config.load(); Map<String, Object> map = config.getAll(); for(String node : map.keySet()) { config.setProperty(node, map.get(node)); } config.save(); } public void save() { config.save(); } } Click the spoiler for some laughs... maybe. Reinventing the wheel (Move your mouse to reveal the content) Reinventing the wheel (open) Reinventing the wheel (close) I got tired of running so many config functions that did the same thing, adding unnecessary lines of code. So I played around for about 20 minutes to create this little beauty that I really enjoy using (personally). The enhanced functions don't include all the basic functions, it simply uses the function I see used most commonly. You can just follow the normal syntax of code to add your own. If you REALLY want to use the normal functions you can use getConfig() to modify the config normally. Config.java (Move your mouse to reveal the content) Config.java (open) Config.java (close) Code:java package yourpackagehere; import java.io.File;import java.util.List;import java.util.Map;import org.bukkit.util.config.Configuration; public class Config { private final Configuration config; /* Setup Configuration Instances * Either: * - new Config("folder", "name"); // reads "file" in "folder" * - new Config(file); // Passes the config file directly * - new Config(config); // Passes the pre-made config */ public Config(String folder, String name) { config = new Configuration(new File(folder, name)); config.load(); } public Config(File file) { config = new Configuration(file); config.load(); } public Config (Configuration config) { this.config = config; this.config.load(); } // Function for people that like editing directly public Configuration getConfig() { return config; } // Header Functions (Adds in Comment Hashes) public void setHeader(String header) { config.setHeader((!header.startsWith("#")?"# ":"")+header); } public void setHeader(String... headerlines) { StringBuilder header = new StringBuilder(); for(String line : headerlines) header.append(!line.startsWith("#")?"# ":"").append(line).append(header.length()>0?"\r\n":""); setHeader(header.toString()); } // Remove a property public void removeProperty(String path) { config.removeProperty(path); } // Basic Get Functions public Object getProperty(String path) { return config.getProperty(path); } public List<Object> getList(String path) { return config.getList(path); } public Map<String, Object> getAll() { return config.getAll(); } // Basic Set Function public void setProperty(String path, Object val) { config.setProperty(path, val); } // Set and Save Property Function public void saveProperty(String path, Object val) { addProperty(path, val); config.save(); } // The following functions add the property if they don't exist public Object getProperty(String path, Object def) { if(isNull(path)) return addProperty(path, def); return config.getProperty(path); } public Integer getInt(String path, Integer def) { if(isNull(path)) return (int) addProperty(path, def); return config.getInt(path, def); } public Boolean getBoolean(String path, Boolean def) { if(isNull(path)) return (Boolean) addProperty(path, def); return config.getBoolean(path, def); } public List<Object> getList(String path, List<Object> def) { if(isNull(path)) return (List<Object>)addProperty(path, def); return config.getList(path); } // Reload/Save functions public void reload() { config.load(); Map<String, Object> map = config.getAll(); for(String node : map.keySet()) { config.setProperty(node, map.get(node)); } config.save(); } public void save() { config.save(); } // Private Functions private Object addProperty(String path, Object val) { config.setProperty(path, val); return val; } private Boolean isNull(String path) { return config.getProperty(path) == null; }} You still have to save, but if you really wanted to you could change addProperty to this: Code:java private Object addProperty(String path, Object val) { config.setProperty(path, val); config.save(); return val; } You know. I didn't realize bukkit did most of this automatically. There goes me for thinking it didn't. =P *Edits most of the code out* I like this layout of code better though. EDIT: Know what, to hell with it. Let this topic forever remain to remind me of the day I reinvented the wheel. Found a bug on reload. Also, if anyone wants a SIMPLE reload function use something like so: Code: // Reload/Save functions public void reload() { config.load(); Map<String, Object> map = config.getAll(); for(String node : map.keySet()) { config.setProperty(node, map.get(node)); } config.save(); } I'm actually wondering if I should make an extension of Configuration, and include various functions... 'Cuz this resource IS silly. But making that would actually be USEFUL. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
hmm sorry but im new and im wondering what you use to call a paramater from a config file EX punishment: ban how would code my plugin to look for what the punishment was? thanks a bunch and sorry for noob question
Ah.. That was my mistake. I should have said "AutoHash Headers" =P You would have to structure the config in a way that it would be easy to find. normally you'd have the path as something like... config.setProperty("punishments."+player.getName(), "value"); that's a basic example, but i don't know what you're trying to do