Search in sources :

Example 41 with DataView

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

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

Example 43 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class EntitySerializer method serialize.

@Override
public DataView serialize(LanternEntity object) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    dataView.set(ID, object.getType().getId());
    // noinspection unchecked
    final ObjectStore<LanternEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(object.getClass()).get();
    store.serialize(object, dataView);
    if (store instanceof IdentifiableObjectStore) {
        ((IdentifiableObjectStore) store).serializeUniqueId(dataView, object.getUniqueId());
    }
    return dataView;
}
Also used : DataView(org.spongepowered.api.data.DataView) ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Example 44 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class EntityStore method serializeValues.

@Override
public void serializeValues(T object, SimpleValueContainer valueContainer, DataView dataView) {
    // Here will we remove all the vanilla properties and delegate the
    // rest through the default serialization system
    valueContainer.remove(Keys.VELOCITY).ifPresent(v -> dataView.set(VELOCITY, toDoubleList(v)));
    valueContainer.remove(Keys.FIRE_TICKS).ifPresent(v -> dataView.set(FIRE_TICKS, v));
    valueContainer.remove(Keys.FALL_DISTANCE).ifPresent(v -> dataView.set(FALL_DISTANCE, v));
    valueContainer.remove(Keys.HEALTH).ifPresent(v -> dataView.set(HEALTH, v.floatValue()));
    valueContainer.remove(Keys.REMAINING_AIR).ifPresent(v -> dataView.set(REMAINING_AIR, v));
    valueContainer.remove(Keys.ABSORPTION).ifPresent(v -> dataView.set(ABSORPTION_AMOUNT, v.floatValue()));
    final DataView spongeView = getOrCreateView(dataView, SPONGE_DATA);
    valueContainer.remove(Keys.MAX_AIR).ifPresent(v -> spongeView.set(MAX_AIR, v));
    valueContainer.remove(Keys.CAN_GRIEF).ifPresent(v -> spongeView.set(CAN_GRIEF, (byte) (v ? 1 : 0)));
    valueContainer.remove(Keys.DISPLAY_NAME).ifPresent(v -> dataView.set(DISPLAY_NAME, LanternTexts.toLegacy(v)));
    valueContainer.remove(Keys.CUSTOM_NAME_VISIBLE).ifPresent(v -> dataView.set(CUSTOM_NAME_VISIBLE, (byte) (v ? 1 : 0)));
    valueContainer.remove(Keys.INVULNERABLE).ifPresent(v -> dataView.set(INVULNERABLE, (byte) (v ? 1 : 0)));
    valueContainer.remove(LanternKeys.PORTAL_COOLDOWN_TICKS).ifPresent(v -> dataView.set(PORTAL_COOLDOWN_TICKS, v));
    valueContainer.remove(Keys.AI_ENABLED).ifPresent(v -> dataView.set(NO_AI, (byte) (v ? 0 : 1)));
    valueContainer.remove(Keys.PERSISTS).ifPresent(v -> dataView.set(PERSISTS, (byte) (v ? 1 : 0)));
    valueContainer.remove(LanternKeys.CAN_PICK_UP_LOOT).ifPresent(v -> dataView.set(CAN_PICK_UP_LOOT, (byte) (v ? 1 : 0)));
    valueContainer.remove(Keys.DISPLAY_NAME).ifPresent(v -> dataView.set(CUSTOM_NAME, LanternTexts.toLegacy(v)));
    valueContainer.remove(Keys.POTION_EFFECTS).ifPresent(v -> {
        if (v.isEmpty()) {
            return;
        }
        // TODO: Allow out impl to use integers as amplifier and use a string as effect id,
        // without breaking the official format
        dataView.set(POTION_EFFECTS, v.stream().map(PotionEffectSerializer::serialize).collect(Collectors.toList()));
    });
    valueContainer.remove(Keys.FOOD_LEVEL).ifPresent(v -> dataView.set(FOOD_LEVEL, v));
    valueContainer.remove(Keys.EXHAUSTION).ifPresent(v -> dataView.set(EXHAUSTION, v.floatValue()));
    valueContainer.remove(Keys.SATURATION).ifPresent(v -> dataView.set(SATURATION, v.floatValue()));
    valueContainer.remove(LanternKeys.IS_ELYTRA_FLYING).ifPresent(v -> dataView.set(IS_ELYTRA_FLYING, (byte) (v ? 1 : 0)));
    valueContainer.remove(Keys.GLOWING).ifPresent(v -> dataView.set(IS_GLOWING, (byte) (v ? 1 : 0)));
    super.serializeValues(object, valueContainer, dataView);
}
Also used : DataView(org.spongepowered.api.data.DataView) PotionEffectSerializer(org.lanternpowered.server.data.io.store.misc.PotionEffectSerializer)

Example 45 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class PotionEffectSerializer method serialize.

public static DataView serialize(PotionEffect potionEffect) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    dataView.set(AMPLIFIER, (byte) potionEffect.getAmplifier());
    dataView.set(DURATION, potionEffect.getDuration());
    dataView.set(AMBIENT, (byte) (potionEffect.isAmbient() ? 1 : 0));
    if (potionEffect.getShowParticles()) {
        dataView.set(SHOW_PARTICLES, (byte) 1);
    }
    final LanternPotionEffectType potionEffectType = (LanternPotionEffectType) potionEffect.getType();
    final int internalId = potionEffectType.getInternalId();
    if (internalId > 0xff) {
        dataView.set(IDENTIFIER, internalId);
    } else {
        dataView.set(IDENTIFIER, (byte) internalId);
    }
    return dataView;
}
Also used : DataView(org.spongepowered.api.data.DataView) LanternPotionEffectType(org.lanternpowered.server.effect.potion.LanternPotionEffectType)

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