use of org.spongepowered.common.data.persistence.datastore.SpongeDataStore 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