package com.carrot.carrotshop.serializer;

import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;

import com.carrot.carrotshop.CarrotShop;
import com.google.common.reflect.TypeToken;

import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;

public class InventorySerializer implements TypeSerializer<Inventory> {

	@Override
	public Inventory deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
		Inventory inv = Inventory.builder().build(CarrotShop.getInstance());
		ConfigurationNode items = value.getNode("items");
		for (ConfigurationNode item : items.getChildrenList()) {
			inv.offer(item.getValue(TypeToken.of(ItemStack.class)));
		}
		return inv;
	}

	@Override
	public void serialize(TypeToken<?> type, Inventory obj, ConfigurationNode value) throws ObjectMappingException {
		ConfigurationNode items = value.getNode("items");
		for (Inventory slot : obj.slots()) {
			if (slot.peek().isPresent()) {
				ConfigurationNode item = items.getAppendedNode();
				item.setValue(TypeToken.of(ItemStack.class), slot.peek().get());
			}
		}
	}
}
@@ -36,16 +36,373 @@
 lizer;%0A%0A
+import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.util.Base64;%0Aimport java.util.Optional;%0A%0Aimport org.spongepowered.api.data.persistence.DataFormats;%0Aimport org.spongepowered.api.data.persistence.InvalidDataException;%0Aimport org.spongepowered.api.data.persistence.InvalidDataFormatException;%0A
 import o
@@ -448,16 +448,16 @@
 entory;%0A
-
 import o
@@ -1067,32 +1067,578 @@
 tionNode items =
+ value.getNode(%22nbtitems%22);%0A%09%09for (ConfigurationNode item : items.getChildrenList()) %7B%0A%09%09%09Optional%3CItemStack%3E itemStack;%0A%09%09%09try %7B%0A%09%09%09%09itemStack = ItemStack.builder().build(DataFormats.NBT.readFrom(new ByteArrayInputStream(Base64.getDecoder().decode(item.getString()))));%0A%09%09%09%09if (itemStack.isPresent()) %7B%0A%09%09%09%09%09inv.offer(itemStack.get());%0A%09%09%09%09%7D%0A%09%09%09%7D catch (InvalidDataException %7C InvalidDataFormatException %7C IOException e) %7B%0A%09%09%09%09CarrotShop.getLogger().warn(%22Error occuered when loading item from NBT%22);%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%0A%09%09%7D%0A%09%09items =
  value.getNode(%22
@@ -1931,32 +1931,90 @@
 tionNode items =
+ value.getNode(%22nbtitems%22);%0A%09%09ConfigurationNode failover =
  value.getNode(%22
@@ -2157,72 +2157,535 @@
 %09%09%09%09
-item.setValue(TypeToken.of(ItemStack.class), slot.peek().get());
+ByteArrayOutputStream os = new ByteArrayOutputStream();%0A%09%09%09%09try %7B%0A%09%09%09%09%09DataFormats.NBT.writeTo(os, slot.peek().get().toContainer());%0A%09%09%09%09%09os.flush();%0A%09%09%09%09%09item.setValue(Base64.getEncoder().encodeToString(os.toByteArray()));%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09ConfigurationNode failoveritem = failover.getAppendedNode();%0A%09%09%09%09%09failoveritem.setValue(TypeToken.of(ItemStack.class), slot.peek().get());%0A%09%09%09%09%09CarrotShop.getLogger().warn(%22Error occuered when saving item as NBT. Trying to save plain%22);%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D
 %0A%09%09%09

Save items as NBT to fix problems with complex items