Skip to content

Instantly share code, notes, and snippets.

@odenforge
Created May 31, 2017 05:56
Show Gist options
  • Save odenforge/b453d7861df5d283cc63408e34bdae6e to your computer and use it in GitHub Desktop.
Save odenforge/b453d7861df5d283cc63408e34bdae6e to your computer and use it in GitHub Desktop.
Item stack serial
public String serializeItemStack(ItemStack itemStack) {
ConfigurationNode node = DataTranslators.CONFIGURATION_NODE.translate(itemStack.createSnapshot().toContainer());
StringWriter stringWriter = new StringWriter();
try {
HoconConfigurationLoader.builder().setSink(() -> new BufferedWriter(stringWriter)).build().save(node);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return stringWriter.toString();
}
public ItemStack deserializeItemStack(String item) {
ConfigurationNode node = null;
try {
node = HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(item))).build().load();
} catch (IOException e) {
e.printStackTrace();
}
if(node != null){
DataView dataView = DataTranslators.CONFIGURATION_NODE.translate(node);
Optional<ItemStackSnapshot> Item = Sponge.getDataManager().deserialize(ItemStackSnapshot.class, dataView);
if(Item.isPresent()){
return Item.get().createStack();
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment