More reflection to avoid accessing record type

This commit is contained in:
fullwall 2023-10-01 17:32:13 +08:00
parent 05048be1f7
commit 28916be24e
1 changed files with 17 additions and 9 deletions

View File

@ -22,18 +22,16 @@ public class SkinProperty {
profile.getProperties().put("textures", new com.mojang.authlib.properties.Property(name, value, signature)); profile.getProperties().put("textures", new com.mojang.authlib.properties.Property(name, value, signature));
} }
public static SkinProperty fromMojang(com.mojang.authlib.properties.Property prop) { public static SkinProperty fromMojang(Object prop) {
if (prop == null) if (prop == null)
return null; return null;
if (GET_NAME_METHOD != null) { try {
try { return new SkinProperty((String) GET_NAME_METHOD.invoke(prop), (String) GET_VALUE_METHOD.invoke(prop),
return new SkinProperty((String) GET_NAME_METHOD.invoke(prop), (String) GET_VALUE_METHOD.invoke(prop), (String) GET_SIGNATURE_METHOD.invoke(prop));
(String) GET_SIGNATURE_METHOD.invoke(prop)); } catch (Throwable e) {
} catch (Throwable e) { e.printStackTrace();
e.printStackTrace(); return null;
}
} }
return new SkinProperty(prop.name(), prop.value(), prop.signature());
} }
public static SkinProperty fromMojangProfile(GameProfile profile) { public static SkinProperty fromMojangProfile(GameProfile profile) {
@ -50,7 +48,17 @@ public class SkinProperty {
private static MethodHandle GET_VALUE_METHOD = null; private static MethodHandle GET_VALUE_METHOD = null;
static { static {
GET_NAME_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getName", false); GET_NAME_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getName", false);
if (GET_NAME_METHOD == null) {
GET_NAME_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "name", false);
}
GET_SIGNATURE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getSignature", false); GET_SIGNATURE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getSignature", false);
if (GET_SIGNATURE_METHOD == null) {
GET_SIGNATURE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "signature",
false);
}
GET_VALUE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getValue", false); GET_VALUE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "getValue", false);
if (GET_VALUE_METHOD == null) {
GET_VALUE_METHOD = NMS.getMethodHandle(com.mojang.authlib.properties.Property.class, "value", false);
}
} }
} }