workaround relocated gson
This commit is contained in:
1 parent
ef3998bd3b
commit
bf01337fcf
4 files changed
+54
-6
No files matched your search
@@ -15,6 +15,13 @@ dependencies {
|
||||
|
||||
// bundled adventure:
|
||||
implementation(libs.adventureApi)
|
||||
implementation(libs.adventureTextSerializerGson)
|
||||
implementation(libs.adventureTextSerializerGson) {
|
||||
//exclude("com.google.code.gson")
|
||||
}
|
||||
implementation(libs.adventureTextSerializerLegacy)
|
||||
}
|
||||
|
||||
tasks.shadowJar {
|
||||
relocate("com.google.gson", "net.megavex.scoreboardlibrary.testplugin.lib.gson")
|
||||
relocate("net.kyori", "net.megavex.scoreboardlibrary.testplugin.lib.kyori")
|
||||
}
|
||||
+31
@@ -1,5 +1,6 @@
|
||||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.modern;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.megavex.scoreboardlibrary.api.team.enums.CollisionRule;
|
||||
@@ -11,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
|
||||
public final class PacketAccessors {
|
||||
@@ -76,6 +78,35 @@ public final class PacketAccessors {
|
||||
IS_1_21_6_OR_ABOVE = is1_21_6OrAbove;
|
||||
}
|
||||
|
||||
public static final String GSON_PKG = "com.go".concat("ogle.gson");
|
||||
public static final boolean IS_GSON_RELOCATED;
|
||||
public static final Object JSON_PARSER;
|
||||
public static final MethodAccessor PARSE_STRING_METHOD;
|
||||
|
||||
static {
|
||||
IS_GSON_RELOCATED = !JsonElement.class.getName().equals(GSON_PKG + ".JsonElement");
|
||||
|
||||
try {
|
||||
Class<?> jsonElementClass = Class.forName(GSON_PKG + ".JsonElement");
|
||||
Class<?> jsonParserClass = Class.forName(GSON_PKG + ".JsonParser");
|
||||
JSON_PARSER = jsonParserClass.getConstructors()[0].newInstance();
|
||||
MethodAccessor parseStringMethod = ReflectUtil.findOptionalMethod(jsonParserClass, false, MethodType.methodType(jsonElementClass, String.class), "parse");
|
||||
if (parseStringMethod == null) {
|
||||
parseStringMethod = ReflectUtil.findMethod(jsonParserClass, true, MethodType.methodType(jsonElementClass, String.class), "parse");
|
||||
//new JsonParser().parse("{}");
|
||||
//System.out.println("found via method 1");
|
||||
} else {
|
||||
//System.out.println("found via method 2");
|
||||
}
|
||||
PARSE_STRING_METHOD = parseStringMethod;
|
||||
//System.out.println(JSON_PARSER);
|
||||
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//(new JsonParser()).parse("");
|
||||
//System.out.println("GSON RELOCATED = " + IS_GSON_RELOCATED);
|
||||
}
|
||||
|
||||
public static final Class<?> PKT_CLASS,
|
||||
SET_OBJECTIVE_PKT_CLASS,
|
||||
SET_DISPLAY_OBJECTIVE_PKT_CLASS,
|
||||
|
||||
+7
-2
@@ -1,6 +1,5 @@
|
||||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.objective;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.megavex.scoreboardlibrary.api.objective.ScoreFormat;
|
||||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.PacketAccessors;
|
||||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.util.ModernComponentProvider;
|
||||
@@ -50,7 +49,13 @@ public final class ScoreFormatConverter {
|
||||
if (format == ScoreFormat.blank()) {
|
||||
return BLANK;
|
||||
} else if (format instanceof ScoreFormat.Styled) {
|
||||
JsonElement json = gson().serializer().toJsonTree(((ScoreFormat.Styled) format).style());
|
||||
Object json = gson().serializer().toJsonTree(((ScoreFormat.Styled) format).style());
|
||||
if (PacketAccessors.IS_GSON_RELOCATED) {
|
||||
//System.out.println("working around 2 " + json.getClass().getName());
|
||||
json = PacketAccessors.PARSE_STRING_METHOD.invoke(PacketAccessors.JSON_PARSER, json.toString());
|
||||
//System.out.println("worked around 2 " + json.getClass().getName());
|
||||
}
|
||||
|
||||
Object result = PacketAccessors.CODEC_PARSE.invoke(STYLE_CODEC, PacketAccessors.JSON_OPS, json);
|
||||
//noinspection OptionalGetWithoutIsPresent
|
||||
Object style = ((Optional<?>) PacketAccessors.RESULT_UNWRAP_METHOD.invoke(result)).get();
|
||||
|
||||
+8
-3
@@ -1,6 +1,5 @@
|
||||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.translation.GlobalTranslator;
|
||||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.PacketAccessors;
|
||||
@@ -62,7 +61,7 @@ public final class ModernComponentProvider {
|
||||
for (Method method : PacketAccessors.CHAT_SERIALIZER_CLASS.getMethods()) {
|
||||
if (method.getParameterCount() >= 1 &&
|
||||
method.getParameterCount() <= 2 &&
|
||||
method.getParameterTypes()[0] == JsonElement.class
|
||||
method.getParameterTypes()[0].getName().equals(PacketAccessors.GSON_PKG + ".JsonElement")
|
||||
) {
|
||||
try {
|
||||
handle = MethodHandles.lookup().unreflect(method);
|
||||
@@ -91,7 +90,13 @@ public final class ModernComponentProvider {
|
||||
if (locale != null) {
|
||||
translated = GlobalTranslator.render(adventure, locale);
|
||||
}
|
||||
JsonElement json = gson().serializeToTree(translated);
|
||||
Object json = gson().serializeToTree(translated);
|
||||
|
||||
if (PacketAccessors.IS_GSON_RELOCATED) {
|
||||
//System.out.println("working around " + json.getClass().getName());
|
||||
json = PacketAccessors.PARSE_STRING_METHOD.invoke(PacketAccessors.JSON_PARSER, json.toString());
|
||||
//System.out.println("worked around " + json.getClass().getName());
|
||||
}
|
||||
|
||||
if (FROM_JSON_METHOD == null) {
|
||||
// 1.21.6+
|
||||
|
||||
Reference in new issue
Block a user