Search in sources :

Example 1 with SimpleClassFactory

use of org.terasology.gestalt.util.reflection.SimpleClassFactory in project Terasology by MovingBlocks.

the class BlockFamilyLibrary method createFamily.

/**
 * Create a family based on the type and instantiate from the the family definition of the block and builder
 *
 * @param blockFamily
 * @param blockFamilyDefinition
 * @param blockBuilderHelper
 * @param shape
 * @return new BlockFamily
 */
public static BlockFamily createFamily(Class<? extends AbstractBlockFamily> blockFamily, BlockFamilyDefinition blockFamilyDefinition, BlockShape shape, BlockBuilderHelper blockBuilderHelper) {
    try {
        SimpleClassFactory simpleClassFactory = new SimpleClassFactory(new ParameterProvider() {

            @Override
            public <T> Optional<T> get(Class<T> type) {
                if (type.isAssignableFrom(BlockBuilderHelper.class)) {
                    return Optional.ofNullable((T) blockBuilderHelper);
                } else if (type.isAssignableFrom(BlockFamilyDefinition.class)) {
                    return Optional.ofNullable((T) blockFamilyDefinition);
                } else if (type.isAssignableFrom(BlockShape.class)) {
                    return Optional.ofNullable((T) shape);
                }
                return Optional.empty();
            }
        });
        BlockFamily result = simpleClassFactory.instantiateClass(blockFamily).get();
        InjectionHelper.inject(result);
        if (result.getURI() == null) {
            throw new Exception("Family Is missng a BlockUri");
        }
        return result;
    } catch (Exception e) {
        logger.error("Failed to load blockFamily {}", blockFamily, e);
    }
    return null;
}
Also used : ParameterProvider(org.terasology.gestalt.util.reflection.ParameterProvider) Optional(java.util.Optional) BlockShape(org.terasology.engine.world.block.shapes.BlockShape) BlockBuilderHelper(org.terasology.engine.world.block.BlockBuilderHelper) SimpleClassFactory(org.terasology.gestalt.util.reflection.SimpleClassFactory)

Example 2 with SimpleClassFactory

use of org.terasology.gestalt.util.reflection.SimpleClassFactory in project Terasology by MovingBlocks.

the class BlockFamilyLibrary method createFamily.

/**
 * Create a family based on the type and instantiate from the the family definition of the block and builder
 *
 * @param blockFamily
 * @param blockFamilyDefinition
 * @param blockBuilderHelper
 * @return
 */
public static BlockFamily createFamily(Class<? extends AbstractBlockFamily> blockFamily, BlockFamilyDefinition blockFamilyDefinition, BlockBuilderHelper blockBuilderHelper) {
    try {
        SimpleClassFactory simpleClassFactory = new SimpleClassFactory(new ParameterProvider() {

            @Override
            public <T> Optional<T> get(Class<T> type) {
                if (type.isAssignableFrom(BlockBuilderHelper.class)) {
                    return Optional.ofNullable((T) blockBuilderHelper);
                } else if (type.isAssignableFrom(BlockFamilyDefinition.class)) {
                    return Optional.ofNullable((T) blockFamilyDefinition);
                }
                return Optional.empty();
            }
        });
        BlockFamily result = simpleClassFactory.instantiateClass(blockFamily).get();
        InjectionHelper.inject(result);
        if (result.getURI() == null) {
            throw new Exception("Family Is missng a BlockUri");
        }
        return result;
    } catch (Exception e) {
        logger.error("Failed to load blockFamily {}", blockFamily, e);
    }
    return null;
}
Also used : ParameterProvider(org.terasology.gestalt.util.reflection.ParameterProvider) Optional(java.util.Optional) BlockBuilderHelper(org.terasology.engine.world.block.BlockBuilderHelper) SimpleClassFactory(org.terasology.gestalt.util.reflection.SimpleClassFactory)

Aggregations

Optional (java.util.Optional)2 BlockBuilderHelper (org.terasology.engine.world.block.BlockBuilderHelper)2 ParameterProvider (org.terasology.gestalt.util.reflection.ParameterProvider)2 SimpleClassFactory (org.terasology.gestalt.util.reflection.SimpleClassFactory)2 BlockShape (org.terasology.engine.world.block.shapes.BlockShape)1