Search in sources :

Example 11 with DataView

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

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

the class NBTTranslationTest method testDotContainerKeys.

@Test
public void testDotContainerKeys() {
    final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(DataQuery.of("my.key.to.data"), 1);
    NBTTagCompound compound = NbtTranslator.getInstance().translateData(container);
    DataView translatedContainer = NbtTranslator.getInstance().translateFrom(compound);
    assertEquals(container, translatedContainer);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Test(org.junit.Test)

Example 13 with DataView

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

the class NBTTranslationTest method testContainerToNBT.

@Test
public void testContainerToNBT() {
    DataManager service = Mockito.mock(DataManager.class);
    DataBuilder<FakeSerializable> builder = new FakeBuilder();
    when(service.getBuilder(FakeSerializable.class)).thenReturn(Optional.of(builder));
    DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    container.set(DataQuery.of("foo"), "bar");
    FakeSerializable temp = new FakeSerializable("bar", 7, 10.0D, "nested");
    container.set(DataQuery.of("myFake"), temp);
    NBTTagCompound compound = NbtTranslator.getInstance().translateData(container);
    DataView translatedContainer = NbtTranslator.getInstance().translateFrom(compound);
    assertEquals(container, translatedContainer);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) DataManager(org.spongepowered.api.data.DataManager) Test(org.junit.Test)

Example 14 with DataView

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

the class HomeDataImpl method from.

// Only required on mutable implementations
@Override
public Optional<HomeData> from(DataContainer container) {
    if (!container.contains(MyHomes.DEFAULT_HOME, MyHomes.HOMES)) {
        return Optional.empty();
    }
    // Loads the structure defined in toContainer
    this.defaultHome = container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).get();
    // Loads the map of homes
    this.homes = Maps.newHashMap();
    DataView homes = container.getView(MyHomes.HOMES.getQuery()).get();
    for (DataQuery homeQuery : homes.getKeys(false)) {
        homes.getSerializable(homeQuery, Home.class).ifPresent(home -> this.homes.put(homeQuery.toString(), home));
    }
    return Optional.of(this);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) Home(org.spongepowered.test.myhomes.data.home.Home)

Example 15 with DataView

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

the class SpongeBlockSnapshotBuilder method buildContent.

@Override
protected Optional<BlockSnapshot> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(DataQueries.BLOCK_STATE, Queries.WORLD_ID, DataQueries.SNAPSHOT_WORLD_POSITION)) {
        return Optional.empty();
    }
    checkDataExists(container, DataQueries.BLOCK_STATE);
    checkDataExists(container, Queries.WORLD_ID);
    final SpongeBlockSnapshotBuilder builder = new SpongeBlockSnapshotBuilder();
    final UUID worldUuid = UUID.fromString(container.getString(Queries.WORLD_ID).get());
    final Vector3i coordinate = DataUtil.getPosition3i(container);
    Optional<String> creatorUuid = container.getString(Queries.CREATOR_ID);
    Optional<String> notifierUuid = container.getString(Queries.NOTIFIER_ID);
    // We now reconstruct the custom data and all extra data.
    final BlockState blockState = container.getSerializable(DataQueries.BLOCK_STATE, BlockState.class).get();
    BlockState extendedState = null;
    if (container.contains(DataQueries.BLOCK_EXTENDED_STATE)) {
        extendedState = container.getSerializable(DataQueries.BLOCK_EXTENDED_STATE, BlockState.class).get();
    } else {
        extendedState = blockState;
    }
    builder.blockState(blockState).extendedState(extendedState).position(coordinate).worldId(worldUuid);
    if (creatorUuid.isPresent()) {
        builder.creator(UUID.fromString(creatorUuid.get()));
    }
    if (notifierUuid.isPresent()) {
        builder.notifier(UUID.fromString(notifierUuid.get()));
    }
    Optional<DataView> unsafeCompound = container.getView(DataQueries.UNSAFE_NBT);
    final NBTTagCompound compound = unsafeCompound.isPresent() ? NbtTranslator.getInstance().translateData(unsafeCompound.get()) : null;
    if (compound != null) {
        builder.unsafeNbt(compound);
    }
    if (container.contains(DataQueries.SNAPSHOT_TILE_DATA)) {
        final List<DataView> dataViews = container.getViewList(DataQueries.SNAPSHOT_TILE_DATA).get();
        DataUtil.deserializeImmutableManipulatorList(dataViews).stream().forEach(builder::add);
    }
    return Optional.of(new SpongeBlockSnapshot(builder));
}
Also used : DataView(org.spongepowered.api.data.DataView) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID)

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