Implement check for tabs in config-file. Fixes resets.

This commit creates a fail-fast exception-throwing guard inside the
method for handling config-reloading in the main plugin class.

If a config-file contains a tab, an exception will be thrown when
the plugin loads (as it did before), which means the plugin will not
load correctly. However, upon reloading with the reload command, an
exception is thrown before the current settings are overwritten.

This fixes a long-standing issue with config-files resetting upon
reload with invalid syntax. Verification is needed, as this commit
has only been tested on OS X where tabs and carriage returns are
injected via vim.
This commit is contained in:
garbagemule 2014-06-05 18:14:04 +02:00
parent 82693b2836
commit 4330885d46
3 changed files with 56 additions and 9 deletions

View File

@ -633,7 +633,7 @@ public class ArenaMasterImpl implements ArenaMaster
for (Arena a : arenas) {
a.forceEnd();
}
plugin.reloadConfig();
plugin.reloadConfigFile();
config = plugin.getConfig();
initialize();
if (wasEnabled) setEnabled(true);

View File

@ -1,6 +1,6 @@
package com.garbagemule.MobArena;
import java.io.File;
import java.io.*;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
@ -52,11 +52,11 @@ public class MobArena extends JavaPlugin
public void onEnable() {
// Initialize config-file
loadConfigFile();
reloadConfigFile();
// Initialize announcements-file
loadAnnouncementsFile();
// Load boss abilities
loadAbilities();
@ -102,9 +102,49 @@ public class MobArena extends JavaPlugin
return getFile();
}
private void loadConfigFile() {
// Create if missing
saveDefaultConfig();
void reloadConfigFile() {
// Check for tab characters in config-file
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(new File(getDataFolder(), "config.yml")));
int row = 0;
String line;
while ((line = in.readLine()) != null) {
row++;
if (line.indexOf('\t') != -1) {
StringBuilder buffy = new StringBuilder();
buffy.append("Found tab in config-file on line ").append(row).append(".");
buffy.append('\n').append("NEVER use tabs! ALWAYS use spaces!");
buffy.append('\n').append(line);
buffy.append('\n');
for (int i = 0; i < line.indexOf('\t'); i++) {
buffy.append(' ');
}
buffy.append('^');
throw new IllegalArgumentException(buffy.toString());
}
}
// Actually reload the config-file
reloadConfig();
} catch (FileNotFoundException e) {
// If the config-file doesn't exist, create the default
Messenger.info("No config-file found, creating default...");
saveDefaultConfig();
} catch (IOException e) {
// Error reading the file, just re-throw
Messenger.severe("There was an error reading the config-file.");
throw new RuntimeException(e);
} finally {
// Java 6 <3
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Set the header and save
getConfig().options().header(getHeader());

View File

@ -1,5 +1,6 @@
package com.garbagemule.MobArena.commands.setup;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import com.garbagemule.MobArena.*;
@ -21,8 +22,14 @@ public class ConfigCommand implements Command
if (args.length != 1) return false;
if (args[0].equals("reload")) {
am.reloadConfig();
Messenger.tell(sender, "Config reloaded.");
try {
am.reloadConfig();
Messenger.tell(sender, "Config reloaded.");
} catch (Exception e) {
Messenger.tell(sender, ChatColor.RED + "ERROR:" + ChatColor.RESET + "\n" + e.getMessage());
Messenger.tell(sender, "MobArena has been " + ChatColor.RED + "disabled" + ChatColor.RESET + ".");
Messenger.tell(sender, "Fix the config-file, then reload it again, and then type " + ChatColor.YELLOW + "/ma enable" + ChatColor.RESET + " to re-enable MobArena.");
}
} else if (args[0].equals("save")) {
am.saveConfig();
Messenger.tell(sender, "Config saved.");