diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java b/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java index 04fc347..b6e2dde 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java @@ -9,6 +9,7 @@ import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collection; import java.util.concurrent.TimeUnit; @@ -73,24 +74,47 @@ public final class Reflections { .withRawClassName("com.mojang.authlib.properties.Property") .toClassReflection().get(); + private static final boolean v1_20_2 = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2); + public static final ReflectionLazyLoader PROPERTY_GET_NAME_METHOD = new ReflectionBuilder(PROPERTY_CLASS) .withMethodName("getName") .withExpectResult(String.class) .toMethodReflection(); + public static final ReflectionLazyLoader PROPERTY_NAME_FIELD = + new ReflectionBuilder(PROPERTY_CLASS) + .withFieldName("name") + .withExpectResult(String.class) + .setStrict(v1_20_2) + .toFieldReflection(); + public static final ReflectionLazyLoader PROPERTY_GET_VALUE_METHOD = new ReflectionBuilder(PROPERTY_CLASS) .withMethodName("getValue") .withExpectResult(String.class) .toMethodReflection(); + public static final ReflectionLazyLoader PROPERTY_VALUE_FIELD = + new ReflectionBuilder(PROPERTY_CLASS) + .withFieldName("value") + .withExpectResult(String.class) + .setStrict(v1_20_2) + .toFieldReflection(); + public static final ReflectionLazyLoader PROPERTY_GET_SIGNATURE_METHOD = new ReflectionBuilder(PROPERTY_CLASS) .withMethodName("getSignature") .withExpectResult(String.class) .toMethodReflection(); + public static final ReflectionLazyLoader PROPERTY_SIGNATURE_FIELD = + new ReflectionBuilder(PROPERTY_CLASS) + .withFieldName("signature") + .withExpectResult(String.class) + .setStrict(v1_20_2) + .toFieldReflection(); + /* * These methods are used for reserving entity ids so regular Minecraft * entity packets don't interfere with our packet-based entities diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java index e86423f..356bb91 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java @@ -1,5 +1,7 @@ package lol.pyr.znpcsplus.skin; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.player.TextureProperty; import com.github.retrooper.packetevents.protocol.player.UserProfile; import com.google.gson.JsonElement; @@ -14,6 +16,7 @@ import java.util.List; public class Skin { private final long timestamp = System.currentTimeMillis(); private final List properties; + private static final boolean V1_20_2 = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2); public Skin(String texture, String signature) { properties = new ArrayList<>(1); @@ -29,9 +32,18 @@ public class Skin { try { Collection properties = (Collection) Reflections.PROPERTY_MAP_VALUES_METHOD.get().invoke(propertyMap); for (Object property : properties) { - String name = (String) Reflections.PROPERTY_GET_NAME_METHOD.get().invoke(property); - String value = (String) Reflections.PROPERTY_GET_VALUE_METHOD.get().invoke(property); - String signature = (String) Reflections.PROPERTY_GET_SIGNATURE_METHOD.get().invoke(property); + String name; + String value; + String signature; + if (V1_20_2) { + name = (String) Reflections.PROPERTY_NAME_FIELD.get().get(property); + value = (String) Reflections.PROPERTY_VALUE_FIELD.get().get(property); + signature = (String) Reflections.PROPERTY_SIGNATURE_FIELD.get().get(property); + } else { + name = (String) Reflections.PROPERTY_GET_NAME_METHOD.get().invoke(property); + value = (String) Reflections.PROPERTY_GET_VALUE_METHOD.get().invoke(property); + signature = (String) Reflections.PROPERTY_GET_SIGNATURE_METHOD.get().invoke(property); + } this.properties.add(new TextureProperty(name, value, signature)); } } catch (IllegalAccessException | InvocationTargetException e) {