crwdns118985:0crwdne118985:0
crwdns118987:0:javadoc:crwdnd118987:0:javadoc:crwdnd118987:0:javadoc:crwdnd118987:0:javadoc:crwdnd118987:0:javadoc:crwdne118987:0
crwdns118989:0crwdne118989:0
crwdns118991:0crwdne118991:0
crwdns118993:0:javadoc:crwdnd118993:0:javadoc:crwdne118993:0
crwdns118995:0:javadoc:crwdne118995: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
crwdns118997:0:javadoc:crwdnd118997:0:javadoc:crwdnd118997:0:javadoc:crwdne118997:0
crwdns118999:0crwdne118999: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();
}
crwdns119001:0crwdne119001:0
import org.spongepowered.api.data.manipulator.mutable.entity.HealthData;
public Optional<HealthData> deserializeHealth(DataView container) {
return Sponge.getDataManager().deserialize(HealthData.class, container);
}
crwdns119003:0:javadoc:crwdne119003:0
crwdns119005:0crwdne119005:0
crwdns119007:0:javadoc:crwdnd119007:0:javadoc:crwdne119007:0
crwdns119009:0:javadoc:crwdnd119009:0:javadoc:crwdnd119009:0:javadoc:crwdnd119009:0:doc:crwdne119009:0
crwdns119011:0crwdne119011: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);
}
crwdns119013:0crwdne119013: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);
}
crwdns119015:0crwdne119015:0
crwdns119017:0:javadoc:crwdnd119017:0:javadoc:crwdne119017:0
crwdns119019:0:javadoc:crwdne119019:0
crwdns119021:0crwdne119021: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;
crwdns119023:0crwdne119023:0
String json = DataFormats.JSON.write(itemStack.toContainer());
crwdns119025:0crwdne119025:0
DataContainer container = DataFormats.JSON.read(json);
crwdns119027:0crwdne119027: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();
}
}
crwdns119029:0crwdne119029: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();
}