Dynamic event priority. Thanks @riking.

This commit is contained in:
Olof Larsson 2013-04-23 12:54:34 +02:00
parent ea18f1dd0d
commit f8c3c6a911
3 changed files with 55 additions and 93 deletions

View File

@ -11,6 +11,7 @@ import org.bukkit.ChatColor;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.factions.listeners.FactionsListenerChat;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.PermUtil; import com.massivecraft.mcore.util.PermUtil;
@ -35,6 +36,7 @@ public class MConf extends Entity<MConf>
super.load(that); super.load(that);
this.upsertPowerPerms(); this.upsertPowerPerms();
FactionsListenerChat.get().setup();
return this; return this;
} }

View File

@ -1,10 +1,12 @@
package com.massivecraft.factions.listeners; package com.massivecraft.factions.listeners;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.Event;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventException;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.EventExecutor;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.chat.ChatFormatter; import com.massivecraft.factions.chat.ChatFormatter;
@ -26,114 +28,71 @@ public class FactionsListenerChat implements Listener
public void setup() 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 // 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); 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 // 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(); String format = event.getFormat();
format = ChatFormatter.format(format, UPlayer.get(event.getPlayer()), null); format = ChatFormatter.format(format, UPlayer.get(event.getPlayer()), null);
event.setFormat(format); 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);
}
} }

View File

@ -366,6 +366,7 @@ public class FactionsListenerMain implements Listener
if (BoardColls.get().getFactionAt(ps).isNone()) return; if (BoardColls.get().getFactionAt(ps).isNone()) return;
List<String> deniedCommands = UConf.get(player).denyCommandsTerritoryRelation.get(rel); List<String> deniedCommands = UConf.get(player).denyCommandsTerritoryRelation.get(rel);
if (deniedCommands == null) return;
if (!containsCommand(command, deniedCommands)) return; if (!containsCommand(command, deniedCommands)) return;
uplayer.msg("<b>You can't use \"<h>/%s<b>\" in %s territory.", Txt.getNicedEnum(rel), command); uplayer.msg("<b>You can't use \"<h>/%s<b>\" in %s territory.", Txt.getNicedEnum(rel), command);