crwdns129887:0crwdne129887:0

crwdns129889:0:javadoc:crwdnd129889:0:javadoc:crwdnd129889:0:javadoc:crwdnd129889:0:javadoc:crwdnd129889:0:javadoc:crwdne129889:0

crwdns129891:0crwdne129891:0

crwdns129893:0crwdne129893:0

crwdns129895:0:javadoc:crwdnd129895:0:javadoc:crwdne129895:0

crwdns129897:0:javadoc:crwdne129897: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

crwdns129899:0:javadoc:crwdnd129899:0:javadoc:crwdnd129899:0:javadoc:crwdne129899:0

crwdns129901:0crwdne129901: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();
}

crwdns129903:0crwdne129903:0

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

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

crwdns129905:0:javadoc:crwdne129905:0

crwdns129907:0crwdne129907:0

crwdns129909:0:javadoc:crwdnd129909:0:javadoc:crwdne129909:0

crwdns129911:0:javadoc:crwdnd129911:0:javadoc:crwdnd129911:0:javadoc:crwdnd129911:0:doc:crwdne129911:0

crwdns129913:0crwdne129913: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);
}

crwdns129915:0crwdne129915: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);
}