Search in sources :

Example 1 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class PreviewWorldScreen method configureProperties.

private void configureProperties() {
    PropertyLayout propLayout = find("properties", PropertyLayout.class);
    propLayout.setOrdering(PropertyOrdering.byLabel());
    propLayout.clear();
    WorldConfigurator worldConfig = worldGenerator.getConfigurator();
    Map<String, Component> params = worldConfig.getProperties();
    for (String key : params.keySet()) {
        Class<? extends Component> clazz = params.get(key).getClass();
        Component comp = config.getModuleConfig(worldGenerator.getUri(), key, clazz);
        if (comp != null) {
            // use the data from the config instead of defaults
            worldConfig.setProperty(key, comp);
        }
    }
    ComponentLibrary compLib = subContext.get(ComponentLibrary.class);
    for (String label : params.keySet()) {
        PropertyProvider provider = new PropertyProvider() {

            @Override
            protected <T> Binding<T> createTextBinding(Object target, FieldMetadata<Object, T> fieldMetadata) {
                return new WorldConfigBinding<>(worldConfig, label, compLib, fieldMetadata);
            }

            @Override
            protected Binding<Float> createFloatBinding(Object target, FieldMetadata<Object, ?> fieldMetadata) {
                return new WorldConfigNumberBinding(worldConfig, label, compLib, fieldMetadata);
            }
        };
        Component target = params.get(label);
        List<Property<?, ?>> properties = provider.createProperties(target);
        propLayout.addProperties(label, properties);
    }
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) PropertyLayout(org.terasology.rendering.nui.layouts.PropertyLayout) PropertyProvider(org.terasology.rendering.nui.properties.PropertyProvider) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) Component(org.terasology.entitySystem.Component) Property(org.terasology.rendering.nui.properties.Property)

Example 2 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class ComponentSerializer method deserializeOnto.

private <T extends Component> Component deserializeOnto(Component targetComponent, EntityData.Component componentData, ComponentMetadata<T> componentMetadata, FieldSerializeCheck<Component> fieldCheck) {
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    DeserializationContext context = new ProtobufDeserializationContext(typeSerializationLibrary);
    Map<FieldMetadata<?, ?>, PersistedData> dataMap = Maps.newHashMapWithExpectedSize(componentData.getFieldCount());
    for (EntityData.NameValue field : componentData.getFieldList()) {
        FieldMetadata<?, ?> fieldInfo = null;
        if (field.hasNameIndex()) {
            fieldInfo = componentMetadata.getField(field.getNameIndex());
        } else if (field.hasName()) {
            fieldInfo = componentMetadata.getField(field.getName());
        }
        if (fieldInfo != null) {
            dataMap.put(fieldInfo, new ProtobufPersistedData(field.getValue()));
        } else if (field.hasName()) {
            logger.warn("Cannot deserialize unknown field '{}' onto '{}'", field.getName(), componentMetadata.getUri());
        }
    }
    serializer.deserializeOnto(targetComponent, dataMap, context, fieldCheck);
    return targetComponent;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) DeserializationContext(org.terasology.persistence.typeHandling.DeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 3 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata 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 4 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class PropertyProvider method createProperties.

public List<Property<?, ?>> createProperties(Object target) {
    List<Property<?, ?>> properties = Lists.newArrayList();
    try {
        Class<?> type = target.getClass();
        ReflectFactory reflectFactory = CoreRegistry.get(ReflectFactory.class);
        CopyStrategyLibrary copyStrategies = new CopyStrategyLibrary(reflectFactory);
        ClassMetadata<?, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), type, reflectFactory, copyStrategies);
        for (Field field : getAllFields(type)) {
            Annotation annotation = getFactory(field);
            if (annotation != null) {
                FieldMetadata<Object, ?> fieldMetadata = (FieldMetadata<Object, ?>) classMetadata.getField(field.getName());
                PropertyFactory factory = factories.get(annotation.annotationType());
                Property property = factory.create(target, fieldMetadata, field.getName(), annotation);
                properties.add(property);
            }
        }
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return properties;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) SimpleUri(org.terasology.engine.SimpleUri) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) Annotation(java.lang.annotation.Annotation) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) Field(java.lang.reflect.Field) DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata)

Example 5 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method cleanUpTemporaryEntity.

private void cleanUpTemporaryEntity(EntityRef entity) {
    Prefab prefab = entity.getParentPrefab();
    for (Component comp : entity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
            entity.removeComponent(comp.getClass());
        }
    }
    entity.removeComponent(NetworkComponent.class);
    if (prefab != null) {
        for (Component comp : prefab.iterateComponents()) {
            Component currentComp = entity.getComponent(comp.getClass());
            if (currentComp == null) {
                entity.addComponent(entityManager.getComponentLibrary().copy(comp));
            } else {
                ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
                boolean changed = false;
                for (FieldMetadata field : metadata.getFields()) {
                    Object expected = field.getValue(comp);
                    if (!Objects.equal(expected, field.getValue(currentComp))) {
                        field.setValue(currentComp, expected);
                        changed = true;
                    }
                }
                if (changed) {
                    entity.saveComponent(currentComp);
                }
            }
        }
    }
    entityManager.destroyEntityWithoutEvents(entity);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Aggregations

FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)7 PersistedData (org.terasology.persistence.typeHandling.PersistedData)3 Map (java.util.Map)2 SimpleUri (org.terasology.engine.SimpleUri)2 Component (org.terasology.entitySystem.Component)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 DefaultClassMetadata (org.terasology.reflection.metadata.DefaultClassMetadata)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)1 ComponentLibrary (org.terasology.entitySystem.metadata.ComponentLibrary)1 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 NetworkComponent (org.terasology.network.NetworkComponent)1 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)1