Search in sources :

Example 56 with DataView

use of org.spongepowered.api.data.DataView 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 57 with DataView

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

the class DataUtil method getSerializedManipulatorList.

private static <T extends DataSerializable> List<DataView> getSerializedManipulatorList(Iterable<T> manipulators, Function<T, DataRegistration> func) {
    checkNotNull(manipulators);
    final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
    for (T manipulator : manipulators) {
        final DataContainer container = DataContainer.createNew();
        container.set(Queries.CONTENT_VERSION, DataVersions.Data.CURRENT_CUSTOM_DATA);
        container.set(DataQueries.DATA_ID, func.apply(manipulator).getId()).set(DataQueries.INTERNAL_DATA, manipulator.toContainer());
        builder.add(container);
    }
    return builder.build();
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) ImmutableList(com.google.common.collect.ImmutableList)

Example 58 with DataView

use of org.spongepowered.api.data.DataView 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 59 with DataView

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

the class DataUtil method getPosition3d.

public static Vector3d getPosition3d(DataView view, DataQuery query) {
    checkDataExists(view, query);
    final DataView internal = view.getView(query).get();
    final double x = internal.getDouble(Queries.POSITION_X).get();
    final double y = internal.getDouble(Queries.POSITION_Y).get();
    final double z = internal.getDouble(Queries.POSITION_Z).get();
    return new Vector3d(x, y, z);
}
Also used : DataView(org.spongepowered.api.data.DataView) Vector3d(com.flowpowered.math.vector.Vector3d)

Example 60 with DataView

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

the class MixinTileEntityLockable method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    if (this.code != null) {
        container.set(DataQueries.BLOCK_ENTITY_LOCK_CODE, this.code.getLock());
    }
    List<DataView> items = Lists.newArrayList();
    for (int i = 0; i < getSizeInventory(); i++) {
        ItemStack stack = getStackInSlot(i);
        if (!stack.isEmpty()) {
            // todo make a helper object for this
            DataContainer stackView = DataContainer.createNew().set(Queries.CONTENT_VERSION, 1).set(DataQueries.BLOCK_ENTITY_SLOT, i).set(DataQueries.BLOCK_ENTITY_SLOT_ITEM, ((org.spongepowered.api.item.inventory.ItemStack) stack).toContainer());
            items.add(stackView);
        }
    }
    container.set(DataQueries.BLOCK_ENTITY_ITEM_CONTENTS, items);
    return container;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) ItemStack(net.minecraft.item.ItemStack)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6