From 3070ff893e925219f34966940b43b3ee5f03594c Mon Sep 17 00:00:00 2001 From: Video Date: Wed, 5 Apr 2023 16:13:05 -0600 Subject: [PATCH] Bugfixes and better admin chat system --- .../totalfreedommod/ChatManager.java | 89 +++++++---- .../totalfreedommod/CommandSpy.java | 4 +- .../api/event/AdminChatEvent.java | 74 +++++++++ .../totalfreedommod/command/Command_tag.java | 3 + .../discord/listener/AdminChatListener.java | 144 +++++++----------- .../discord/listener/BukkitNative.java | 23 ++- .../discord/util/SnowflakeEntry.java | 16 ++ 7 files changed, 225 insertions(+), 128 deletions(-) create mode 100644 commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index b27e3e43..d9f3c926 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -3,12 +3,14 @@ package me.totalfreedom.totalfreedommod; import com.google.common.base.Strings; import io.papermc.paper.event.player.AsyncChatEvent; import me.totalfreedom.totalfreedommod.admin.Admin; +import me.totalfreedom.totalfreedommod.api.event.AdminChatEvent; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; +import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; @@ -44,7 +46,6 @@ public class ChatManager extends FreedomService String steamrolled = FUtil.steamroll(event.originalMessage()); Player player = event.getPlayer(); FPlayer fPlayer = plugin.pl.getPlayer(player); - PlayerData playerData = plugin.pl.getData(player); // Chat is disabled if (!ConfigEntry.TOGGLE_CHAT.getBoolean() && !plugin.al.isAdmin(player)) @@ -63,7 +64,7 @@ public class ChatManager extends FreedomService // Admin chat is enabled else if (fPlayer.inAdminChat()) { - adminChat(player, steamrolled); + adminChat(player, steamrolled, true); event.setCancelled(true); return; } @@ -140,6 +141,41 @@ public class ChatManager extends FreedomService Placeholder.component("message", filtered))); } + @EventHandler + public void onAdminChat(AdminChatEvent event) + { + Displayable display = event.getDisplayable(); + + Component defaultFormat = FUtil.miniMessage("[ADMIN] ]: ", + Placeholder.component("prefix", event.getPrefix()), + Placeholder.component("name", event.getName()), + Placeholder.component("tag", getColoredTag(display)), + Placeholder.component("message", event.getMessage())); + + plugin.getComponentLogger().info(defaultFormat); + ; + server.getOnlinePlayers().stream().filter(player -> plugin.al.isAdmin(player)).forEach(player -> + { + Admin admin = plugin.al.getAdmin(player); + if (!Strings.isNullOrEmpty(admin.getAcFormat())) + { + String format = admin.getAcFormat(); + String flatAbv = FUtil.miniMessage(display.getAbbr()); + + player.sendMessage(FUtil.miniMessage(format, + Placeholder.component("prefix", event.getPrefix()), + Placeholder.component("name", event.getName()), + Placeholder.unparsed("rank", flatAbv), + TagResolver.resolver("rankcolor", Tag.styling(getColor(display))), + Placeholder.component("message", event.getMessage()))); + } + else + { + player.sendMessage(defaultFormat); + } + }); + } + public TextColor getColor(Displayable display) { return display.getColor(); @@ -150,40 +186,27 @@ public class ChatManager extends FreedomService return display.getColoredTag(); } + public void adminChat(Component name, Displayable displayable, Component message, boolean async) + { + AdminChatEvent event = new AdminChatEvent(name, displayable, message, async); + event.callEvent(); + } + + public void adminChat(Key key, Component prefix, Component name, Displayable displayable, Component message, boolean async) + { + AdminChatEvent event = new AdminChatEvent(key, prefix, name, displayable, message, async); + event.callEvent(); + } + public void adminChat(CommandSender sender, String message) + { + adminChat(sender, message, false); + } + + public void adminChat(CommandSender sender, String message, boolean async) { Displayable display = plugin.rm.getDisplay(sender); - FLog.info("[ADMIN] " + sender.getName() + " " + display.getTag() + ": " + message, true); - - if (plugin.dc != null) - { - plugin.dc.messageAdminChatChannel(sender.getName() + " \u00BB " + message); - } - - server.getOnlinePlayers().stream().filter(player -> plugin.al.isAdmin(player)).forEach(player -> - { - Admin admin = plugin.al.getAdmin(player); - if (!Strings.isNullOrEmpty(admin.getAcFormat())) - { - String format = admin.getAcFormat(); - String flatAbv = FUtil.miniMessage(display.getAbbr()); - - player.sendMessage(FUtil.miniMessage(format, - Placeholder.unparsed("name", sender.getName()), - Placeholder.unparsed("rank", flatAbv), - TagResolver.resolver("rankcolor", Tag.styling(getColor(display))), - Placeholder.unparsed("message", message))); - } - else - { - Component defaultFormat = FUtil.miniMessage("[ADMIN] ]: ", - Placeholder.unparsed("", sender.getName()), - Placeholder.component("", getColoredTag(display)), - Placeholder.unparsed("", message)); - - player.sendMessage(defaultFormat); - } - }); + adminChat(Component.text(sender.getName()), display, Component.text(message), async); } public void reportAction(Player reporter, String reportedName, String report) diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/CommandSpy.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/CommandSpy.java index 5d2730d6..f4d685de 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/CommandSpy.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/CommandSpy.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerCommandPreprocessEvent; @@ -24,6 +24,6 @@ public class CommandSpy extends FreedomService server.getOnlinePlayers().stream().filter(player -> plugin.al.isAdmin(player) && plugin.al.getAdmin(player).getCommandSpy() && player != event.getPlayer()).forEach(player -> player.sendMessage(Component.text(event.getPlayer().getName()).append(Component.text(": ")) - .append(Component.text(event.getMessage())))); + .append(Component.text(event.getMessage())).color(NamedTextColor.GRAY))); } } \ No newline at end of file diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java new file mode 100644 index 00000000..0426372f --- /dev/null +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java @@ -0,0 +1,74 @@ +package me.totalfreedom.totalfreedommod.api.event; + +import me.totalfreedom.totalfreedommod.rank.Displayable; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class AdminChatEvent extends Event +{ + private static HandlerList handlerList = new HandlerList(); + //-- + private Key identifier; + private Component prefix = Component.empty(); + private Component name; + private Displayable displayable; + private Component message; + + public AdminChatEvent(Key identifier, Component prefix, Component name, Displayable rank, Component message, boolean async) + { + super(async); + this.identifier = identifier; + this.prefix = prefix; + this.name = name; + this.displayable = rank; + this.message = message; + } + + public AdminChatEvent(Component name, Displayable rank, Component message, boolean async) + { + super(async); + this.identifier = Key.key("tfm", "default"); + this.name = name; + this.displayable = rank; + this.message = message; + } + + public static HandlerList getHandlerList() + { + return handlerList; + } + + @Override + public @NotNull HandlerList getHandlers() + { + return handlerList; + } + + public Key getIdentifier() + { + return identifier; + } + + public Component getPrefix() + { + return prefix; + } + + public Component getName() + { + return name; + } + + public Displayable getDisplayable() + { + return displayable; + } + + public Component getMessage() + { + return message; + } +} diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tag.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tag.java index be406793..4b495636 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tag.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tag.java @@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.PlayerData; +import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -155,6 +156,8 @@ public class Command_tag extends FreedomCommand int tagLimit = (plugin.al.isAdmin(sender) ? 30 : 20); if (steamrolled.length() > tagLimit) { + FLog.debug("FUCK YOU " + steamrolled.length()); + FLog.debug("FUCK YOU " + steamrolled); msgNew("That tag is too long (Max is characters).", Placeholder.unparsed("max", String.valueOf(tagLimit))); return true; } diff --git a/discord/src/main/java/me/totalfreedom/discord/listener/AdminChatListener.java b/discord/src/main/java/me/totalfreedom/discord/listener/AdminChatListener.java index a0e8b46d..c0a95aa1 100644 --- a/discord/src/main/java/me/totalfreedom/discord/listener/AdminChatListener.java +++ b/discord/src/main/java/me/totalfreedom/discord/listener/AdminChatListener.java @@ -11,20 +11,27 @@ import me.totalfreedom.discord.TFD4J; import me.totalfreedom.discord.util.SnowflakeEntry; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.admin.Admin; +import me.totalfreedom.totalfreedommod.api.event.AdminChatEvent; import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Title; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.TextComponent; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import org.bukkit.Bukkit; import org.bukkit.ChatColor; public class AdminChatListener { + private static final Key identifier = Key.key("tfd4j", "discord_admin_chat"); + //-- private final Bot bot; private final TFD4J tfd4j; @@ -34,11 +41,6 @@ public class AdminChatListener this.bot = tfd4j.getBot(); } - public static TextColor getColor(Displayable display) - { - return display.getColor(); - } - public void adminChatBound() { tfd4j.getBot() @@ -62,65 +64,22 @@ public class AdminChatListener public void createMessageSpec(MessageCreateEvent m) { Member member = m.getMember().orElseThrow(IllegalAccessError::new); - String tag = tfd4j.getBot().getTFM().getDisplay(member); + Component name = Component.text(member.getDisplayName()); Message msg = m.getMessage(); - String mediamessage = ChatColor.YELLOW + "[Media]"; - StringBuilder logmessage = new StringBuilder(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_AQUA + "Discord" + ChatColor.DARK_GRAY + "] " + ChatColor.RESET); - String lm = ChatColor.DARK_RED + member.getDisplayName() + " " - + ChatColor.DARK_GRAY + tag + ChatColor.DARK_GRAY - + ChatColor.WHITE + ": " + ChatColor.GOLD + FUtil.colorize(msg.getContent()); - logmessage.append(lm); - - if (!msg.getAttachments().isEmpty()) + TextComponent attachments = Component.empty(); + for (Attachment attachment : msg.getAttachments()) { - - logmessage.append(mediamessage); // Actually for logging... - + attachments = attachments.append( + Component.text("[Media] ", NamedTextColor.YELLOW) + .clickEvent(ClickEvent.openUrl(attachment.getUrl())) + .hoverEvent(HoverEvent.showText(Component.text(attachment.getUrl())))); } - FLog.info(logmessage.toString()); - Bukkit.getOnlinePlayers().stream().filter(player -> TotalFreedomMod.getPlugin().al.isAdmin(player)).forEach(player -> - { - StringBuilder message = new StringBuilder(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_AQUA + "Discord" + ChatColor.DARK_GRAY + "] " + ChatColor.RESET); + TextComponent message = Component.text(msg.getContent()).append(msg.getAttachments().isEmpty() ? + Component.empty() : Component.space().append(attachments)); - ComponentBuilder builder = new ComponentBuilder(message.toString()); - - Admin admin = TotalFreedomMod.getPlugin().al.getAdmin(player); - String format = admin.getAcFormat(); - if (!Strings.isNullOrEmpty(format)) - { - Displayable display = getDisplay(member); - TextColor color = getColor(display); - String m1 = format.replace("%name%", member.getDisplayName()) - .replace("%rank%", FUtil.miniMessage(display.getAbbr())) - .replace("%rankcolor%", color.toString()) - .replace("%msg%", FUtil.colorize(msg.getContent())); - builder.append(FUtil.colorize(m1)); - - } else - { - String m1 = ChatColor.DARK_RED + member.getDisplayName() + " " - + ChatColor.DARK_GRAY + tag + ChatColor.DARK_GRAY - + ChatColor.WHITE + ": " + ChatColor.GOLD + FUtil.colorize(msg.getContent()); - builder.append(m1); - } - - if (!msg.getAttachments().isEmpty()) - { - for (Attachment attachment : msg.getAttachments()) - { - TextComponent text = new TextComponent(mediamessage); - text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, attachment.getUrl())); - if (!msg.getContent().isEmpty()) - { - builder.append(" "); - } - builder.append(text); - } - } - player.spigot().sendMessage(builder.create()); - }); + TotalFreedomMod.getPlugin().cm.adminChat(identifier, FUtil.miniMessage("[Discord] "), name, getDisplay(member), message, true); } public Displayable getDisplay(Member member) @@ -129,33 +88,40 @@ public class AdminChatListener // Server Owner if (server == null) throw new IllegalStateException(); - return member.getRoles().map(role -> - { - if (role.getId().equals(SnowflakeEntry.OWNER_ROLE_ID.getSnowflake())) - { - return Title.OWNER; - } else if (role.getId().equals(SnowflakeEntry.DEVELOPER_ROLE_ID.getSnowflake())) - { - return Title.DEVELOPER; - } else if (role.getId().equals(SnowflakeEntry.EXECUTIVE_ROLE_ID.getSnowflake())) - { - return Title.EXECUTIVE; - } else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake())) - { - return Title.ASST_EXEC; - } else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake())) - { - return GroupProvider.SENIOR_ADMIN.getGroup(); - } else if (role.getId().equals(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake())) - { - return GroupProvider.ADMIN.getGroup(); - } else if (role.getId().equals(SnowflakeEntry.MASTERBUILDER_ROLE_ID.getSnowflake())) - { - return GroupProvider.MASTER_BUILDER.getGroup(); - } else - { - return null; - } - }).blockFirst(); + return member.getRoles() + .filter(role -> SnowflakeEntry.acceptableRoleIDs().contains(role.getId())) + .map(role -> + { + if (role.getId().equals(SnowflakeEntry.OWNER_ROLE_ID.getSnowflake())) + { + return Title.OWNER; + } else if (role.getId().equals(SnowflakeEntry.DEVELOPER_ROLE_ID.getSnowflake())) + { + return Title.DEVELOPER; + } else if (role.getId().equals(SnowflakeEntry.EXECUTIVE_ROLE_ID.getSnowflake())) + { + return Title.EXECUTIVE; + } else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake())) + { + return Title.ASST_EXEC; + } else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake())) + { + return GroupProvider.SENIOR_ADMIN.getGroup(); + } else if (role.getId().equals(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake())) + { + return GroupProvider.ADMIN.getGroup(); + } else if (role.getId().equals(SnowflakeEntry.MASTERBUILDER_ROLE_ID.getSnowflake())) + { + return GroupProvider.MASTER_BUILDER.getGroup(); + } else + { + return GroupProvider.OP.getGroup(); // This should never be reached. + } + }).blockFirst(); + } + + public static Key getIdentifier() + { + return identifier; } } diff --git a/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java b/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java index ea4ec64f..eceb040a 100644 --- a/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java +++ b/discord/src/main/java/me/totalfreedom/discord/listener/BukkitNative.java @@ -1,10 +1,14 @@ package me.totalfreedom.discord.listener; +import io.papermc.paper.event.player.AsyncChatEvent; import me.totalfreedom.discord.Bot; import me.totalfreedom.discord.TFD4J; import me.totalfreedom.discord.util.Utilities; import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.api.event.AdminChatEvent; import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import me.totalfreedom.totalfreedommod.util.FUtil; +import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.md_5.bungee.api.ChatColor; @@ -20,6 +24,8 @@ import org.bukkit.event.player.PlayerQuitEvent; public class BukkitNative implements Listener { + + //-- private final TotalFreedomMod commons; private final Bot bot; private final TFD4J tfd4j; @@ -74,17 +80,26 @@ public class BukkitNative implements Listener } @EventHandler(ignoreCancelled = true) - public void onAsyncPlayerChat(AsyncPlayerChatEvent event) + public void onAsyncPlayerChat(AsyncChatEvent event) { Player player = event.getPlayer(); - String message = event.getMessage(); + String message = FUtil.steamroll(event.message()); if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !tfd4j.getServer().hasWhitelist() - && !commons.pl.getPlayer(player).isMuted() && bot != null) + && !commons.pl.getPlayer(player).isMuted() && !commons.pl.getPlayer(player).inAdminChat() && bot != null) { tfd4j.getImpl().messageChatChannel(player.getName() + " \u00BB " - + ChatColor.stripColor(message), true); + + message, true); + } + } + + @EventHandler + public void onAdminChat(AdminChatEvent event) + { + if (!event.getIdentifier().equals(AdminChatListener.getIdentifier())) + { + tfd4j.getImpl().messageAdminChatChannel(FUtil.steamroll(event.getName()) + " ยป " + FUtil.steamroll(event.getMessage())); } } } diff --git a/discord/src/main/java/me/totalfreedom/discord/util/SnowflakeEntry.java b/discord/src/main/java/me/totalfreedom/discord/util/SnowflakeEntry.java index d064ad5a..91d0fc38 100644 --- a/discord/src/main/java/me/totalfreedom/discord/util/SnowflakeEntry.java +++ b/discord/src/main/java/me/totalfreedom/discord/util/SnowflakeEntry.java @@ -3,6 +3,9 @@ package me.totalfreedom.discord.util; import discord4j.common.util.Snowflake; import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import java.util.HashSet; +import java.util.Set; + public enum SnowflakeEntry { ADMIN_CHAT_CHANNEL_ID(ConfigEntry.DISCORD_ADMINCHAT_CHANNEL_ID), @@ -32,4 +35,17 @@ public enum SnowflakeEntry { return Snowflake.of(entry.getString()); } + + public static Set acceptableRoleIDs() + { + Set acceptableRoleIDs = new HashSet<>(); + acceptableRoleIDs.add(SnowflakeEntry.OWNER_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.DEVELOPER_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.EXECUTIVE_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake()); + acceptableRoleIDs.add(SnowflakeEntry.MASTERBUILDER_ROLE_ID.getSnowflake()); + return acceptableRoleIDs; + } }