From f269b07998a98b7af64c24689c679f2f90f9ebd8 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Thu, 24 Jul 2014 04:47:23 +0200 Subject: [PATCH] Actually fix the config-reset bug. Some of the config-file resets were fixed in a previous commit: 4330885d469dd3f0e71676527d05f061785344aa The resets fixed previously were ones where snakeyaml would throw a fit due to tabs. Because an exception is thrown, the call to saveConfig() is never reached, which means the config-file is never reset. However, errors resulting from other illegal characters were not caught because the built-in reloadConfig() method catches, prints and then simply does nothing about any exceptions thrown, which means the actual YamlConfiguration would be empty. Saving this empty file would result in a reset, because MobArena, during initialization, sees missing bits and pieces and tries to insert them. Thus, the resets were not an issue with MobArena's attempt to keep the config-file tidy, but the reckless handling of errors in the built-in config methods in JavaPlugin. This commit completely overrides the methods in JavaPlugin that would result in these resets, and the MobArena plugin class stores its own reference to the config file and an associated FileConfiguration. When the reloadConfig() and saveConfig() methods are called, the checked exceptions are caught and rethrown as unchecked exceptions, causing the plugin to crash when it needs to do so. --- .../garbagemule/MobArena/ArenaMasterImpl.java | 2 +- src/com/garbagemule/MobArena/MobArena.java | 53 ++++++++++++++----- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index 85b3a61..e9196dd 100644 --- a/src/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -633,7 +633,7 @@ public class ArenaMasterImpl implements ArenaMaster for (Arena a : arenas) { a.forceEnd(); } - plugin.reloadConfigFile(); + plugin.reloadConfig(); config = plugin.getConfig(); initialize(); if (wasEnabled) setEnabled(true); diff --git a/src/com/garbagemule/MobArena/MobArena.java b/src/com/garbagemule/MobArena/MobArena.java index e501912..7b2d45c 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -11,6 +11,8 @@ import net.milkbowl.vault.economy.EconomyResponse.ResponseType; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -45,6 +47,9 @@ public class MobArena extends JavaPlugin // Vault private Economy economy; + + private File configFile; + private FileConfiguration config; public static final double MIN_PLAYER_DISTANCE_SQUARED = 225D; public static final int ECONOMY_MONEY_ID = -29; @@ -52,7 +57,13 @@ public class MobArena extends JavaPlugin public void onEnable() { // Initialize config-file - reloadConfigFile(); + configFile = new File(getDataFolder(), "config.yml"); + config = new YamlConfiguration(); + reloadConfig(); + + // Set the header and save + getConfig().options().header(getHeader()); + saveConfig(); // Initialize announcements-file loadAnnouncementsFile(); @@ -101,8 +112,20 @@ public class MobArena extends JavaPlugin public File getPluginFile() { return getFile(); } - - void reloadConfigFile() { + + @Override + public FileConfiguration getConfig() { + return config; + } + + @Override + public void reloadConfig() { + // Check if the config-file exists + if (!configFile.exists()) { + Messenger.info("No config-file found, creating default..."); + saveDefaultConfig(); + } + // Check for tab characters in config-file BufferedReader in = null; try { @@ -126,29 +149,33 @@ public class MobArena extends JavaPlugin } // Actually reload the config-file - reloadConfig(); + config.load(configFile); + } catch (InvalidConfigurationException e) { + throw new RuntimeException("\n\n>>>\n>>> There is an error in your config-file! Handle it!\n>>> Here is what snakeyaml says:\n>>>\n\n" + e.getMessage()); } catch (FileNotFoundException e) { - // If the config-file doesn't exist, create the default - Messenger.info("No config-file found, creating default..."); - saveDefaultConfig(); + throw new IllegalStateException("Config-file could not be created for some reason! "); } catch (IOException e) { // Error reading the file, just re-throw - Messenger.severe("There was an error reading the config-file."); - throw new RuntimeException(e); + Messenger.severe("There was an error reading the config-file:\n" + e.getMessage()); } finally { // Java 6 <3 if (in != null) { try { in.close(); } catch (IOException e) { - e.printStackTrace(); + // Swallow } } } + } - // Set the header and save - getConfig().options().header(getHeader()); - saveConfig(); + @Override + public void saveConfig() { + try { + config.save(configFile); + } catch (IOException e) { + e.printStackTrace(); + } } private void loadAnnouncementsFile() {