private static WorldGenerator loadGenerator(Class<?> generatorClass, SimpleUri uri) throws UnresolvedWorldGeneratorException { if (isValidWorldGenerator(generatorClass)) { try { return (WorldGenerator) generatorClass.getConstructor(SimpleUri.class).newInstance(uri); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new UnresolvedWorldGeneratorException("Failed to instantiate world generator '" + uri + "'", e); } } else { throw new UnresolvedWorldGeneratorException(uri + " is not a valid world generator"); } }
/** * @param uri uri of the world generator to create. * @param context that will be used to inject teh world generator. * @param environment to be searched for the world generator class. * @return a new world generator with the specified uri. */ public static WorldGenerator createWorldGenerator(SimpleUri uri, Context context, ModuleEnvironment environment) throws UnresolvedWorldGeneratorException { for (Class<?> generatorClass : environment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) { RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class); SimpleUri generatorUri = new SimpleUri(environment.getModuleProviding(generatorClass), annotation.id()); if (generatorUri.equals(uri)) { WorldGenerator worldGenerator = loadGenerator(generatorClass, generatorUri); InjectionHelper.inject(worldGenerator, context); return worldGenerator; } } throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found"); }
/** * @param uri uri of the world generator to create. * @param context objects from this context will be injected into the * @return The instantiated world generator. */ public static WorldGenerator createGenerator(SimpleUri uri, Context context) throws UnresolvedWorldGeneratorException { ModuleManager moduleManager = context.get(ModuleManager.class); Module module = moduleManager.getEnvironment().get(uri.getModuleName()); if (module == null) { DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry()); ResolutionResult result = resolver.resolve(uri.getModuleName()); if (!result.isSuccess()) { if (moduleManager.getRegistry().getLatestModuleVersion(uri.getModuleName()) == null) { throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found"); } else { throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - unable to resolve module dependencies"); } } try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) { return createWorldGenerator(uri, context, environment); } } else { return createWorldGenerator(uri, context, moduleManager.getEnvironment()); } }