Preemptive EssentialsChat 3.x support, since they'll be removing the old local chat integration method; new implementation will need improvement to support relation coloring, but for now it otherwise works.

When chat event hits an UnknownFormatConversionException which requires better chat configuration, the quick dirty fix of setting chatTagInsertIndex to 0 is now automatically applied.
This commit is contained in:
Brettflan
2012-02-02 19:31:33 -06:00
parent dcd167d4e0
commit 4f4b988b42
5 changed files with 115 additions and 27 deletions

View File

@ -1,15 +1,22 @@
package com.massivecraft.factions.integration;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.P;
import com.earth2me.essentials.chat.EssentialsChat;
import com.earth2me.essentials.chat.IEssentialsChatListener;
import com.earth2me.essentials.chat.EssentialsLocalChatEvent;
/*
* This Essentials integration handler is for newer 3.x.x versions of Essentials which don't have "IEssentialsChatListener"
*/
public class EssentialsFeatures
{
private static EssentialsChat essChat;
@ -19,21 +26,11 @@ public class EssentialsFeatures
essChat = instance;
try
{
essChat.addEssentialsChatListener("Factions", new IEssentialsChatListener()
{
public boolean shouldHandleThisChat(PlayerChatEvent event)
{
return P.p.shouldLetFactionsHandleThisChat(event);
}
public String modifyMessage(PlayerChatEvent event, Player target, String message)
{
return message.replace(Conf.chatTagReplaceString, P.p.getPlayerFactionTagRelation(event.getPlayer(), target)).replace("[FACTION_TITLE]", P.p.getPlayerTitle(event.getPlayer()));
}
});
P.p.log("Found and will integrate chat with "+essChat.getDescription().getFullName());
Bukkit.getServer().getPluginManager().registerEvents(new LocalChatListener(), P.p);
P.p.log("Found and will integrate chat with newer "+essChat.getDescription().getFullName());
// As of Essentials 2.8+, curly braces are not accepted and are instead replaced with square braces, so... deal with it
if (essChat.getDescription().getVersion().startsWith("2.8.") && Conf.chatTagReplaceString.contains("{"))
// curly braces used to be accepted by the format string EssentialsChat but no longer are, so... deal with chatTagReplaceString which might need updating
if (Conf.chatTagReplaceString.contains("{"))
{
Conf.chatTagReplaceString = Conf.chatTagReplaceString.replace("{", "[").replace("}", "]");
P.p.log("NOTE: as of Essentials 2.8+, we've had to switch the default chat replacement tag from \"{FACTION}\" to \"[FACTION]\". This has automatically been updated for you.");
@ -45,11 +42,17 @@ public class EssentialsFeatures
}
}
public static void unhookChat()
private static class LocalChatListener implements Listener
{
if (essChat != null)
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerChat(EssentialsLocalChatEvent event)
{
essChat.removeEssentialsChatListener("Factions");
Player speaker = event.getPlayer();
String format = event.getFormat();
format = format.replace(Conf.chatTagReplaceString, P.p.getPlayerFactionTag(speaker)).replace("[FACTION_TITLE]", P.p.getPlayerTitle(speaker));
event.setFormat(format);
// NOTE: above doesn't do relation coloring. if/when we can get a local recipient list from EssentialsLocalChatEvent, we'll probably
// want to pass it on to FactionsPlayerListener.onPlayerChat(PlayerChatEvent event) rather than duplicating code
}
}
}

View File

@ -0,0 +1,59 @@
package com.massivecraft.factions.integration;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.P;
import com.earth2me.essentials.chat.EssentialsChat;
import com.earth2me.essentials.chat.IEssentialsChatListener;
/*
* This Essentials integration handler is for older 2.x.x versions of Essentials which have "IEssentialsChatListener"
*/
public class EssentialsOldVersionFeatures
{
private static EssentialsChat essChat;
public static void integrateChat(EssentialsChat instance)
{
essChat = instance;
try
{
essChat.addEssentialsChatListener("Factions", new IEssentialsChatListener()
{
public boolean shouldHandleThisChat(PlayerChatEvent event)
{
return P.p.shouldLetFactionsHandleThisChat(event);
}
public String modifyMessage(PlayerChatEvent event, Player target, String message)
{
return message.replace(Conf.chatTagReplaceString, P.p.getPlayerFactionTagRelation(event.getPlayer(), target)).replace("[FACTION_TITLE]", P.p.getPlayerTitle(event.getPlayer()));
}
});
P.p.log("Found and will integrate chat with "+essChat.getDescription().getFullName());
// As of Essentials 2.8+, curly braces are not accepted and are instead replaced with square braces, so... deal with it
if (essChat.getDescription().getVersion().startsWith("2.8.") && Conf.chatTagReplaceString.contains("{"))
{
Conf.chatTagReplaceString = Conf.chatTagReplaceString.replace("{", "[").replace("}", "]");
P.p.log("NOTE: as of Essentials 2.8+, we've had to switch the default chat replacement tag from \"{FACTION}\" to \"[FACTION]\". This has automatically been updated for you.");
}
}
catch (NoSuchMethodError ex)
{
essChat = null;
}
}
public static void unhookChat()
{
if (essChat != null)
{
essChat.removeEssentialsChatListener("Factions");
}
}
}