use of org.spongepowered.api.data.persistence.DataStore in project SpongeCommon by SpongePowered.
the class DataUtil method serializeSpongeData.
public static <T extends SpongeDataHolderBridge & DataCompoundHolder> boolean serializeSpongeData(final T dataHolder) {
CompoundTag compound = dataHolder.data$getCompound();
if (compound == null) {
compound = new CompoundTag();
dataHolder.data$setCompound(compound);
}
// Remove all previous SpongeData
compound.remove(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT.asString("."));
final DataContainer allData = NBTTranslator.INSTANCE.translate(compound);
// Clear old custom data root
final DataView customDataRoot = allData.createView(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT);
// Add back failed data
dataHolder.bridge$getFailedData().forEach(customDataRoot::set);
final DataManipulator.Mutable manipulator = dataHolder.bridge$getManipulator();
final Type dataHolderType = dataHolder.getClass();
manipulator.getKeys().stream().map(key -> SpongeDataManager.getDatastoreRegistry().getDataStore(key, dataHolderType)).forEach(dataStore -> dataStore.serialize(manipulator, allData));
// If data is still present after cleanup merge it back into nbt
if (DataUtil.cleanupEmptySpongeData(allData)) {
compound.merge(NBTTranslator.INSTANCE.translate(allData));
}
if (compound.isEmpty()) {
dataHolder.data$setCompound(null);
return false;
}
return true;
}
use of org.spongepowered.api.data.persistence.DataStore in project SpongeCommon by SpongePowered.
the class CustomDataTest method onRegisterData.
@Listener
public void onRegisterData(final RegisterDataEvent event) {
final ResourceKey key = ResourceKey.of(this.plugin, "mydata");
this.myDataKey = Key.builder().key(key).elementType(Integer.class).build();
final DataProvider<Value<Integer>, Integer> blockDataProvider = DataProvider.mutableBuilder().key(this.myDataKey).dataHolder(ServerLocation.class).get(this::getData).set(this::setData).delete(this::removeData).build();
final DataStore dataStore = DataStore.of(this.myDataKey, DataQuery.of("mykey"), ItemStack.class, User.class, ServerPlayer.class, BlockEntity.class, Entity.class);
final DataRegistration myRegistration = DataRegistration.builder().dataKey(this.myDataKey).store(dataStore).provider(blockDataProvider).build();
event.register(myRegistration);
// Or if it is super simple data
this.mySimpleDataKey = Key.from(this.plugin, "mysimpledata", String.class);
event.register(DataRegistration.of(this.mySimpleDataKey, ItemStack.class));
this.myItemTypeKey = Key.from(this.plugin, "myitemtypedata", ItemType.class);
event.register(DataRegistration.of(this.myItemTypeKey, ItemStack.class));
}
use of org.spongepowered.api.data.persistence.DataStore in project SpongeCommon by SpongePowered.
the class SpongeDataRegistration method getDataStore0.
private Optional<DataStore> getDataStore0(final Type type) {
DataStore dataStore = this.dataStoreMap.get(type);
if (dataStore != null) {
return Optional.of(dataStore);
}
for (final Map.Entry<Type, DataStore> entry : this.dataStoreMap.entrySet()) {
if (GenericTypeReflector.isSuperType(entry.getKey(), type)) {
dataStore = entry.getValue();
this.dataStoreMap.put(type, dataStore);
return Optional.of(dataStore);
}
}
return Optional.empty();
}
use of org.spongepowered.api.data.persistence.DataStore in project SpongeCommon by SpongePowered.
the class DataUtil method deserializeSpongeData.
public static <T extends SpongeDataHolderBridge & DataCompoundHolder> void deserializeSpongeData(final T dataHolder) {
final CompoundTag compound = dataHolder.data$getCompound();
if (compound == null) {
return;
}
final DataContainer allData = NBTTranslator.INSTANCE.translate(compound);
// Upgrade v2->v3
DataUtil.upgradeDataVersion(compound, allData);
// Run content-updaters and collect failed data
final Class<? extends DataHolder> typeToken = dataHolder.getClass().asSubclass(DataHolder.class);
allData.getView(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT).ifPresent(customData -> {
for (final DataQuery keyNamespace : customData.keys(false)) {
final DataView keyedData = customData.getView(keyNamespace).get();
for (final DataQuery keyValue : keyedData.keys(false)) {
final ResourceKey dataStoreKey = ResourceKey.of(keyNamespace.asString("."), keyValue.asString("."));
final DataView dataStoreData = keyedData.getView(keyValue).get();
final Integer contentVersion = dataStoreData.getInt(Constants.Sponge.Data.V3.CONTENT_VERSION).orElse(1);
final Optional<DataStore> dataStore = SpongeDataManager.getDatastoreRegistry().getDataStore(dataStoreKey, typeToken);
if (dataStore.isPresent()) {
if (dataStore.get() instanceof SpongeDataStore) {
((SpongeDataStore) dataStore.get()).getUpdaterFor(contentVersion).ifPresent(updater -> {
dataStoreData.set(Constants.Sponge.Data.V3.CONTENT, updater.update(dataStoreData.getView(Constants.Sponge.Data.V3.CONTENT).get()));
SpongeCommon.logger().info("Updated datastore {} from {} to {} ", dataStoreKey.asString(), contentVersion, ((SpongeDataStore) dataStore.get()).getVersion());
});
}
} else {
dataHolder.bridge$addFailedData(keyNamespace.then(keyValue), dataStoreData);
}
}
}
});
// Initialize sponge data holder
dataHolder.bridge$mergeDeserialized(DataManipulator.mutableOf());
for (final DataStore dataStore : SpongeDataManager.getDatastoreRegistry().getDataStoresForType(typeToken)) {
// Deserialize to Manipulator
final DataManipulator.Mutable deserialized = dataStore.deserialize(allData);
try {
// and set data in CustomDataHolderBridge
dataHolder.bridge$mergeDeserialized(deserialized);
} catch (final Exception e) {
SpongeCommon.logger().error("Could not merge data from datastore: {}", deserialized, e);
}
}
}
Aggregations