Search in sources :

Example 1 with TypeHandlerFactory

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

the class AssetTypeHandlerFactoryTest method testCreate.

@Test
public void testCreate() {
    TypeHandlerFactory factory = new AssetTypeHandlerFactory();
    List<TypeInfo<? extends Asset>> typesToTest = Lists.newArrayList(TypeInfo.of(Texture.class), TypeInfo.of(UIElement.class), TypeInfo.of(StaticSound.class), TypeInfo.of(StreamingSound.class));
    for (TypeInfo<? extends Asset> typeInfo : typesToTest) {
        Optional<? extends TypeHandler<? extends Asset>> typeHandler = factory.create(typeInfo, null);
        assertTrue(typeHandler.isPresent());
        assertTrue(typeHandler.get() instanceof AssetTypeHandler);
    }
}
Also used : UIElement(org.terasology.nui.asset.UIElement) StaticSound(org.terasology.engine.audio.StaticSound) AssetTypeHandler(org.terasology.engine.persistence.typeHandling.extensionTypes.AssetTypeHandler) Asset(org.terasology.gestalt.assets.Asset) TypeHandlerFactory(org.terasology.persistence.typeHandling.TypeHandlerFactory) StreamingSound(org.terasology.engine.audio.StreamingSound) TypeInfo(org.terasology.reflection.TypeInfo) Texture(org.terasology.engine.rendering.assets.texture.Texture) Test(org.junit.jupiter.api.Test)

Example 2 with TypeHandlerFactory

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

the class EnvironmentSwitchHandler method registerTypeHandlers.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void registerTypeHandlers(Context context, TypeHandlerLibrary library, ModuleEnvironment environment) {
    for (Class<? extends TypeHandler> handler : environment.getSubtypesOf(TypeHandler.class)) {
        RegisterTypeHandler register = handler.getAnnotation(RegisterTypeHandler.class);
        if (register != null) {
            Optional<Type> opt = GenericsUtil.getTypeParameterBindingForInheritedClass(handler, TypeHandler.class, 0);
            if (opt.isPresent()) {
                TypeHandler instance = InjectionHelper.createWithConstructorInjection(handler, context);
                InjectionHelper.inject(instance, context);
                library.addTypeHandler(TypeInfo.of(opt.get()), instance);
            }
        }
    }
    for (Class<? extends TypeHandlerFactory> clazz : environment.getSubtypesOf(TypeHandlerFactory.class)) {
        if (!clazz.isAnnotationPresent(RegisterTypeHandlerFactory.class)) {
            continue;
        }
        TypeHandlerFactory instance = InjectionHelper.createWithConstructorInjection(clazz, context);
        InjectionHelper.inject(instance, context);
        library.addTypeHandlerFactory(instance);
    }
}
Also used : Type(java.lang.reflect.Type) RegisterTypeHandlerFactory(org.terasology.engine.persistence.typeHandling.RegisterTypeHandlerFactory) RegisterTypeHandler(org.terasology.engine.persistence.typeHandling.RegisterTypeHandler) TypeHandlerFactory(org.terasology.persistence.typeHandling.TypeHandlerFactory) RegisterTypeHandlerFactory(org.terasology.engine.persistence.typeHandling.RegisterTypeHandlerFactory) CollisionGroupTypeHandler(org.terasology.engine.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) RegisterTypeHandler(org.terasology.engine.persistence.typeHandling.RegisterTypeHandler)

Aggregations

TypeHandlerFactory (org.terasology.persistence.typeHandling.TypeHandlerFactory)2 Type (java.lang.reflect.Type)1 Test (org.junit.jupiter.api.Test)1 StaticSound (org.terasology.engine.audio.StaticSound)1 StreamingSound (org.terasology.engine.audio.StreamingSound)1 RegisterTypeHandler (org.terasology.engine.persistence.typeHandling.RegisterTypeHandler)1 RegisterTypeHandlerFactory (org.terasology.engine.persistence.typeHandling.RegisterTypeHandlerFactory)1 AssetTypeHandler (org.terasology.engine.persistence.typeHandling.extensionTypes.AssetTypeHandler)1 CollisionGroupTypeHandler (org.terasology.engine.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler)1 Texture (org.terasology.engine.rendering.assets.texture.Texture)1 Asset (org.terasology.gestalt.assets.Asset)1 UIElement (org.terasology.nui.asset.UIElement)1 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)1 TypeInfo (org.terasology.reflection.TypeInfo)1