Skip to content

Commit

Permalink
Fixed resolvable profile error for setting textures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olzie-12 committed Sep 19, 2024
1 parent e37c0f4 commit fb91ba0
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.decentsoftware.holograms.api.utils.reflect.Version;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.SkullType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -18,6 +19,7 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -41,10 +43,20 @@ public final class SkullUtils {
private static Field PROFILE_FIELD;
private static Method SET_PROFILE_METHOD;
private static boolean INITIALIZED = false;
private static Constructor<?> RESOLVABLE_PROFILE_CONSTRUCTOR;

private static Method PROPERTY_VALUE_METHOD;
private static Function<Property, String> VALUE_RESOLVER;

static {
try {
Class<?> resolvableProfileClass = Class.forName("net.minecraft.world.item.component.ResolvableProfile");
RESOLVABLE_PROFILE_CONSTRUCTOR = resolvableProfileClass.getConstructor(GameProfile.class);
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
// old version, no resolvable profile class.
}
}

/**
* Get the Base64 texture of the given skull ItemStack.
*
Expand Down Expand Up @@ -128,12 +140,11 @@ public static void setSkullTexture(@NonNull ItemStack itemStack, @NonNull String

PropertyMap properties = profile.getProperties();
properties.put("textures", property);

if (SET_PROFILE_METHOD == null && !INITIALIZED) {
try {
// This method only exists in versions 1.16 and up. For older versions, we use reflection
// to set the profile field directly.
SET_PROFILE_METHOD = meta.getClass().getDeclaredMethod("setProfile", GameProfile.class);
SET_PROFILE_METHOD = meta.getClass().getDeclaredMethod("setProfile", RESOLVABLE_PROFILE_CONSTRUCTOR == null ? GameProfile.class : RESOLVABLE_PROFILE_CONSTRUCTOR.getClass());
SET_PROFILE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
// Server is running an older version.
Expand All @@ -142,7 +153,7 @@ public static void setSkullTexture(@NonNull ItemStack itemStack, @NonNull String
}

if (SET_PROFILE_METHOD != null) {
SET_PROFILE_METHOD.invoke(meta, profile);
SET_PROFILE_METHOD.invoke(meta, RESOLVABLE_PROFILE_CONSTRUCTOR == null ? profile : RESOLVABLE_PROFILE_CONSTRUCTOR.newInstance(profile));
} else {
if (PROFILE_FIELD == null) {
PROFILE_FIELD = meta.getClass().getDeclaredField("profile");
Expand Down

0 comments on commit fb91ba0

Please sign in to comment.