Last active
August 29, 2015 14:26
-
-
Save vemacs/893789eb32bfff32ea44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 org.bukkit.Bukkit; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.inventory.meta.SkullMeta; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| public class SkullUtils { | |
| private static String serverVersion; | |
| private static Field metaSkullProfile; | |
| private static Class<?> getOBCClass(String className) { | |
| String fullName = "org.bukkit.craftbukkit." + serverVersion + "." + className; | |
| Class<?> clazz = null; | |
| try { | |
| clazz = Class.forName(fullName); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return clazz; | |
| } | |
| static { | |
| String name = Bukkit.getServer().getClass().getName(); | |
| String[] parts = name.split("\\."); | |
| serverVersion = parts[3]; | |
| Class<?> craftMetaSkull = getOBCClass("inventory.CraftMetaSkull"); | |
| try { | |
| metaSkullProfile = craftMetaSkull.getDeclaredField("profile"); | |
| metaSkullProfile.setAccessible(true); | |
| } catch (NoSuchFieldException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private static Method getProfile; | |
| private static Object getGameProfileFor(Player p) { | |
| if (getProfile == null) { | |
| try { | |
| getProfile = p.getClass().getDeclaredMethod("getProfile"); | |
| getProfile.setAccessible(true); | |
| } catch (NoSuchMethodException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| try { | |
| return getProfile.invoke(p); | |
| } catch (IllegalAccessException | InvocationTargetException e) { | |
| e.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| private static void setGameProfileFor(SkullMeta sm, Object profile) { | |
| try { | |
| metaSkullProfile.set(sm, profile); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void setOwner(SkullMeta sm, String owner) { | |
| Player p = Bukkit.getPlayerExact(owner); | |
| if (p != null) { | |
| setGameProfileFor(sm, getGameProfileFor(p)); | |
| } else { | |
| sm.setOwner(owner); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment