Factions/src/com/massivecraft/factions/engine/EngineChat.java

142 lines
3.8 KiB
Java
Raw Normal View History

package com.massivecraft.factions.engine;
2017-03-24 13:05:58 +01:00
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.chat.ChatFormatter;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.event.EventMassiveCorePlayerToRecipientChat;
import com.massivecraft.massivecore.util.MUtil;
import org.bukkit.Bukkit;
2015-05-16 12:19:36 +02:00
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
2013-04-18 15:18:47 +02:00
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.EventExecutor;
2016-02-25 22:28:09 +01:00
public class EngineChat extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineChat i = new EngineChat();
public static EngineChat get() { return i; }
2016-02-25 22:28:09 +01:00
public EngineChat()
{
this.setPlugin(Factions.get());
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
2016-02-25 22:28:09 +01:00
public void setActiveInner(boolean active)
{
2016-02-25 22:28:09 +01:00
if ( ! active) return;
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);
}
2013-08-22 13:37:08 +02:00
if (MConf.get().chatParseTags)
{
2014-06-04 14:02:23 +02:00
Bukkit.getPluginManager().registerEvent(EventMassiveCorePlayerToRecipientChat.class, this, EventPriority.NORMAL, new ParseRelcolorEventExecutor(), Factions.get(), true);
}
}
// -------------------------------------------- //
2013-04-18 15:18:47 +02:00
// SET FORMAT
// -------------------------------------------- //
private class SetFormatEventExecutor implements EventExecutor
2013-04-18 15:18:47 +02:00
{
@Override
public void execute(Listener listener, Event event) throws EventException
{
try
{
if (!(event instanceof AsyncPlayerChatEvent)) return;
setFormat((AsyncPlayerChatEvent)event);
}
catch (Throwable t)
{
throw new EventException(t);
}
}
2013-04-18 15:18:47 +02:00
}
public static void setFormat(AsyncPlayerChatEvent event)
2013-08-22 13:37:08 +02:00
{
event.setFormat(MConf.get().chatSetFormatTo);
2013-04-18 15:18:47 +02:00
}
// -------------------------------------------- //
// PARSE TAGS
// -------------------------------------------- //
private class ParseTagsEventExecutor implements EventExecutor
{
@Override
public void execute(Listener listener, Event event) throws EventException
{
try
{
if (!(event instanceof AsyncPlayerChatEvent)) return;
parseTags((AsyncPlayerChatEvent)event);
}
catch (Throwable t)
{
throw new EventException(t);
}
}
}
public static void parseTags(AsyncPlayerChatEvent event)
2013-04-18 15:18:47 +02:00
{
2015-05-16 12:19:36 +02:00
Player player = event.getPlayer();
2015-05-16 14:30:49 +02:00
if (MUtil.isntPlayer(player)) return;
2015-05-16 12:19:36 +02:00
2013-04-18 15:18:47 +02:00
String format = event.getFormat();
2015-05-16 12:19:36 +02:00
format = ChatFormatter.format(format, player, null);
2013-04-18 15:18:47 +02:00
event.setFormat(format);
}
// -------------------------------------------- //
// PARSE RELCOLOR
// -------------------------------------------- //
private class ParseRelcolorEventExecutor implements EventExecutor
{
@Override
public void execute(Listener listener, Event event) throws EventException
{
try
{
2014-06-04 14:02:23 +02:00
if (!(event instanceof EventMassiveCorePlayerToRecipientChat)) return;
parseRelcolor((EventMassiveCorePlayerToRecipientChat)event);
}
catch (Throwable t)
{
throw new EventException(t);
}
}
}
2014-06-04 14:02:23 +02:00
public static void parseRelcolor(EventMassiveCorePlayerToRecipientChat event)
{
2013-08-22 13:37:08 +02:00
String format = event.getFormat();
format = ChatFormatter.format(format, event.getSender(), event.getRecipient());
event.setFormat(format);
}
}