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);
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations