23
23
24
24
import com .google .common .collect .Lists ;
25
25
import com .mojang .authlib .GameProfile ;
26
- import com .mojang .authlib .properties .Property ;
27
26
import java .lang .invoke .MethodType ;
27
+
28
+ import com .mojang .authlib .properties .Property ;
28
29
import org .bukkit .Bukkit ;
29
30
import org .bukkit .OfflinePlayer ;
30
31
import org .bukkit .block .Block ;
65
66
* @see XMaterial
66
67
*/
67
68
public class XSkull {
69
+
68
70
protected static final MethodHandle
69
71
CRAFT_META_SKULL_PROFILE_GETTER , CRAFT_META_SKULL_PROFILE_SETTER ,
70
- CRAFT_META_SKULL_BLOCK_SETTER , PROPERTY_GETVALUE ;
72
+ CRAFT_META_SKULL_BLOCK_SETTER , PROPERTY_GET_VALUE ;
71
73
72
74
/**
73
75
* Some people use this without quotes surrounding the keys, not sure what that'd work.
@@ -91,7 +93,7 @@ public class XSkull {
91
93
/**
92
94
* In v1.20.2 there were some changes to the mojang API.
93
95
*/
94
- private static final boolean NULLABILITY_RECORD_UPDATE = ReflectionUtils .VERSION .equals ("v1_20_R2" );
96
+ private static final boolean NULLABILITY_RECORD_UPDATE = ReflectionUtils .VERSION .equals ("v1_20_R2" ) || ReflectionUtils . supports ( 21 ) ;
95
97
96
98
/**
97
99
* Does using a random UUID have any advantage?
@@ -106,10 +108,17 @@ public class XSkull {
106
108
* get base64 information from player's UUID.
107
109
*/
108
110
private static final String TEXTURES = "https://textures.minecraft.net/texture/" ;
111
+ private static final Class <?> CLASS_PROPERTY ;
109
112
110
113
static {
114
+ try {
115
+ CLASS_PROPERTY = Class .forName ("com.mojang.authlib.properties.Property" );
116
+ } catch (ClassNotFoundException e ) {
117
+ throw new RuntimeException (e );
118
+ }
119
+
111
120
MethodHandles .Lookup lookup = MethodHandles .lookup ();
112
- MethodHandle profileSetter = null , profileGetter = null , blockSetter = null , propGetval = null ;
121
+ MethodHandle profileSetter = null , profileGetter = null , blockSetter = null , propGetval = null , propConstructor = null ;
113
122
114
123
try {
115
124
Class <?> CraftMetaSkull = ReflectionUtils .getCraftClass ("inventory.CraftMetaSkull" );
@@ -139,16 +148,21 @@ public class XSkull {
139
148
e .printStackTrace ();
140
149
}
141
150
142
- if (! NULLABILITY_RECORD_UPDATE ) {
151
+ if (NULLABILITY_RECORD_UPDATE ) {
143
152
try {
144
- //noinspection JavaLangInvokeHandleSignature
145
- propGetval = lookup .findVirtual (Property .class , "getValue" , MethodType .methodType (String .class ));
153
+ propGetval = lookup .findVirtual (CLASS_PROPERTY , "value" , MethodType .methodType (String .class ));
154
+ } catch (Throwable ex ) {
155
+ ex .printStackTrace ();
156
+ }
157
+ } else {
158
+ try {
159
+ propGetval = lookup .findVirtual (CLASS_PROPERTY , "getValue" , MethodType .methodType (String .class ));
146
160
} catch (Throwable ex ) {
147
161
ex .printStackTrace ();
148
162
}
149
163
}
150
164
151
- PROPERTY_GETVALUE = propGetval ;
165
+ PROPERTY_GET_VALUE = propGetval ;
152
166
CRAFT_META_SKULL_PROFILE_SETTER = profileSetter ;
153
167
CRAFT_META_SKULL_PROFILE_GETTER = profileGetter ;
154
168
CRAFT_META_SKULL_BLOCK_SETTER = blockSetter ;
@@ -310,7 +324,7 @@ public static ItemBuilder.SkullTexture getSkinValue(@NotNull ItemMeta skull) {
310
324
} catch (Exception ignored ) {
311
325
}
312
326
if (profile != null && !profile .getProperties ().get ("textures" ).isEmpty ()) {
313
- for (Property property : profile .getProperties ().get ("textures" )) {
327
+ for (Object property : profile .getProperties ().get ("textures" )) {
314
328
String value = getPropertyValue (property );
315
329
if (!value .isEmpty ()) {
316
330
return new ItemBuilder .SkullTexture (value , profile .getId ());
@@ -321,19 +335,15 @@ public static ItemBuilder.SkullTexture getSkinValue(@NotNull ItemMeta skull) {
321
335
}
322
336
323
337
/**
324
- * They changed {@link Property} to a Java record in 1.20.2
338
+ * They changed Property to a Java record in 1.20.2
325
339
*
326
340
* @since 4.0.1
327
341
*/
328
- private static String getPropertyValue (Property property ) {
329
- if (NULLABILITY_RECORD_UPDATE ) {
330
- return property .value ();
331
- } else {
332
- try {
333
- return (String ) PROPERTY_GETVALUE .invoke (property );
334
- } catch (Throwable e ) {
335
- throw new RuntimeException (e );
336
- }
342
+ private static String getPropertyValue (Object property ) {
343
+ try {
344
+ return (String ) PROPERTY_GET_VALUE .invoke (property );
345
+ } catch (Throwable e ) {
346
+ throw new RuntimeException (e );
337
347
}
338
348
}
339
349
0 commit comments