Created
April 11, 2024 14:56
-
-
Save Krakenied/a3eac7c0704f606bb2b90309f88101bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mojang.authlib.GameProfile; | |
import com.mojang.authlib.properties.Property; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.jetbrains.annotations.NotNull; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.UUID; | |
/** | |
* An utility class to get skulls with specified base64 texture set. | |
*/ | |
@SuppressWarnings({"CallToPrintStackTrace", "deprecation"}) | |
public final class SkullUtil { | |
private static Method setProfileMethod; | |
private static Field profileField; | |
private static boolean ran; | |
public static @NotNull ItemStack getSkull(final @NotNull String base64, final int amount) { | |
final ItemStack item = new ItemStack(Material.PLAYER_HEAD, amount, (short) 3); | |
final ItemMeta meta = item.getItemMeta(); | |
if (!ran) { | |
final Class<? extends ItemMeta> clazz = meta.getClass(); | |
try { | |
setProfileMethod = clazz.getDeclaredMethod("setProfile", GameProfile.class); | |
setProfileMethod.setAccessible(true); | |
} catch (final NoSuchMethodException ignored) { | |
// introduced in 1.15.1, can be ignored safely | |
} | |
try { | |
profileField = clazz.getDeclaredField("profile"); | |
profileField.setAccessible(true); | |
} catch (final NoSuchFieldException e) { | |
e.printStackTrace(); | |
} | |
ran = true; | |
} | |
final GameProfile profile = new GameProfile(UUID.randomUUID(), ""); | |
profile.getProperties().put("textures", new Property("textures", base64)); | |
setProfileLabel: | |
{ | |
if (setProfileMethod != null) { | |
try { | |
setProfileMethod.invoke(meta, profile); | |
break setProfileLabel; | |
} catch (final IllegalAccessException | InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (profileField != null) { | |
try { | |
profileField.set(meta, profile); | |
break setProfileLabel; | |
} catch (final IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
final IllegalStateException e = new IllegalStateException("could not set profile for player head, server version: " + Bukkit.getBukkitVersion()); | |
e.printStackTrace(); | |
} | |
item.setItemMeta(meta); | |
return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment