Search in sources :

Example 26 with DataContainer

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

the class SimpleData method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = DataContainer.createNew();
    container.set(DataQuery.of("myInt"), this.testInt);
    container.set(DataQuery.of("myDouble"), this.testDouble);
    container.set(DataQuery.of("myString"), this.testString);
    container.set(DataQuery.of("myStringList"), Arrays.asList(this.testList));
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 27 with DataContainer

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

the class ManipulatorTest method testSerialization.

@SuppressWarnings("unchecked")
@Test
public void testSerialization() {
    try {
        final Constructor<?> ctor = this.manipulatorClass.getConstructor();
        final DataManipulator<?, ?> manipulator = (DataManipulator<?, ?>) ctor.newInstance();
        final DataContainer container = manipulator.toContainer();
        if (this.builder != null) {
            final Optional<DataManipulator<?, ?>> optional = (Optional<DataManipulator<?, ?>>) this.builder.build(container);
            if (!optional.isPresent()) {
                throw new IllegalArgumentException("[Serialization]: A builder did not translate the data manipulator: " + this.dataName + "\n[Serialization]: Providing the DataContainer: " + container.toString());
            }
            final DataManipulator<?, ?> deserialized = this.builder.build(container).get();
            assertThat(manipulator.equals(deserialized), is(true));
        }
    } catch (NoSuchMethodException | InstantiationException | InvocationTargetException e) {
        throw new PEBKACException("Exceptions thrown trying to construct: " + this.dataName, e);
    } catch (Exception e) {
        throw new RuntimeException("There was an unknown exception trying to test " + this.dataName + ". Probably because the DataManipulator relies on an implementation class.", e);
    }
}
Also used : Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PEBKACException(org.spongepowered.api.util.PEBKACException) DataContainer(org.spongepowered.api.data.DataContainer) PEBKACException(org.spongepowered.api.util.PEBKACException) Test(org.junit.Test)

Example 28 with DataContainer

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

the class SimpleData method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = DataContainer.createNew();
    container.set(DataQuery.of("myInt"), this.testInt);
    container.set(DataQuery.of("myDouble"), this.testDouble);
    container.set(DataQuery.of("myString"), this.testString);
    container.set(DataQuery.of("myStringList"), Arrays.asList(this.testList));
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 29 with DataContainer

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

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

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)110 DataView (org.spongepowered.api.data.DataView)30 DataQuery (org.spongepowered.api.data.DataQuery)28 Test (org.junit.Test)24 List (java.util.List)15 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 Nullable (javax.annotation.Nullable)11 Collection (java.util.Collection)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CatalogType (org.spongepowered.api.CatalogType)9 Sponge (org.spongepowered.api.Sponge)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 DataSerializable (org.spongepowered.api.data.DataSerializable)7