Search in sources :

Example 66 with DataView

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)));
}
Also used : DataView(org.spongepowered.api.data.DataView) Vector3d(com.flowpowered.math.vector.Vector3d)

Example 67 with DataView

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);
            }
        }
    }
}
Also used : DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ImmutableList(com.google.common.collect.ImmutableList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ArrayList(java.util.ArrayList) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder) NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Example 68 with DataView

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);
        }
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 69 with DataView

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));
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) Test(org.junit.Test)

Example 70 with DataView

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()));
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) Test(org.junit.Test)

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