Skip to content

Commit

Permalink
fix: support outdated md5 chat libs in component parser
Browse files Browse the repository at this point in the history
Fixes #357
  • Loading branch information
diogotcorreia committed Dec 18, 2023
1 parent d0ba292 commit 7dd0ed2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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()
}
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -277,7 +287,7 @@ private List<BaseComponent> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}

}

0 comments on commit 7dd0ed2

Please sign in to comment.