diff --git a/lib/EssentialsChat.jar b/lib/EssentialsChat.jar new file mode 100644 index 00000000..ec0a3cfa Binary files /dev/null and b/lib/EssentialsChat.jar differ diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index 0e6935fb..e2e296ac 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -19,6 +19,7 @@ import com.massivecraft.factions.cmd.CmdAutoHelp; import com.massivecraft.factions.cmd.FCmdRoot; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.EssentialsFeatures; +import com.massivecraft.factions.integration.EssentialsOldVersionFeatures; import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.SpoutFeatures; //import com.massivecraft.factions.integration.Worldguard; @@ -176,7 +177,25 @@ public class P extends MPlugin if (test != null && test.isEnabled()) { essChat = (EssentialsChat)test; - EssentialsFeatures.integrateChat(essChat); + + // try newer Essentials 3.x integration method + try + { + Class.forName("com.earth2me.essentials.chat.EssentialsLocalChatEvent"); + EssentialsFeatures.integrateChat(essChat); + } + catch (ClassNotFoundException ex) + { + // no? try older Essentials 2.x integration method + try + { + EssentialsOldVersionFeatures.integrateChat(essChat); + } + catch (NoClassDefFoundError ex2) + { + // no known method for hooking into Essentials chat stuff + } + } } } @@ -184,7 +203,13 @@ public class P extends MPlugin { if (essChat != null) { - EssentialsFeatures.unhookChat(); + try + { + EssentialsOldVersionFeatures.unhookChat(); + } + catch (NoClassDefFoundError ex) + { + } } } diff --git a/src/com/massivecraft/factions/integration/EssentialsFeatures.java b/src/com/massivecraft/factions/integration/EssentialsFeatures.java index a642f8a1..d8f63406 100644 --- a/src/com/massivecraft/factions/integration/EssentialsFeatures.java +++ b/src/com/massivecraft/factions/integration/EssentialsFeatures.java @@ -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 } } } diff --git a/src/com/massivecraft/factions/integration/EssentialsOldVersionFeatures.java b/src/com/massivecraft/factions/integration/EssentialsOldVersionFeatures.java new file mode 100644 index 00000000..e867820a --- /dev/null +++ b/src/com/massivecraft/factions/integration/EssentialsOldVersionFeatures.java @@ -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"); + } + } +} diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 4519a0d8..a50b22ad 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -74,10 +74,10 @@ public class FactionsPlayerListener implements Listener if (!Conf.chatTagReplaceString.isEmpty() && eventFormat.contains(Conf.chatTagReplaceString)) { // we're using the "replace" method of inserting the faction tags - // if they stuck "{FACTION_TITLE}" in there, go ahead and do it too - if (eventFormat.contains("{FACTION_TITLE}")) + // if they stuck "[FACTION_TITLE]" in there, go ahead and do it too + if (eventFormat.contains("[FACTION_TITLE]")) { - eventFormat = eventFormat.replace("{FACTION_TITLE}", me.getTitle()); + eventFormat = eventFormat.replace("[FACTION_TITLE]", me.getTitle()); } InsertIndex = eventFormat.indexOf(Conf.chatTagReplaceString); eventFormat = eventFormat.replace(Conf.chatTagReplaceString, ""); @@ -124,8 +124,9 @@ public class FactionsPlayerListener implements Listener } catch (UnknownFormatConversionException ex) { + Conf.chatTagInsertIndex = 0; P.p.log(Level.SEVERE, "Critical error in chat message formatting!"); - P.p.log(Level.SEVERE, "NOTE: To fix this quickly, running this command should work: f config chatTagInsertIndex 0"); + P.p.log(Level.SEVERE, "NOTE: This has been automatically fixed right now by setting chatTagInsertIndex to 0."); P.p.log(Level.SEVERE, "For a more proper fix, please read this regarding chat configuration: http://massivecraft.com/plugins/factions/config#Chat_configuration"); return; }