Skip to content

Instantly share code, notes, and snippets.

@MrMicky-FR
Last active June 2, 2019 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMicky-FR/fb2bb4d4673f7d0281c5ef83fb41a377 to your computer and use it in GitHub Desktop.
Save MrMicky-FR/fb2bb4d4673f7d0281c5ef83fb41a377 to your computer and use it in GitHub Desktop.
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