Skip to content

Commit 07555ea

Browse files
Fix #91
1 parent 86468c9 commit 07555ea

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionRegistry.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.authlib.GameProfile;
44
import org.bukkit.Bukkit;
55

6+
import java.lang.reflect.Constructor;
67
import java.lang.reflect.Field;
78
import java.lang.reflect.Method;
89

@@ -17,10 +18,15 @@ public class ReflectionRegistry {
1718
public static final Class<?> PAPER_PLUGIN_CLASS_LOADER_CLASS = getClassOrNull("io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader");
1819
public static final Class<?> CB_CRAFT_META_SKULL_CLASS = getCBClass("inventory.CraftMetaSkull");
1920
public static final Class<?> CB_CRAFT_META_ITEM_CLASS = getCBClass("inventory.CraftMetaItem");
21+
public static final Class<?> RESOLVABLE_PROFILE_CLASS = getClassOrNull("net.minecraft.world.item.component.ResolvableProfile");
22+
23+
// Constructors
24+
public static final Constructor<?> RESOLVABLE_PROFILE_FROM_GAME_PROFILE_CONSTRUCTOR = getConstructorOrNull(RESOLVABLE_PROFILE_CLASS, false, GameProfile.class);
2025

2126
// Methods
2227
public static final Method PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD;
2328
public static final Method CB_CRAFT_META_SKULL_SET_PROFILE_METHOD = getMethodOrNull(CB_CRAFT_META_SKULL_CLASS, true, "setProfile", GameProfile.class); // since spigot 1.14.4 or paper 1.15.1
29+
public static final Method CB_CRAFT_META_SKULL_SET_RESOLVABLE_PROFILE_METHOD = getMethodOrNull(CB_CRAFT_META_SKULL_CLASS, true, "setProfile", RESOLVABLE_PROFILE_CLASS);
2430

2531
// Fields
2632
public static final Field PLUGIN_CLASS_LOADER_PLUGIN_FIELD = getField(PLUGIN_CLASS_LOADER_CLASS, true, "plugin");

inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ public class ReflectionUtils {
5959
}
6060
}
6161

62+
public static <T> @Nullable Constructor<T> getConstructorOrNull(@Nullable Class<T> clazz, boolean declared, @Nullable Class<?> @NotNull ... parameterTypes) {
63+
if (clazz == null)
64+
return null;
65+
66+
try {
67+
Constructor<T> constructor = declared ? clazz.getDeclaredConstructor(parameterTypes) : clazz.getConstructor(parameterTypes);
68+
if (declared) constructor.setAccessible(true);
69+
return constructor;
70+
} catch (Throwable t) {
71+
return null;
72+
}
73+
}
74+
6275
public static <T> @NotNull T constructEmpty(@NotNull Class<?> clazz) {
6376
try {
6477
return (T) getConstructor(clazz, true).newInstance();
@@ -85,7 +98,10 @@ public class ReflectionUtils {
8598
}
8699
}
87100

88-
public static @Nullable Method getMethodOrNull(@NotNull Class<?> clazz, boolean declared, @NotNull String name, @NotNull Class<?> @NotNull ... parameterTypes) {
101+
public static @Nullable Method getMethodOrNull(@Nullable Class<?> clazz, boolean declared, @NotNull String name, @Nullable Class<?> @NotNull ... parameterTypes) {
102+
if (clazz == null)
103+
return null;
104+
89105
try {
90106
Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes);
91107
if (declared) method.setAccessible(true);

invui-core/src/main/java/xyz/xenondevs/invui/item/builder/SkullBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ private void setGameProfile(@NotNull HeadTexture texture) {
7474
ItemMeta meta = item.getItemMeta();
7575

7676
if (gameProfile != null) {
77-
if (ReflectionRegistry.CB_CRAFT_META_SKULL_SET_PROFILE_METHOD != null) {
77+
if (ReflectionRegistry.CB_CRAFT_META_SKULL_SET_RESOLVABLE_PROFILE_METHOD != null && ReflectionRegistry.RESOLVABLE_PROFILE_FROM_GAME_PROFILE_CONSTRUCTOR != null) {
78+
Object resolvableProfile = ReflectionUtils.construct(ReflectionRegistry.RESOLVABLE_PROFILE_FROM_GAME_PROFILE_CONSTRUCTOR, gameProfile);
79+
ReflectionUtils.invokeMethod(ReflectionRegistry.CB_CRAFT_META_SKULL_SET_RESOLVABLE_PROFILE_METHOD, meta, resolvableProfile);
80+
} else if (ReflectionRegistry.CB_CRAFT_META_SKULL_SET_PROFILE_METHOD != null) {
7881
ReflectionUtils.invokeMethod(ReflectionRegistry.CB_CRAFT_META_SKULL_SET_PROFILE_METHOD, meta, gameProfile);
7982
} else {
8083
ReflectionUtils.setFieldValue(ReflectionRegistry.CB_CRAFT_META_SKULL_PROFILE_FIELD, meta, gameProfile);

0 commit comments

Comments
 (0)