Enhanced Bukkit Config Class

Discussion in 'Resources' started by feildmaster, Aug 12, 2011.

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

    feildmaster Retired Staff

    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 (open)
    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 (open)
    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 (open)

    Code:java
    1. package yourpackagehere;
    2.  
    3. import java.io.File;
    4. import java.util.List;
    5. import java.util.Map;
    6. import org.bukkit.util.config.Configuration;
    7.  
    8. public class Config {
    9. private final Configuration config;
    10.  
    11. /* Setup Configuration Instances
    12.   * Either:
    13.   * - new Config("folder", "name"); // reads "file" in "folder"
    14.   * - new Config(file); // Passes the config file directly
    15.   * - new Config(config); // Passes the pre-made config
    16.   */
    17. public Config(String folder, String name) {
    18. config = new Configuration(new File(folder, name));
    19. config.load();
    20. }
    21. public Config(File file) {
    22. config = new Configuration(file);
    23. config.load();
    24. }
    25. public Config (Configuration config) {
    26. this.config = config;
    27. this.config.load();
    28. }
    29.  
    30. // Function for people that like editing directly
    31. public Configuration getConfig() {
    32. return config;
    33. }
    34.  
    35. // Header Functions (Adds in Comment Hashes)
    36. public void setHeader(String header) {
    37. config.setHeader((!header.startsWith("#")?"# ":"")+header);
    38. }
    39. public void setHeader(String... headerlines) {
    40. StringBuilder header = new StringBuilder();
    41.  
    42. for(String line : headerlines)
    43. header.append(!line.startsWith("#")?"# ":"").append(line).append(header.length()>0?"\r\n":"");
    44.  
    45. setHeader(header.toString());
    46. }
    47.  
    48. // Remove a property
    49. public void removeProperty(String path) {
    50. config.removeProperty(path);
    51. }
    52.  
    53. // Basic Get Functions
    54. public Object getProperty(String path) {
    55. return config.getProperty(path);
    56. }
    57. public List<Object> getList(String path) {
    58. return config.getList(path);
    59. }
    60. public Map<String, Object> getAll() {
    61. return config.getAll();
    62. }
    63.  
    64. // Basic Set Function
    65. public void setProperty(String path, Object val) {
    66. config.setProperty(path, val);
    67. }
    68. // Set and Save Property Function
    69. public void saveProperty(String path, Object val) {
    70. addProperty(path, val);
    71. config.save();
    72. }
    73.  
    74. // The following functions add the property if they don't exist
    75. public Object getProperty(String path, Object def) {
    76. if(isNull(path))
    77. return addProperty(path, def);
    78. return config.getProperty(path);
    79. }
    80. public Integer getInt(String path, Integer def) {
    81. if(isNull(path))
    82. return (int) addProperty(path, def);
    83. return config.getInt(path, def);
    84. }
    85. public Boolean getBoolean(String path, Boolean def) {
    86. if(isNull(path))
    87. return (Boolean) addProperty(path, def);
    88. return config.getBoolean(path, def);
    89. }
    90. public List<Object> getList(String path, List<Object> def) {
    91. if(isNull(path))
    92. return (List<Object>)addProperty(path, def);
    93. return config.getList(path);
    94. }
    95.  
    96. // Reload/Save functions
    97. public void reload() {
    98. config.load();
    99. Map<String, Object> map = config.getAll();
    100. for(String node : map.keySet()) {
    101. config.setProperty(node, map.get(node));
    102. }
    103. config.save();
    104. }
    105. public void save() {
    106. config.save();
    107. }
    108.  
    109. // Private Functions
    110. private Object addProperty(String path, Object val) {
    111. config.setProperty(path, val);
    112. return val;
    113. }
    114. private Boolean isNull(String path) {
    115. return config.getProperty(path) == null;
    116. }
    117. }
    118.  

    You still have to save, but if you really wanted to you could change addProperty to this:
    Code:java
    1. private Object addProperty(String path, Object val) {
    2. config.setProperty(path, val);
    3. config.save();
    4. return val;
    5. }
    6.  



    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.
     
    Last edited by a moderator: May 18, 2016
  2. Offline

    jzsig

    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 :p
     
  3. Offline

    tips48

    How do you comment? :O
     
  4. Offline

    feildmaster Retired Staff

    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
     
Thread Status:
Not open for further replies.

Share This Page