From fb8fa3fa545d4b485ade154cd2fa8ef8a8fd7c8d Mon Sep 17 00:00:00 2001 From: NEZNAMY Date: Sat, 14 Dec 2024 11:34:02 +0100 Subject: [PATCH] [MiniMessage] Implement Keybind component support as well (on 1.19+) --- .../paper/PaperComponentConverter.java | 13 ++++++----- .../ReflectionComponentConverter.java | 22 ++++++++++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/bukkit/paper/src/main/java/me/neznamy/tab/platforms/paper/PaperComponentConverter.java b/bukkit/paper/src/main/java/me/neznamy/tab/platforms/paper/PaperComponentConverter.java index a67523b2d..736adf448 100644 --- a/bukkit/paper/src/main/java/me/neznamy/tab/platforms/paper/PaperComponentConverter.java +++ b/bukkit/paper/src/main/java/me/neznamy/tab/platforms/paper/PaperComponentConverter.java @@ -3,6 +3,7 @@ package me.neznamy.tab.platforms.paper; import me.neznamy.tab.platforms.bukkit.nms.converter.ComponentConverter; import me.neznamy.tab.shared.chat.*; import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.KeybindComponent; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.TextDecoration; @@ -54,12 +55,12 @@ public class PaperComponentConverter extends ComponentConverter { @NotNull private Component fromAdventure(@NotNull net.kyori.adventure.text.Component component) { - MutableComponent nmsComponent; - if (component instanceof TextComponent component1) { - nmsComponent = Component.literal(component1.content()); - } else if (component instanceof TranslatableComponent component1) { - nmsComponent = Component.translatable(component1.key()); - } else throw new IllegalStateException("Cannot convert " + component.getClass().getName()); + MutableComponent nmsComponent = switch (component) { + case TextComponent text -> Component.literal(text.content()); + case TranslatableComponent translate -> Component.translatable(translate.key()); + case KeybindComponent keyBind -> Component.keybind(keyBind.keybind()); + default -> throw new IllegalStateException("Cannot convert " + component.getClass().getName()); + }; net.kyori.adventure.text.format.TextColor color = component.color(); Key font = component.style().font(); diff --git a/bukkit/src/main/java/me/neznamy/tab/platforms/bukkit/nms/converter/ReflectionComponentConverter.java b/bukkit/src/main/java/me/neznamy/tab/platforms/bukkit/nms/converter/ReflectionComponentConverter.java index 048c7a520..7c202a725 100644 --- a/bukkit/src/main/java/me/neznamy/tab/platforms/bukkit/nms/converter/ReflectionComponentConverter.java +++ b/bukkit/src/main/java/me/neznamy/tab/platforms/bukkit/nms/converter/ReflectionComponentConverter.java @@ -6,6 +6,8 @@ import me.neznamy.tab.shared.chat.*; import me.neznamy.tab.shared.util.FunctionWithException; import me.neznamy.tab.shared.util.ReflectionUtils; import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.KeybindComponent; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.TextDecoration; @@ -26,6 +28,7 @@ public class ReflectionComponentConverter extends ComponentConverter { private final FunctionWithException newTextComponent; private final FunctionWithException newTranslatableComponent; + private final FunctionWithException newKeybindComponent; private final BiFunction convertModifier; private final Class ChatModifier = BukkitReflection.getClass("network.chat.Style", "network.chat.ChatModifier", "ChatModifier"); @@ -53,8 +56,13 @@ public class ReflectionComponentConverter extends ComponentConverter { if (BukkitReflection.getMinorVersion() >= 19) { Method IChatBaseComponent_b = ReflectionUtils.getMethod(IChatBaseComponent, new String[] {"b", "literal"}, String.class); newTextComponent = text -> IChatBaseComponent_b.invoke(null, text); + Method IChatBaseComponent_c = ReflectionUtils.getMethod(IChatBaseComponent, new String[] {"c", "translatable"}, String.class); newTranslatableComponent = text -> IChatBaseComponent_c.invoke(null, text); + + Method IChatBaseComponent_d = ReflectionUtils.getMethod(IChatBaseComponent, new String[] {"d", "keybind"}, String.class); + newKeybindComponent = text -> IChatBaseComponent_d.invoke(null, text); + Class IChatMutableComponent = BukkitReflection.getClass("network.chat.MutableComponent", "network.chat.IChatMutableComponent", "IChatMutableComponent"); Component_modifier = ReflectionUtils.getOnlyField(IChatMutableComponent, ChatModifier); ChatBaseComponent_addSibling = ReflectionUtils.getOnlyMethod(IChatMutableComponent, IChatMutableComponent, IChatBaseComponent); @@ -67,6 +75,10 @@ public class ReflectionComponentConverter extends ComponentConverter { Constructor newChatMessage = ChatMessage.getConstructor(String.class, Object[].class); newTranslatableComponent = text -> newChatMessage.newInstance(text, new Object[0]); + newKeybindComponent = text -> { + throw new UnsupportedOperationException("Keybind component conversion is not implemented"); + }; + Class ChatBaseComponent = BukkitReflection.getClass("network.chat.BaseComponent", "network.chat.ChatBaseComponent", "ChatBaseComponent"); Component_modifier = ReflectionUtils.getOnlyField(ChatBaseComponent, ChatModifier); ChatBaseComponent_addSibling = ReflectionUtils.getOnlyMethod(ChatBaseComponent, IChatBaseComponent, IChatBaseComponent); @@ -116,13 +128,17 @@ public class ReflectionComponentConverter extends ComponentConverter { @SneakyThrows @NotNull - private Object fromAdventure(@NotNull net.kyori.adventure.text.Component component) { + private Object fromAdventure(@NotNull Component component) { Object nmsComponent; if (component instanceof TextComponent) { nmsComponent = newTextComponent.apply(((TextComponent) component).content()); } else if (component instanceof TranslatableComponent) { nmsComponent = newTranslatableComponent.apply(((TranslatableComponent)component).key()); - } else throw new IllegalStateException("Cannot convert " + component.getClass().getName()); + } else if (component instanceof KeybindComponent) { + nmsComponent = newKeybindComponent.apply(((KeybindComponent)component).keybind()); + } else { + throw new IllegalStateException("Cannot convert " + component.getClass().getName()); + } net.kyori.adventure.text.format.TextColor color = component.color(); Key font = component.style().font(); @@ -135,7 +151,7 @@ public class ReflectionComponentConverter extends ComponentConverter { component.style().hasDecoration(TextDecoration.OBFUSCATED), font == null ? null : font.asString() )); - for (net.kyori.adventure.text.Component extra : component.children()) { + for (Component extra : component.children()) { ChatBaseComponent_addSibling.invoke(nmsComponent, fromAdventure(extra)); } return nmsComponent;