From 4330885d469dd3f0e71676527d05f061785344aa Mon Sep 17 00:00:00 2001 From: garbagemule Date: Thu, 5 Jun 2014 18:14:04 +0200 Subject: [PATCH] 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. --- .../garbagemule/MobArena/ArenaMasterImpl.java | 2 +- src/com/garbagemule/MobArena/MobArena.java | 52 ++++++++++++++++--- .../commands/setup/ConfigCommand.java | 11 +++- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index e9196dd..85b3a61 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.reloadConfig(); + plugin.reloadConfigFile(); 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 48f8cc1..e501912 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -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()); diff --git a/src/com/garbagemule/MobArena/commands/setup/ConfigCommand.java b/src/com/garbagemule/MobArena/commands/setup/ConfigCommand.java index c919164..40a48f4 100644 --- a/src/com/garbagemule/MobArena/commands/setup/ConfigCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/ConfigCommand.java @@ -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.");