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