Search in sources :

Example 1 with IValueContainer

use of org.lanternpowered.server.data.IValueContainer in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method deserialize.

@SuppressWarnings("unchecked")
@Override
public void deserialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        deserializeValues(object, simpleValueContainer, dataView);
        final Optional<DataView> optDataView = dataView.getView(VALUES);
        if (optDataView.isPresent()) {
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            final DataView valuesView = optDataView.get();
            for (Map.Entry<DataQuery, Object> entry : valuesView.getValues(false).entrySet()) {
                final Key key = KeyRegistryModule.get().getById(entry.getKey().toString()).orElse(null);
                if (key == null) {
                    Lantern.getLogger().warn("Unable to deserialize the data value with key: {} because it doesn't exist.", entry.getKey().toString());
                } else {
                    final TypeToken<?> typeToken = key.getElementToken();
                    final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                    if (dataTypeSerializer == null) {
                        Lantern.getLogger().warn("Unable to deserialize the data key value: {}, " + "no supported deserializer exists.", key.getId());
                    } else {
                        if (simpleValueContainer.get(key).isPresent()) {
                            Lantern.getLogger().warn("Duplicate usage of the key: {}", key.getId());
                        } else {
                            simpleValueContainer.set(key, dataTypeSerializer.deserialize(typeToken, context, entry.getValue()));
                        }
                    }
                }
            }
        }
        for (Map.Entry<Key<?>, Object> entry : simpleValueContainer.getValues().entrySet()) {
            final Element element = (Element) valueContainer.getValueCollection().getElement((Key) entry.getKey()).orElse(null);
            if (element != null) {
                element.set(entry.getValue());
            } else {
                Lantern.getLogger().warn("Attempted to offer a unsupported value with key \"{}\" to the object {}", entry.getKey().toString(), object.toString());
            }
        }
        dataView.getView(DataQueries.SPONGE_DATA).ifPresent(view -> DataHelper.deserializeRawContainerData(dataView, valueContainer, DataQueries.CUSTOM_MANIPULATORS));
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Example 2 with IValueContainer

use of org.lanternpowered.server.data.IValueContainer in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method serialize.

@SuppressWarnings("unchecked")
@Override
public void serialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        for (KeyRegistration<?, ?> registration : valueContainer.getValueCollection().getAll()) {
            if (!(registration instanceof Element)) {
                continue;
            }
            final Key key = registration.getKey();
            final Object element = ((Element) registration).get();
            if (element != null) {
                simpleValueContainer.set(key, element);
            }
        }
        // Serialize the values, all written values will be removed from
        // the simple value container
        serializeValues(object, simpleValueContainer, dataView);
        // Write the rest to the Values tag
        final Map<Key<?>, Object> values = simpleValueContainer.getValues();
        if (!values.isEmpty()) {
            final DataView valuesView = dataView.createView(VALUES);
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            for (Map.Entry<Key<?>, Object> entry : values.entrySet()) {
                final TypeToken<?> typeToken = entry.getKey().getElementToken();
                final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                if (dataTypeSerializer == null) {
                    Lantern.getLogger().warn("Unable to serialize the data key value: " + entry.getKey());
                } else {
                    valuesView.set(DataQuery.of(entry.getKey().getId()), dataTypeSerializer.serialize(typeToken, context, entry.getValue()));
                }
            }
            if (valuesView.isEmpty()) {
                dataView.remove(VALUES);
            }
        }
        DataHelper.serializeRawContainerData(getOrCreateView(dataView, DataQueries.SPONGE_DATA), valueContainer, DataQueries.CUSTOM_MANIPULATORS);
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 IValueContainer (org.lanternpowered.server.data.IValueContainer)2 Element (org.lanternpowered.server.data.element.Element)2 SimpleValueContainer (org.lanternpowered.server.data.io.store.SimpleValueContainer)2 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)2 DataTypeSerializerContext (org.lanternpowered.server.data.persistence.DataTypeSerializerContext)2 DataView (org.spongepowered.api.data.DataView)2 Key (org.spongepowered.api.data.key.Key)2 DataQuery (org.spongepowered.api.data.DataQuery)1