crwdns149125:0crwdne149125:0

crwdns149127:0:javadoc:crwdnd149127:0:javadoc:crwdnd149127:0:javadoc:crwdnd149127:0:javadoc:crwdnd149127:0:javadoc:crwdne149127:0

crwdns149129:0crwdne149129:0

crwdns149131:0crwdne149131:0

crwdns149133:0:javadoc:crwdnd149133:0:javadoc:crwdne149133:0

crwdns149135:0:javadoc:crwdne149135:0

import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.key.Keys;

DataContainer serializedHealth = healthData.toContainer();
double currentHealth = serializedHealth.getDouble(Keys.HEALTH.getQuery()).get();
currentHealth == healthData.health().get();  // true

crwdns149137:0:javadoc:crwdnd149137:0:javadoc:crwdnd149137:0:javadoc:crwdne149137:0

crwdns149139:0crwdne149139:0

import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.manipulator.mutable.entity.HealthData;
import org.spongepowered.api.util.persistence.DataBuilder;

import java.util.Optional;

public Optional<HealthData> deserializeHealth(DataView container) {
    final Optional<DataBuilder<HealthData>> builder = Sponge.getDataManager().getBuilder(HealthData.class);
    if (builder.isPresent()) {
        return builder.get().build(container);
    }
    return Optional.empty();
}

crwdns149141:0crwdne149141:0

import org.spongepowered.api.data.manipulator.mutable.entity.HealthData;

public Optional<HealthData> deserializeHealth(DataView container) {
    return Sponge.getDataManager().deserialize(HealthData.class, container);
}

crwdns149143:0:javadoc:crwdne149143:0

crwdns149145:0crwdne149145:0

crwdns149147:0:javadoc:crwdnd149147:0:javadoc:crwdne149147:0

crwdns149149:0:javadoc:crwdnd149149:0:javadoc:crwdnd149149:0:javadoc:crwdnd149149:0:doc:crwdne149149:0

crwdns149151:0crwdne149151:0

import ninja.leaping.configurate.ConfigurationNode;
import org.spongepowered.api.data.persistence.DataTranslator;
import org.spongepowered.api.data.persistence.DataTranslators;

public ConfigurationNode translateToConfig(HealthData data) {
    final DataTranslator<ConfigurationNode> translator = DataTranslators.CONFIGURATION_NODE;
    final DataView container = data.toContainer();
    return translator.translate(container);
}

crwdns149153:0crwdne149153:0

import java.util.Optional;

public Optional<HealthData> translateFromConfig(ConfigurationNode node) {
    final DataTranslator<ConfigurationNode> translator = DataTranslators.CONFIGURATION_NODE;
    final DataView container = translator.translate(node);
    return deserializeHealth(container);
}

crwdns149155:0crwdne149155:0

crwdns149157:0:javadoc:crwdnd149157:0:javadoc:crwdne149157:0

crwdns149159:0:javadoc:crwdne149159:0

crwdns149161:0crwdne149161: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;

crwdns149163:0crwdne149163:0

String json = DataFormats.JSON.write(itemStack.toContainer());

crwdns149165:0crwdne149165:0

DataContainer container = DataFormats.JSON.read(json);

crwdns149167:0crwdne149167: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();
    }
}

crwdns149169:0crwdne149169: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();
}