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
|
|
|
|
|
|
|
try {
|
2016-05-12 22:02:01 +02:00
|
|
|
if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
|
|
|
name = jsonObject.get(NAME).getAsString();
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IllegalStateException exception) {
|
2016-05-12 22:02:01 +02:00
|
|
|
// TODO We could probably log here that an invalid piece of data was found
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-05-12 22:02:01 +02:00
|
|
|
if (jsonObject.has(META_VALUE) && jsonObject.get(META_VALUE).getAsJsonPrimitive().isNumber()) {
|
|
|
|
metaValue = jsonObject.get(META_VALUE).getAsInt();
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IllegalStateException exception) {
|
2016-05-12 22:02:01 +02:00
|
|
|
// TODO We could probably log here that an invalid piece of data was found
|
2016-05-12 04:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-05-12 22:02:01 +02:00
|
|
|
if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).getAsJsonPrimitive().isString()) {
|
2016-05-12 04:56:39 +02:00
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (NBTException e) {
|
|
|
|
}
|
2016-05-12 22:02:01 +02:00
|
|
|
catch (IllegalStateException exception) {
|
|
|
|
// TODO We could probably log here that an invalid piece of data was found
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
if (src != null) {
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
2016-05-12 22:02:01 +02:00
|
|
|
jsonObject.addProperty(NAME, Item.itemRegistry.getNameForObject(src.getItem()));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|