fix prefixes

This commit is contained in:
Taah 2023-08-29 20:41:15 -07:00
parent 2a2152603b
commit c51d8da5b7
6 changed files with 48 additions and 18 deletions

View File

@ -5,11 +5,14 @@ import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.hook.VaultHook;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage; import dev.plex.util.minimessage.SafeMiniMessage;
import dev.plex.util.redis.MessageUtil; import dev.plex.util.redis.MessageUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -43,12 +46,13 @@ public class AdminChatCMD extends PlexCommand
if (playerSender != null) if (playerSender != null)
{ {
player = plugin.getPlayerCache().getPlexPlayer(playerSender.getUniqueId()); player = plugin.getPlayerCache().getPlexPlayer(playerSender.getUniqueId());
prefix = player.getPrefix(); prefix = PlexUtils.mmSerialize(VaultHook.getPrefix(player));
} }
else else
{ {
prefix = messageString(LegacyComponentSerializer.legacyAmpersand().serialize(mmString("<dark_gray>[<dark_purple>Console<dark_gray]"))); prefix = "<dark_gray>[<dark_purple>Console<dark_gray>]";
} }
PlexLog.debug("admin chat prefix: {0}", prefix);
String message = StringUtils.join(args, " "); String message = StringUtils.join(args, " ");
plugin.getServer().getConsoleSender().sendMessage(messageComponent("adminChatFormat", sender.getName(), prefix, message)); plugin.getServer().getConsoleSender().sendMessage(messageComponent("adminChatFormat", sender.getName(), prefix, message));
MessageUtil.sendStaffChat(sender, SafeMiniMessage.mmDeserialize(message), PlexUtils.adminChat(sender.getName(), prefix, message).toArray(UUID[]::new)); MessageUtil.sendStaffChat(sender, SafeMiniMessage.mmDeserialize(message), PlexUtils.adminChat(sender.getName(), prefix, message).toArray(UUID[]::new));

View File

@ -1,17 +1,18 @@
package dev.plex.hook; package dev.plex.hook;
import dev.plex.Plex;
import dev.plex.meta.PlayerMeta;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.UUID;
public class VaultHook public class VaultHook
{ {
private static Chat CHAT; private static Chat CHAT;
@ -53,20 +54,36 @@ public class VaultHook
return PERMISSIONS; return PERMISSIONS;
} }
public static Component getPrefix(UUID uuid)
{
return getPrefix(Bukkit.getOfflinePlayer(uuid));
}
public static Component getPrefix(PlexPlayer plexPlayer) public static Component getPrefix(PlexPlayer plexPlayer)
{
return getPrefix(Bukkit.getOfflinePlayer(plexPlayer.getUuid()));
}
public static Component getPrefix(OfflinePlayer player)
{ {
if (VaultHook.getChat() == null || VaultHook.getPermission() == null) if (VaultHook.getChat() == null || VaultHook.getPermission() == null)
{ {
return null; return Component.empty();
} }
if (PlexUtils.DEVELOPERS.contains(plexPlayer.getUuid().toString())) if (PlexUtils.DEVELOPERS.contains(player.getUniqueId().toString()))
{ {
return PlexUtils.mmDeserialize("<dark_gray>[<dark_purple>Developer<dark_gray>]"); return PlexUtils.mmDeserialize("<dark_gray>[<dark_purple>Developer</dark_gray>]");
} }
Player bukkitPlayer = Bukkit.getPlayer(plexPlayer.getUuid()); String group = VaultHook.getPermission().getPrimaryGroup(null, player);
String group = VaultHook.getPermission().getPrimaryGroup(bukkitPlayer); if (group == null || group.isEmpty()) {
String vaultPrefix = VaultHook.getChat().getGroupPrefix(bukkitPlayer.getWorld(), group); return Component.empty();
return LegacyComponentSerializer.legacyAmpersand().deserialize(vaultPrefix); }
String vaultPrefix = VaultHook.getChat().getGroupPrefix((String) null, group);
if (vaultPrefix == null || vaultPrefix.isEmpty()) {
return Component.empty();
}
PlexLog.debug("prefix: {0}", PlexUtils.legacyToMiniString(vaultPrefix).replace("<", "\\<"));
return SafeMiniMessage.mmDeserializeWithoutEvents(PlexUtils.legacyToMiniString(vaultPrefix));
} }
public static Permission getPermission() public static Permission getPermission()

View File

@ -1,5 +1,6 @@
package dev.plex.listener.impl; package dev.plex.listener.impl;
import dev.plex.hook.VaultHook;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggleable; import dev.plex.listener.annotation.Toggleable;
import dev.plex.meta.PlayerMeta; import dev.plex.meta.PlayerMeta;
@ -11,6 +12,7 @@ import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -43,8 +45,9 @@ public class ChatListener extends PlexListener
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId());
if (plexPlayer.isStaffChat()) if (plexPlayer.isStaffChat())
{ {
MessageUtil.sendStaffChat(event.getPlayer(), event.message(), PlexUtils.adminChat(event.getPlayer().getName(), plexPlayer.getPrefix(), SafeMiniMessage.mmSerialize(event.message())).toArray(UUID[]::new)); String prefix = PlexUtils.mmSerialize(VaultHook.getPrefix(event.getPlayer())); // Don't use PlexPlayer#getPrefix because that returns their custom set prefix and not their group's
plugin.getServer().getConsoleSender().sendMessage(PlexUtils.messageComponent("adminChatFormat", event.getPlayer().getName(), SafeMiniMessage.mmSerialize(event.message())).replaceText(URL_REPLACEMENT_CONFIG)); MessageUtil.sendStaffChat(event.getPlayer(), event.message(), PlexUtils.adminChat(event.getPlayer().getName(), prefix, SafeMiniMessage.mmSerialize(event.message())).toArray(UUID[]::new));
plugin.getServer().getConsoleSender().sendMessage(PlexUtils.messageComponent("adminChatFormat", event.getPlayer().getName(), prefix, PlexUtils.legacyToMiniString(SafeMiniMessage.mmSerializeWithoutEvents(event.message()))).replaceText(URL_REPLACEMENT_CONFIG));
event.setCancelled(true); event.setCancelled(true);
return; return;
} }

View File

@ -192,6 +192,11 @@ public class PlexUtils implements PlexBase
return MINI_MESSAGE.deserialize(input); return MINI_MESSAGE.deserialize(input);
} }
public static String mmSerialize(Component input)
{
return MINI_MESSAGE.serialize(input);
}
public static Component mmCustomDeserialize(String input, TagResolver... resolvers) public static Component mmCustomDeserialize(String input, TagResolver... resolvers)
{ {
return MiniMessage.builder().tags(TagResolver.builder().resolvers(resolvers).build()).build().deserialize(input); return MiniMessage.builder().tags(TagResolver.builder().resolvers(resolvers).build()).build().deserialize(input);

View File

@ -3,6 +3,7 @@ package dev.plex.util.redis;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.gson.Gson; import com.google.gson.Gson;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.hook.VaultHook;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage; import dev.plex.util.minimessage.SafeMiniMessage;
@ -41,7 +42,7 @@ public class MessageUtil
{ {
}.getType()); }.getType());
String sender = object.getString("sender").isEmpty() ? "CONSOLE" : object.getString("sender"); String sender = object.getString("sender").isEmpty() ? "CONSOLE" : object.getString("sender");
PlexUtils.adminChat(sender, plugin.getPlayerCache().getPlexPlayer(Bukkit.getPlayer(sender).getUniqueId()).getPrefix(), object.getString("message"), ignore); PlexUtils.adminChat(sender, !sender.equals("CONSOLE") ? PlexUtils.mmSerialize(VaultHook.getPrefix(UUID.fromString(sender))) : "<dark_gray>[<dark_purple>Console<dark_gray>]", object.getString("message"), ignore);
String[] server = object.getString("server").split(":"); String[] server = object.getString("server").split(":");
if (!Bukkit.getServer().getIp().equalsIgnoreCase(server[0]) || Bukkit.getServer().getPort() != Integer.parseInt(server[1])) if (!Bukkit.getServer().getIp().equalsIgnoreCase(server[0]) || Bukkit.getServer().getPort() != Integer.parseInt(server[1]))
{ {
@ -77,7 +78,7 @@ public class MessageUtil
String miniMessage = SafeMiniMessage.mmSerialize(message); String miniMessage = SafeMiniMessage.mmSerialize(message);
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.put("sender", sender instanceof Player player ? player.getName() : ""); object.put("sender", sender instanceof Player player ? player.getUniqueId().toString() : "");
object.put("message", miniMessage); object.put("message", miniMessage);
object.put("ignore", GSON.toJson(ignore)); object.put("ignore", GSON.toJson(ignore));
object.put("server", String.format("%s:%s", Bukkit.getServer().getIp(), Bukkit.getServer().getPort())); object.put("server", String.format("%s:%s", Bukkit.getServer().getIp(), Bukkit.getServer().getPort()));

View File

@ -130,7 +130,7 @@ toggleCommandSpy: "<gray>CommandSpy has been"
enabled: "<gray>enabled." enabled: "<gray>enabled."
disabled: "<gray>disabled." disabled: "<gray>disabled."
# 0 - The admin / staff member # 0 - The admin / staff member
# 1 - The player's prefix # 1 - The player's group's prefix if any
# 2 - The message # 2 - The message
adminChatFormat: '<dark_gray>[<blue>AdminChat<dark_gray>] <dark_red>{0} {1} <gray>» <gold>{2}' adminChatFormat: '<dark_gray>[<blue>AdminChat<dark_gray>] <dark_red>{0} {1} <gray>» <gold>{2}'
# 0 - Whether it was toggled on or off # 0 - Whether it was toggled on or off