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;
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations