Search in sources :

Example 11 with DataManipulator

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

the class MixinItemStack method getContainers.

@Override
public Collection<DataManipulator<?, ?>> getContainers() {
    final List<DataManipulator<?, ?>> manipulators = Lists.newArrayList();
    final Item item = this.shadow$getItem();
    // Null items should be impossible to create
    if (item == null) {
        final PrettyPrinter printer = new PrettyPrinter(60);
        printer.add("Null Item found!").centre().hr();
        printer.add("An ItemStack has a null ItemType! This is usually not supported as it will likely have issues elsewhere.");
        printer.add("Please ask help for seeing if this is an issue with a mod and report it!");
        printer.add("Printing a Stacktrace:");
        printer.add(new Exception());
        printer.log(SpongeImpl.getLogger(), Level.WARN);
        return manipulators;
    }
    ((IMixinItem) item).getManipulatorsFor((net.minecraft.item.ItemStack) (Object) this, manipulators);
    if (hasManipulators()) {
        final List<DataManipulator<?, ?>> customManipulators = this.getCustomManipulators();
        manipulators.addAll(customManipulators);
    }
    return manipulators;
}
Also used : IMixinItem(org.spongepowered.common.interfaces.item.IMixinItem) Item(net.minecraft.item.Item) IMixinItem(org.spongepowered.common.interfaces.item.IMixinItem) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Example 12 with DataManipulator

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

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

the class SpongeBlockSnapshotBuilder method from.

@Override
public SpongeBlockSnapshotBuilder from(Location<World> location) {
    this.blockState = location.getBlock();
    this.worldUuid = location.getExtent().getUniqueId();
    this.coords = location.getBlockPosition();
    if (this.blockState.getType() instanceof ITileEntityProvider) {
        if (location.hasTileEntity()) {
            this.compound = new NBTTagCompound();
            org.spongepowered.api.block.tileentity.TileEntity te = location.getTileEntity().get();
            ((TileEntity) te).writeToNBT(this.compound);
            this.manipulators = ((IMixinCustomDataHolder) te).getCustomManipulators().stream().map(DataManipulator::asImmutable).collect(Collectors.toList());
        }
    }
    return this;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ITileEntityProvider(net.minecraft.block.ITileEntityProvider) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 14 with DataManipulator

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

Example 15 with DataManipulator

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

the class CustomDataNbtUtil method writeCustomData.

public static void writeCustomData(NBTTagCompound compound, DataHolder dataHolder) {
    if (dataHolder instanceof IMixinCustomDataHolder) {
        final List<DataManipulator<?, ?>> manipulators = ((IMixinCustomDataHolder) dataHolder).getCustomManipulators();
        if (!manipulators.isEmpty()) {
            final List<DataView> manipulatorViews = DataUtil.getSerializedManipulatorList(manipulators);
            final NBTTagList manipulatorTagList = new NBTTagList();
            for (DataView dataView : manipulatorViews) {
                manipulatorTagList.appendTag(NbtTranslator.getInstance().translateData(dataView));
            }
            compound.setTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, manipulatorTagList);
        }
        final List<DataView> failedData = ((IMixinCustomDataHolder) dataHolder).getFailedData();
        if (!failedData.isEmpty()) {
            final NBTTagList failedList = new NBTTagList();
            for (DataView failedDatum : failedData) {
                failedList.appendTag(NbtTranslator.getInstance().translateData(failedDatum));
            }
            compound.setTag(NbtDataUtil.FAILED_CUSTOM_DATA, failedList);
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

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