Skip to content

Commit 943a72e

Browse files
committed
Fix Spigot Components serializer on legacy servers
1 parent 181643d commit 943a72e

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/main/java/me/rothes/protocolstringreplacer/utils/SpigotUtils.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package me.rothes.protocolstringreplacer.utils;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import me.rothes.protocolstringreplacer.ProtocolStringReplacer;
46
import net.md_5.bungee.api.chat.BaseComponent;
7+
import net.md_5.bungee.api.chat.ItemTag;
8+
import net.md_5.bungee.api.chat.KeybindComponent;
9+
import net.md_5.bungee.api.chat.ScoreComponent;
10+
import net.md_5.bungee.api.chat.SelectorComponent;
511
import net.md_5.bungee.api.chat.TextComponent;
12+
import net.md_5.bungee.api.chat.TranslatableComponent;
13+
import net.md_5.bungee.api.chat.hover.content.Entity;
14+
import net.md_5.bungee.api.chat.hover.content.EntitySerializer;
15+
import net.md_5.bungee.api.chat.hover.content.Item;
16+
import net.md_5.bungee.api.chat.hover.content.ItemSerializer;
17+
import net.md_5.bungee.api.chat.hover.content.Text;
18+
import net.md_5.bungee.api.chat.hover.content.TextSerializer;
619
import net.md_5.bungee.chat.ComponentSerializer;
20+
import net.md_5.bungee.chat.KeybindComponentSerializer;
21+
import net.md_5.bungee.chat.ScoreComponentSerializer;
22+
import net.md_5.bungee.chat.SelectorComponentSerializer;
23+
import net.md_5.bungee.chat.TextComponentSerializer;
24+
import net.md_5.bungee.chat.TranslatableComponentSerializer;
725

826
import java.lang.reflect.Field;
927

@@ -17,22 +35,41 @@ public class SpigotUtils {
1735
for (Field declaredField : ComponentSerializer.class.getDeclaredFields()) {
1836
if (declaredField.getType() == Gson.class) {
1937
declaredField.setAccessible(true);
20-
Gson gson = (Gson) declaredField.get(null);
21-
temp = gson.newBuilder().disableHtmlEscaping().create();
38+
temp = (Gson) declaredField.get(null);
39+
try {
40+
temp = temp.newBuilder().disableHtmlEscaping().create();
41+
} catch (NoSuchMethodError e) {
42+
GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
43+
try {
44+
gsonBuilder.registerTypeAdapter(BaseComponent.class, new ComponentSerializer()).
45+
registerTypeAdapter(TextComponent.class, new TextComponentSerializer()).
46+
registerTypeAdapter(TranslatableComponent.class, new TranslatableComponentSerializer()).
47+
registerTypeAdapter(KeybindComponent.class, new KeybindComponentSerializer()).
48+
registerTypeAdapter(ScoreComponent.class, new ScoreComponentSerializer()).
49+
registerTypeAdapter(SelectorComponent.class, new SelectorComponentSerializer()).
50+
registerTypeAdapter(Entity.class, new EntitySerializer()).
51+
registerTypeAdapter(Text.class, new TextSerializer()).
52+
registerTypeAdapter(Item.class, new ItemSerializer()).
53+
registerTypeAdapter(ItemTag.class, new ItemTag.Serializer());
54+
} catch (NoClassDefFoundError ignored) {
55+
// Some types are not available on legacy servers.
56+
}
57+
temp = gsonBuilder.create();
58+
}
2259
break;
2360
}
2461
}
25-
} catch (IllegalAccessException e) {
26-
throw new RuntimeException(e);
62+
} catch (Throwable e) {
63+
ProtocolStringReplacer.error("Unable to disableHtmlEscaping for SpigotComponentSerializer:", e);
2764
}
2865
psrSerializer = temp;
2966
}
3067

3168
public static String serializeComponents(BaseComponent... components) {
3269
if ( components.length == 1 ) {
33-
return psrSerializer.toJson( components[0] );
70+
return psrSerializer.toJson(components[0]);
3471
} else {
35-
return psrSerializer.toJson( new TextComponent( components ) );
72+
return psrSerializer.toJson(new TextComponent(components));
3673
}
3774
}
3875

0 commit comments

Comments
 (0)