AdminChatMailer.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.gmail.nossr50.chat.mailer;
  2. import com.gmail.nossr50.chat.author.Author;
  3. import com.gmail.nossr50.chat.message.AdminChatMessage;
  4. import com.gmail.nossr50.chat.message.ChatMessage;
  5. import com.gmail.nossr50.config.ChatConfig;
  6. import com.gmail.nossr50.datatypes.chat.ChatChannel;
  7. import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
  8. import com.gmail.nossr50.events.chat.McMMOChatEvent;
  9. import com.gmail.nossr50.locale.LocaleLoader;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.util.text.TextUtils;
  12. import net.kyori.adventure.audience.Audience;
  13. import net.kyori.adventure.text.TextComponent;
  14. import org.bukkit.Bukkit;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.command.ConsoleCommandSender;
  17. import org.bukkit.plugin.Plugin;
  18. import org.jetbrains.annotations.NotNull;
  19. import java.util.function.Predicate;
  20. public class AdminChatMailer extends AbstractChatMailer {
  21. public AdminChatMailer(Plugin pluginRef) {
  22. super(pluginRef);
  23. }
  24. public static final @NotNull String MCMMO_CHAT_ADMINCHAT_PERMISSION = "mcmmo.chat.adminchat";
  25. /**
  26. * Constructs an audience of admins
  27. *
  28. * @return an audience of admins
  29. */
  30. public @NotNull Audience constructAudience() {
  31. return mcMMO.getAudiences().filter(predicate());
  32. }
  33. /**
  34. * Predicate used to filter the audience
  35. *
  36. * @return admin chat audience predicate
  37. */
  38. public @NotNull Predicate<CommandSender> predicate() {
  39. return (commandSender) -> commandSender.isOp()
  40. || commandSender.hasPermission(MCMMO_CHAT_ADMINCHAT_PERMISSION)
  41. || (ChatConfig.getInstance().isConsoleIncludedInAudience(ChatChannel.ADMIN) && commandSender instanceof ConsoleCommandSender);
  42. }
  43. /**
  44. * Styles a string using a locale entry
  45. *
  46. * @param author message author
  47. * @param message message contents
  48. * @param canColor whether to replace colors codes with colors in the raw message
  49. * @return the styled string, based on a locale entry
  50. */
  51. public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message, boolean canColor) {
  52. if(canColor) {
  53. return LocaleLoader.getTextComponent("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), message);
  54. } else {
  55. return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), message));
  56. }
  57. }
  58. @Override
  59. public void sendMail(@NotNull ChatMessage chatMessage) {
  60. chatMessage.sendMessage();
  61. }
  62. /**
  63. * Processes a chat message from an author to an audience of admins
  64. *
  65. * @param author the author
  66. * @param rawString the raw message as the author typed it before any styling
  67. * @param isAsync whether this is being processed asynchronously
  68. * @param canColor whether the author can use colors in chat
  69. */
  70. public void processChatMessage(@NotNull Author author, @NotNull String rawString, boolean isAsync, boolean canColor) {
  71. AdminChatMessage chatMessage = new AdminChatMessage(pluginRef, author, constructAudience(), rawString, addStyle(author, rawString, canColor));
  72. McMMOChatEvent chatEvent = new McMMOAdminChatEvent(pluginRef, chatMessage, isAsync);
  73. Bukkit.getPluginManager().callEvent(chatEvent);
  74. if(!chatEvent.isCancelled()) {
  75. sendMail(chatMessage);
  76. }
  77. }
  78. }