Search in sources :

Example 1 with SerializedDataTransaction

use of org.spongepowered.common.data.persistence.SerializedDataTransaction in project SpongeCommon by SpongePowered.

the class SpongeItemStackBuilder method fromContainer.

@Override
public ItemStack.Builder fromContainer(DataView container) {
    checkNotNull(container);
    if (!container.contains(DataQueries.ITEM_TYPE) || !container.contains(DataQueries.ITEM_COUNT) || !container.contains(DataQueries.ITEM_DAMAGE_VALUE)) {
        return this;
    }
    reset();
    final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
    quantity(count);
    final String itemTypeId = getData(container, DataQueries.ITEM_TYPE, String.class);
    final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemTypeId).get();
    itemType(itemType);
    this.damageValue = getData(container, DataQueries.ITEM_DAMAGE_VALUE, Integer.class);
    if (container.contains(DataQueries.UNSAFE_NBT)) {
        final NBTTagCompound compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
        if (compound.hasKey(NbtDataUtil.SPONGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
            compound.removeTag(NbtDataUtil.SPONGE_DATA);
        }
        this.compound = compound;
    }
    if (container.contains(DataQueries.DATA_MANIPULATORS)) {
        final List<DataView> views = container.getViewList(DataQueries.DATA_MANIPULATORS).get();
        final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
        final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
        this.itemDataSet = new HashSet<>();
        manipulators.forEach(this.itemDataSet::add);
    }
    return this;
}
Also used : DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ItemType(org.spongepowered.api.item.ItemType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 2 with SerializedDataTransaction

use of org.spongepowered.common.data.persistence.SerializedDataTransaction in project SpongeCommon by SpongePowered.

the class MixinItemStack method readFromNbt.

@Override
public void readFromNbt(NBTTagCompound compound) {
    if (compound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
        final NBTTagList list = compound.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
        if (!list.hasNoTags()) {
            compound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
            final List<DataView> views = Lists.newArrayList();
            for (int i = 0; i < list.tagCount(); i++) {
                final NBTTagCompound dataCompound = list.getCompoundTagAt(i);
                views.add(NbtTranslator.getInstance().translateFrom(dataCompound));
            }
            final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
            final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
            for (DataManipulator<?, ?> manipulator : manipulators) {
                offerCustom(manipulator, MergeFunction.IGNORE_ALL);
            }
            if (!transaction.failedData.isEmpty()) {
                addFailedData(transaction.failedData);
            }
        } else {
            compound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
            if (compound.hasNoTags()) {
                getTagCompound().removeTag(NbtDataUtil.SPONGE_DATA);
                return;
            }
        }
    }
    if (compound.hasKey(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_LIST)) {
        final NBTTagList list = compound.getTagList(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_COMPOUND);
        final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
        if (list.tagCount() != 0) {
            for (int i = 0; i < list.tagCount(); i++) {
                final NBTTagCompound internal = list.getCompoundTagAt(i);
                builder.add(NbtTranslator.getInstance().translateFrom(internal));
            }
        }
        // Re-attempt to deserialize custom data
        final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
        final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
        for (DataManipulator<?, ?> manipulator : manipulators) {
            offer(manipulator);
        }
        if (!transaction.failedData.isEmpty()) {
            this.addFailedData(transaction.failedData);
        }
    }
    if (compound.hasNoTags()) {
        getTagCompound().removeTag(NbtDataUtil.SPONGE_DATA);
        if (getTagCompound().hasNoTags()) {
            setTagCompound(null);
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ImmutableList(com.google.common.collect.ImmutableList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 3 with SerializedDataTransaction

use of org.spongepowered.common.data.persistence.SerializedDataTransaction in project SpongeCommon by SpongePowered.

the class SpongeItemStackBuilder method buildContent.

@Override
protected Optional<ItemStack> buildContent(DataView container) throws InvalidDataException {
    checkNotNull(container);
    if (!container.contains(DataQueries.ITEM_TYPE) || !container.contains(DataQueries.ITEM_COUNT) || !container.contains(DataQueries.ITEM_DAMAGE_VALUE)) {
        return Optional.empty();
    }
    final String itemTypeId = getData(container, DataQueries.ITEM_TYPE, String.class);
    final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
    final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemTypeId).get();
    final int damage = getData(container, DataQueries.ITEM_DAMAGE_VALUE, Integer.class);
    final net.minecraft.item.ItemStack itemStack = new net.minecraft.item.ItemStack((Item) itemType, count, damage);
    if (container.contains(DataQueries.UNSAFE_NBT)) {
        final NBTTagCompound compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
        itemStack.setTagCompound(compound);
    }
    if (container.contains(DataQueries.DATA_MANIPULATORS)) {
        final List<DataView> views = container.getViewList(DataQueries.DATA_MANIPULATORS).get();
        final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
        final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
        for (DataManipulator<?, ?> manipulator : manipulators) {
            ((IMixinCustomDataHolder) itemStack).offerCustom(manipulator, MergeFunction.IGNORE_ALL);
        }
        if (!transaction.failedData.isEmpty()) {
            ((IMixinCustomDataHolder) itemStack).addFailedData(transaction.failedData);
        }
    }
    return Optional.of((ItemStack) itemStack);
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ItemType(org.spongepowered.api.item.ItemType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder) DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 4 with SerializedDataTransaction

use of org.spongepowered.common.data.persistence.SerializedDataTransaction in project SpongeCommon by SpongePowered.

the class DataUtil method deserializeManipulatorList.

public static SerializedDataTransaction deserializeManipulatorList(List<DataView> containers) {
    checkNotNull(containers);
    final SerializedDataTransaction.Builder builder = SerializedDataTransaction.builder();
    for (DataView view : containers) {
        final DataView updated = updateDataViewForDataManipulator(view);
        findDataId(builder, updated).ifPresent(dataId -> tryDeserializeManipulator(builder, updated, dataId));
    }
    return builder.build();
}
Also used : DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction)

Example 5 with SerializedDataTransaction

use of org.spongepowered.common.data.persistence.SerializedDataTransaction in project SpongeCommon by SpongePowered.

the class CustomDataNbtUtil method readCustomData.

@SuppressWarnings("unchecked")
public static void readCustomData(NBTTagCompound compound, DataHolder dataHolder) {
    if (dataHolder instanceof IMixinCustomDataHolder) {
        if (compound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
            final NBTTagList list = compound.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
            final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
            if (list != null && list.tagCount() != 0) {
                for (int i = 0; i < list.tagCount(); i++) {
                    final NBTTagCompound internal = list.getCompoundTagAt(i);
                    builder.add(NbtTranslator.getInstance().translateFrom(internal));
                }
            }
            try {
                final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
                final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
                for (DataManipulator<?, ?> manipulator : manipulators) {
                    dataHolder.offer(manipulator);
                }
                if (!transaction.failedData.isEmpty()) {
                    ((IMixinCustomDataHolder) dataHolder).addFailedData(transaction.failedData);
                }
            } catch (InvalidDataException e) {
                SpongeImpl.getLogger().error("Could not translate custom plugin data! ", e);
            }
        }
        if (compound.hasKey(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_LIST)) {
            final NBTTagList list = compound.getTagList(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_COMPOUND);
            final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
            if (list != null && list.tagCount() != 0) {
                for (int i = 0; i < list.tagCount(); i++) {
                    final NBTTagCompound internal = list.getCompoundTagAt(i);
                    builder.add(NbtTranslator.getInstance().translateFrom(internal));
                }
            }
            // We want to attempt to refresh the failed data if it does succeed in getting read.
            compound.removeTag(NbtDataUtil.FAILED_CUSTOM_DATA);
            // Re-attempt to deserialize custom data
            final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
            final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
            List<Class<? extends DataManipulator<?, ?>>> classesLoaded = new ArrayList<>();
            for (DataManipulator<?, ?> manipulator : manipulators) {
                if (!classesLoaded.contains(manipulator.getClass())) {
                    classesLoaded.add((Class<? extends DataManipulator<?, ?>>) manipulator.getClass());
                    // ignore the failed data for removal.
                    if (!((IMixinCustomDataHolder) dataHolder).getCustom(manipulator.getClass()).isPresent()) {
                        dataHolder.offer(manipulator);
                    }
                }
            }
            if (!transaction.failedData.isEmpty()) {
                ((IMixinCustomDataHolder) dataHolder).addFailedData(transaction.failedData);
            }
        }
    }
}
Also used : DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ImmutableList(com.google.common.collect.ImmutableList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ArrayList(java.util.ArrayList) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder) NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Aggregations

DataView (org.spongepowered.api.data.DataView)5 SerializedDataTransaction (org.spongepowered.common.data.persistence.SerializedDataTransaction)5 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)4 ImmutableList (com.google.common.collect.ImmutableList)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)2 ItemType (org.spongepowered.api.item.ItemType)2 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)2 ArrayList (java.util.ArrayList)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1