Search in sources :

Example 6 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class FloatTypeHandler method deserializeCollection.

@Override
public List<Float> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<Float> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            if (item.isNumber()) {
                result.add(item.getAsFloat());
            } else {
                result.add(null);
            }
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Example 7 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class IntTypeHandler method deserializeCollection.

@Override
public List<Integer> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<Integer> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            if (item.isNumber()) {
                result.add(item.getAsInteger());
            } else {
                result.add(null);
            }
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Example 8 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class LongTypeHandler method deserializeCollection.

@Override
public List<Long> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<Long> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            if (item.isNumber()) {
                result.add(item.getAsLong());
            } else {
                result.add(null);
            }
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Example 9 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class MappedContainerTypeHandler method serialize.

@Override
public PersistedData serialize(T value, SerializationContext context) {
    if (value == null) {
        return context.createNull();
    }
    Map<String, PersistedData> mappedData = Maps.newLinkedHashMap();
    for (Map.Entry<FieldMetadata<T, ?>, TypeHandler<?>> entry : mappedFields.entrySet()) {
        Object val = entry.getKey().getValue(value);
        if (val != null) {
            TypeHandler handler = entry.getValue();
            PersistedData fieldValue = handler.serialize(val, context);
            if (fieldValue != null) {
                mappedData.put(entry.getKey().getName(), fieldValue);
            }
        }
    }
    return context.create(mappedData);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) SimpleTypeHandler(org.terasology.persistence.typeHandling.SimpleTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) Map(java.util.Map)

Example 10 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class NetEntityRefTypeHandler method serializeCollection.

@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
    List<PersistedData> items = Lists.newArrayList();
    for (EntityRef ref : value) {
        BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            Vector3i blockPos = blockComponent.getPosition();
            items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
        } else {
            NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
            if (netComponent != null) {
                items.add(context.create(netComponent.getNetworkId()));
            } else {
                items.add(context.createNull());
            }
        }
    }
    return context.create(items);
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) PersistedData(org.terasology.persistence.typeHandling.PersistedData) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

PersistedData (org.terasology.persistence.typeHandling.PersistedData)17 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)6 Test (org.junit.Test)4 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)4 Serializer (org.terasology.persistence.typeHandling.Serializer)4 ProtobufPersistedData (org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData)4 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)3 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)3 Map (java.util.Map)2 SerializationContext (org.terasology.persistence.typeHandling.SerializationContext)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 EntityData (org.terasology.protobuf.EntityData)2 Color (org.terasology.rendering.nui.Color)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Component (org.terasology.entitySystem.Component)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 Vector3i (org.terasology.math.geom.Vector3i)1 NetworkComponent (org.terasology.network.NetworkComponent)1