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