From f305b3a119326c1df7b7a124261b023ae6d2be2c Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 23 Nov 2010 22:31:28 -0800 Subject: [PATCH] Hooks are now conditionally loaded. summary-on-start is now disabled by default and mentioned in the README. --- README.txt | 7 +++ src/WorldGuard.java | 107 ++++++++++++++++++++++++------------ src/WorldGuardListener.java | 2 +- 3 files changed, 81 insertions(+), 35 deletions(-) diff --git a/README.txt b/README.txt index 2f8f30ec..64533ca5 100644 --- a/README.txt +++ b/README.txt @@ -37,6 +37,13 @@ A "worldguard.properties" will be created the first the time that you load WorldGuard on your server. You can either restart your server or use /reloadplugin WorldGuard to reload the configuraton file after editing it. +NOTE: Some features may require a custom version of hMod. When the server +is loaded with the plugin, WorldGuard will tell you missing features. + +- summary-on-start (def. false) + Print a summary of WorldGuard's protection settings at-a-glance when + the plugin is loaded. + - item-durability (def. true) Enables item durability. diff --git a/src/WorldGuard.java b/src/WorldGuard.java index cda11b2e..e89f1e9b 100644 --- a/src/WorldGuard.java +++ b/src/WorldGuard.java @@ -21,6 +21,8 @@ import java.util.logging.Level; import java.util.jar.Manifest; import java.util.jar.Attributes; +import java.util.List; +import java.util.ArrayList; import java.net.URL; import java.io.*; @@ -53,40 +55,58 @@ public WorldGuard() { public void initialize() { PluginLoader loader = etc.getLoader(); - loader.addListener(PluginLoader.Hook.COMMAND, listener, this, - PluginListener.Priority.MEDIUM); - loader.addListener(PluginLoader.Hook.SERVERCOMMAND, listener, this, - PluginListener.Priority.MEDIUM); - loader.addListener(PluginLoader.Hook.EXPLODE, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.IGNITE, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.FLOW, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.LOGINCHECK, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.LOGIN, listener, this, - PluginListener.Priority.MEDIUM); - loader.addListener(PluginLoader.Hook.BLOCK_CREATED, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.BLOCK_DESTROYED, listener, this, - PluginListener.Priority.CRITICAL); - loader.addListener(PluginLoader.Hook.BLOCK_BROKEN, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.DISCONNECT, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.ITEM_DROP, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.ITEM_PICK_UP, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.COMPLEX_BLOCK_CHANGE, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.COMPLEX_BLOCK_SEND, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.INVENTORY_CHANGE, listener, this, - PluginListener.Priority.HIGH); - loader.addListener(PluginLoader.Hook.BLOCK_PHYSICS, listener, this, - PluginListener.Priority.MEDIUM); + List missingFeatures = new ArrayList(); + + registerHook("COMMAND", PluginListener.Priority.MEDIUM); + registerHook("SERVERCOMMAND", PluginListener.Priority.MEDIUM); + if (!registerHook("EXPLODE", PluginListener.Priority.HIGH)) { + missingFeatures.add("disabling TNT or creeper explosions"); + } + if (!registerHook("IGNITE", PluginListener.Priority.HIGH)) { + missingFeatures.add("disabling fire or lava fire"); + } + if (!registerHook("FLOW", PluginListener.Priority.HIGH)) { + missingFeatures.add("controlling lava flow or sponges"); + } + registerHook("LOGINCHECK", PluginListener.Priority.HIGH); + registerHook("LOGIN", PluginListener.Priority.MEDIUM); + registerHook("BLOCK_CREATED", PluginListener.Priority.HIGH); + registerHook("BLOCK_DESTROYED", PluginListener.Priority.CRITICAL); + registerHook("BLOCK_BROKEN", PluginListener.Priority.HIGH); + registerHook("DISCONNECT", PluginListener.Priority.HIGH); + registerHook("ITEM_DROP", PluginListener.Priority.HIGH); + if (!registerHook("ITEM_PICK_UP", PluginListener.Priority.HIGH)) { + missingFeatures.add("denying item pickups"); + } + registerHook("COMPLEX_BLOCK_CHANGE", PluginListener.Priority.HIGH); + registerHook("COMPLEX_BLOCK_SEND", PluginListener.Priority.HIGH); + registerHook("INVENTORY_CHANGE", PluginListener.Priority.HIGH); + if (!registerHook("BLOCK_PHYSICS", PluginListener.Priority.MEDIUM)) { + missingFeatures.add("controlling physics on gravel, sand, or portal blocks"); + } + + if (missingFeatures.size() > 0) { + logger.log(Level.WARNING, "WorldGuard: Your version of hMod does not support " + + joinString(missingFeatures, ", ", 0) + "."); + } + } + + /** + * Conditionally registers a hook. + * + * @param name + * @param priority + * @return where the hook was registered correctly + */ + public boolean registerHook(String name, PluginListener.Priority priority) { + try { + PluginLoader.Hook hook = PluginLoader.Hook.valueOf(name); + etc.getLoader().addListener(hook, listener, this, priority); + return true; + } catch (IllegalArgumentException e) { + logger.log(Level.WARNING, "WorldGuard: Missing hook " + name + "!"); + return false; + } } /** @@ -134,4 +154,23 @@ private String getVersion() { return "(unknown)"; } } + + /** + * Joins a string from an array of strings. + * + * @param str + * @param delimiter + * @return + */ + private static String joinString(List str, String delimiter, + int initialIndex) { + if (str.size() == 0) { + return ""; + } + StringBuilder buffer = new StringBuilder(str.get(0)); + for (int i = initialIndex + 1; i < str.size(); i++) { + buffer.append(delimiter).append(str.get(i)); + } + return buffer.toString(); + } } diff --git a/src/WorldGuardListener.java b/src/WorldGuardListener.java index dcc07669..36d2f6c0 100644 --- a/src/WorldGuardListener.java +++ b/src/WorldGuardListener.java @@ -206,7 +206,7 @@ public void loadConfiguration() { } // Print an overview of settings - if (properties.getBoolean("summary-on-start", true)) { + if (properties.getBoolean("summary-on-start", false)) { logger.log(Level.INFO, enforceOneSession ? "WorldGuard: Single session is enforced." : "WorldGuard: Single session is NOT ENFORCED."); logger.log(Level.INFO, blockTNT ? "WorldGuard: TNT ignition is blocked."