Fixed getting skin from online player in 1.20.2

This commit is contained in:
D3v1s0m 2023-10-21 11:41:15 +05:30
parent 11b81ba5fc
commit 232f8b8db3
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
2 changed files with 39 additions and 3 deletions

@ -9,6 +9,7 @@ import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -73,24 +74,47 @@ public final class Reflections {
.withRawClassName("com.mojang.authlib.properties.Property") .withRawClassName("com.mojang.authlib.properties.Property")
.toClassReflection().get(); .toClassReflection().get();
private static final boolean v1_20_2 = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2);
public static final ReflectionLazyLoader<Method> PROPERTY_GET_NAME_METHOD = public static final ReflectionLazyLoader<Method> PROPERTY_GET_NAME_METHOD =
new ReflectionBuilder(PROPERTY_CLASS) new ReflectionBuilder(PROPERTY_CLASS)
.withMethodName("getName") .withMethodName("getName")
.withExpectResult(String.class) .withExpectResult(String.class)
.toMethodReflection(); .toMethodReflection();
public static final ReflectionLazyLoader<Field> PROPERTY_NAME_FIELD =
new ReflectionBuilder(PROPERTY_CLASS)
.withFieldName("name")
.withExpectResult(String.class)
.setStrict(v1_20_2)
.toFieldReflection();
public static final ReflectionLazyLoader<Method> PROPERTY_GET_VALUE_METHOD = public static final ReflectionLazyLoader<Method> PROPERTY_GET_VALUE_METHOD =
new ReflectionBuilder(PROPERTY_CLASS) new ReflectionBuilder(PROPERTY_CLASS)
.withMethodName("getValue") .withMethodName("getValue")
.withExpectResult(String.class) .withExpectResult(String.class)
.toMethodReflection(); .toMethodReflection();
public static final ReflectionLazyLoader<Field> PROPERTY_VALUE_FIELD =
new ReflectionBuilder(PROPERTY_CLASS)
.withFieldName("value")
.withExpectResult(String.class)
.setStrict(v1_20_2)
.toFieldReflection();
public static final ReflectionLazyLoader<Method> PROPERTY_GET_SIGNATURE_METHOD = public static final ReflectionLazyLoader<Method> PROPERTY_GET_SIGNATURE_METHOD =
new ReflectionBuilder(PROPERTY_CLASS) new ReflectionBuilder(PROPERTY_CLASS)
.withMethodName("getSignature") .withMethodName("getSignature")
.withExpectResult(String.class) .withExpectResult(String.class)
.toMethodReflection(); .toMethodReflection();
public static final ReflectionLazyLoader<Field> 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 * These methods are used for reserving entity ids so regular Minecraft
* entity packets don't interfere with our packet-based entities * entity packets don't interfere with our packet-based entities

@ -1,5 +1,7 @@
package lol.pyr.znpcsplus.skin; 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.TextureProperty;
import com.github.retrooper.packetevents.protocol.player.UserProfile; import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -14,6 +16,7 @@ import java.util.List;
public class Skin { public class Skin {
private final long timestamp = System.currentTimeMillis(); private final long timestamp = System.currentTimeMillis();
private final List<TextureProperty> properties; private final List<TextureProperty> properties;
private static final boolean V1_20_2 = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2);
public Skin(String texture, String signature) { public Skin(String texture, String signature) {
properties = new ArrayList<>(1); properties = new ArrayList<>(1);
@ -29,9 +32,18 @@ public class Skin {
try { try {
Collection<?> properties = (Collection<?>) Reflections.PROPERTY_MAP_VALUES_METHOD.get().invoke(propertyMap); Collection<?> properties = (Collection<?>) Reflections.PROPERTY_MAP_VALUES_METHOD.get().invoke(propertyMap);
for (Object property : properties) { for (Object property : properties) {
String name = (String) Reflections.PROPERTY_GET_NAME_METHOD.get().invoke(property); String name;
String value = (String) Reflections.PROPERTY_GET_VALUE_METHOD.get().invoke(property); String value;
String signature = (String) Reflections.PROPERTY_GET_SIGNATURE_METHOD.get().invoke(property); 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)); this.properties.add(new TextureProperty(name, value, signature));
} }
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {