Advertisement
Gleemingknight

Untitled

Nov 22nd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package raton.meme.hcf.listener;
  2.  
  3. import com.google.common.collect.ImmutableSet;
  4. import com.google.common.collect.ImmutableSet.Builder;
  5. import com.google.common.collect.MapMaker;
  6. import java.util.Collection;
  7. import java.util.Locale;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import java.util.UUID;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13.  
  14. import me.gleeming.hcf.HCF;
  15. import me.gleeming.hcf.factionutils.event.FactionChatEvent;
  16. import me.gleeming.hcf.factionutils.struct.ChatChannel;
  17. import me.gleeming.hcf.factionutils.type.PlayerFaction;
  18. import me.gleeming.perms.Database.MongoDB;
  19. import me.gleeming.perms.Ranks.Rank;
  20. import me.gleeming.perms.Ranks.RankManager;
  21. import raton.meme.hcf.HCF;
  22. import raton.meme.hcf.factionutils.event.FactionChatEvent;
  23. import raton.meme.hcf.factionutils.struct.ChatChannel;
  24. import raton.meme.hcf.factionutils.type.PlayerFaction;
  25.  
  26. import org.apache.commons.lang.StringUtils;
  27. import org.bukkit.Bukkit;
  28. import org.bukkit.ChatColor;
  29. import org.bukkit.command.CommandSender;
  30. import org.bukkit.command.ConsoleCommandSender;
  31. import org.bukkit.entity.Player;
  32. import org.bukkit.event.EventHandler;
  33. import org.bukkit.event.EventPriority;
  34. import org.bukkit.event.Listener;
  35. import org.bukkit.event.player.AsyncPlayerChatEvent;
  36. import org.bukkit.plugin.PluginManager;
  37.  
  38. public class ChatListener
  39. implements Listener
  40. {
  41. private static final String EOTW_CAPPER_PREFIX = ChatColor.YELLOW + " TESTEOTWPREFIX ";
  42. private static final ImmutableSet<UUID> EOTW_CAPPERS;
  43. private static final String DOUBLE_POST_BYPASS_PERMISSION = "hcf.doublepost.bypass";
  44.  
  45. static
  46. {
  47. ImmutableSet.Builder<UUID> builder = ImmutableSet.builder();
  48.  
  49. EOTW_CAPPERS = builder.build();
  50. }
  51.  
  52. private static final Pattern PATTERN = Pattern.compile("\\W");
  53. private final HCF plugin;
  54.  
  55. public ChatListener(HCF plugin)
  56. {
  57. this.plugin = plugin;
  58. }
  59.  
  60. @EventHandler(ignoreCancelled=true, priority=EventPriority.HIGHEST)
  61. public void onPlayerChat(AsyncPlayerChatEvent event)
  62. {
  63. String message = event.getMessage();
  64. Player player = event.getPlayer();
  65.  
  66. PlayerFaction playerFaction = this.plugin.getFactionManager().getPlayerFaction(player);
  67. ChatChannel chatChannel = playerFaction == null ? ChatChannel.PUBLIC : playerFaction.getMember(player).getChatChannel();
  68.  
  69. Set<Player> recipients = event.getRecipients();
  70. if ((chatChannel == ChatChannel.FACTION) || (chatChannel == ChatChannel.ALLIANCE)) {
  71. if (isGlobalChannel(message))
  72. {
  73. message = message.substring(1, message.length()).trim();
  74. event.setMessage(message);
  75. }
  76. else
  77. {
  78. Collection<Player> online = playerFaction.getOnlinePlayers();
  79. if (chatChannel == ChatChannel.ALLIANCE)
  80. {
  81. Collection<PlayerFaction> allies = playerFaction.getAlliedFactions();
  82. for (PlayerFaction ally : allies) {
  83. online.addAll(ally.getOnlinePlayers());
  84. }
  85. }
  86. recipients.retainAll(online);
  87. event.setFormat(chatChannel.getRawFormat(player));
  88.  
  89. Bukkit.getPluginManager().callEvent(new FactionChatEvent(true, playerFaction, player, chatChannel, recipients, event.getMessage()));
  90. return;
  91. }
  92. }
  93. Rank rank = RankManager.getRank(MongoDB.getRank(player));
  94. String format = ChatColor.translateAlternateColorCodes('&', rank.getPrefix() + player.getName() + " &8\u00BB &f") + event.getMessage();
  95. String displayName = ChatColor.WHITE + format;
  96.  
  97. event.setFormat(format);
  98. event.setCancelled(true);
  99.  
  100. ConsoleCommandSender console = Bukkit.getConsoleSender();
  101. console.sendMessage(getFormattedMessage(player, playerFaction, displayName, message, console));
  102. for (Player recipient : event.getRecipients()) {
  103. recipient.sendMessage(getFormattedMessage(player, playerFaction, displayName, message, recipient));
  104. }
  105. }
  106.  
  107. private String getFormattedMessage(Player player, PlayerFaction playerFaction, String playerDisplayName, String message, CommandSender viewer)
  108. {
  109. String tag = playerFaction == null ? ChatColor.RED + "*" : playerFaction.getDisplayName(viewer);
  110. return ChatColor.DARK_GRAY + "[" + tag + ChatColor.DARK_GRAY + "] " + (EOTW_CAPPERS.contains(player.getUniqueId()) ? EOTW_CAPPER_PREFIX : "") +
  111. String.format(Locale.ENGLISH, playerDisplayName, new Object[] { player.getName(), message });
  112. }
  113.  
  114. private boolean isGlobalChannel(String input)
  115. {
  116. int length = input.length();
  117. if ((length <= 1) || (!input.startsWith("!"))) {
  118. return false;
  119. }
  120. for (int i = 1; i < length; i++)
  121. {
  122. char character = input.charAt(i);
  123. if (character != ' ')
  124. {
  125. if (character != '/') {
  126. break;
  127. }
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement