Search in sources :

Example 1 with ComponentLibrary

use of org.terasology.entitySystem.metadata.ComponentLibrary 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 ComponentLibrary

use of org.terasology.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchToGameEnvironment.

@SuppressWarnings("unchecked")
public void handleSwitchToGameEnvironment(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    CopyStrategyLibrary copyStrategyLibrary = context.get(CopyStrategyLibrary.class);
    copyStrategyLibrary.clear();
    for (Class<? extends CopyStrategy> copyStrategy : moduleManager.getEnvironment().getSubtypesOf(CopyStrategy.class)) {
        if (copyStrategy.getAnnotation(RegisterCopyStrategy.class) == null) {
            continue;
        }
        Class<?> targetType = ReflectionUtil.getTypeParameterForSuper(copyStrategy, CopyStrategy.class, 0);
        if (targetType != null) {
            registerCopyStrategy(copyStrategyLibrary, targetType, copyStrategy);
        } else {
            logger.error("Cannot register CopyStrategy '{}' - unable to determine target type", copyStrategy);
        }
    }
    ReflectFactory reflectFactory = context.get(ReflectFactory.class);
    TypeSerializationLibrary typeSerializationLibrary = TypeSerializationLibrary.createDefaultLibrary(reflectFactory, copyStrategyLibrary);
    typeSerializationLibrary.add(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    context.put(TypeSerializationLibrary.class, typeSerializationLibrary);
    // Entity System Library
    EntitySystemLibrary library = new EntitySystemLibrary(context, typeSerializationLibrary);
    context.put(EntitySystemLibrary.class, library);
    ComponentLibrary componentLibrary = library.getComponentLibrary();
    context.put(ComponentLibrary.class, componentLibrary);
    context.put(EventLibrary.class, library.getEventLibrary());
    context.put(ClassMetaLibrary.class, new ClassMetaLibraryImpl(context));
    registerComponents(componentLibrary, moduleManager.getEnvironment());
    registerTypeHandlers(context, typeSerializationLibrary, moduleManager.getEnvironment());
    BlockFamilyFactoryRegistry blockFamilyFactoryRegistry = context.get(BlockFamilyFactoryRegistry.class);
    loadFamilies((DefaultBlockFamilyFactoryRegistry) blockFamilyFactoryRegistry, moduleManager.getEnvironment());
    ModuleAwareAssetTypeManager assetTypeManager = context.get(ModuleAwareAssetTypeManager.class);
    /*
         * The registering of the prefab formats is done in this method, because it needs to be done before
         * the environment of the asset manager gets changed.
         *
         * It can't be done before this method gets called because the ComponentLibrary isn't
         * existing then yet.
         */
    unregisterPrefabFormats(assetTypeManager);
    registeredPrefabFormat = new PrefabFormat(componentLibrary, typeSerializationLibrary);
    assetTypeManager.registerCoreFormat(Prefab.class, registeredPrefabFormat);
    registeredPrefabDeltaFormat = new PrefabDeltaFormat(componentLibrary, typeSerializationLibrary);
    assetTypeManager.registerCoreDeltaFormat(Prefab.class, registeredPrefabDeltaFormat);
    assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
}
Also used : ModuleAwareAssetTypeManager(org.terasology.assets.module.ModuleAwareAssetTypeManager) PrefabFormat(org.terasology.entitySystem.prefab.internal.PrefabFormat) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) PrefabDeltaFormat(org.terasology.entitySystem.prefab.internal.PrefabDeltaFormat) ModuleManager(org.terasology.engine.module.ModuleManager) RegisterCopyStrategy(org.terasology.reflection.copy.RegisterCopyStrategy) CollisionGroupTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary) DefaultBlockFamilyFactoryRegistry(org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry) BlockFamilyFactoryRegistry(org.terasology.world.block.family.BlockFamilyFactoryRegistry)

Example 3 with ComponentLibrary

use of org.terasology.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchToPreviewEnvironment.

public void handleSwitchToPreviewEnvironment(Context context, ModuleEnvironment environment) {
    cheapAssetManagerUpdate(context, environment);
    ComponentLibrary library = new ComponentLibrary(context);
    context.put(ComponentLibrary.class, library);
    registerComponents(library, environment);
}
Also used : ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary)

Example 4 with ComponentLibrary

use of org.terasology.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.

the class Actor method getComponentField.

public Object getComponentField(ComponentFieldUri uri) {
    ComponentLibrary componentLibrary = CoreRegistry.get(EntitySystemLibrary.class).getComponentLibrary();
    ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(uri.getComponentUri());
    if (metadata == null) {
        return null;
    }
    Component component = entity.getComponent(metadata.getType());
    if (component == null) {
        return null;
    }
    FieldMetadata<?, ?> fieldMetadata = metadata.getField(uri.getFieldName());
    if (fieldMetadata == null) {
        return null;
    }
    Field field = fieldMetadata.getField();
    try {
        return field.get(component);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) Component(org.terasology.entitySystem.Component)

Example 5 with ComponentLibrary

use of org.terasology.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.

the class ComponentSerializerTest method setup.

@Before
public void setup() {
    context = new ContextImpl();
    context.put(ModuleManager.class, moduleManager);
    CoreRegistry.setContext(context);
    TypeSerializationLibrary serializationLibrary = new TypeSerializationLibrary(reflectFactory, copyStrategyLibrary);
    serializationLibrary.add(Vector3f.class, new Vector3fTypeHandler());
    serializationLibrary.add(Quat4f.class, new Quat4fTypeHandler());
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    context.put(NetworkSystem.class, networkSystem);
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager entityManager = context.get(EngineEntityManager.class);
    entityManager.getComponentLibrary().register(new SimpleUri("test", "gettersetter"), GetterSetterComponent.class);
    entityManager.getComponentLibrary().register(new SimpleUri("test", "string"), StringComponent.class);
    entityManager.getComponentLibrary().register(new SimpleUri("test", "integer"), IntegerComponent.class);
    ComponentLibrary componentLibrary = entityManager.getComponentLibrary();
    componentSerializer = new ComponentSerializer(componentLibrary, serializationLibrary);
}
Also used : EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) Vector3fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector3fTypeHandler) NetworkSystem(org.terasology.network.NetworkSystem) SimpleUri(org.terasology.engine.SimpleUri) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary) Quat4fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Quat4fTypeHandler) ContextImpl(org.terasology.context.internal.ContextImpl) ComponentSerializer(org.terasology.persistence.serializers.ComponentSerializer) Before(org.junit.Before)

Aggregations

ComponentLibrary (org.terasology.entitySystem.metadata.ComponentLibrary)6 TypeSerializationLibrary (org.terasology.persistence.typeHandling.TypeSerializationLibrary)3 Before (org.junit.Before)2 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)2 ContextImpl (org.terasology.context.internal.ContextImpl)2 ModuleManager (org.terasology.engine.module.ModuleManager)2 Component (org.terasology.entitySystem.Component)2 EntitySystemLibrary (org.terasology.entitySystem.metadata.EntitySystemLibrary)2 PrefabFormat (org.terasology.entitySystem.prefab.internal.PrefabFormat)2 NetworkSystem (org.terasology.network.NetworkSystem)2 Field (java.lang.reflect.Field)1 SimpleUri (org.terasology.engine.SimpleUri)1 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 PrefabData (org.terasology.entitySystem.prefab.PrefabData)1 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)1 PojoPrefabManager (org.terasology.entitySystem.prefab.internal.PojoPrefabManager)1 PrefabDeltaFormat (org.terasology.entitySystem.prefab.internal.PrefabDeltaFormat)1 ComponentSerializer (org.terasology.persistence.serializers.ComponentSerializer)1 CollisionGroupTypeHandler (org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler)1