From 6fc77234660fc1bf313b88a2da363ed94cbf51dd Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 26 Mar 2013 09:16:09 +0100 Subject: [PATCH] Add a gson adapter for PlayerInventory. --- src/com/massivecraft/mcore/MCore.java | 3 ++ .../mcore/adapter/InventoryAdapter.java | 14 +++---- .../mcore/adapter/PlayerInventoryAdapter.java | 39 +++++++++++++++++++ .../mcore/util/InventoryUtil.java | 5 +++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/com/massivecraft/mcore/adapter/PlayerInventoryAdapter.java diff --git a/src/com/massivecraft/mcore/MCore.java b/src/com/massivecraft/mcore/MCore.java index 357561ed..3053715c 100644 --- a/src/com/massivecraft/mcore/MCore.java +++ b/src/com/massivecraft/mcore/MCore.java @@ -9,10 +9,12 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import com.massivecraft.mcore.adapter.InventoryAdapter; import com.massivecraft.mcore.adapter.ItemStackAdapter; import com.massivecraft.mcore.adapter.MongoURIAdapter; +import com.massivecraft.mcore.adapter.PlayerInventoryAdapter; import com.massivecraft.mcore.cmd.CmdMcore; import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures; import com.massivecraft.mcore.mixin.ScheduledTeleportEngine; @@ -67,6 +69,7 @@ public class MCore extends MPlugin .registerTypeAdapter(MongoURI.class, MongoURIAdapter.get()) .registerTypeAdapter(ItemStack.class, ItemStackAdapter.get()) .registerTypeAdapter(Inventory.class, InventoryAdapter.get()) + .registerTypeAdapter(PlayerInventory.class, PlayerInventoryAdapter.get()) .registerTypeAdapter(PS.class, PSAdapter.get()); } diff --git a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java index 1a9f661e..e12be197 100644 --- a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java +++ b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java @@ -38,6 +38,13 @@ public class InventoryAdapter implements JsonDeserializer, JsonSerial public static final String CHESTPLATE = "chestplate"; public static final String LEGGINGS = "leggings"; public static final String BOOTS = "boots"; + + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static InventoryAdapter i = new InventoryAdapter(); + public static InventoryAdapter get() { return i; } // -------------------------------------------- // // IMPLEMENTATION @@ -214,11 +221,4 @@ public class InventoryAdapter implements JsonDeserializer, JsonSerial return ret; } - // -------------------------------------------- // - // INSTANCE - // -------------------------------------------- // - - public static InventoryAdapter i = new InventoryAdapter(); - public static InventoryAdapter get() { return i; } - } \ No newline at end of file diff --git a/src/com/massivecraft/mcore/adapter/PlayerInventoryAdapter.java b/src/com/massivecraft/mcore/adapter/PlayerInventoryAdapter.java new file mode 100644 index 00000000..a4f30b78 --- /dev/null +++ b/src/com/massivecraft/mcore/adapter/PlayerInventoryAdapter.java @@ -0,0 +1,39 @@ +package com.massivecraft.mcore.adapter; + +import java.lang.reflect.Type; + +import org.bukkit.inventory.PlayerInventory; + +import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.mcore.xlib.gson.JsonDeserializer; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonParseException; +import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; +import com.massivecraft.mcore.xlib.gson.JsonSerializer; + +public class PlayerInventoryAdapter implements JsonDeserializer, JsonSerializer +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static PlayerInventoryAdapter i = new PlayerInventoryAdapter(); + public static PlayerInventoryAdapter get() { return i; } + + // -------------------------------------------- // + // IMPLEMENTATION + // -------------------------------------------- // + + @Override + public JsonElement serialize(PlayerInventory src, Type typeOfSrc, JsonSerializationContext context) + { + return InventoryAdapter.toJson(src); + } + + @Override + public PlayerInventory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + return (PlayerInventory) InventoryAdapter.fromJson(json); + } + +} \ No newline at end of file diff --git a/src/com/massivecraft/mcore/util/InventoryUtil.java b/src/com/massivecraft/mcore/util/InventoryUtil.java index d7db13ad..90f4bc76 100644 --- a/src/com/massivecraft/mcore/util/InventoryUtil.java +++ b/src/com/massivecraft/mcore/util/InventoryUtil.java @@ -218,6 +218,11 @@ public class InventoryUtil return ret; } + public static PlayerInventory cloneInventory(PlayerInventory inventory) + { + return (PlayerInventory)cloneInventory((Inventory)inventory); + } + // -------------------------------------------- // // SET CONTENT // -------------------------------------------- //