Last active
June 2, 2019 17:26
-
-
Save MrMicky-FR/fb2bb4d4673f7d0281c5ef83fb41a377 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
package fr.mrmicky.testplugin; | |
import com.mojang.authlib.GameProfile; | |
import com.mojang.authlib.properties.Property; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.meta.SkullMeta; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.UUID; | |
/** | |
* Small class to apply custom textures to SkullMeta | |
* | |
* @author MrMicky - https://mrmicky.fr | |
*/ | |
public final class SkullUtils { | |
public static void applyTextures(SkullMeta meta, UUID uuid, String value) { | |
applyTextures(meta, uuid, value, null); | |
} | |
public static void applyTextures(SkullMeta meta, UUID uuid, String value, String signature) { | |
GameProfile gameProfile = new GameProfile(uuid, null); | |
gameProfile.getProperties().put("textures", new Property("textures", value, signature)); | |
applyProfile(meta, gameProfile); | |
} | |
public static void applyProfile(SkullMeta meta, Player player) { | |
try { | |
GameProfile gameProfile = (GameProfile) player.getClass().getDeclaredMethod("getProfile").invoke(player); | |
applyProfile(meta, gameProfile); | |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void applyProfile(SkullMeta meta, GameProfile gameProfile) { | |
try { | |
Field profileField = meta.getClass().getDeclaredField("profile"); | |
profileField.setAccessible(true); | |
profileField.set(meta, gameProfile); | |
} catch (NoSuchFieldException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment