[MiniMessage] Implement Keybind component support as well (on 1.19+)

This commit is contained in:
NEZNAMY committed 2024-12-14 11:34:02 +01:00
1 parent b89bf5933b
commit fb8fa3fa54
2 files changed
+26 -9

No files matched your search

@@ -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();
@@ -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<String, Object> newTextComponent;
private final FunctionWithException<String, Object> newTranslatableComponent;
private final FunctionWithException<String, Object> newKeybindComponent;
private final BiFunction<ChatModifier, Boolean, Object> 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;