From abafb7a6c5f186f3f995aa09a5a59e955422d98d Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sun, 4 Nov 2012 03:03:38 +0100 Subject: [PATCH] Fix configuration handling. --- .../comphenix/protocol/CommandProtocol.java | 11 +++--- .../comphenix/protocol/ProtocolConfig.java | 39 +++++++++++++++++-- .../comphenix/protocol/ProtocolLibrary.java | 20 +++++----- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index 7039907a..494d6f84 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -35,7 +35,7 @@ class CommandProtocol extends CommandBase { String subCommand = args[0]; // Only return TRUE if we executed the correct command - if (subCommand.equalsIgnoreCase("config")) + if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) reloadConfiguration(sender); else if (subCommand.equalsIgnoreCase("check")) checkVersion(sender); @@ -52,7 +52,7 @@ class CommandProtocol extends CommandBase { @Override public void run() { UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true); - sender.sendMessage(ChatColor.BLUE + "Version check: " + result.toString()); + sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); } }); @@ -65,7 +65,7 @@ class CommandProtocol extends CommandBase { @Override public void run() { UpdateResult result = updater.update(UpdateType.DEFAULT, true); - sender.sendMessage(ChatColor.BLUE + "Update: " + result.toString()); + sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); } }); @@ -76,12 +76,13 @@ class CommandProtocol extends CommandBase { * Prevent further automatic updates until the next delay. */ public void updateFinished() { - config.setAutoLastTime(System.currentTimeMillis()); + long currentTime = System.currentTimeMillis() / ProtocolLibrary.MILLI_PER_SECOND; + + config.setAutoLastTime(currentTime); config.saveAll(); } public void reloadConfiguration(CommandSender sender) { - plugin.saveConfig(); plugin.reloadConfig(); sender.sendMessage(ChatColor.BLUE + "Reloaded configuration!"); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java index cb1acd17..09e5313d 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java @@ -1,5 +1,7 @@ package com.comphenix.protocol; +import java.io.File; + import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.plugin.Plugin; @@ -26,6 +28,7 @@ class ProtocolConfig { private Plugin plugin; private Configuration config; + private boolean loadingSections; private ConfigurationSection global; private ConfigurationSection updater; @@ -35,29 +38,57 @@ class ProtocolConfig { } public ProtocolConfig(Plugin plugin, Configuration config) { - this.config = config; + this.plugin = plugin; + reloadConfig(); + } + + /** + * Reload configuration file. + */ + public void reloadConfig() { + this.config = plugin.getConfig(); loadSections(true); } - + /** * Load data sections. * @param copyDefaults - whether or not to copy configuration defaults. */ private void loadSections(boolean copyDefaults) { + if (loadingSections) + return; + if (config != null) { global = config.getConfigurationSection(SECTION_GLOBAL); } if (global != null) { updater = global.getConfigurationSection(SECTION_AUTOUPDATER); } - + // Automatically copy defaults - if (copyDefaults && (global == null || updater == null)) { + if (copyDefaults && (!getFile().exists() || global == null || updater == null)) { + loadingSections = true; + + if (config != null) + config.options().copyDefaults(true); plugin.saveDefaultConfig(); config = plugin.getConfig(); + + loadingSections = false; loadSections(false); + + // Inform the user + System.out.println("[ProtocolLib] Created default configuration."); } } + + /** + * Retrieve a reference to the configuration file. + * @return Configuration file on disk. + */ + public File getFile() { + return new File(plugin.getDataFolder(), "config.yml"); + } /** * Retrieve whether or not ProtocolLib should determine if a new version has been released. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index c74a6858..1a1d5db3 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -17,7 +17,6 @@ package com.comphenix.protocol; -import java.io.File; import java.io.IOException; import java.util.logging.Handler; import java.util.logging.LogRecord; @@ -43,7 +42,11 @@ import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; */ public class ProtocolLibrary extends JavaPlugin { - private static final long MILLI_PER_SECOND = 1000; + /** + * The number of milliseconds per second. + */ + static final long MILLI_PER_SECOND = 1000; + private static final String PERMISSION_INFO = "protocol.info"; // There should only be one protocol manager, so we'll make it static @@ -93,7 +96,7 @@ public class ProtocolLibrary extends JavaPlugin { config = new ProtocolConfig(this); } catch (Exception e) { reporter.reportWarning(this, "Cannot load configuration", e); - + // Load it again deleteConfig(); config = new ProtocolConfig(this); @@ -118,17 +121,16 @@ public class ProtocolLibrary extends JavaPlugin { } private void deleteConfig() { - File configFile = new File(getDataFolder(), "config.yml"); - - // Delete the file - configFile.delete(); + config.getFile().delete(); } @Override public void reloadConfig() { super.reloadConfig(); // Reload configuration - config = new ProtocolConfig(this); + if (config != null) { + config.reloadConfig(); + } } private void broadcastUsers(final String permission) { @@ -237,7 +239,7 @@ public class ProtocolLibrary extends JavaPlugin { long currentTime = System.currentTimeMillis() / MILLI_PER_SECOND; // Should we update? - if (currentTime < config.getAutoLastTime() + config.getAutoDelay()) { + if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) { // Initiate the update as if it came from the console if (config.isAutoDownload()) commandProtocol.updateVersion(getServer().getConsoleSender());