Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support outdated md5 chat libs in component parser #370

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading