2016-05-12 04:56:39 +02:00
|
|
|
package com.pahimar.ee3.util.serialize;
|
|
|
|
|
|
|
|
import com.google.gson.*;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.JsonToNBT;
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTException;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
|
|
|
public class ItemStackSerializer implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
private static final String NAME = "name";
|
|
|
|
private static final String META_VALUE = "metaValue";
|
|
|
|
private static final String TAG_COMPOUND = "tagCompound";
|
2016-05-12 04:56:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
|
|
|
|
if (json.isJsonObject()) {
|
2016-05-12 22:02:01 +02:00
|
|
|
JsonObject jsonObject = json.getAsJsonObject();
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
String name = null;
|
|
|
|
int metaValue = 0;
|
|
|
|
NBTTagCompound tagCompound = null;
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-21 05:12:40 +02:00
|
|
|
if (jsonObject.has(NAME) && jsonObject.get(NAME).isJsonPrimitive()) {
|
|
|
|
name = jsonObject.getAsJsonPrimitive(NAME).getAsString();
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:12:40 +02:00
|
|
|
if (jsonObject.has(META_VALUE) && jsonObject.get(META_VALUE).isJsonPrimitive()) {
|
|
|
|
try {
|
|
|
|
metaValue = jsonObject.getAsJsonPrimitive(META_VALUE).getAsInt();
|
|
|
|
}
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
// TODO Logging
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:12:40 +02:00
|
|
|
if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).isJsonPrimitive()) {
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-21 05:12:40 +02:00
|
|
|
try {
|
|
|
|
NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.getAsJsonPrimitive(TAG_COMPOUND).getAsString());
|
2016-05-12 04:56:39 +02:00
|
|
|
if (nbtBase instanceof NBTTagCompound) {
|
2016-05-12 22:02:01 +02:00
|
|
|
tagCompound = (NBTTagCompound) nbtBase;
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-21 05:12:40 +02:00
|
|
|
catch (NBTException e) {
|
|
|
|
// TODO Logging
|
|
|
|
}
|
2016-05-12 22:02:01 +02:00
|
|
|
}
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
if (name != null) {
|
|
|
|
Item item = (Item) Item.itemRegistry.getObject(name);
|
2016-05-12 04:56:39 +02:00
|
|
|
|
|
|
|
if (item != null) {
|
2016-05-12 22:02:01 +02:00
|
|
|
ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(name), 1, metaValue);
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
if (tagCompound != null) {
|
|
|
|
itemStack.setTagCompound(tagCompound);
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
|
|
2016-05-26 18:02:37 +02:00
|
|
|
if (src != null && src.getItem() != null) {
|
2016-05-12 04:56:39 +02:00
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
2016-05-26 18:02:37 +02:00
|
|
|
if (Item.itemRegistry.getNameForObject(src.getItem()) != null) {
|
|
|
|
jsonObject.addProperty(NAME, Item.itemRegistry.getNameForObject(src.getItem()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return JsonNull.INSTANCE;
|
|
|
|
}
|
2016-05-12 22:02:01 +02:00
|
|
|
|
|
|
|
if (src.getItemDamage() != 0) {
|
|
|
|
jsonObject.addProperty(META_VALUE, src.getItemDamage());
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:56:39 +02:00
|
|
|
if (src.getTagCompound() != null) {
|
2016-05-12 22:02:01 +02:00
|
|
|
jsonObject.addProperty(TAG_COMPOUND, src.getTagCompound().toString());
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:02:37 +02:00
|
|
|
return JsonNull.INSTANCE;
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
}
|