From 7dd0ed2e29f14d3a9f20d9c6460403e74671ebc7 Mon Sep 17 00:00:00 2001 From: Diogo Correia Date: Mon, 18 Dec 2023 20:37:00 +0100 Subject: [PATCH] fix: support outdated md5 chat libs in component parser Fixes #357 --- .../language/parser/AdvancedComponent.java | 34 ++++++++++++------- .../triton/utils/ModernComponentGetters.java | 25 ++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/com/rexcantor64/triton/utils/ModernComponentGetters.java diff --git a/core/src/main/java/com/rexcantor64/triton/language/parser/AdvancedComponent.java b/core/src/main/java/com/rexcantor64/triton/language/parser/AdvancedComponent.java index 3cb5a60c..0f06a0b1 100644 --- a/core/src/main/java/com/rexcantor64/triton/language/parser/AdvancedComponent.java +++ b/core/src/main/java/com/rexcantor64/triton/language/parser/AdvancedComponent.java @@ -2,9 +2,9 @@ import com.rexcantor64.triton.Triton; import com.rexcantor64.triton.utils.ComponentUtils; +import com.rexcantor64.triton.utils.ModernComponentGetters; import lombok.Getter; import lombok.val; -import lombok.var; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.*; @@ -77,11 +77,15 @@ public static AdvancedComponent fromBaseComponent(boolean onlyText, BaseComponen builder.append(uuid.toString()); hasHover = true; } - if (comp.getFontRaw() != null) { - builder.append("\uE800") - .append(comp.getFontRaw()) - .append("\uE802"); - hasFont = true; + try { + if (comp.getFontRaw() != null) { + builder.append("\uE800") + .append(comp.getFontRaw()) + .append("\uE802"); + hasFont = true; + } + } catch (NoSuchMethodError ignore) { + // old versions of Spigot don't have getFontRaw() } } } @@ -102,11 +106,17 @@ public static AdvancedComponent fromBaseComponent(boolean onlyText, BaseComponen args.add(fromBaseComponent(false, arg)); advancedComponent.setTranslatableArguments(uuid.toString(), args); } - if (!onlyText && comp instanceof KeybindComponent) { - KeybindComponent kc = (KeybindComponent) comp; - builder.append("\uE700") - .append(kc.getKeybind()) - .append("\uE700"); + if (!onlyText) { + try { + ModernComponentGetters.getKeybind(comp) + .ifPresent(keybind -> { + builder.append("\uE700") + .append(keybind) + .append("\uE700"); + }); + } catch (NoClassDefFoundError ignore) { + // old versions of Spigot don't have KeybindComponent + } } if (comp.getExtra() != null) { AdvancedComponent component = fromBaseComponent(onlyText, comp.getExtra() @@ -277,7 +287,7 @@ private List toBaseComponent(String text) { component = new TextComponent(""); ComponentUtils.copyFormatting(previousComponent, component); } - KeybindComponent kc = new KeybindComponent(key.toString()); + BaseComponent kc = ModernComponentGetters.newKeybindComponent(key.toString()); ComponentUtils.copyFormatting(component, kc); list.add(kc); } else { diff --git a/core/src/main/java/com/rexcantor64/triton/utils/ModernComponentGetters.java b/core/src/main/java/com/rexcantor64/triton/utils/ModernComponentGetters.java new file mode 100644 index 00000000..3987cb2e --- /dev/null +++ b/core/src/main/java/com/rexcantor64/triton/utils/ModernComponentGetters.java @@ -0,0 +1,25 @@ +package com.rexcantor64.triton.utils; + +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.KeybindComponent; + +import java.util.Optional; + +/** + * Avoid loading "modern" (as in, not 1.8.8) md_5's chat library classes on old Spigot versions. + * This will be removed in Triton v4 (where Adventure is used instead). + */ +public class ModernComponentGetters { + + public static Optional getKeybind(BaseComponent component) { + if (component instanceof KeybindComponent) { + return Optional.ofNullable(((KeybindComponent) component).getKeybind()); + } + return Optional.empty(); + } + + public static BaseComponent newKeybindComponent(String keybind) { + return new KeybindComponent(keybind); + } + +}