From 2592772d8ae4499a888a5255930e05a6d0db65d8 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 14 Apr 2015 11:20:04 +0200 Subject: [PATCH] Proper erialization of SkullMeta. Include the UUID. --- .../adapter/ItemStackAdapterInnerV1_8.java | 41 +++++++ .../massivecore/util/HeadUtil.java | 111 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/com/massivecraft/massivecore/util/HeadUtil.java diff --git a/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java b/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java index 997be06f..a0aef88b 100644 --- a/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java +++ b/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java @@ -5,6 +5,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.UUID; import org.bukkit.DyeColor; import org.bukkit.block.banner.Pattern; @@ -12,7 +13,9 @@ import org.bukkit.block.banner.PatternType; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.meta.BannerMeta; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; +import com.massivecraft.massivecore.util.HeadUtil; import com.massivecraft.massivecore.xlib.gson.JsonArray; import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonObject; @@ -29,6 +32,8 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7 public static final String ITEM_FLAGS = "flags"; + public static final String SKULL_OWNER_ID = "skullid"; + public static final String BANNER_BASE = "banner-base"; public static final String BANNER_PATTERNS = "banner"; @@ -152,6 +157,42 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7 } } + // -------------------------------------------- // + // SPECIFIC META: SKULL + // -------------------------------------------- // + + @Override + public void transferSkull(SkullMeta meta, JsonObject json, boolean meta2json) + { + if (meta2json) + { + if ( ! meta.hasOwner()) return; + + String name = HeadUtil.getName(meta); + if (name != null) json.addProperty(SKULL_OWNER, name); + + UUID id = HeadUtil.getId(meta); + if (id != null) json.addProperty(SKULL_OWNER_ID, id.toString()); + } + else + { + JsonElement element; + + String name = null; + element = json.get(SKULL_OWNER); + if (element != null) name = element.getAsString(); + + UUID id = null; + element = json.get(SKULL_OWNER_ID); + if (element != null) id = UUID.fromString(element.getAsString()); + + if (name != null || id != null) + { + HeadUtil.set(meta, name, id); + } + } + } + // -------------------------------------------- // // SPECIFIC META: BANNER // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/util/HeadUtil.java b/src/com/massivecraft/massivecore/util/HeadUtil.java new file mode 100644 index 00000000..7a611b19 --- /dev/null +++ b/src/com/massivecraft/massivecore/util/HeadUtil.java @@ -0,0 +1,111 @@ +package com.massivecraft.massivecore.util; + +import java.lang.reflect.Field; +import java.util.UUID; + +import org.bukkit.inventory.meta.SkullMeta; + +import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; + +public class HeadUtil +{ + // -------------------------------------------- // + // REFLECTION CACHE + // -------------------------------------------- // + + public static Class classCraftMetaSkull; + public static Field fieldCraftMetaSkullDotProfile; + + public static Class classGameProfile; + public static Field fieldGameProfileDotId; + public static Field fieldGameProfileDotName; + + static + { + try + { + classCraftMetaSkull = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftMetaSkull"); + fieldCraftMetaSkullDotProfile = ReflectionUtil.getField(classCraftMetaSkull, "profile"); + + classGameProfile = Class.forName("com.mojang.authlib.GameProfile"); + fieldGameProfileDotId = ReflectionUtil.getField(classGameProfile, "id"); + fieldGameProfileDotName = ReflectionUtil.getField(classGameProfile, "name"); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + // -------------------------------------------- // + // GAME PROFILE SIMPLE + // -------------------------------------------- // + + public static Object getGameProfile(SkullMeta meta) + { + return ReflectionUtil.getField(fieldCraftMetaSkullDotProfile, meta); + } + + public static void setGameProfile(SkullMeta meta, Object gameProfile) + { + ReflectionUtil.setField(fieldCraftMetaSkullDotProfile, meta, gameProfile); + } + + // -------------------------------------------- // + // GET + // -------------------------------------------- // + + public static String getGameProfileName(Object gameProfile) + { + return (String) ReflectionUtil.getField(fieldGameProfileDotName, gameProfile); + } + + public static UUID getGameProfileId(Object gameProfile) + { + return (UUID) ReflectionUtil.getField(fieldGameProfileDotId, gameProfile); + } + + // -------------------------------------------- // + // SET + // -------------------------------------------- // + + public static void setGameProfileName(Object gameProfile, String name) + { + ReflectionUtil.setField(fieldGameProfileDotName, gameProfile, name); + } + + public static void setGameProfileId(Object gameProfile, UUID id) + { + ReflectionUtil.setField(fieldGameProfileDotId, gameProfile, id); + } + + // -------------------------------------------- // + // ASDF + // -------------------------------------------- // + + public static String getName(SkullMeta meta) + { + // Object gameProfile = getGameProfile(meta); + // if (gameProfile == null) return null; + // return getGameProfileName(gameProfile); + + return meta.getOwner(); + } + + public static UUID getId(SkullMeta meta) + { + Object gameProfile = getGameProfile(meta); + if (gameProfile == null) return null; + return getGameProfileId(gameProfile); + } + + public static void set(SkullMeta meta, String name, UUID id) + { + meta.setOwner(name != null ? name : "adsf"); + + Object gameProfile = getGameProfile(meta); + setGameProfileName(gameProfile, name); + setGameProfileId(gameProfile, id); + } + +}