@Override public void initialise(GameEngine engine, Context rootContext) { rootContext.put(WorldGeneratorManager.class, new WorldGeneratorManager(rootContext)); } }
@Override public WorldGeneratorInfo get() { // get the default generator from the config. This is likely to have a user triggered selection. WorldGeneratorInfo info = worldGeneratorManager.getWorldGeneratorInfo(config.getWorldGeneration().getDefaultGenerator()); if (info != null && getAllEnabledModuleNames().contains(info.getUri().getModuleName())) { return info; } // just use the first available generator for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) { if (getAllEnabledModuleNames().contains(worldGenInfo.getUri().getModuleName())) { set(worldGenInfo); return worldGenInfo; } } return null; }
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"); } }
genTexture(); worldGenerator = WorldGeneratorManager.createWorldGenerator(worldGenUri, subContext, environment); worldGenerator.setWorldSeed(seed.getText());
WorldGenerator worldGenerator; try { worldGenerator = WorldGeneratorManager.createGenerator(worldInfo.getWorldGenerator(), context); } catch (UnresolvedWorldGeneratorException e) { logger.error("Unable to load world generator {}. Available world generators: {}", worldInfo.getWorldGenerator(), worldGeneratorManager.getWorldGenerators()); context.get(GameEngine.class).changeState(new StateMainMenu("Failed to resolve world generator.")); return true; // We need to return true, otherwise the loading state will just call us again immediately
@Override public List<WorldGeneratorInfo> get() { // grab all the module names and their dependencies // This grabs modules from `config.getDefaultModSelection()` which is updated in AdvancedGameSetupScreen final Set<Name> enabledModuleNames = new HashSet<>(getAllEnabledModuleNames()); final List<WorldGeneratorInfo> result = Lists.newArrayList(); for (WorldGeneratorInfo option : worldGeneratorManager.getWorldGenerators()) { if (enabledModuleNames.contains(option.getUri().getModuleName())) { result.add(option); } } return result; } });
public WorldGeneratorManager(Context context) { this.context = context; refresh(); }
/** * @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"); }
@Override public void set(String value) { selectedWorld = value; try { if (findWorldByName(selectedWorld).getWorldGenerator() == null) { worldGenerator = WorldGeneratorManager.createWorldGenerator(findWorldByName(selectedWorld).getWorldGeneratorInfo().getUri(), context, environment); findWorldByName(selectedWorld).setWorldGenerator(worldGenerator); } else { worldGenerator = findWorldByName(selectedWorld).getWorldGenerator(); } if (worldGenerator.getWorldSeed() == null) { worldGenerator.setWorldSeed(createSeed(selectedWorld)); } previewGen = new FacetLayerPreview(environment, worldGenerator); updatePreview(); } catch (UnresolvedWorldGeneratorException e) { e.printStackTrace(); } } });
private void setDefaultGeneratorOfGameplayModule(Module module) { // Set the default generator of the selected gameplay module SimpleUri defaultWorldGenerator = StandardModuleExtension.getDefaultWorldGenerator(module); if (defaultWorldGenerator != null) { for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) { if (worldGenInfo.getUri().equals(defaultWorldGenerator)) { config.getWorldGeneration().setDefaultGenerator(worldGenInfo.getUri()); } } } config.save(); }
private void saveConfiguration() { // moduleConfig passes the module collection to other screens ModuleConfig moduleConfig = config.getDefaultModSelection(); moduleConfig.clear(); // Fetch all the selected/activated modules using allSortedModules // instead of fetching only selected/activated modules from filtered collection // of modules using sortedModules allSortedModules.stream().filter(info -> info.isSelected() && info.isExplicitSelection()) .forEach(info -> moduleConfig.addModule(info.getMetadata().getId())); SimpleUri defaultGenerator = config.getWorldGeneration().getDefaultGenerator(); ModuleSelectionInfo info = modulesLookup.get(defaultGenerator.getModuleName()); if (info != null && !info.isSelected()) { config.getWorldGeneration().setDefaultGenerator(new SimpleUri()); } worldGenManager.refresh(); config.save(); }
/** * This method sets the world whose properties are to be changed. This function is called before the screen comes * to the forefront. * @param subContext the new environment created in {@link UniverseSetupScreen} * @param worldSelected the world whose configurations are to be changed. * @throws UnresolvedWorldGeneratorException */ public void setWorld(Context subContext, WorldSetupWrapper worldSelected) throws UnresolvedWorldGeneratorException { world = worldSelected; context = subContext; SimpleUri worldGenUri = worldSelected.getWorldGeneratorInfo().getUri(); environment = context.get(ModuleEnvironment.class); context.put(WorldGeneratorPluginLibrary.class, new TempWorldGeneratorPluginLibrary(environment, context)); if (world.getWorldGenerator() == null) { worldGenerator = WorldGeneratorManager.createWorldGenerator(worldGenUri, context, environment); world.setWorldGenerator(worldGenerator); } else { worldGenerator = world.getWorldGenerator(); } configureProperties(); }
if (tempEnvironment.getModuleProviding(generatorClass).equals(module.getId())) { RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class); if (isValidWorldGenerator(generatorClass)) { SimpleUri uri = new SimpleUri(moduleId, annotation.id()); infos.add(new WorldGeneratorInfo(uri, annotation.displayName(), annotation.description()));
/** * @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()); } }
@Override public void onOpened() { try { if (findWorldByName(selectedWorld).getWorldGenerator() == null) { worldGenerator = WorldGeneratorManager.createWorldGenerator(findWorldByName(selectedWorld).getWorldGeneratorInfo().getUri(), context, environment); findWorldByName(selectedWorld).setWorldGenerator(worldGenerator); } else { worldGenerator = findWorldByName(selectedWorld).getWorldGenerator(); } if (worldGenerator.getWorldSeed().isEmpty()) { worldGenerator.setWorldSeed(createSeed(selectedWorld)); } previewGen = new FacetLayerPreview(environment, worldGenerator); updatePreview(); } catch (UnresolvedWorldGeneratorException e) { e.printStackTrace(); } }
private void setWorldGenerators() { for (WorldSetupWrapper worldSetupWrapper : worldList) { if (worldSetupWrapper.getWorldGenerator() == null) { try { worldSetupWrapper.setWorldGenerator(WorldGeneratorManager.createWorldGenerator(findWorldByName(worldSetupWrapper.getWorldName().toString()).getWorldGeneratorInfo().getUri(), context, environment)); } catch (UnresolvedWorldGeneratorException e) { e.printStackTrace(); } } worldSetupWrapper.getWorldGenerator().setWorldSeed(createSeed(worldSetupWrapper.getWorldName().toString())); } }