Search in sources :

Example 6 with DataView

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

the class DataUpdaterDelegate method update.

@Override
public DataView update(DataView content) {
    // backup
    final DataView copied = content.copy();
    DataView updated = copied;
    for (DataContentUpdater updater : this.updaters) {
        try {
            updated = updater.update(updated);
        } catch (Exception e) {
            Exception exception = new RuntimeException("There was error attempting to update some data for the content updater:" + updater.getClass().getName() + "\nThe original data is being returned, possibly causing " + "issues later on, \nbut the original data should not be lost. Please notify the developer " + "of this exception with the stacktrace.", e);
            exception.printStackTrace();
            return copied;
        }
    }
    return updated;
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 7 with DataView

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

the class SpongeItemStackSnapshotDataBuilder method buildContent.

@Override
protected Optional<ItemStackSnapshot> buildContent(DataView container) throws InvalidDataException {
    if (container.contains(Constants.ItemStack.TYPE, Constants.ItemStack.COUNT)) {
        final ItemType itemType = container.getRegistryValue(Constants.ItemStack.TYPE, RegistryTypes.ITEM_TYPE, Sponge.game()).get();
        if (itemType == ItemTypes.AIR.get()) {
            return Optional.of(ItemStackSnapshot.empty());
        }
        final int count = container.getInt(Constants.ItemStack.COUNT).get();
        @Nullable final CompoundTag compound;
        if (container.contains(Constants.Sponge.UNSAFE_NBT)) {
            compound = NBTTranslator.INSTANCE.translate(container.getView(Constants.Sponge.UNSAFE_NBT).get());
            SpongeItemStack.BuilderImpl.fixEnchantmentData(itemType, compound);
        } else {
            compound = null;
        }
        final ImmutableList<DataManipulator.Immutable> manipulators;
        if (container.contains(Constants.Sponge.DATA_MANIPULATORS)) {
            final List<DataView> views = container.getViewList(Constants.Sponge.DATA_MANIPULATORS).get();
            // TODO manipulators = DataUtil.deserializeImmutableManipulatorList(container.getViewList(Constants.Sponge.DATA_MANIPULATORS).get());
            manipulators = ImmutableList.of();
        } else {
            manipulators = ImmutableList.of();
        }
        return Optional.of(new SpongeItemStackSnapshot(itemType, count, manipulators, compound));
    }
    return Optional.empty();
}
Also used : SpongeItemStackSnapshot(org.spongepowered.common.item.SpongeItemStackSnapshot) DataView(org.spongepowered.api.data.persistence.DataView) ItemType(org.spongepowered.api.item.ItemType) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 8 with DataView

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

the class FriendlyByteBufMixin_API method cbuf$getDataView.

public DataView cbuf$getDataView(final int index) {
    final int oldIndex = this.readerIndex();
    this.readerIndex(index);
    final DataView data = this.cbuf$readDataView();
    this.readerIndex(oldIndex);
    return data;
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView)

Example 9 with DataView

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

the class ItemStackMixin_API method setRawData.

@Override
public void setRawData(final DataView container) throws InvalidDataException {
    Preconditions.checkNotNull(container);
    if (this.shadow$isEmpty()) {
        throw new IllegalArgumentException("Cannot set data on empty item stacks!");
    }
    if (!container.contains(Constants.Sponge.UNSAFE_NBT)) {
        throw new InvalidDataException("There's no NBT Data set in the provided container");
    }
    final DataView nbtData = container.getView(Constants.Sponge.UNSAFE_NBT).get();
    try {
        final int integer = container.getInt(Constants.ItemStack.DAMAGE_VALUE).orElse(this.shadow$getDamageValue());
        this.shadow$setDamageValue(integer);
        final CompoundTag stackCompound = NBTTranslator.INSTANCE.translate(nbtData);
        this.shadow$setTag(stackCompound);
    } catch (final Exception e) {
        throw new InvalidDataException("Unable to set raw data or translate raw data for ItemStack setting", e);
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) CompoundTag(net.minecraft.nbt.CompoundTag) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Example 10 with DataView

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

the class SchematicTranslator method deserializeBlockContainer.

private static void deserializeBlockContainer(final DataView view, final SpongeArchetypeVolume archetypeVolume, final int width, final int length, final Vector3i offset, final boolean needsFixers) {
    final MutableBimapPalette<BlockState, BlockType> palette;
    final DataView paletteMap = view.getView(Constants.Sponge.Schematic.BLOCK_PALETTE).orElseThrow(() -> new InvalidDataException("Missing BlockPalette as required by Schematic Specification"));
    final Set<DataQuery> paletteKeys = paletteMap.keys(false);
    // If we had a default palette_max we don't want to allocate all
    // that space for nothing so we use a sensible default instead
    palette = new MutableBimapPalette<>(PaletteTypes.BLOCK_STATE_PALETTE.get(), Sponge.game().registry(RegistryTypes.BLOCK_TYPE), RegistryTypes.BLOCK_TYPE, paletteKeys.size());
    for (final DataQuery key : paletteKeys) {
        final BlockState state = BlockStateSerializerDeserializer.deserialize(key.parts().get(0)).orElseGet(() -> BlockTypes.BEDROCK.get().defaultState());
        palette.assign(state, paletteMap.getInt(key).orElseThrow(() -> new IllegalStateException("Somehow got a missing biome in the palette map for schematic")));
    }
    final byte[] blockData = (byte[]) view.get(Constants.Sponge.Schematic.BLOCK_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
    SchematicTranslator.readByteArrayData(width, (width * length), offset, palette, blockData, archetypeVolume, BlockVolume.Modifiable::setBlock);
    view.getViewList(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER).ifPresent(tileData -> tileData.forEach(SchematicTranslator.deserializeBlockEntities(offset, archetypeVolume, needsFixers)));
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Aggregations

DataView (org.spongepowered.api.data.persistence.DataView)32 DataQuery (org.spongepowered.api.data.persistence.DataQuery)16 DataContainer (org.spongepowered.api.data.persistence.DataContainer)10 CompoundTag (net.minecraft.nbt.CompoundTag)9 ResourceKey (org.spongepowered.api.ResourceKey)8 Map (java.util.Map)6 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)6 Optional (java.util.Optional)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collection (java.util.Collection)3 DataContentUpdater (org.spongepowered.api.data.persistence.DataContentUpdater)3 DataSerializable (org.spongepowered.api.data.persistence.DataSerializable)3 DataStore (org.spongepowered.api.data.persistence.DataStore)3 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)3 Vector3i (org.spongepowered.math.vector.Vector3i)3 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (io.leangen.geantyref.TypeToken)2