From 9de900483476eba4520f2f7bfcb6c7a67be3f9dc Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 21 Aug 2011 23:05:03 +0200 Subject: [PATCH] - Moved Bukkit event registration from WorldEditPlugin into the respective listeners. - Changed registerEvent to the string-based system WorldGuard has, which should improve compatibility with differing Bukkit versions. --- .../WorldEditCriticalPlayerListener.java | 5 +++- .../bukkit/WorldEditPlayerListener.java | 4 +++ .../worldedit/bukkit/WorldEditPlugin.java | 26 +++++++++++-------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditCriticalPlayerListener.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditCriticalPlayerListener.java index b7b59f573..172bbf070 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditCriticalPlayerListener.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditCriticalPlayerListener.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.bukkit; import org.bukkit.entity.Player; +import org.bukkit.event.Event.Priority; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; @@ -39,6 +40,8 @@ public class WorldEditCriticalPlayerListener extends PlayerListener { */ public WorldEditCriticalPlayerListener(WorldEditPlugin plugin) { this.plugin = plugin; + + plugin.registerEvent("PLAYER_JOIN", this, Priority.Lowest); } /** @@ -50,7 +53,7 @@ public class WorldEditCriticalPlayerListener extends PlayerListener { public void onPlayerJoin(PlayerJoinEvent event) { wrapPlayer(event.getPlayer()).dispatchCUIHandshake(); } - + private BukkitPlayer wrapPlayer(Player player) { return new BukkitPlayer(plugin, plugin.getServerInterface(), player); } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java index 6dee2f290..3d3304a5d 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java @@ -51,6 +51,10 @@ public class WorldEditPlayerListener extends PlayerListener { */ public WorldEditPlayerListener(WorldEditPlugin plugin) { this.plugin = plugin; + + plugin.registerEvent("PLAYER_QUIT", this); + plugin.registerEvent("PLAYER_INTERACT", this); + plugin.registerEvent("PLAYER_COMMAND_PREPROCESS", this); } /** diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 84515b888..0956dd07e 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -32,6 +32,7 @@ import org.bukkit.event.Event.Priority; import org.bukkit.event.Event; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerListener; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import com.sk89q.bukkit.migration.PermissionsResolverManager; import com.sk89q.bukkit.migration.PermissionsResolverServerListener; @@ -135,14 +136,11 @@ public class WorldEditPlugin extends JavaPlugin { * Register the events used by WorldEdit. */ protected void registerEvents() { + @SuppressWarnings("unused") PlayerListener playerListener = new WorldEditPlayerListener(this); + @SuppressWarnings("unused") PlayerListener criticalPlayerListener = new WorldEditCriticalPlayerListener(this); - registerEvent(Event.Type.PLAYER_QUIT, playerListener); - registerEvent(Event.Type.PLAYER_INTERACT, playerListener); - registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener); - registerEvent(Event.Type.PLAYER_JOIN, criticalPlayerListener, Priority.Lowest); - // The permissions resolver has some hooks of its own (new PermissionsResolverServerListener(perms)).register(this); } @@ -154,9 +152,16 @@ public class WorldEditPlugin extends JavaPlugin { * @param listener * @param priority */ - protected void registerEvent(Event.Type type, Listener listener, Priority priority) { - getServer().getPluginManager() - .registerEvent(type, listener, priority, this); + protected void registerEvent(String typeName, Listener listener, Priority priority) { + try { + Event.Type type = Event.Type.valueOf(typeName); + PluginManager pm = getServer().getPluginManager(); + pm.registerEvent(type, listener, priority, this); + } catch (IllegalArgumentException e) { + logger.info("WorldGuard: Unable to register missing event type " + typeName); + } + /*getServer().getPluginManager() + .registerEvent(type, listener, priority, this);*/ } /** @@ -165,9 +170,8 @@ public class WorldEditPlugin extends JavaPlugin { * @param type * @param listener */ - protected void registerEvent(Event.Type type, Listener listener) { - getServer().getPluginManager() - .registerEvent(type, listener, Priority.Normal, this); + protected void registerEvent(String typeName, Listener listener) { + registerEvent(typeName, listener, Event.Priority.Normal); } /**