ChatManager.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.gmail.nossr50.chat;
  2. import com.gmail.nossr50.chat.author.Author;
  3. import com.gmail.nossr50.chat.author.ConsoleAuthor;
  4. import com.gmail.nossr50.chat.mailer.AdminChatMailer;
  5. import com.gmail.nossr50.chat.mailer.PartyChatMailer;
  6. import com.gmail.nossr50.config.ChatConfig;
  7. import com.gmail.nossr50.datatypes.chat.ChatChannel;
  8. import com.gmail.nossr50.datatypes.party.Party;
  9. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  10. import com.gmail.nossr50.locale.LocaleLoader;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.util.Misc;
  13. import com.gmail.nossr50.util.Permissions;
  14. import com.gmail.nossr50.util.text.StringUtils;
  15. import net.kyori.adventure.audience.Audience;
  16. import net.kyori.adventure.text.TextComponent;
  17. import org.bukkit.command.ConsoleCommandSender;
  18. import org.jetbrains.annotations.NotNull;
  19. //TODO: Micro optimization - Cache audiences and update cache when needed
  20. public class ChatManager {
  21. private final @NotNull AdminChatMailer adminChatMailer;
  22. private final @NotNull PartyChatMailer partyChatMailer;
  23. private final @NotNull ConsoleAuthor consoleAuthor;
  24. private final @NotNull Audience consoleAudience;
  25. private final boolean isChatEnabled;
  26. public ChatManager(@NotNull mcMMO pluginRef) {
  27. adminChatMailer = new AdminChatMailer(pluginRef);
  28. partyChatMailer = new PartyChatMailer(pluginRef);
  29. this.consoleAuthor = new ConsoleAuthor(LocaleLoader.getString("Chat.Identity.Console"));
  30. this.consoleAudience = mcMMO.getAudiences().filter((cs) -> cs instanceof ConsoleCommandSender);
  31. this.isChatEnabled = ChatConfig.getInstance().isChatEnabled();
  32. }
  33. /**
  34. * Handles player messaging when they are either in party chat or admin chat modes
  35. *
  36. * @param mmoPlayer target player
  37. * @param rawMessage the raw message from the player as it was typed
  38. * @param isAsync whether or not this is getting processed via async
  39. */
  40. public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String rawMessage, boolean isAsync) {
  41. processPlayerMessage(mmoPlayer, mmoPlayer.getChatChannel(), rawMessage, isAsync);
  42. }
  43. /**
  44. * Handles player messaging for a specific chat channel
  45. *
  46. * @param mmoPlayer target player
  47. * @param args the raw command arguments from the player
  48. * @param chatChannel target channel
  49. */
  50. public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String[] args, @NotNull ChatChannel chatChannel) {
  51. String chatMessageWithoutCommand = buildChatMessage(args);
  52. //Commands are never async
  53. processPlayerMessage(mmoPlayer, chatChannel, chatMessageWithoutCommand, false);
  54. }
  55. /**
  56. * Handles player messaging for a specific chat channel
  57. *
  58. * @param mmoPlayer target player
  59. * @param chatChannel target chat channel
  60. * @param rawMessage raw chat message as it was typed
  61. * @param isAsync whether or not this is getting processed via async
  62. */
  63. private void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel chatChannel, @NotNull String rawMessage, boolean isAsync) {
  64. switch (chatChannel) {
  65. case ADMIN:
  66. adminChatMailer.processChatMessage(mmoPlayer.getPlayerAuthor(), rawMessage, isAsync, Permissions.colorChat(mmoPlayer.getPlayer()));
  67. break;
  68. case PARTY:
  69. partyChatMailer.processChatMessage(mmoPlayer.getPlayerAuthor(), rawMessage, mmoPlayer.getParty(), isAsync, Permissions.colorChat(mmoPlayer.getPlayer()), Misc.isPartyLeader(mmoPlayer));
  70. break;
  71. case PARTY_OFFICER:
  72. case NONE:
  73. break;
  74. }
  75. }
  76. /**
  77. * Handles console messaging to admins
  78. * @param rawMessage raw message from the console
  79. */
  80. public void processConsoleMessage(@NotNull String rawMessage) {
  81. adminChatMailer.processChatMessage(getConsoleAuthor(), rawMessage, false, true);
  82. }
  83. /**
  84. * Handles console messaging to admins
  85. * @param args raw command args from the console
  86. */
  87. public void processConsoleMessage(@NotNull String[] args) {
  88. processConsoleMessage(buildChatMessage(args));
  89. }
  90. /**
  91. * Handles console messaging to a specific party
  92. * @param rawMessage raw message from the console
  93. * @param party target party
  94. */
  95. public void processConsoleMessage(@NotNull String rawMessage, @NotNull Party party) {
  96. partyChatMailer.processChatMessage(getConsoleAuthor(), rawMessage, party, false, true, false);
  97. }
  98. /**
  99. * Gets a console author
  100. * @return a {@link ConsoleAuthor}
  101. */
  102. private @NotNull Author getConsoleAuthor() {
  103. return consoleAuthor;
  104. }
  105. /**
  106. * Change the chat channel of a {@link McMMOPlayer}
  107. * Targeting the channel a player is already in will remove that player from the chat channel
  108. * @param mmoPlayer target player
  109. * @param targetChatChannel target chat channel
  110. */
  111. public void setOrToggleChatChannel(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel targetChatChannel) {
  112. if(targetChatChannel == mmoPlayer.getChatChannel()) {
  113. //Disabled message
  114. mmoPlayer.getPlayer().sendMessage(LocaleLoader.getString("Chat.Channel.Off", StringUtils.getCapitalized(targetChatChannel.toString())));
  115. mmoPlayer.setChatMode(ChatChannel.NONE);
  116. } else {
  117. mmoPlayer.setChatMode(targetChatChannel);
  118. mmoPlayer.getPlayer().sendMessage(LocaleLoader.getString("Chat.Channel.On", StringUtils.getCapitalized(targetChatChannel.toString())));
  119. }
  120. }
  121. /**
  122. * Create a chat message from an array of {@link String}
  123. * @param args array of {@link String}
  124. * @return a String built from the array
  125. */
  126. private @NotNull String buildChatMessage(@NotNull String[] args) {
  127. StringBuilder stringBuilder = new StringBuilder();
  128. for(int i = 0; i < args.length; i++) {
  129. if(i + 1 >= args.length) {
  130. stringBuilder.append(args[i]);
  131. } else {
  132. stringBuilder.append(args[i]).append(" ");
  133. }
  134. }
  135. return stringBuilder.toString();
  136. }
  137. /**
  138. * Whether or not the player is allowed to send a message to the chat channel they are targeting
  139. * @param mmoPlayer target player
  140. * @return true if the player can send messages to that chat channel
  141. */
  142. public boolean isMessageAllowed(@NotNull McMMOPlayer mmoPlayer) {
  143. switch (mmoPlayer.getChatChannel()) {
  144. case ADMIN:
  145. if(mmoPlayer.getPlayer().isOp() || Permissions.adminChat(mmoPlayer.getPlayer())) {
  146. return true;
  147. }
  148. break;
  149. case PARTY:
  150. if(mmoPlayer.getParty() != null && Permissions.partyChat(mmoPlayer.getPlayer())) {
  151. return true;
  152. }
  153. break;
  154. case PARTY_OFFICER:
  155. case NONE:
  156. return false;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Sends just the console a message
  162. * @param author author of the message
  163. * @param message message contents in component form
  164. */
  165. public void sendConsoleMessage(@NotNull Author author, @NotNull TextComponent message) {
  166. consoleAudience.sendMessage(author, message);
  167. }
  168. /**
  169. * Whether the mcMMO chat system which handles party and admin chat is enabled or disabled
  170. * @return true if mcMMO chat processing (for party/admin chat) is enabled
  171. */
  172. public boolean isChatEnabled() {
  173. return isChatEnabled;
  174. }
  175. /**
  176. * Whether or not a specific chat channel is enabled
  177. * ChatChannels are enabled/disabled via user config
  178. *
  179. * If chat is disabled, this always returns false
  180. * If NONE is passed as a {@link ChatChannel} it will return true
  181. * @param chatChannel target chat channel
  182. * @return true if the chat channel is enabled
  183. */
  184. public boolean isChatChannelEnabled(@NotNull ChatChannel chatChannel) {
  185. if(!isChatEnabled) {
  186. return false;
  187. } else {
  188. switch(chatChannel) {
  189. case ADMIN:
  190. case PARTY:
  191. case PARTY_OFFICER:
  192. return ChatConfig.getInstance().isChatChannelEnabled(chatChannel);
  193. case NONE:
  194. return true;
  195. default:
  196. return false;
  197. }
  198. }
  199. }
  200. }