Skip to content

Commit

Permalink
[MiniMessage] Implement Keybind component support as well (on 1.19+)
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Dec 14, 2024
1 parent b89bf59 commit fb8fa3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -54,12 +55,12 @@ public Component convert(@NotNull TabComponent component, boolean modern) {

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;
Expand All @@ -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");
Expand Down Expand Up @@ -53,8 +56,13 @@ public ReflectionComponentConverter() throws ReflectiveOperationException {
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);
Expand All @@ -67,6 +75,10 @@ public ReflectionComponentConverter() throws ReflectiveOperationException {
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);
Expand Down Expand Up @@ -116,13 +128,17 @@ public Object convert(@NotNull TabComponent component, boolean modern) {

@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();
Expand All @@ -135,7 +151,7 @@ private Object fromAdventure(@NotNull net.kyori.adventure.text.Component compone
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;
Expand Down

0 comments on commit fb8fa3f

Please sign in to comment.