Search in sources :

Example 1 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project SpongeCommon by SpongePowered.

the class MixinTileEntity method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(Queries.WORLD_ID, ((World) this.world).getUniqueId().toString()).set(Queries.POSITION_X, this.getPos().getX()).set(Queries.POSITION_Y, this.getPos().getY()).set(Queries.POSITION_Z, this.getPos().getZ()).set(DataQueries.BLOCK_ENTITY_TILE_TYPE, this.tileType.getId());
    final NBTTagCompound compound = new NBTTagCompound();
    this.writeToNBT(compound);
    // We must filter the custom data so it isn't stored twice
    NbtDataUtil.filterSpongeCustomData(compound);
    container.set(DataQueries.UNSAFE_NBT, NbtTranslator.getInstance().translateFrom(compound));
    final Collection<DataManipulator<?, ?>> manipulators = ((IMixinCustomDataHolder) this).getCustomManipulators();
    if (!manipulators.isEmpty()) {
        container.set(DataQueries.DATA_MANIPULATORS, DataUtil.getSerializedManipulatorList(manipulators));
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) World(org.spongepowered.api.world.World) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 2 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project SpongeCommon by SpongePowered.

the class MixinWorld_Data method offer.

@Override
public DataTransactionResult offer(int x, int y, int z, DataManipulator<?, ?> manipulator, MergeFunction function) {
    final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
    final ImmutableDataManipulator<?, ?> immutableDataManipulator = manipulator.asImmutable();
    if (blockState.supports((Class) immutableDataManipulator.getClass())) {
        final List<ImmutableValue<?>> old = new ArrayList<>(blockState.getValues());
        final BlockState newState = blockState.with(immutableDataManipulator).get();
        old.removeAll(newState.getValues());
        setBlock(x, y, z, newState);
        return DataTransactionResult.successReplaceResult(old, manipulator.getValues());
    }
    return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(manipulator, function)).orElseGet(() -> DataTransactionResult.failResult(manipulator.getValues()));
}
Also used : SpongeImpl(org.spongepowered.common.SpongeImpl) Value(org.spongepowered.api.data.value.mutable.Value) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) PropertyStore(org.spongepowered.api.data.property.PropertyStore) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ImmutableList(com.google.common.collect.ImmutableList) Mixin(org.spongepowered.asm.mixin.Mixin) MergeFunction(org.spongepowered.api.data.merge.MergeFunction) Location(org.spongepowered.api.world.Location) ImmutableSet(com.google.common.collect.ImmutableSet) DirectionFacingProvider(org.spongepowered.common.registry.provider.DirectionFacingProvider) Collection(java.util.Collection) BaseValue(org.spongepowered.api.data.value.BaseValue) EnumFacing(net.minecraft.util.EnumFacing) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataHolder(org.spongepowered.api.data.DataHolder) BlockState(org.spongepowered.api.block.BlockState) Direction(org.spongepowered.api.util.Direction) List(java.util.List) DataView(org.spongepowered.api.data.DataView) World(org.spongepowered.api.world.World) Property(org.spongepowered.api.data.Property) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) Optional(java.util.Optional) Collections(java.util.Collections) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) BlockState(org.spongepowered.api.block.BlockState) ArrayList(java.util.ArrayList)

Example 3 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project SpongeCommon by SpongePowered.

the class MixinEntity method toContainer.

@Override
public DataContainer toContainer() {
    final Transform<World> transform = getTransform();
    final NBTTagCompound compound = new NBTTagCompound();
    writeToNBT(compound);
    // We must filter the custom data so it isn't stored twice
    NbtDataUtil.filterSpongeCustomData(compound);
    final DataContainer unsafeNbt = NbtTranslator.getInstance().translateFrom(compound);
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(DataQueries.ENTITY_CLASS, this.getClass().getName()).set(Queries.WORLD_ID, transform.getExtent().getUniqueId().toString()).createView(DataQueries.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, transform.getPosition().getX()).set(Queries.POSITION_Y, transform.getPosition().getY()).set(Queries.POSITION_Z, transform.getPosition().getZ()).getContainer().createView(DataQueries.ENTITY_ROTATION).set(Queries.POSITION_X, transform.getRotation().getX()).set(Queries.POSITION_Y, transform.getRotation().getY()).set(Queries.POSITION_Z, transform.getRotation().getZ()).getContainer().createView(DataQueries.ENTITY_SCALE).set(Queries.POSITION_X, transform.getScale().getX()).set(Queries.POSITION_Y, transform.getScale().getY()).set(Queries.POSITION_Z, transform.getScale().getZ()).getContainer().set(DataQueries.ENTITY_TYPE, this.entityType.getId()).set(DataQueries.UNSAFE_NBT, unsafeNbt);
    final Collection<DataManipulator<?, ?>> manipulators = ((IMixinCustomDataHolder) this).getCustomManipulators();
    if (!manipulators.isEmpty()) {
        container.set(DataQueries.DATA_MANIPULATORS, DataUtil.getSerializedManipulatorList(manipulators));
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) World(org.spongepowered.api.world.World) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 4 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator 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 5 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator 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)

Aggregations

DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)21 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)13 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)8 DataView (org.spongepowered.api.data.DataView)7 ImmutableList (com.google.common.collect.ImmutableList)6 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)6 Optional (java.util.Optional)5 DataContainer (org.spongepowered.api.data.DataContainer)5 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)4 SerializedDataTransaction (org.spongepowered.common.data.persistence.SerializedDataTransaction)4 ArrayList (java.util.ArrayList)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 DataManipulatorRegistration (org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)3 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)3 World (org.spongepowered.api.world.World)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 List (java.util.List)2 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)2 DataRegistration (org.spongepowered.api.data.DataRegistration)2