Skip to content

Commit 159dd6d

Browse files
committed
convert to bungeecord component manually
1 parent 7407b9b commit 159dd6d

6 files changed

Lines changed: 328 additions & 13 deletions

File tree

modules/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ subprojects {
2323
)
2424
setupLibraries(
2525
key="adventure",
26-
"net.kyori:adventure-api:4.21.0",
27-
"net.kyori:adventure-text-serializer-bungeecord:4.4.1",
26+
"net.kyori:adventure-api:4.25.0",
2827
"net.kyori:adventure-text-serializer-legacy:4.4.1",
29-
"net.kyori:adventure-text-minimessage:4.21.0",
28+
"net.kyori:adventure-text-minimessage:4.25.0",
3029
)
3130
}

modules/library/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
compileOnly("me.clip:placeholderapi:2.11.6")
2525
compileOnly("com.zaxxer:HikariCP:4.0.3")
2626
compileOnly("com.mojang:authlib:2.1.28")
27+
compileOnly("net.md-5:bungeecord-chat:1.21-R0.5-SNAPSHOT")
2728

2829
applyLibraries("adventure", "compileOnly")
2930
applyLibraries("nbt-api", "compileOnly")

modules/library/src/main/java/top/mrxiaom/pluginbase/utils/adventure/audience/AudienceConsole.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@
22

33
import net.kyori.adventure.audience.Audience;
44
import net.kyori.adventure.text.Component;
5-
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
5+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
66
import net.md_5.bungee.api.chat.BaseComponent;
77
import org.bukkit.Bukkit;
88
import org.bukkit.command.ConsoleCommandSender;
99
import org.jetbrains.annotations.NotNull;
10+
import top.mrxiaom.pluginbase.utils.adventure.serializer.BungeeComponentSerializer;
1011

1112
public class AudienceConsole implements Audience {
13+
private static boolean SUPPORT_BUNGEE = true;
1214
public static final Audience INSTANCE = new AudienceConsole();
13-
private static final BungeeComponentSerializer bungee = BungeeComponentSerializer.get();
15+
private static final LegacyComponentSerializer legacy = LegacyComponentSerializer.legacySection();
1416
private final ConsoleCommandSender console = Bukkit.getConsoleSender();
1517
private AudienceConsole() {}
1618

1719
@Override
1820
public void sendMessage(@NotNull Component message) {
19-
BaseComponent[] components = bungee.serialize(message);
20-
console.spigot().sendMessage(components);
21+
if (SUPPORT_BUNGEE) {
22+
try {
23+
BaseComponent components = BungeeComponentSerializer.serialize(message);
24+
console.spigot().sendMessage(components);
25+
return;
26+
} catch (LinkageError e) {
27+
SUPPORT_BUNGEE = false;
28+
}
29+
}
30+
console.sendMessage(legacy.serialize(message));
2131
}
2232
}

modules/library/src/main/java/top/mrxiaom/pluginbase/utils/adventure/audience/AudiencePlayer.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,54 @@
55
import net.kyori.adventure.sound.Sound;
66
import net.kyori.adventure.sound.SoundStop;
77
import net.kyori.adventure.text.Component;
8-
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
98
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
109
import net.kyori.adventure.title.Title;
1110
import net.md_5.bungee.api.ChatMessageType;
1211
import net.md_5.bungee.api.chat.BaseComponent;
12+
import net.md_5.bungee.api.chat.TextComponent;
1313
import org.bukkit.Location;
1414
import org.bukkit.SoundCategory;
1515
import org.bukkit.entity.Player;
1616
import org.jetbrains.annotations.NotNull;
17+
import top.mrxiaom.pluginbase.utils.adventure.serializer.BungeeComponentSerializer;
1718

1819
public class AudiencePlayer implements Audience {
20+
private static boolean SUPPORT_BUNGEE = true;
1921
private static final LegacyComponentSerializer legacy = LegacyComponentSerializer.legacySection();
20-
private static final BungeeComponentSerializer bungee = BungeeComponentSerializer.get();
2122
private final Player player;
2223
public AudiencePlayer(Player player) {
2324
this.player = player;
2425
}
2526

2627
@Override
2728
public void sendMessage(@NotNull Component message) {
28-
BaseComponent[] components = bungee.serialize(message);
29-
player.spigot().sendMessage(components);
29+
if (SUPPORT_BUNGEE) {
30+
try {
31+
BaseComponent components = BungeeComponentSerializer.serialize(message);
32+
player.spigot().sendMessage(components);
33+
return;
34+
} catch (LinkageError e) {
35+
SUPPORT_BUNGEE = false;
36+
}
37+
}
38+
player.sendMessage(legacy.serialize(message));
3039
}
3140

3241
@Override
3342
public void sendActionBar(@NotNull Component message) {
34-
BaseComponent[] components = bungee.serialize(message);
35-
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, components);
43+
if (SUPPORT_BUNGEE) {
44+
try {
45+
BaseComponent components = BungeeComponentSerializer.serialize(message);
46+
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, components);
47+
} catch (LinkageError e) {
48+
SUPPORT_BUNGEE = false;
49+
}
50+
}
51+
try {
52+
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(legacy.serialize(message)));
53+
} catch (LinkageError ignored) {
54+
player.sendMessage(legacy.serialize(message));
55+
}
3656
}
3757

3858
@Override
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package top.mrxiaom.pluginbase.utils.adventure.serializer;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.TranslationArgument;
5+
import net.kyori.adventure.text.object.ObjectContents;
6+
import net.kyori.adventure.text.object.PlayerHeadObjectContents;
7+
import net.kyori.adventure.text.object.SpriteObjectContents;
8+
import net.md_5.bungee.api.chat.*;
9+
import net.md_5.bungee.api.chat.objects.PlayerObject;
10+
import net.md_5.bungee.api.chat.objects.SpriteObject;
11+
import net.md_5.bungee.api.chat.player.Profile;
12+
import net.md_5.bungee.api.chat.player.Property;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.UUID;
17+
18+
public class BungeeComponentSerializer {
19+
public static BaseComponent serialize(Component input) {
20+
BaseComponent component = convert(input);
21+
for (Styles style : Styles.values()) {
22+
style.apply(component, input);
23+
}
24+
25+
for (Component child : input.children()) {
26+
component.addExtra(serialize(child));
27+
}
28+
return component;
29+
}
30+
31+
@SuppressWarnings({"deprecation"})
32+
private static BaseComponent convert(Component input) {
33+
if (input instanceof net.kyori.adventure.text.TextComponent) {
34+
return new TextComponent(((net.kyori.adventure.text.TextComponent) input).content());
35+
}
36+
if (input instanceof net.kyori.adventure.text.TranslatableComponent) {
37+
String key = ((net.kyori.adventure.text.TranslatableComponent) input).key();
38+
List<TranslationArgument> arguments = ((net.kyori.adventure.text.TranslatableComponent) input).arguments();
39+
List<Object> args = new ArrayList<>();
40+
for (TranslationArgument argument : arguments) {
41+
args.add(argument.value());
42+
}
43+
TranslatableComponent component = new TranslatableComponent(key, args.toArray());
44+
try {
45+
String fallback = ((net.kyori.adventure.text.TranslatableComponent) input).fallback();
46+
if (fallback != null) {
47+
component.setFallback(fallback);
48+
}
49+
} catch (LinkageError ignored) {
50+
}
51+
return component;
52+
}
53+
try {
54+
if (input instanceof net.kyori.adventure.text.KeybindComponent) {
55+
String keybind = ((net.kyori.adventure.text.KeybindComponent) input).keybind();
56+
return new KeybindComponent(keybind);
57+
}
58+
} catch (LinkageError ignored) {}
59+
try {
60+
if (input instanceof net.kyori.adventure.text.ScoreComponent) {
61+
String name = ((net.kyori.adventure.text.ScoreComponent) input).name();
62+
String objective = ((net.kyori.adventure.text.ScoreComponent) input).objective();
63+
String value = ((net.kyori.adventure.text.ScoreComponent) input).value();
64+
if (value != null) {
65+
return new ScoreComponent(name, objective, value);
66+
} else {
67+
return new ScoreComponent(name, objective);
68+
}
69+
}
70+
} catch (LinkageError ignored) {}
71+
try {
72+
if (input instanceof net.kyori.adventure.text.SelectorComponent) {
73+
String selector = ((net.kyori.adventure.text.SelectorComponent) input).pattern();
74+
Component separator = ((net.kyori.adventure.text.SelectorComponent) input).separator();
75+
if (separator != null) {
76+
return new SelectorComponent(selector, serialize(separator));
77+
} else {
78+
return new SelectorComponent(selector);
79+
}
80+
}
81+
} catch (LinkageError ignored) {}
82+
try {
83+
if (input instanceof net.kyori.adventure.text.ObjectComponent) {
84+
ObjectContents contents = ((net.kyori.adventure.text.ObjectComponent) input).contents();
85+
if (contents instanceof PlayerHeadObjectContents) {
86+
PlayerHeadObjectContents head = (PlayerHeadObjectContents) contents;
87+
String name = head.name();
88+
UUID uuid = head.id();
89+
boolean hat = head.hat();
90+
List<PlayerHeadObjectContents.ProfileProperty> props = head.profileProperties();
91+
Property[] properties = new Property[props.size()];
92+
for (int i = 0; i < props.size(); i++) {
93+
String propName = props.get(i).name();
94+
String propValue = props.get(i).value();
95+
String propSign = props.get(i).signature();
96+
if (propSign != null) {
97+
properties[i] = new Property(propName, propValue, propSign);
98+
} else {
99+
properties[i] = new Property(propName, propValue);
100+
}
101+
}
102+
Profile profile = new Profile(properties);
103+
profile.setName(name);
104+
profile.setUuid(uuid);
105+
return new ObjectComponent(new PlayerObject(profile, hat));
106+
}
107+
if (contents instanceof SpriteObjectContents) {
108+
SpriteObjectContents props = (SpriteObjectContents) contents;
109+
String atlas = props.atlas().asString();
110+
String sprite = props.sprite().asString();
111+
return new ObjectComponent(new SpriteObject(atlas, sprite));
112+
}
113+
}
114+
} catch (LinkageError ignored) {}
115+
return new TextComponent("");
116+
}
117+
}

0 commit comments

Comments
 (0)