Better legacy component support, removes SignListener (#81)

* Better legacy component support, removes SignListener

* Removes unused imports
This commit is contained in:
Video 2024-01-26 16:12:17 -07:00 committed by GitHub
parent 912dff0ec9
commit fbd36161d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 38 deletions

View File

@ -11,6 +11,7 @@ import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -56,20 +57,18 @@ public class SetLoginMessageCMD extends PlexCommand
validateMessage(message); validateMessage(message);
plexPlayer.setLoginMessage(message); plexPlayer.setLoginMessage(message);
return messageComponent("setOtherPlayersLoginMessage", plexPlayer.getName(), return messageComponent("setOtherPlayersLoginMessage", plexPlayer.getName(),
message.replace("%player%", plexPlayer.getName())); MiniMessage.miniMessage().serialize(PlexUtils.stringToComponent(message.replace("%player%", plexPlayer.getName()))));
} }
if (isConsole(sender)) if (isConsole(sender))
{ {
return messageComponent("noPermissionConsole"); return messageComponent("noPermissionConsole");
} }
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayer(playerSender.getUniqueId()); PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayer(playerSender.getUniqueId());
String message = StringUtils.join(args, " ", 0, args.length); String message = StringUtils.join(args, " ", 0, args.length)
message = message.replace(plexPlayer.getName(), "%player%"); .replace(plexPlayer.getName(), "%player%");
message = PlexUtils.legacyToMiniString(message);
validateMessage(message); validateMessage(message);
plexPlayer.setLoginMessage(message); plexPlayer.setLoginMessage(message);
return messageComponent("setOwnLoginMessage", return messageComponent("setOwnLoginMessage", PlexUtils.stringToComponent(message.replace("%player%", plexPlayer.getName())));
message.replace("%player%", plexPlayer.getName()));
} }
return null; return null;
} }

View File

@ -50,16 +50,15 @@ public class TagCMD extends PlexCommand
{ {
return usage("/tag set <prefix>"); return usage("/tag set <prefix>");
} }
String prefix = PlexUtils.legacyToMiniString(StringUtils.join(args, " ", 1, args.length));
Component convertedComponent = SafeMiniMessage.mmDeserializeWithoutEvents(prefix); Component convertedComponent = PlexUtils.stringToComponent(StringUtils.join(args, " ", 1, args.length));
if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16)) if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16))
{ {
return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16)); return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16));
} }
player.setPrefix(prefix); player.setPrefix(MiniMessage.miniMessage().serialize(convertedComponent));
DataUtils.update(player); DataUtils.update(player);
return messageComponent("prefixSetTo", MiniMessage.miniMessage().serialize(convertedComponent)); return messageComponent("prefixSetTo", MiniMessage.miniMessage().serialize(convertedComponent));
} }

View File

@ -82,8 +82,8 @@ public class VaultHook
if (vaultPrefix == null || vaultPrefix.isEmpty()) { if (vaultPrefix == null || vaultPrefix.isEmpty()) {
return Component.empty(); return Component.empty();
} }
PlexLog.debug("prefix: {0}", PlexUtils.legacyToMiniString(vaultPrefix).replace("<", "\\<")); PlexLog.debug("prefix: {0}", SafeMiniMessage.mmSerializeWithoutEvents(PlexUtils.stringToComponent(vaultPrefix)).replace("<", "\\<"));
return SafeMiniMessage.mmDeserializeWithoutEvents(PlexUtils.legacyToMiniString(vaultPrefix)); return PlexUtils.stringToComponent(vaultPrefix);
} }
public static Permission getPermission() public static Permission getPermission()

View File

@ -12,7 +12,6 @@ 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;
@ -47,7 +46,7 @@ public class ChatListener extends PlexListener
{ {
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 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
MessageUtil.sendStaffChat(event.getPlayer(), event.message(), PlexUtils.adminChat(event.getPlayer().getName(), prefix, SafeMiniMessage.mmSerialize(event.message())).toArray(UUID[]::new)); 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)); plugin.getServer().getConsoleSender().sendMessage(PlexUtils.messageComponent("adminChatFormat", event.getPlayer().getName(), prefix, SafeMiniMessage.mmSerialize(event.message().replaceText(URL_REPLACEMENT_CONFIG))));
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -105,7 +104,7 @@ public class ChatListener extends PlexListener
private static void defaultChatProcessing(AsyncChatEvent event, PlexPlayer plexPlayer) private static void defaultChatProcessing(AsyncChatEvent event, PlexPlayer plexPlayer)
{ {
String text = PlexUtils.legacyToMiniString(PlexUtils.getTextFromComponent(event.message())); String text = PlexUtils.getTextFromComponent(event.message());
event.message(SafeMiniMessage.mmDeserializeWithoutEvents(text)); event.message(PlexUtils.stringToComponent(text));
} }
} }

View File

@ -65,7 +65,7 @@ public class PlayerListener<T> extends PlexListener
String loginMessage = PlayerMeta.getLoginMessage(plexPlayer); String loginMessage = PlayerMeta.getLoginMessage(plexPlayer);
if (!loginMessage.isEmpty()) if (!loginMessage.isEmpty())
{ {
PlexUtils.broadcast(loginMessage); PlexUtils.broadcast(PlexUtils.stringToComponent(loginMessage));
} }
plexPlayer.loadNotes(); plexPlayer.loadNotes();

View File

@ -1,22 +0,0 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.SignChangeEvent;
public class SignListener extends PlexListener
{
private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.legacyAmpersand();
@EventHandler(priority = EventPriority.LOW)
public void onSignEdit(SignChangeEvent event)
{
for (int i = 0; i < event.lines().size(); i++)
{
event.line(i, LEGACY_COMPONENT_SERIALIZER.deserialize(PlexUtils.getTextFromComponent(event.line(i))));
}
}
}

View File

@ -7,10 +7,13 @@ import dev.plex.PlexBase;
import dev.plex.event.BroadcastEvent; import dev.plex.event.BroadcastEvent;
import dev.plex.listener.impl.ChatListener; import dev.plex.listener.impl.ChatListener;
import dev.plex.storage.StorageType; import dev.plex.storage.StorageType;
import dev.plex.util.minimessage.SafeMiniMessage;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -29,6 +32,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Stack; import java.util.Stack;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PlexUtils implements PlexBase public class PlexUtils implements PlexBase
@ -44,6 +48,8 @@ public class PlexUtils implements PlexBase
"a52f1f08-a398-400a-bca4-2b74b81feae6" // Allink "a52f1f08-a398-400a-bca4-2b74b81feae6" // Allink
); );
private static final Pattern LEGACY_FORMATTING_PATTERN = Pattern.compile(".*(?i)(([§&])((#[a-f0-9]{3,6})|([0-9a-fklmnor]))).*");
public static <T> T addToArrayList(List<T> list, T object) public static <T> T addToArrayList(List<T> list, T object)
{ {
list.add(object); list.add(object);
@ -146,6 +152,17 @@ public class PlexUtils implements PlexBase
return component; return component;
} }
public static Component stringToComponent(String input)
{
input = cleanString(input);
return LEGACY_FORMATTING_PATTERN.matcher(input).find() ?
LegacyComponentSerializer.legacyAmpersand().deserialize(input.replaceAll("([§&]+)(k+)", "") // Ugly hack, but it tries to prevent &k and any attempts to bypass it.
).decoration(TextDecoration.OBFUSCATED, TextDecoration.State.FALSE) :
SafeMiniMessage.mmDeserializeWithoutEvents(input);
}
@Deprecated
public static String legacyToMiniString(String input) public static String legacyToMiniString(String input)
{ {
return cleanString(input.replace("&a", "<green>") return cleanString(input.replace("&a", "<green>")