Created
May 31, 2017 05:56
-
-
Save odenforge/b453d7861df5d283cc63408e34bdae6e to your computer and use it in GitHub Desktop.
Item stack serial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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