YACT - Yet Another Configuration Tutorial

Discussion in 'Resources' started by iffa, Oct 29, 2011.

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

    iffa

    Introduction
    First I want to say that never ever use BBCode with Google Chrome, at least on a Finnish keyboard layout. It enters an awesome arabian text mode that I don't want to use. Now to the tutorial, this is pretty much a copy-pasta tutorial, but I'll have some text in here too.

    Your config-class
    Code:java
    1. // Package Declaration
    2. package my.plugin.here.config;
    3.  
    4. // Java Imports
    5. import java.io.File;
    6. import java.io.FileNotFoundException;
    7. import java.io.FileOutputStream;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10. import java.util.EnumMap;
    11. import java.util.Map;
    12.  
    13. // Bukkit Imports
    14. import org.bukkit.Bukkit;
    15. import org.bukkit.configuration.InvalidConfigurationException;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17.  
    18. /**
    19.  * A class that handles all configuration files.
    20.  *
    21.  * @author iffa
    22.  * @author Pandarr
    23.  * @author Sammy
    24.  * @author kitskub
    25.  */
    26. public class MyConfig {
    27. // Variables
    28. private static Map<ConfigFile, YamlConfiguration> config = new EnumMap<ConfigFile, YamlConfiguration>(ConfigFile.class);
    29. private static Map<ConfigFile, File> configFile = new EnumMap<ConfigFile, File>(ConfigFile.class);
    30. private static Map<ConfigFile, Boolean> loaded = new EnumMap<ConfigFile, Boolean>(ConfigFile.class);
    31.  
    32. /**
    33.   * Gets the configuration file.
    34.   *
    35.   * @param configfile ConfigFile to get
    36.   *
    37.   * @return YamlConfiguration object
    38.   */
    39. public static YamlConfiguration getConfig(ConfigFile configfile) {
    40. if (loaded.get(configfile) == null || !loaded.get(configfile)) {
    41. loadConfig(configfile);
    42. }
    43. return config.get(configfile);
    44. }
    45.  
    46. /**
    47.   * Gets the configuration file.
    48.   *
    49.   * @param configfile ConfigFile to get
    50.   *
    51.   * @return Configuration file
    52.   */
    53. public static File getConfigFile(ConfigFile configfile) {
    54. return configFile.get(configfile);
    55. }
    56.  
    57. /**
    58.   * Checks if the configuration file is loaded.
    59.   *
    60.   * @param configfile ConfigFile to get
    61.   *
    62.   * @return True if configuraton file is loaded
    63.   */
    64. public static boolean getLoaded(ConfigFile configfile) {
    65. return loaded.get(configfile);
    66. }
    67.  
    68. /**
    69.   * Loads all configuration files. (can be used to save a total of 2 lines!)
    70.   */
    71. public static void loadConfigs() {
    72. for (ConfigFile configfile : ConfigFile.values()) {
    73. loadConfig(configfile);
    74. }
    75. }
    76.  
    77. /**
    78.   * Loads the configuration file from the .jar.
    79.   *
    80.   * @param configfile ConfigFile to load
    81.   */
    82. public static void loadConfig(ConfigFile configfile) {
    83. configFile.put(configfile, new File(Bukkit.getServer().getPluginManager().getPlugin("MyPlugin").getDataFolder(), configfile.getFile()));
    84. if (configFile.get(configfile).exists()) {
    85. config.put(configfile, new YamlConfiguration());
    86. try {
    87. config.get(configfile).load(configFile.get(configfile));
    88. } catch (FileNotFoundException ex) {
    89. //SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    90. loaded.put(configfile, false);
    91. return;
    92. } catch (IOException ex) {
    93. //SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    94. loaded.put(configfile, false);
    95. return;
    96. } catch (InvalidConfigurationException ex) {
    97. //SpaceMessageHandler.print(Level.WARNING, ex.getMessage());
    98. loaded.put(configfile, false);
    99. return;
    100. }
    101. loaded.put(configfile, true);
    102. } else {
    103. try {
    104. Bukkit.getServer().getPluginManager().getPlugin("MyPlugin").getDataFolder().mkdir();
    105. InputStream jarURL = MyConfig.class.getResourceAsStream("/" + configfile.getFile());
    106. copyFile(jarURL, configFile.get(configfile));
    107. config.put(configfile, new YamlConfiguration());
    108. config.get(configfile).load(configFile.get(configfile));
    109. loaded.put(configfile, true);
    110. //SpaceMessageHandler.print(Level.INFO, "Generated " + configfile.getFile() + " file");
    111. } catch (Exception e) {
    112. //SpaceMessageHandler.print(Level.SEVERE, e.toString());
    113. }
    114. }
    115. }
    116.  
    117. /**
    118.   * Copies a file to a new location.
    119.   *
    120.   * @param in InputStream
    121.   * @param out File
    122.   *
    123.   * @throws Exception
    124.   */
    125. static private void copyFile(InputStream in, File out) throws Exception {
    126. InputStream fis = in;
    127. try {
    128. byte[] buf = new byte[1024];
    129. int i = 0;
    130. while ((i = fis.read(buf)) != -1) {
    131. fos.write(buf, 0, i);
    132. }
    133. } catch (Exception e) {
    134. throw e;
    135. } finally {
    136. if (fis != null) {
    137. fis.close();
    138. }
    139. if (fos != null) {
    140. fos.close();
    141. }
    142. }
    143. }
    144.  
    145. /**
    146.   * Constructor of MyConfig.
    147.   */
    148. private MyConfig() {
    149. }
    150.  
    151. /**
    152.   * All config files.
    153.   */
    154. public enum ConfigFile {
    155. // Enums
    156. CONFIG("config.yml"),
    157. SOMETHING("another.yml"),
    158. YADDAYADDA("rofl.yml"),
    159. CUPCAKES("cake.yml");
    160. // Variables
    161. private String file;
    162.  
    163. /**
    164.   * Constructor of ConfigFile.
    165.   *
    166.   * @param file File name
    167.   */
    168. ConfigFile(String file) {
    169. this.file = file;
    170. }
    171.  
    172. /**
    173.   * Gets the file associated with the enum.
    174.   *
    175.   * @return File associated wiht the enum
    176.   */
    177. public String getFile() {
    178. return this.file;
    179. }
    180. }
    181. }

    As you can see, the ConfigFile-enum contains all configuration files of your plugin. This allows easy adding:
    • Add the configuration file to your .jar (like you would normally do)
    • Add the configuration file to the enum and include the name of the file
    See, it's that easy. Now if you want to handle them, it's going to take a little extra effort. If you make changes to your configuration files, you can just do
    Code:java
    1. try {
    2. MyConfig.getConfig(ConfigFile.CUPCAKES).save(MyConfig.getConfigFile(ConfigFile.CUPCAKES));
    3. } catch (IOException ex) {
    4. Logger.getLogger(MyConfigHandler.class.getName()).log(Level.SEVERE, null, ex);
    5. }

    And to load all configs on startup, you can just call MyConfig.loadConfigs()!
    Your config handler-class
    Alright, so we have the class that handles creating and loading configuration files, but we need to make a class to handle these configuration files (methods to get values easily etc):
    Code:java
    1. // Package Declaration
    2. package my.plugin.here.config;
    3.  
    4. // MyPlugin Imports
    5. import my.plugin.here.config.MyConfig.ConfigFile;
    6.  
    7. /**
    8.  * Yadda yadda useful methods here.
    9.  *
    10.  * @author iffa
    11.  */
    12. public class MyConfigHandler {
    13. /**
    14.   * Gets 'some value' from cupcakes.yml.
    15.   *
    16.   * @return True if cupcakes are delicious
    17.   */
    18. public static boolean getSomeValue() {
    19. return MyConfig.getConfig(ConfigFile.CUPCAKES).getBoolean("are.cupcakes.delicious");
    20. }
    21.  
    22. // More methods here VV
    23.  
    24. /**
    25.   * Constructor of MyConfigHandler.
    26.   */
    27. private MyConfigHandler() {
    28. }
    29. }


    If you has problems, I'll try to helps. You're grammar errores are annoy though!

    [Mod edit (TnT)] Cleaned up language.
     
  2. Offline

    Kohle

    Excuse me if I am the most retarded person in the world, (I may just be tired) but where would I put
    Code:Java
    1.  
    2. try {
    3. MyConfig.getConfig(ConfigFile.CUPCAKES).save(MyConfig.getConfigFile(ConfigFile.CUPCAKES));
    4. } catch (IOException ex) {
    5. Logger.getLogger(MyConfigHandler.class.getName()).log(Level.SEVERE, null, ex);
    6. }
     
  3. Offline

    iffa

    Errm, right. If you set config file values you can use that to save the config file.
     
Thread Status:
Not open for further replies.

Share This Page