Solved Resource Problems

Discussion in 'Plugin Development' started by JoeyDevs, Mar 27, 2014.

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

    JoeyDevs

    Resource: http://forums.bukkit.org/threads/re...st-way-to-handle-multiple-config-files.194271

    Code:java
    1. at nl.joeydevs.realtokens.ConfigManager.load(ConfigManager.java:63) ~[?:


    ConfigManager.java
    Code:java
    1. package nl.joeydevs.realtokens;
    2. /*
    3. * The MIT License
    4. *
    5. * Copyright 2013 Goblom.
    6. *
    7. * Permission is hereby granted, free of charge, to any person obtaining a copy
    8. * of this software and associated documentation files (the "Software"), to deal
    9. * in the Software without restriction, including without limitation the rights
    10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11. * copies of the Software, and to permit persons to whom the Software is
    12. * furnished to do so, subject to the following conditions:
    13. *
    14. * The above copyright notice and this permission notice shall be included in
    15. * all copies or substantial portions of the Software.
    16. *
    17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23. * THE SOFTWARE.
    24. */
    25.  
    26. import java.io.File;
    27. import java.util.HashMap;
    28. import java.util.Map;
    29. import org.bukkit.configuration.file.FileConfiguration;
    30. import org.bukkit.configuration.file.YamlConfiguration;
    31. import org.bukkit.plugin.Plugin;
    32.  
    33. /**
    34. * Config Manager v1
    35. *
    36. * Easily Create, Load and get data from files in your plugins folder. Supports
    37. * only YAML configuration files, can hold as many configurations as you want.
    38. *
    39. * @author Goblom
    40. */
    41. public class ConfigManager {
    42.  
    43. public static Map<String, FileConfiguration> configs = new HashMap();
    44.  
    45. /**
    46. * Checks to see if the ConfigManager knows about fileName
    47. *
    48. * @param fileName file to check
    49. * @return true if file is loaded, false if not
    50. */
    51. public static boolean isFileLoaded(String fileName) {
    52. return configs.containsKey(fileName);
    53. }
    54.  
    55. /**
    56. * Loads a files configuration into Memory
    57. *
    58. * @param plugin Plugin to load file from if fileName does not exist in
    59. * Plugins folder
    60. * @param fileName File to load
    61. */
    62. public static void load(Plugin plugin, String fileName) {
    63. File file = new File(plugin.getDataFolder(), fileName);
    64. if (!file.exists()) {
    65. try {
    66. plugin.saveResource(fileName, false);
    67. } catch (Exception e) {
    68. e.printStackTrace();
    69. }
    70. }
    71. if (!isFileLoaded(fileName)) {
    72. configs.put(fileName, YamlConfiguration.loadConfiguration(file));
    73. }
    74. }
    75.  
    76. /**
    77. * Gets the FileConfiguration for a specified file
    78. *
    79. * @param fileName File to load data from
    80. * @return File Configuration
    81. */
    82. public static FileConfiguration get(String fileName) {
    83. if (isFileLoaded(fileName)) {
    84. return configs.get(fileName);
    85. }
    86. return null;
    87. }
    88.  
    89. /**
    90. * Updates the FileConfiguration at the given path. If path already exists
    91. * this will return false.
    92. *
    93. * @param fileName File to update
    94. * @param path Path to update
    95. * @param value Data to set at path
    96. * @return True if successful, otherwise false
    97. */
    98. public static boolean update(String fileName, String path, Object value) {
    99. if (isFileLoaded(fileName)) {
    100. if (!configs.get(fileName).contains(path)) {
    101. configs.get(fileName).set(path, value);
    102. return true;
    103. }
    104. }
    105. return false;
    106. }
    107.  
    108. /**
    109. * Sets data at any given path. If path already exists it will be over
    110. * written.
    111. *
    112. * @param fileName File to update
    113. * @param path Path to set
    114. * @param value Data to set at path
    115. */
    116. public static void set(String fileName, String path, Object value) {
    117. if (isFileLoaded(fileName)) {
    118. configs.get(fileName).set(path, value);
    119. }
    120. }
    121.  
    122. /**
    123. * Create YAML Comments at the given path
    124. *
    125. * @param fileName File to add comments to
    126. * @param path Path to add comments too
    127. * @param comments Comments to add
    128. *
    129. * @deprecated Not Tested/Experimental
    130. */
    131. public void addComment(String fileName, String path, String... comments) {
    132. if (isFileLoaded(fileName)) {
    133. for (String comment : comments) {
    134. if (!configs.get(fileName).contains(path)) {
    135. configs.get(fileName).set("_COMMENT_" + comments.length, " " + comment);
    136. }
    137. }
    138. }
    139. }
    140.  
    141. /**
    142. * Removes a path from the FileConfiguration.
    143. *
    144. * @param fileName File to update
    145. * @param path Path to remove
    146. */
    147. public static void remove(String fileName, String path) {
    148. if (isFileLoaded(fileName)) {
    149. configs.get(fileName).set(path, null);
    150. }
    151. }
    152.  
    153. /**
    154. * Checks if a file has a path.
    155. *
    156. * @param fileName File to check
    157. * @param path Path to check
    158. * @return True if path exists, otherwise false.
    159. */
    160. public static boolean contains(String fileName, String path) {
    161. if (isFileLoaded(fileName)) {
    162. return configs.get(fileName).contains(path);
    163. }
    164. return false;
    165. }
    166.  
    167. /**
    168. * Reload the config from the given Plugin.
    169. *
    170. * @param plugin Plugin to get the File from
    171. * @param fileName File to reload
    172. */
    173. public static void reload(Plugin plugin, String fileName) {
    174. File file = new File(plugin.getDataFolder(), fileName);
    175. if (isFileLoaded(fileName)) {
    176. try {
    177. configs.get(fileName).load(file);
    178. } catch (Exception e) {
    179. e.printStackTrace();
    180. }
    181. }
    182. }
    183.  
    184. /**
    185. * Save the config for the given plugin
    186. *
    187. * @param plugin Plugin dir to save to the file to
    188. * @param fileName File to save
    189. */
    190. public static void save(Plugin plugin, String fileName) {
    191. File file = new File(plugin.getDataFolder(), fileName);
    192. if (isFileLoaded(fileName)) {
    193. try {
    194. configs.get(fileName).save(file);
    195. } catch (Exception e) {
    196. e.printStackTrace();
    197. }
    198. }
    199. }
    200. }
    201.  


    RealTokens.java
    Code:java
    1.  
    2. public RealTokens plugin;
    3. //.....
    4. public void onEnable() {
    5. this.getServer().getConsoleSender().sendMessage(this.PPrefix + ChatColor.YELLOW + "This plugin is now: " + ChatColor.GREEN + "Enabled");
    6. this.getServer().getConsoleSender().sendMessage(this.PPrefix + ChatColor.YELLOW + "Plugin made by ~" + ChatColor.WHITE + "JoeyDevs");
    7. // ========= [ Load YML files ]
    8. ConfigManager.load(plugin, "players.yml");
    9. ConfigManager.get("players.yml").options().copyDefaults(true);
    10. ConfigManager.save(plugin, "players.yml");
    11. getConfig().options().copyDefaults(true);
    12. saveConfig();
    13. }


    This won't work I don't know what I'm doing wrong here so please help me. How t fix this?
     
  2. Offline

    L33m4n123

  3. Offline

    JoeyDevs


    This was very usefull -_-
     
  4. Offline

    L33m4n123

    I told you what the error was. Told you why it is so [you nowhere set the value of the plugin variable anywhere in your code]

    if you cannot work with that then I do not know what you want
     
  5. Offline

    JoeyDevs


    Sorry, thanks. I figure it out.

    I have placed the 'plugin' -> 'this' and now the error is gone
     
Thread Status:
Not open for further replies.

Share This Page