Skip to content

Instantly share code, notes, and snippets.

@Shadows-of-Fire
Created August 6, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shadows-of-Fire/b9ea0b463bda375f992773b0b3dc0e4e to your computer and use it in GitHub Desktop.
Save Shadows-of-Fire/b9ea0b463bda375f992773b0b3dc0e4e to your computer and use it in GitHub Desktop.
private static class ItemAdapter extends TypeAdapter<ItemStack> {
@Override
public void write(JsonWriter out, ItemStack s) throws IOException {
out.beginObject();
out.name("item").value(s.getItem().getRegistryName().toString());
out.name("meta").value(s.getItemDamage());
out.name("count").value(s.getCount());
if (s.hasTagCompound()) out.beginObject().name("nbt").value(s.getTagCompound().toString()).endObject();
out.endObject();
}
@Override
public ItemStack read(JsonReader in) throws IOException {
in.beginObject();
in.nextName();
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(in.nextString()));
in.nextName();
int meta = in.nextInt();
in.nextName();
int count = in.nextInt();
NBTTagCompound tag = null;
if (in.hasNext()) {
try {
in.nextName();
tag = JsonToNBT.getTagFromJson(in.nextString());
} catch (NBTException e) {
e.printStackTrace();
}
}
ItemStack stack = new ItemStack(item, count, meta);
stack.setTagCompound(tag);
in.endObject();
return stack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment