Fix configuration handling.

This commit is contained in:
Kristian S. Stangeland 2012-11-04 03:03:38 +01:00
parent f5dce019df
commit abafb7a6c5
3 changed files with 52 additions and 18 deletions

View File

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

View File

@ -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.

View File

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