From f8c3c6a9115870e75ad6fab31f3c95e6ce2629d8 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 23 Apr 2013 12:54:34 +0200 Subject: [PATCH] Dynamic event priority. Thanks @riking. --- .../massivecraft/factions/entity/MConf.java | 2 + .../listeners/FactionsListenerChat.java | 145 +++++++----------- .../listeners/FactionsListenerMain.java | 1 + 3 files changed, 55 insertions(+), 93 deletions(-) diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index 05d15260..d5bb7ac8 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -11,6 +11,7 @@ import org.bukkit.ChatColor; import org.bukkit.event.EventPriority; import org.bukkit.permissions.PermissionDefault; +import com.massivecraft.factions.listeners.FactionsListenerChat; import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.PermUtil; @@ -35,6 +36,7 @@ public class MConf extends Entity super.load(that); this.upsertPowerPerms(); + FactionsListenerChat.get().setup(); return this; } diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerChat.java b/src/com/massivecraft/factions/listeners/FactionsListenerChat.java index 68a5c26b..e4c52100 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerChat.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerChat.java @@ -1,10 +1,12 @@ package com.massivecraft.factions.listeners; import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; +import org.bukkit.event.Event; +import org.bukkit.event.EventException; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.plugin.EventExecutor; import com.massivecraft.factions.Factions; import com.massivecraft.factions.chat.ChatFormatter; @@ -26,114 +28,71 @@ public class FactionsListenerChat implements Listener public void setup() { - Bukkit.getPluginManager().registerEvents(this, Factions.get()); + HandlerList.unregisterAll(this); + + if (MConf.get().chatSetFormat) + { + Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, this, MConf.get().chatSetFormatAt, new SetFormatEventExecutor(), Factions.get(), true); + } + + if (MConf.get().chatParseTags) + { + Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, this, MConf.get().chatParseTagsAt, new ParseTagsEventExecutor(), Factions.get(), true); + } } // -------------------------------------------- // // SET FORMAT // -------------------------------------------- // - public static void setFormat(AsyncPlayerChatEvent event, EventPriority currentPriority) + private class SetFormatEventExecutor implements EventExecutor + { + @Override + public void execute(Listener listener, Event event) throws EventException + { + try + { + if (!AsyncPlayerChatEvent.class.isAssignableFrom(event.getClass())) return; + setFormat((AsyncPlayerChatEvent)event); + } + catch (Throwable t) + { + throw new EventException(t); + } + } + } + + public static void setFormat(AsyncPlayerChatEvent event) { - // If we are setting the chat format ... - if (!MConf.get().chatSetFormat) return; - - // ... and this is the right priority ... - if (currentPriority != MConf.get().chatSetFormatAt) return; - - // ... then set the format. event.setFormat(MConf.get().chatSetFormatTo); } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void setFormatLowest(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.LOWEST); - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void setFormatLow(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.LOW); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void setFormatNormal(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.NORMAL); - } - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void setFormatHigh(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.HIGH); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void setFormatHighest(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.HIGHEST); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void setFormatMonitor(AsyncPlayerChatEvent event) - { - setFormat(event, EventPriority.MONITOR); - } - // -------------------------------------------- // // PARSE TAGS // -------------------------------------------- // - - public static void parseTags(AsyncPlayerChatEvent event, EventPriority currentPriority) + + private class ParseTagsEventExecutor implements EventExecutor + { + @Override + public void execute(Listener listener, Event event) throws EventException + { + try + { + if (!AsyncPlayerChatEvent.class.isAssignableFrom(event.getClass())) return; + parseTags((AsyncPlayerChatEvent)event); + } + catch (Throwable t) + { + throw new EventException(t); + } + } + } + + public static void parseTags(AsyncPlayerChatEvent event) { - // If we are setting the chat format ... - if (!MConf.get().chatParseTags) return; - - // ... and this is the right priority ... - if (currentPriority != MConf.get().chatParseTagsAt) return; - - // ... then parse tags a.k.a. "format the format". String format = event.getFormat(); - format = ChatFormatter.format(format, UPlayer.get(event.getPlayer()), null); event.setFormat(format); } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void parseTagsLowest(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.LOWEST); - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void parseTagsLow(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.LOW); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void parseTagsNormal(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.NORMAL); - } - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void parseTagsHigh(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.HIGH); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void parseTagsHighest(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.HIGHEST); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void parseTagsMonitor(AsyncPlayerChatEvent event) - { - parseTags(event, EventPriority.MONITOR); - } - } diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index 90022b29..75cc683a 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -366,6 +366,7 @@ public class FactionsListenerMain implements Listener if (BoardColls.get().getFactionAt(ps).isNone()) return; List deniedCommands = UConf.get(player).denyCommandsTerritoryRelation.get(rel); + if (deniedCommands == null) return; if (!containsCommand(command, deniedCommands)) return; uplayer.msg("You can't use \"/%s\" in %s territory.", Txt.getNicedEnum(rel), command);