crwdns138625:0crwdne138625:0
Warning
crwdns138627:0crwdne138627:0
crwdns156967:0:javadoc:crwdnd156967:0:javadoc:crwdnd156967:0:javadoc:crwdnd156967:0:javadoc:crwdnd156967:0:javadoc:crwdne156967:0
crwdns138631:0crwdne138631:0
crwdns138633:0crwdne138633:0
crwdns138635:0:javadoc:crwdnd138635:0:javadoc:crwdne138635:0
crwdns138657:0crwdne138657:0
crwdns156969:0:javadoc:crwdnd156969:0:javadoc:crwdne156969:0
crwdns138661:0:javadoc:crwdne138661:0
crwdns138663:0crwdne138663:0
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.persistence.DataFormats;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
crwdns138665:0crwdne138665:0
String json = DataFormats.JSON.write(itemStack.toContainer());
crwdns138667:0crwdne138667:0
DataContainer container = DataFormats.JSON.read(json);
crwdns138669:0crwdne138669:0
public void writeItemStackSnapshotToFile(ItemStackSnapshot itemStackSnapshot, Path path) {
DataContainer dataContainer = itemStackSnapshot.toContainer();
try (OutputStream outputStream = Files.newOutputStream(path)) {
DataFormats.NBT.writeTo(outputStream, dataContainer);
} catch (IOException e) {
// For the purposes of this example, we just print the error to the console. However,
// as this exception indicates the file didn't save, you should handle this in a way
// more suitable for your plugin.
e.printStackTrace();
}
}
crwdns138671:0crwdne138671:0
public Optional<ItemStackSnapshot> readItemStackSnapshotFromFile(Path path) {
try (InputStream inputStream = Files.newInputStream(path)) {
DataContainer dataContainer = DataFormats.NBT.readFrom(inputStream);
return Sponge.getDataManager().deserialize(ItemStackSnapshot.class, dataContainer);
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}