Solved Config resets after reload

Discussion in 'Plugin Development' started by PreFiXAUT, May 24, 2014.

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

    PreFiXAUT

    Hi Guys, I got a question to Configs. Even when it's a basic thing, I never understood the thing with the Configs.
    Currently I have the issue, that when I'm changing something in the Config and reload the server, it will set the Config-File to the Default-Config again. I have no Idea why, but it's doing it >_>
    I watched several Tutorials to check if I'm doing something wrong, but I do the same as all others too, and for them it's working -.-

    Source:
    Show Spoiler

    Code:java
    1. @Override
    2. public void onEnable() {
    3. logger = getLogger();
    4. checkDatabaseFiles();
    5. getConfig().options().copyHeader(true);
    6. getConfig().options().copyDefaults(true);
    7. saveConfig();



    I hope someone can help me, thanks in advance.
     
  2. Offline

    JasonDL13

    try using saveDefaultConfig() on the onEnable and saveConfig on the onDisable
     
  3. Offline

    PreFiXAUT

    Also tried this already, but then it's just adding the Header and not more.
     
  4. Offline

    AoH_Ruthless

  5. Offline

    PreFiXAUT

    AoH_Ruthless
    xD if you want so ...
    Show Spoiler

    Code:java
    1. package net.prefixaut.lobbys;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileReader;
    6. import java.lang.reflect.Method;
    7. import java.util.ArrayList;
    8. import java.util.Arrays;
    9. import java.util.HashMap;
    10. import java.util.UUID;
    11. import java.util.logging.Level;
    12. import java.util.logging.Logger;
    13.  
    14. import net.prefixaut.lobbys.commands.ConfigCommand;
    15. import net.prefixaut.lobbys.commands.ConfigTabCompleter;
    16. import net.prefixaut.lobbys.commands.LPremiumCommand;
    17. import net.prefixaut.lobbys.commands.LPremiumTabCompleter;
    18. import net.prefixaut.lobbys.commands.LeaveCommand;
    19. import net.prefixaut.lobbys.commands.LobbysCommand;
    20. import net.prefixaut.lobbys.commands.LobbysTabCompleter;
    21. import net.prefixaut.lobbys.commands.NickCommand;
    22. import net.prefixaut.lobbys.listeners.CustomEventListener;
    23. import net.prefixaut.lobbys.listeners.MainListener;
    24. import net.prefixaut.lobbys.listeners.TagAPIListener;
    25. import net.prefixaut.lobbys.tasks.FiveMinTask;
    26. import net.prefixaut.lobbys.tasks.SignUpdaterTask;
    27. import net.prefixaut.lobbys.utilitys.Updater;
    28.  
    29. import org.bukkit.Bukkit;
    30. import org.bukkit.configuration.file.FileConfiguration;
    31. import org.bukkit.configuration.file.YamlConfiguration;
    32. import org.bukkit.permissions.Permission;
    33. import org.bukkit.plugin.PluginManager;
    34. import org.bukkit.plugin.java.JavaPlugin;
    35. import org.bukkit.scheduler.BukkitTask;
    36.  
    37. /**
    38. * The "Main" Class of Lobbys. It's Basically the Main-Class for Bukkit, which registers all Files, Commands, Listeners'n'Stuff.
    39. * @author PreFiXAUT
    40. *
    41. */
    42. public class Main extends JavaPlugin {
    43.  
    44. public static String name = "Lobbys";
    45. public static String version = "1.1.0";
    46. public static String status = "RELEASE";
    47. public static String complete = name + " v" + version + "-" + status;
    48.  
    49. // .ppdb - Prefix Plugin DataBase
    50. // Main Files
    51. public static File db_lobbydata = new File("plugins/Lobbys","lobbydata.ppdb");
    52. public static File db_lobbydestination = new File("plugins/Lobbys","lobbydestionation.ppdb");
    53. public static File db_lobbyreturn = new File("plugins/Lobbys","lobbyreturn.ppdb");
    54. public static File db_lobbyplayers = new File("plugins/Lobbys","lobbyplayers.ppdb");
    55. public static File db_premiumplayers = new File("plugins/Lobbys","premiumplayers.ppdb");
    56. // Secoundary Files
    57. public static File nicknames = new File("plugins/Lobbys","nicknames.txt");
    58.  
    59. // Config and Logger
    60. public static File config_file = new File("plugins/Lobbys","config.yml");
    61. public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(config_file);
    62. private static Logger logger;
    63. // Checking if maximum output is enabled.
    64. private static boolean maxmsg() {
    65. if (cfg == null) return false;
    66. if (cfg.get("debug-mode") != null) return cfg.getBoolean("max-messages");
    67. else return false;
    68. }
    69. // Tasks
    70. public static BukkitTask update;
    71. public static BukkitTask fivemintask;
    72. // Permissions
    73. public static Permission per_help = new Permission("lobbys.help");
    74. public static Permission per_add = new Permission("lobbys.add");
    75. public static Permission per_info = new Permission("lobbys.info");
    76. public static Permission per_update = new Permission("lobbys.update");
    77. public static Permission per_close = new Permission("lobbys.close");
    78. public static Permission per_open = new Permission("lobbys.open");
    79. public static Permission per_remove = new Permission("lobbys.remove");
    80. public static Permission per_join = new Permission("lobbys.join");
    81. public static Permission per_change = new Permission("lobbys.change");
    82. public static Permission per_all = new Permission("lobbys.all");
    83. public static Permission per_config = new Permission("lobbys.config");
    84. public static Permission per_premium_add = new Permission("lobbys.premium.add");
    85. public static Permission per_premium_remove = new Permission("lobbys.premium.remove");
    86. public static Permission per_premium_edit = new Permission("lobbys.premium.edit");
    87. public static Permission per_premium_get = new Permission("lobbys.premium.get");
    88. public static Permission per_premium_join_kick = new Permission("lobbys.premium.kick");
    89. public static Permission per_premium_join_slots = new Permission("lobbys.premium.slots");
    90. public static Permission per_premium_join_prmlob = new Permission("lobbys.premium.premiumlobbys");
    91. public static Permission per_nick = new Permission("lobbys.nick");
    92. public static ArrayList<Permission> permissions = new ArrayList<Permission>(Arrays.asList(per_help, per_add, per_info, per_update, per_close, per_open, per_remove,
    93. per_join, per_change, per_all, per_config, per_premium_join_kick, per_premium_join_slots, per_premium_join_prmlob, per_nick, per_premium_add, per_premium_remove, per_premium_edit, per_premium_get));
    94. // TagAPI Nametags
    95. public static HashMap<UUID, String> playerTags = new HashMap<UUID,String>();
    96.  
    97. /* TO DO LIST:
    98. * [X] ADD: Custom Events
    99. * [X] ADD: MySQL Premium Acc
    100. * [X] ADD: Premium Feature
    101. * [X] ADD: Permissions compatibility
    102. * [X] ADD: BungeeCord implementation
    103. * [X] ADD: Permium Settings
    104. * [X] ADD: Maximum-Messages for Debuging.
    105. * [X] ADD: Tab-Completer
    106. * [X] FIX: New Databases
    107. * [X] FIX: '/nick' Command
    108. * [X] FIX: Updater (Versions-Update Versions)
    109. * [X] FIX: Updater (Plugin-Updater Class)
    110. * [X] FIX: Config-Reset Bug
    111. * [X] RMV: Hide Command'n'stuff (useless)
    112. */
    113.  
    114. /**
    115. * Checks if all Databases/Files exist
    116. */
    117. public static void checkDatabaseFiles()
    118. {
    119. if (maxmsg()) printMessage("Checking Databases ...");
    120. try {
    121. int temp = 0;
    122. if (LobbysHelper.checkFile(config_file)) temp++;
    123. if (LobbysHelper.checkFile(db_lobbydata)) temp++;
    124. if (LobbysHelper.checkFile(db_lobbydestination)) temp++;
    125. if (LobbysHelper.checkFile(db_lobbyreturn)) temp++;
    126. if (LobbysHelper.checkFile(db_lobbyplayers)) temp++;
    127. if (LobbysHelper.checkFile(nicknames)) temp++;
    128. if (LobbysHelper.checkFile(db_premiumplayers)) temp++;
    129. if (temp > 0) printMessage("" + temp + " new Databases have been created!");
    130. if (maxmsg()) printMessage("Checking Databases done!");
    131. } catch (Exception e) {
    132. }
    133. }
    134.  
    135. public void checkConfig() {
    136. try {
    137. BufferedReader br = new BufferedReader(new FileReader(config_file));
    138. String read;
    139. boolean v110 = false;
    140. while ((read = br.readLine()) != null) {
    141. if (!read.trim().startsWith("#")) continue;
    142. if (read.contains("VERSION")) {
    143. String[] complete_version = read.split("VERSION=");
    144. if (complete_version.length < 2) continue;
    145. String tmp = complete_version[1];
    146. String[] version = tmp.split("\\.");
    147. if (version.length < 1) {
    148. if (tmp.equalsIgnoreCase("1.1.0")) {
    149. v110 = true;
    150. break;
    151. }
    152. } else if (LobbysHelper.isNumber(version[0]) && LobbysHelper.isNumber(version[1]) && LobbysHelper.isNumber(version[2])) {
    153. int dot1 = Integer.parseInt(version[0]);
    154. int dot2 = Integer.parseInt(version[1]);
    155. int dot3 = Integer.parseInt(version[2]);
    156. if (dot1 == 1 && dot2 == 1 && dot3 == 0) {
    157. v110 = true;
    158. break;
    159. }
    160. }
    161. }
    162. }
    163. br.close();
    164. if (!v110) {
    165. if (cfg.get("reset") != null) cfg.set("reset", null);
    166. if (cfg.get("enable-hide") != null) cfg.set("enable-hide", null);
    167. if (cfg.get("maximal-messages") != null) cfg.set("maximal-messages", null);
    168. if (cfg.get("updates") != null) cfg.set("updates", "NO_DOWNLOAD");
    169. cfg.save(config_file);
    170. }
    171. } catch (Exception e) {
    172. e.printStackTrace();
    173. }
    174. }
    175.  
    176. @Override
    177. public void onEnable() {
    178. logger = getLogger();
    179. checkDatabaseFiles();
    180. getConfig().options().copyHeader(true);
    181. getConfig().options().copyDefaults(true);
    182. saveConfig();
    183. checkConfig();
    184. if (cfg.get("updates") == null) {
    185. printMessage(Level.WARNING,"No UpdateType was found in the Config-File. Skipping Updates.");
    186. } else {
    187. try {
    188. if (maxmsg()) Main.printMessage("Checking for new Version of Lobbys ...");
    189. Updater.UpdateType type = Updater.UpdateType.valueOf(cfg.getString("updates"));
    190. if (type == null) printMessage(Level.WARNING, "No valid UpdateType entered in the Config-File! Skipping Updates.");
    191. else {
    192. Updater updater = new Updater(this, 74807, this.getFile(), type, true);
    193. Updater.UpdateResult result = updater.getResult();
    194. if (result == Updater.UpdateResult.UPDATE_AVAILABLE) {
    195. Main.printMessage(Level.INFO, "A new Version of Lobbys is aviable! (" + updater.getLatestName() + "-" + updater.getLatestType() + ")");
    196. Main.printMessage(Level.INFO, "Please download/install the new Version for Bugsfixes and more Features.");
    197. } else if (result == Updater.UpdateResult.SUCCESS) {
    198. Main.printMessage("A new Version (" + updater.getLatestName() + "-" + updater.getLatestType() + ") has been downloaded and installed!");
    199. Main.printMessage("Please restart/reload your Server to activate the new Version!");
    200. } else if (result == Updater.UpdateResult.NO_UPDATE) {
    201. if (maxmsg()) Main.printMessage("No newer Version of Lobbys is aviable. You're good to go :)");
    202. } else if (result != Updater.UpdateResult.DISABLED) {
    203. Main.printMessage(Level.WARNING, "An Error occurred while checking for an Update for Lobbys. Please check all Options again!");
    204. } else {
    205. if (maxmsg()) Main.printMessage("Updates has been disabled.");
    206. }
    207. }
    208. } catch (Exception e) {
    209. printMessage(Level.WARNING, "No valid UpdateType entered in the Config-File! Skipping Updates.");
    210. }
    211. }
    212. if (maxmsg()) printMessage("Loading Databases ...");
    213. Lobbys.loadDatabases();
    214. // Lobbys.clearDatabases(true);
    215. if (maxmsg()) printMessage("Databases loaded!");
    216. if (maxmsg()) printMessage("Checking Databases ...");
    217. if (Lobbys.updateVersion110b_100r()) {
    218. if (Lobbys.updateVersion100r_110r()) {
    219. if (maxmsg()) printMessage("Checked Databases for Updates. Databases are up to date.");
    220. }
    221. }
    222.  
    223. boolean flag = false;
    224. if (Lobbys.resetLobbys() == false) { printMessage(Level.SEVERE, "ERROR by resetting the Lobbys!"); flag=true;}
    225. if (flag) printMessage(Level.SEVERE, "It's recommend to stop the Server and check all Files again, otherwise big problems could happen!");
    226. else { if (maxmsg()) printMessage("Checking Files complete!");}
    227. if (maxmsg()) printMessage("loading ...");
    228. if (maxmsg()) printMessage("Enabling Commands ...");
    229. this.getCommand("config").setExecutor(new ConfigCommand(this));
    230. this.getCommand("config").setTabCompleter(new ConfigTabCompleter());
    231. this.getCommand("lobbys").setExecutor(new LobbysCommand());
    232. this.getCommand("lobbys").setTabCompleter(new LobbysTabCompleter());
    233. this.getCommand("leave").setExecutor(new LeaveCommand());
    234. this.getCommand("lpremium").setExecutor(new LPremiumCommand());
    235. this.getCommand("lpremium").setTabCompleter(new LPremiumTabCompleter());
    236. this.getCommand("nick").setExecutor(new NickCommand());
    237. if (maxmsg()) printMessage("Commands enabled!");
    238. if (maxmsg()) printMessage("Enabling Listeners ...");
    239. PluginManager pm = this.getServer().getPluginManager();
    240. pm.registerEvents(new MainListener(), this);
    241. pm.registerEvents(new CustomEventListener(), this);
    242. if (pm.getPlugin("TagAPI") != null) {
    243. pm.registerEvents(new TagAPIListener(), this);
    244. } else Main.printMessage("TagAPI is not aviable on this Server. For full functionality please install it on your Server.");
    245. if (maxmsg()) printMessage("Listeners enabled!");
    246. if (maxmsg()) printMessage("Adding Permissions ...");
    247. try {
    248. for (Permission per : permissions) {
    249. pm.addPermission(per);
    250. }
    251. } catch (Exception e) {
    252. printMessage(Level.WARNING, "The Permissions could not be added, because they were already set.");
    253. }
    254. if (maxmsg()) printMessage("Permissions added!");
    255. if (maxmsg()) printMessage("Starting Tasks ...");
    256. update = new SignUpdaterTask().runTaskTimer(this, 0L, 5L);
    257. fivemintask = new FiveMinTask().runTaskTimer(this, 0L, 20*60*5L);
    258. if (maxmsg()) printMessage("Tasks started!");
    259.  
    260. if (maxmsg()) printMessage("Enabling BungeeCord-Features ...");
    261. Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    262. Bukkit.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageHandler());
    263. if (maxmsg()) printMessage("BungeeCord-Features enabled!");
    264.  
    265. if (maxmsg()) printMessage("Loading complete!");
    266. printMessage(complete + " successfully started!");
    267. }
    268.  
    269. @Override
    270. public void onDisable() {
    271. if (maxmsg()) printMessage("Shutting down Tasks ...");
    272. try {
    273. update.cancel();
    274. } catch (Exception e) {} catch (Error e) {}
    275. if (maxmsg()) printMessage("Tasks shutted down!");
    276. if (maxmsg()) printMessage("Resetting Lobbys ...");
    277. if (Lobbys.resetLobbys()) { if (maxmsg()) printMessage("Lobbys reseted!");}
    278. else printMessage(Level.WARNING, "ERROR by resetting Lobbys!");
    279. if (maxmsg()) printMessage("Saving Config ...");
    280. this.saveConfig();
    281. if (maxmsg()) printMessage("Saving Config done!");
    282. if (maxmsg()) printMessage("Saving Databases ...");
    283. Lobbys.saveDatabases();
    284. if (maxmsg()) printMessage("Databases saved!");
    285. if (maxmsg()) printMessage("Closing MySQL-Connection ...");
    286. try {
    287. if (LobbysMySQL.closeConnection()) {
    288. if (maxmsg()) printMessage("MySQL-Connection closed!");
    289. }
    290. } catch (Error error) {
    291. try {
    292. Class<?> cl = Class.forName("net.prefixaut.lobbys.LobbysMySQL");
    293. Method meth = cl.getDeclaredMethod("closeConnection");
    294. meth.invoke(null, new Object[]{});
    295. if (maxmsg()) printMessage("MySQL-Connection closed!");
    296. printMessage(Level.SEVERE,"ERROR by getting LobbysMySQL-Class. Could not close MySQL-Connection.");
    297. } catch (NoSuchMethodException e) {
    298. printMessage(Level.SEVERE, "ERROR by getting the Closing-Method. Could not close MySQL-Connection.");
    299. } catch (Exception e) {
    300. printMessage(Level.SEVERE, "ERROR by closing MySQL-Connection.");
    301. }
    302. }
    303. printMessage(complete + " has successfully shutdown!");
    304. }
    305.  
    306. public static void printMessage(String msg) {
    307. if (msg == null || msg.equals("")) return;
    308. logger.log(Level.INFO, msg);
    309. }
    310.  
    311. public static void printMessage(Level level, String msg) {
    312. if (msg == null || msg.equals("")) return;
    313. if (level == null) level = Level.INFO;
    314. logger.log(level,msg);
    315. }
    316. }
    317.  

     
  6. Offline

    caderape

    getConfig().options().copyDefaults(true)
    Its maybe cuz u ask to copy the defaut config after a reload.

    You can try this.

    public File config;
    this.config = new File(getDataFolder(), "config.yml");
    if (!(this.config.exists()))
    {try
    {getConfig().options().copyDefaults(true);}
    catch (Exception e )
    {e.printStackTrace();}}
     
  7. Offline

    PreFiXAUT

    But shouldn't it be called always, so when a Variable/Setting in the Config is not there, it will automaticly add it again with the default-value. That's how I understand the copyDefault > Only copies the missing Settings.
     
  8. Offline

    mine-care

    Well in your on enable you might have it to saveconfig() that means no matter what, it changes the config to the default, can you post the one able method?
     
  9. Offline

    PreFiXAUT

    mine-care Already posted the entire class.
     
  10. Offline

    mine-care

    U have it all in just one class? Lol..
    Also I can't scroll to find the on enable, iOS won't help at all
    And instead of fields like version ="1.1.0" u can use
    This.plugin.getversion
    :p
     
  11. Offline

    PreFiXAUT

    No? It's just the Bukkit-Startup. You should see the rest.
    Yeah I know but I don't want to do it like that.
    How does that help me at all?
     
  12. Offline

    mine-care

    PreFiXAUT okay :-( I just wanted to tell u about the version thing... Sorry but again can't send me the one able because I can't scroll on it, Also you mean custom config? Or bukkit config?
     
  13. Offline

    PreFiXAUT

    Didn't mean it bad if you think that.
    I'm just gonna post it on pastebin so you're able to see it ;) >>MAIN-CLASS<<
    Of course my own Config, what should i do with the bukkit-config xD >>CONFIG-FILE<<
     
  14. Offline

    mine-care

    Well you use getconfig, options.savedefaults that resets it.
    Instead do this.savedefaultconfig
     
  15. Offline

    PreFiXAUT

    mine-care
    As I mentioned in
    I already tested it. Just for safety I tried it again > still overrides the Config.
    Could it might be the 1.7.9.Development Build? but they didn't change something in the Config as far as I know :/
     
  16. Offline

    mine-care

    Now dat is weird... Try deleting the data folder and restart so we see the behavior of the plugin, also are u sure config is correct? Sometimes where something is wrong with config, bukkit returns it to defaults, soo in your case if a default is wrong then it is doing it endlessly mabe you have wrong the spaces before sections, it's 2 spaces before each subsection...
     
  17. Offline

    PreFiXAUT

    All data is correct. I just change a setting from false to true and reload the server. The config is 100% valid, checked it 2 times and otherwise I'ld get a stacktracke in the console.
     
  18. Offline

    mine-care

    W
    Wow I sugest you contact a bukkit dev staff that is online.. Sorry cant help...
     
  19. Offline

    PreFiXAUT

  20. Offline

    shohouku


    Don't save your config in OnDisable.

    I'v tried it and it works for me.
     
  21. Offline

    PreFiXAUT

    Already saw your post, but I thought I had no saveConfig in my onDisable. I didn't even notice it xD
    Anyway also works for me now, thanks for the reminder :)
     
Thread Status:
Not open for further replies.

Share This Page