Search in sources :

Example 1 with DataCompoundHolder

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

the class ServerPlayerMixin method impl$copyDataOnRespawn.

@Inject(method = "restoreFrom(Lnet/minecraft/server/level/ServerPlayer;Z)V", at = @At("HEAD"))
private void impl$copyDataOnRespawn(final net.minecraft.server.level.ServerPlayer oldPlayer, final boolean respawnFromEnd, final CallbackInfo ci) {
    // Copy Sponge data
    if (oldPlayer instanceof DataCompoundHolder) {
        final DataCompoundHolder oldEntity = (DataCompoundHolder) oldPlayer;
        DataUtil.syncDataToTag(oldEntity);
        final CompoundTag compound = oldEntity.data$getCompound();
        ((DataCompoundHolder) this).data$setCompound(compound);
        DataUtil.syncTagToData(this);
    }
    // Update boss bars
    SpongeAdventure.forEachBossBar(bar -> ((BossEventBridge) bar).bridge$replacePlayer(oldPlayer, (net.minecraft.server.level.ServerPlayer) (Object) this));
}
Also used : DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) CompoundTag(net.minecraft.nbt.CompoundTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with DataCompoundHolder

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

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)2 DataCompoundHolder (org.spongepowered.common.bridge.data.DataCompoundHolder)2 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1 Optional (java.util.Optional)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 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1 SpongeCommon (org.spongepowered.common.SpongeCommon)1 SpongeDataHolderBridge (org.spongepowered.common.bridge.data.SpongeDataHolderBridge)1 NBTTranslator (org.spongepowered.common.data.persistence.NBTTranslator)1 SpongeDataStore (org.spongepowered.common.data.persistence.datastore.SpongeDataStore)1 Constants (org.spongepowered.common.util.Constants)1