Proper erialization of SkullMeta. Include the UUID.

This commit is contained in:
Olof Larsson 2015-04-14 11:20:04 +02:00
parent 79e1d4171e
commit 2592772d8a
2 changed files with 152 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.block.banner.Pattern; 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.ItemFlag;
import org.bukkit.inventory.meta.BannerMeta; import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.ItemMeta; 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.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; 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 ITEM_FLAGS = "flags";
public static final String SKULL_OWNER_ID = "skullid";
public static final String BANNER_BASE = "banner-base"; public static final String BANNER_BASE = "banner-base";
public static final String BANNER_PATTERNS = "banner"; 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 // SPECIFIC META: BANNER
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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);
}
}