Hi!
My goal is to save any ItemStackSnapshot
(with data or custom data) in a global way. I don’t like to attach the ItemStackSnapshot to a player or something else. It should be accessible globally. I decided to use a config file.
There are DataTranslator
s for translating a DataContainer
to a ConfigurationNode
. That works as intended.
Deserializing a ConfigurationNode
back to a DataView
also works(the data is present when debugging it).
The problem arises when I try to get the serialized ItemStackSnapshot
out of the DataView
.
Here is my little test code:
CommentedConfigurationNode rootNode = configLoader.load();
CommentedConfigurationNode node = rootNode.getNode("itemstack-snapshot");
ItemStackSnapshot itemStackSnapshot = ItemStack.builder()
.quantity(1)
.itemType(ItemTypes.WRITTEN_BOOK)
.add(Keys.BOOK_AUTHOR, Text.of("Author One"))
.build().createSnapshot();
// To node
ConfigurationNode itemStackNode = DataTranslators.CONFIGURATION_NODE.translate(itemStackSnapshot.toContainer());
// From node
DataContainer dataContainer = DataTranslators.CONFIGURATION_NODE.translate(itemStackNode);
ItemStackSnapshot deserializedItemStackSnapshot = Sponge.getDataManager()
.deserialize(ItemStackSnapshot.class, dataContainer).orElse(null);
// And see result in config file for easier debugging
node.setValue(itemStackNode);
configLoader.save(rootNode);
The critical part is
ItemStackSnapshot deserializedItemStackSnapshot = Sponge.getDataManager() .deserialize(ItemStackSnapshot.class, dataContainer).orElse(null);
The deserializedItemStackSnapshot
isn’t null
, that’s not the problem. The problem is that the data(here the author) is not present anymore. It was present in the deserialized DataView
.
Thanks in advance!