Issue at converting json itemStack to ItemStack.
OOP-778 opened this issue · 5 comments
Hello, here I'm back again.
So I have new issue.
So I convert into gson the itemstack using adapter that uses the method to serialize
new JsonParser().parse(NBTItem.convertItemtoNBT(itemStack).asNBTString())
And to deserialize
'ItemStack itemStack = NBTItem.convertNBTtoItem(new NBTContainer(utf8(jsonElement.toString())));'
The issue is that when deserialized it returns AIR.
I've checked for differences at gson toString() vs asNBTString() and they return same stuff, but there's few differences which I think is causing that to happen. The values are Count and Damage. They're values contain qoutes. Which is why I think this is messing up. Not sure tho.
Here's my test code
`
JsonElement serialized = gson.toJsonTree(event.getItem());
serialized.getAsJsonObject().remove("class");
NBTContainer nbtContainer = new NBTContainer(ItemStackAdapter.utf8(serialized.toString()));
System.out.println("1: " + nbtContainer.asNBTString());
System.out.println("2: " + NBTItem.convertItemtoNBT(event.getItem()).asNBTString());
System.out.println("1v: " + NBTItem.convertNBTtoItem(new NBTContainer(nbtContainer.asNBTString())));
System.out.println("2v: " + NBTItem.convertNBTtoItem(new NBTContainer(NBTItem.convertItemtoNBT(event.getItem()).asNBTString())));`
And here's the output
1: {id:"minecraft:hopper",Count:"4b",tag:{lvl:"1",display:{Lore:["This hopper teleports specified monster type","To it's location & freezes them","* Current mob: Pig"],Name:"[MobGrind] Hopper"},isGlobal:"true",isAuto:"false",ent:"PIG",type:"Grind",name0:"grindHopper"},Damage:"0s"} 2: {id:"minecraft:hopper",Count:4b,tag:{lvl:"1",display:{Lore:["This hopper teleports specified monster type","To it's location & freezes them","* Current mob: Pig"],Name:"[MobGrind] Hopper"},isGlobal:"true",isAuto:"false",ent:"PIG",type:"Grind",name0:"grindHopper"},Damage:0s}
`1v: ItemStack{AIR x 0}
2v: ItemStack{HOPPER x 4, TILE_ENTITY_META:{meta-type=TILE_ENTITY, display-name=[MobGrind] Hopper, lore=[This hopper teleports specified monster type, To it's location & freezes them, * Current mob: Pig], internal=H4sIAAAAAAAAABXKQQqAIBAF0B9mDHWZOkIra9cVjKYQJhW1oNtXywevBQhKbkE1EMhlI2G1grqkiwmNy+NVAvRuJX9W7AvUMhv6xhMZ2iTnN4L29uQe3fFzCjFywgvcWwRyXAAAAA==, blockMaterial=HOPPER}}
`
P.S: Sorry for formatting, github is messed up :D
Don't try to directly use Gson against the ItemStacks. https://github.com/tr7zw/Item-NBT-API/wiki/Converting-Minecraft-Objects-to-NBT-and-Strings has a complete guide on how to convert ItemStack <-> NBT <-> String.
I have an database system where it uses gson to serialize/deserialize objects, and I must have an ItemStack adapter that converts the itmeStack to an JsonObject
Worst-case converte the ItemStacks to a String and then put the String into the JsonObject. And on the other end get the String out and then recreate the Itemstack. But honestly, what database design requires you to have everything as a JsonObject?