diff --git a/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullProfile.java b/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullProfile.java new file mode 100644 index 0000000..a1a15ca --- /dev/null +++ b/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullProfile.java @@ -0,0 +1,33 @@ +package eu.decentsoftware.holograms.api.utils.items; + +import com.mojang.authlib.GameProfile; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +class SkullProfile { + public static Constructor gameProfileField; + + static { + try { + Class resolvableProfileClass = Class.forName("net.minecraft.world.item.component.ResolvableProfile"); + Class gameProfileClass = Class.forName("com.mojang.authlib.GameProfile"); + + Constructor constructor = resolvableProfileClass.getDeclaredConstructor(gameProfileClass); + constructor.setAccessible(true); + gameProfileField = constructor; + + } catch (Exception e) { + e.printStackTrace(); + gameProfileField = null; + } + } + + public static Object getObject(GameProfile profile) { + try { + return gameProfileField.newInstance(profile); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullUtils.java b/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullUtils.java index 2b21404..755bbf2 100644 --- a/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullUtils.java +++ b/src/main/java/eu/decentsoftware/holograms/api/utils/items/SkullUtils.java @@ -148,7 +148,17 @@ public static void setSkullTexture(@NonNull ItemStack itemStack, @NonNull String PROFILE_FIELD = meta.getClass().getDeclaredField("profile"); PROFILE_FIELD.setAccessible(true); } - PROFILE_FIELD.set(meta, profile); + + // https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java?at=8d52226914ce4b99f35be3d6954f35616d92476d + // SPIGOT-7882, #1467: Fix conversion of name in Profile Component to empty if it is missing + try { + PROFILE_FIELD.set(meta, profile); + } catch (Throwable ignored) { + PROFILE_FIELD.set( + meta, + SkullProfile.getObject(profile) + ); + } } } itemStack.setItemMeta(meta);