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); }
private <T extends Component> void updateComponent( EntityRef blockEntity, ComponentMetadata<T> metadata, T targetComponent) { T currentComp = blockEntity.getComponent(metadata.getType()); if (currentComp != null) { boolean changed = false; for (FieldMetadata<T, ?> field : metadata.getFields()) { Object newVal = field.getValue(targetComponent); if (!Objects.equal(field.getValue(currentComp), newVal)) { field.setValue(currentComp, newVal); changed = true; } } if (changed) { blockEntity.saveComponent(currentComp); } } }
@Override public Property create( Object target, final FieldMetadata<Object, ?> fieldMetadata, String id, OneOf.Enum info) { Class<?> cls = fieldMetadata.getType(); Object[] items = cls.getEnumConstants(); UIDropdown dropdown = new UIDropdown(); dropdown.bindOptions(new DefaultBinding(Arrays.asList(items))); Binding binding = createTextBinding(target, fieldMetadata); dropdown.bindSelection(binding); String label = fromLabelOrId(info.label(), id); return new Property<>(label, binding, dropdown, info.description()); }
@Override public Property create( Object target, FieldMetadata<Object, ?> fieldMetadata, String id, TextField info) { UITextEntry<T> text = new UITextEntry<>(); Binding<T> textBinding = createTextBinding(target, (FieldMetadata<Object, T>) fieldMetadata); TextMapper<T> textMapper = createTextMapping(fieldMetadata.getType()); text.setFormatter(textMapper); text.setParser(textMapper); text.bindValue(textBinding); String label = fromLabelOrId(info.label(), id); return new Property<>(label, textBinding, text, info.description()); }
protected Binding<Float> createFloatBinding( Object target, final FieldMetadata<Object, ?> fieldMetadata) { Class<?> type = fieldMetadata.getType(); if (type == Integer.class || type == Integer.TYPE) { return new Binding<Float>() { @Override public Float get() { return ((Integer) fieldMetadata.getValueChecked(target)).floatValue(); } @Override public void set(Float value) { fieldMetadata.setValue(target, value.intValue()); } }; } else if (type == Float.class || type == Float.TYPE) { return new Binding<Float>() { @Override public Float get() { return (Float) fieldMetadata.getValueChecked(target); } @Override public void set(Float value) { fieldMetadata.setValue(target, value); } }; } else if (type == Double.class || type == double.class) { return new Binding<Float>() { @Override public Float get() { return ((Double) fieldMetadata.getValueChecked(target)).floatValue(); } @Override public void set(Float value) { fieldMetadata.setValue(target, value.doubleValue()); } }; } else { throw new IllegalArgumentException( "Cannot create Binding<Float> for a field of type " + type); } }
@Override public void set(T value) { fieldMetadata.setValue(target, value); }
@Override public T get() { return fieldMetadata.getValueChecked(target); }