Actually fix the config-reset bug.

Some of the config-file resets were fixed in a previous commit:
4330885d46

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.
This commit is contained in:
garbagemule 2014-07-24 04:47:23 +02:00
parent 6ae083c993
commit f269b07998
2 changed files with 41 additions and 14 deletions

View File

@ -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);

View File

@ -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! <o>");
} 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() {