use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class VelocityDataProcessor method fill.
@Override
public Optional<VelocityData> fill(DataContainer container, VelocityData velocityData) {
checkDataExists(container, Keys.VELOCITY.getQuery());
final DataView internalView = container.getView(Keys.VELOCITY.getQuery()).get();
final double x = getData(internalView, DataQueries.VELOCITY_X, Double.class);
final double y = getData(internalView, DataQueries.VELOCITY_Y, Double.class);
final double z = getData(internalView, DataQueries.VELOCITY_Z, Double.class);
return Optional.of(velocityData.set(Keys.VELOCITY, new Vector3d(x, y, z)));
}
use of org.spongepowered.api.data.DataView 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);
}
}
}
}
use of org.spongepowered.api.data.DataView 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);
}
}
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testCopy.
@Test
public void testCopy() {
final DataContainer container = DataContainer.createNew();
container.set(of("Foo"), "foo");
final DataContainer newContainer = container.copy();
assertTrue(container.equals(newContainer));
container.set(of("Foo", "bar"), "foo.bar");
final DataView internal = container.getView(of("Foo")).get().copy();
final DataContainer internalCopy = DataContainer.createNew().set(of("bar"), "foo.bar");
assertTrue(internal.equals(internalCopy));
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testGetMaps.
@Test
public void testGetMaps() {
DataView view = DataContainer.createNew();
view.set(of("foo", "bar", "foo"), "foo");
view.set(of("foo", "bar", "bar"), "foobar");
view.set(of("foo", "bar", "baz"), "foobarbaz");
view.set(of("bar"), 1);
Map<DataQuery, Object> shallowMap = Maps.newLinkedHashMap();
shallowMap.put(of("bar"), 1);
final Map<DataQuery, Object> internalView = Maps.newLinkedHashMap();
internalView.put(of("foo"), "foo");
internalView.put(of("bar"), "foobar");
internalView.put(of("baz"), "foobarbaz");
Map<DataQuery, Object> intermediateMap = Maps.newLinkedHashMap();
intermediateMap.put(of("bar"), internalView);
shallowMap.put(of("foo"), intermediateMap);
Map<DataQuery, Object> shallowValues = view.getValues(false);
assertTrue(shallowValues.entrySet().equals(shallowMap.entrySet()));
final Map<DataQuery, Object> deepMap = Maps.newLinkedHashMap();
deepMap.put(of("bar"), 1);
deepMap.put(of("foo", "bar", "foo"), "foo");
deepMap.put(of("foo", "bar", "bar"), "foobar");
deepMap.put(of("foo", "bar", "baz"), "foobarbaz");
deepMap.put(of("foo"), intermediateMap);
intermediateMap.put(of("bar", "foo"), "foo");
intermediateMap.put(of("bar", "bar"), "foobar");
intermediateMap.put(of("bar", "baz"), "foobarbaz");
deepMap.put(of("foo", "bar"), internalView);
Map<DataQuery, Object> deepValues = view.getValues(true);
assertTrue(deepValues.keySet().equals(deepMap.keySet()));
assertTrue(deepValues.entrySet().equals(deepMap.entrySet()));
}
Aggregations