Search in sources :

Example 1 with SpongeDataHolderBridge

use of org.spongepowered.common.bridge.data.SpongeDataHolderBridge 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 SpongeDataHolderBridge

use of org.spongepowered.common.bridge.data.SpongeDataHolderBridge in project SpongeCommon by SpongePowered.

the class EntityMaxAirMixin method bridge$setMaxAir.

@Override
public void bridge$setMaxAir(int maxAir) {
    Entity entity = (Entity) (Object) this;
    if (entity.getAirSupply() > maxAir) {
        entity.setAirSupply(maxAir);
    }
    if (this.bridge$getDefaultMaxAir() == maxAir) {
        this.impl$maxAir = null;
        ((SpongeDataHolderBridge) this).bridge$remove(Keys.MAX_AIR);
        return;
    }
    this.impl$maxAir = maxAir;
    ((SpongeDataHolderBridge) this).bridge$offer(Keys.MAX_AIR, maxAir);
}
Also used : Entity(net.minecraft.world.entity.Entity) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge)

Example 3 with SpongeDataHolderBridge

use of org.spongepowered.common.bridge.data.SpongeDataHolderBridge in project SpongeCommon by SpongePowered.

the class SpongeEntitySnapshotBuilder method from.

@Override
public SpongeEntitySnapshotBuilder from(final Entity entity) {
    this.reset();
    this.entityReference = new WeakReference<>(entity);
    this.worldKey = entity.serverLocation().worldKey();
    this.position = entity.transform().position();
    this.rotation = entity.transform().rotation();
    this.scale = entity.transform().scale();
    this.entityType = entity.type();
    this.uniqueId = entity.uniqueId();
    this.manipulator = ((SpongeDataHolderBridge) entity).bridge$getManipulator().copy();
    this.compound = new CompoundTag();
    ((net.minecraft.world.entity.Entity) entity).saveWithoutId(this.compound);
    return this;
}
Also used : Entity(org.spongepowered.api.entity.Entity) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with SpongeDataHolderBridge

use of org.spongepowered.common.bridge.data.SpongeDataHolderBridge in project SpongeCommon by SpongePowered.

the class MapItemSavedDataMixin method bridge$setDecorations.

@SuppressWarnings("SuspiciousMethodCalls")
@Override
public void bridge$setDecorations(final Set<org.spongepowered.api.map.decoration.MapDecoration> newDecorations) {
    this.decorations.clear();
    for (final org.spongepowered.api.map.decoration.MapDecoration decoration : newDecorations) {
        this.impl$addDecorationToDecorationsMapIfNotExists((MapDecoration) decoration);
    }
    for (final MapDecoration existingDecoration : this.decorations.values()) {
        if (!newDecorations.contains(existingDecoration)) {
            // Removed.
            ((MapDecorationBridge) existingDecoration).notifyRemovedFromMap((MapInfo) this);
            this.setDirty();
        }
    }
    if (newDecorations.isEmpty()) {
        ((SpongeDataHolderBridge) this).bridge$remove(Keys.MAP_DECORATIONS);
    } else {
        ((SpongeDataHolderBridge) this).bridge$offer(Keys.MAP_DECORATIONS, newDecorations);
    }
}
Also used : MapDecoration(net.minecraft.world.level.saveddata.maps.MapDecoration) MapDecorationBridge(org.spongepowered.common.bridge.world.storage.MapDecorationBridge) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge)

Aggregations

SpongeDataHolderBridge (org.spongepowered.common.bridge.data.SpongeDataHolderBridge)4 CompoundTag (net.minecraft.nbt.CompoundTag)2 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Entity (net.minecraft.world.entity.Entity)1 MapDecoration (net.minecraft.world.level.saveddata.maps.MapDecoration)1 ResourceKey (org.spongepowered.api.ResourceKey)1 DataHolder (org.spongepowered.api.data.DataHolder)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 Queries (org.spongepowered.api.data.persistence.Queries)1 Entity (org.spongepowered.api.entity.Entity)1 SpongeCommon (org.spongepowered.common.SpongeCommon)1 DataCompoundHolder (org.spongepowered.common.bridge.data.DataCompoundHolder)1 MapDecorationBridge (org.spongepowered.common.bridge.world.storage.MapDecorationBridge)1 NBTTranslator (org.spongepowered.common.data.persistence.NBTTranslator)1