Search in sources :

Example 1 with DataStore

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;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) Constants(org.spongepowered.common.util.Constants) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge) DataHolder(org.spongepowered.api.data.DataHolder) DataView(org.spongepowered.api.data.persistence.DataView) SpongeCommon(org.spongepowered.common.SpongeCommon) Queries(org.spongepowered.api.data.persistence.Queries) NBTTranslator(org.spongepowered.common.data.persistence.NBTTranslator) DataManipulator(org.spongepowered.api.data.DataManipulator) DataStore(org.spongepowered.api.data.persistence.DataStore) CompoundTag(net.minecraft.nbt.CompoundTag) DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) DataQuery(org.spongepowered.api.data.persistence.DataQuery) Type(java.lang.reflect.Type) ResourceKey(org.spongepowered.api.ResourceKey) Optional(java.util.Optional) Collections(java.util.Collections) SpongeDataStore(org.spongepowered.common.data.persistence.datastore.SpongeDataStore) DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer) Type(java.lang.reflect.Type) DataManipulator(org.spongepowered.api.data.DataManipulator) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with DataStore

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));
}
Also used : DataRegistration(org.spongepowered.api.data.DataRegistration) DataStore(org.spongepowered.api.data.persistence.DataStore) ItemType(org.spongepowered.api.item.ItemType) Value(org.spongepowered.api.data.value.Value) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ResourceKey(org.spongepowered.api.ResourceKey) Listener(org.spongepowered.api.event.Listener)

Example 3 with DataStore

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();
}
Also used : Type(java.lang.reflect.Type) DataStore(org.spongepowered.api.data.persistence.DataStore) Map(java.util.Map)

Example 4 with DataStore

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);
        }
    }
}
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

DataStore (org.spongepowered.api.data.persistence.DataStore)4 ResourceKey (org.spongepowered.api.ResourceKey)3 Type (java.lang.reflect.Type)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 DataManipulator (org.spongepowered.api.data.DataManipulator)2 DataContainer (org.spongepowered.api.data.persistence.DataContainer)2 DataQuery (org.spongepowered.api.data.persistence.DataQuery)2 DataView (org.spongepowered.api.data.persistence.DataView)2 SpongeDataStore (org.spongepowered.common.data.persistence.datastore.SpongeDataStore)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 DataHolder (org.spongepowered.api.data.DataHolder)1 DataRegistration (org.spongepowered.api.data.DataRegistration)1 Queries (org.spongepowered.api.data.persistence.Queries)1 Value (org.spongepowered.api.data.value.Value)1 Listener (org.spongepowered.api.event.Listener)1 ItemType (org.spongepowered.api.item.ItemType)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 SpongeCommon (org.spongepowered.common.SpongeCommon)1