Search in sources :

Example 1 with SpongeDataStore

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);
        }
    }
}
Also used : DataManipulator(org.spongepowered.api.data.DataManipulator) ResourceKey(org.spongepowered.api.ResourceKey) DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer) SpongeDataStore(org.spongepowered.common.data.persistence.datastore.SpongeDataStore) DataStore(org.spongepowered.api.data.persistence.DataStore) SpongeDataStore(org.spongepowered.common.data.persistence.datastore.SpongeDataStore) DataQuery(org.spongepowered.api.data.persistence.DataQuery) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)1 ResourceKey (org.spongepowered.api.ResourceKey)1 DataManipulator (org.spongepowered.api.data.DataManipulator)1 DataContainer (org.spongepowered.api.data.persistence.DataContainer)1 DataQuery (org.spongepowered.api.data.persistence.DataQuery)1 DataStore (org.spongepowered.api.data.persistence.DataStore)1 DataView (org.spongepowered.api.data.persistence.DataView)1 SpongeDataStore (org.spongepowered.common.data.persistence.datastore.SpongeDataStore)1