Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. package com.massivecraft.factions.listeners;
  2.  
  3. import java.util.UnknownFormatConversionException;
  4. import java.util.logging.Level;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.EventPriority;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.player.PlayerChatEvent;
  13.  
  14. import com.massivecraft.factions.Conf;
  15. import com.massivecraft.factions.FPlayer;
  16. import com.massivecraft.factions.FPlayers;
  17. import com.massivecraft.factions.Faction;
  18. import com.massivecraft.factions.P;
  19. import com.massivecraft.factions.struct.ChatMode;
  20. import com.massivecraft.factions.struct.Relation;
  21.  
  22.  
  23. public class FactionsChatListener implements Listener
  24. {
  25. public P p;
  26. public FactionsChatListener(P p)
  27. {
  28. this.p = p;
  29. }
  30.  
  31. // this is for handling slashless command usage and faction/alliance chat, set at lowest priority so Factions gets to them first
  32. @EventHandler(priority = EventPriority.LOWEST)
  33. public void onPlayerEarlyChat(PlayerChatEvent event)
  34. {
  35. if (event.isCancelled()) return;
  36.  
  37. Player talkingPlayer = event.getPlayer();
  38. String msg = event.getMessage();
  39. FPlayer me = FPlayers.i.get(talkingPlayer);
  40. ChatMode chat = me.getChatMode();
  41.  
  42. // slashless factions commands need to be handled here if the user isn't in public chat mode
  43. if (chat != ChatMode.PUBLIC && p.handleCommand(talkingPlayer, msg, false, true))
  44. {
  45. if (Conf.logPlayerCommands)
  46. Bukkit.getLogger().log(Level.INFO, "[PLAYER_COMMAND] "+talkingPlayer.getName()+": "+msg);
  47. event.setCancelled(true);
  48. return;
  49. }
  50.  
  51. if(chat == ChatMode.PUBLIC){
  52. for(Player player : Bukkit.getOnlinePlayers()) {
  53. player.sendMessage(e.getMessage());
  54. }
  55. }
  56.  
  57.  
  58. // Is it a faction chat message?
  59. if (chat == ChatMode.FACTION)
  60. {
  61. Faction myFaction = me.getFaction();
  62.  
  63. String message = String.format(Conf.factionChatFormat, me.describeTo(myFaction), msg);
  64. myFaction.sendMessage(message);
  65.  
  66. Bukkit.getLogger().log(Level.INFO, ChatColor.stripColor("FactionChat "+myFaction.getTag()+": "+message));
  67.  
  68. //Send to any players who are spying chat
  69. for (FPlayer fplayer : FPlayers.i.getOnline())
  70. {
  71. if(fplayer.isSpyingChat() && fplayer.getFaction() != myFaction)
  72. fplayer.sendMessage("[FCspy] "+myFaction.getTag()+": "+message);
  73. }
  74.  
  75. event.setCancelled(true);
  76. return;
  77. }
  78. else if (chat == ChatMode.ALLIANCE)
  79. {
  80. Faction myFaction = me.getFaction();
  81.  
  82. String message = String.format(Conf.allianceChatFormat, ChatColor.stripColor(me.getNameAndTag()), msg);
  83.  
  84. //Send message to our own faction
  85. myFaction.sendMessage(message);
  86.  
  87. //Send to all our allies
  88. for (FPlayer fplayer : FPlayers.i.getOnline())
  89. {
  90. if(myFaction.getRelationTo(fplayer) == Relation.ALLY)
  91. fplayer.sendMessage(message);
  92.  
  93. //Send to any players who are spying chat
  94. else if(fplayer.isSpyingChat())
  95. fplayer.sendMessage("[ACspy]: " + message);
  96. }
  97.  
  98. Bukkit.getLogger().log(Level.INFO, ChatColor.stripColor("AllianceChat: "+message));
  99.  
  100. event.setCancelled(true);
  101. return;
  102. }
  103. }
  104.  
  105. // this is for handling insertion of the player's faction tag, set at highest priority to give other plugins a chance to modify chat first
  106. @EventHandler(priority = EventPriority.HIGHEST)
  107. public void onPlayerChat(PlayerChatEvent event)
  108. {
  109. if (event.isCancelled()) return;
  110.  
  111. // Are we to insert the Faction tag into the format?
  112. // If we are not to insert it - we are done.
  113. if ( ! Conf.chatTagEnabled || Conf.chatTagHandledByAnotherPlugin) return;
  114.  
  115. Player talkingPlayer = event.getPlayer();
  116. String msg = event.getMessage();
  117. String eventFormat = event.getFormat();
  118. FPlayer me = FPlayers.i.get(talkingPlayer);
  119. int InsertIndex = 0;
  120.  
  121. if (!Conf.chatTagReplaceString.isEmpty() && eventFormat.contains(Conf.chatTagReplaceString))
  122. {
  123. // we're using the "replace" method of inserting the faction tags
  124. // if they stuck "[FACTION_TITLE]" in there, go ahead and do it too
  125. if (eventFormat.contains("[FACTION_TITLE]"))
  126. {
  127. eventFormat = eventFormat.replace("[FACTION_TITLE]", me.getTitle());
  128. }
  129. InsertIndex = eventFormat.indexOf(Conf.chatTagReplaceString);
  130. eventFormat = eventFormat.replace(Conf.chatTagReplaceString, "");
  131. Conf.chatTagPadAfter = false;
  132. Conf.chatTagPadBefore = false;
  133. }
  134. else if (!Conf.chatTagInsertAfterString.isEmpty() && eventFormat.contains(Conf.chatTagInsertAfterString))
  135. {
  136. // we're using the "insert after string" method
  137. InsertIndex = eventFormat.indexOf(Conf.chatTagInsertAfterString) + Conf.chatTagInsertAfterString.length();
  138. }
  139. else if (!Conf.chatTagInsertBeforeString.isEmpty() && eventFormat.contains(Conf.chatTagInsertBeforeString))
  140. {
  141. // we're using the "insert before string" method
  142. InsertIndex = eventFormat.indexOf(Conf.chatTagInsertBeforeString);
  143. }
  144. else
  145. {
  146. // we'll fall back to using the index place method
  147. InsertIndex = Conf.chatTagInsertIndex;
  148. if (InsertIndex > eventFormat.length())
  149. return;
  150. }
  151.  
  152. String formatStart = eventFormat.substring(0, InsertIndex) + ((Conf.chatTagPadBefore && !me.getChatTag().isEmpty()) ? " " : "");
  153. String formatEnd = ((Conf.chatTagPadAfter && !me.getChatTag().isEmpty()) ? " " : "") + eventFormat.substring(InsertIndex);
  154.  
  155. String nonColoredMsgFormat = formatStart + me.getChatTag().trim() + formatEnd;
  156.  
  157. // Relation Colored?
  158. if (Conf.chatTagRelationColored)
  159. {
  160. }
  161. // We must choke the standard message and send out individual messages to all players
  162. // Why? Because the relations will differ.
  163. event.setCancelled(true);
  164.  
  165. for (Player listeningPlayer : event.getRecipients())
  166. {
  167. FPlayer you = FPlayers.i.get(listeningPlayer);
  168. try
  169. {
  170. }
  171. catch (UnknownFormatConversionException ex)
  172. {
  173. Conf.chatTagInsertIndex = 0;
  174. P.p.log(Level.SEVERE, "Critical error in chat message formatting!");
  175. P.p.log(Level.SEVERE, "NOTE: This has been automatically fixed right now by setting chatTagInsertIndex to 0.");
  176. P.p.log(Level.SEVERE, "For a more proper fix, please read this regarding chat configuration: http://massivecraft.com/plugins/factions/config#Chat_configuration");
  177. return;
  178. }
  179. }
  180.  
  181. // Write to the log... We will write the non colored message.
  182. String nonColoredMsg = ChatColor.stripColor(String.format(nonColoredMsgFormat, talkingPlayer.getDisplayName(), msg));
  183. Bukkit.getLogger().log(Level.INFO, nonColoredMsg);
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement