Search in sources :

Example 1 with Module

use of org.terasology.module.Module in project Terasology by MovingBlocks.

the class CreateGameScreen method initialise.

@Override
@SuppressWarnings("unchecked")
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    UILabel gameTypeTitle = find("gameTypeTitle", UILabel.class);
    if (gameTypeTitle != null) {
        gameTypeTitle.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                if (loadingAsServer) {
                    return translationSystem.translate("${engine:menu#select-multiplayer-game-sub-title}");
                } else {
                    return translationSystem.translate("${engine:menu#select-singleplayer-game-sub-title}");
                }
            }
        });
    }
    final UIText worldName = find("worldName", UIText.class);
    setGameName(worldName);
    final UIText seed = find("seed", UIText.class);
    if (seed != null) {
        seed.setText(new FastRandom().nextString(32));
    }
    final UIDropdownScrollable<Module> gameplay = find("gameplay", UIDropdownScrollable.class);
    gameplay.setOptions(getGameplayModules());
    gameplay.setVisibleOptions(3);
    gameplay.bindSelection(new Binding<Module>() {

        Module selected;

        @Override
        public Module get() {
            return selected;
        }

        @Override
        public void set(Module value) {
            setSelectedGameplayModule(value);
            selected = value;
        }
    });
    gameplay.setOptionRenderer(new StringTextRenderer<Module>() {

        @Override
        public String getString(Module value) {
            return value.getMetadata().getDisplayName().value();
        }

        @Override
        public void draw(Module value, Canvas canvas) {
            canvas.getCurrentStyle().setTextColor(validateModuleDependencies(value.getId()) ? Color.WHITE : Color.RED);
            super.draw(value, canvas);
            canvas.getCurrentStyle().setTextColor(Color.WHITE);
        }
    });
    UILabel gameplayDescription = find("gameplayDescription", UILabel.class);
    gameplayDescription.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Module selectedModule = gameplay.getSelection();
            if (selectedModule != null) {
                return selectedModule.getMetadata().getDescription().value();
            } else {
                return "";
            }
        }
    });
    final UIDropdownScrollable<WorldGeneratorInfo> worldGenerator = find("worldGenerator", UIDropdownScrollable.class);
    if (worldGenerator != null) {
        worldGenerator.bindOptions(new ReadOnlyBinding<List<WorldGeneratorInfo>>() {

            @Override
            public List<WorldGeneratorInfo> get() {
                // grab all the module names and their dependencies
                // This grabs modules from `config.getDefaultModSelection()` which is updated in SelectModulesScreen
                Set<Name> enabledModuleNames = getAllEnabledModuleNames().stream().collect(Collectors.toSet());
                List<WorldGeneratorInfo> result = Lists.newArrayList();
                for (WorldGeneratorInfo option : worldGeneratorManager.getWorldGenerators()) {
                    if (enabledModuleNames.contains(option.getUri().getModuleName())) {
                        result.add(option);
                    }
                }
                return result;
            }
        });
        worldGenerator.setVisibleOptions(3);
        worldGenerator.bindSelection(new Binding<WorldGeneratorInfo>() {

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

            @Override
            public void set(WorldGeneratorInfo value) {
                if (value != null) {
                    config.getWorldGeneration().setDefaultGenerator(value.getUri());
                }
            }
        });
        worldGenerator.setOptionRenderer(new StringTextRenderer<WorldGeneratorInfo>() {

            @Override
            public String getString(WorldGeneratorInfo value) {
                if (value != null) {
                    return value.getDisplayName();
                }
                return "";
            }
        });
        final UIButton playButton = find("play", UIButton.class);
        playButton.bindEnabled(new Binding<Boolean>() {

            @Override
            public Boolean get() {
                return validateModuleDependencies(gameplay.getSelection().getId());
            }

            @Override
            public void set(Boolean value) {
                playButton.setEnabled(value);
            }
        });
    }
    WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
    WidgetUtil.trySubscribe(this, "play", button -> {
        if (worldGenerator.getSelection() == null) {
            MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
            if (errorMessagePopup != null) {
                errorMessagePopup.setMessage("No World Generator Selected", "Select a world generator (you may need to activate a mod with a generator first).");
            }
        } else {
            GameManifest gameManifest = new GameManifest();
            gameManifest.setTitle(worldName.getText());
            gameManifest.setSeed(seed.getText());
            DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
            ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
            if (!result.isSuccess()) {
                MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
                if (errorMessagePopup != null) {
                    errorMessagePopup.setMessage("Invalid Module Selection", "Please review your module seleciton and try again");
                }
                return;
            }
            for (Module module : result.getModules()) {
                gameManifest.addModule(module.getId(), module.getVersion());
            }
            // Time at dawn + little offset to spawn in a brighter env.
            float timeOffset = 0.25f + 0.025f;
            WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, gameManifest.getSeed(), (long) (WorldTime.DAY_LENGTH * timeOffset), worldGenerator.getSelection().getUri());
            gameManifest.addWorld(worldInfo);
            gameEngine.changeState(new StateLoading(gameManifest, (loadingAsServer) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
        }
    });
    UIButton previewSeed = find("previewSeed", UIButton.class);
    ReadOnlyBinding<Boolean> worldGeneratorSelected = new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return worldGenerator != null && worldGenerator.getSelection() != null;
        }
    };
    previewSeed.bindEnabled(worldGeneratorSelected);
    PreviewWorldScreen screen = getManager().createScreen(PreviewWorldScreen.ASSET_URI, PreviewWorldScreen.class);
    WidgetUtil.trySubscribe(this, "previewSeed", button -> {
        if (screen != null) {
            screen.bindSeed(BindHelper.bindBeanProperty("text", seed, String.class));
            try {
                screen.setEnvironment();
                triggerForwardAnimation(screen);
            } catch (Exception e) {
                String msg = "Unable to load world for a 2D preview:\n" + e.toString();
                getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", msg);
                logger.error("Unable to load world for a 2D preview", e);
            }
        }
    });
    WidgetUtil.trySubscribe(this, "mods", w -> triggerForwardAnimation(SelectModulesScreen.ASSET_URI));
}
Also used : Set(java.util.Set) ResolutionResult(org.terasology.module.ResolutionResult) WorldGeneratorInfo(org.terasology.world.generator.internal.WorldGeneratorInfo) UIButton(org.terasology.rendering.nui.widgets.UIButton) UIText(org.terasology.rendering.nui.widgets.UIText) WorldInfo(org.terasology.world.internal.WorldInfo) List(java.util.List) UILabel(org.terasology.rendering.nui.widgets.UILabel) StateLoading(org.terasology.engine.modes.StateLoading) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) Canvas(org.terasology.rendering.nui.Canvas) FastRandom(org.terasology.utilities.random.FastRandom) DependencyResolver(org.terasology.module.DependencyResolver) GameManifest(org.terasology.game.GameManifest) Module(org.terasology.module.Module)

Example 2 with Module

use of org.terasology.module.Module in project Terasology by MovingBlocks.

the class CreateGameScreen method getGameplayModules.

private List<Module> getGameplayModules() {
    List<Module> gameplayModules = Lists.newArrayList();
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        Module latestVersion = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
        if (!latestVersion.isOnClasspath()) {
            if (StandardModuleExtension.isGameplayModule(latestVersion)) {
                gameplayModules.add(latestVersion);
            }
        }
    }
    Collections.sort(gameplayModules, (o1, o2) -> o1.getMetadata().getDisplayName().value().compareTo(o2.getMetadata().getDisplayName().value()));
    return gameplayModules;
}
Also used : Module(org.terasology.module.Module) Name(org.terasology.naming.Name)

Example 3 with Module

use of org.terasology.module.Module in project Terasology by MovingBlocks.

the class SelectModulesScreen method downloadModules.

private void downloadModules(Iterable<Module> modulesToDownload) {
    final WaitPopup<List<Module>> popup = getManager().pushScreen(WaitPopup.ASSET_URI, WaitPopup.class);
    popup.onSuccess(newModules -> {
        for (Module module : newModules) {
            modulesLookup.get(module.getId()).setLocalVersion(module);
            updateValidToSelect();
        }
    });
    ModuleInstaller operation = moduleManager.getInstallManager().createInstaller(modulesToDownload, new DownloadPopupProgressListener(popup));
    popup.startOperation(operation, true);
}
Also used : ModuleInstaller(org.terasology.engine.module.ModuleInstaller) List(java.util.List) ArrayList(java.util.ArrayList) UIList(org.terasology.rendering.nui.widgets.UIList) Module(org.terasology.module.Module)

Example 4 with Module

use of org.terasology.module.Module in project Terasology by MovingBlocks.

the class SelectModulesScreen method onClosed.

@Override
public void onClosed() {
    // moduleConfig passes the module collection to the Create Game Screen.
    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();
}
Also used : SelectModulesConfig(org.terasology.config.SelectModulesConfig) CoreScreenLayer(org.terasology.rendering.nui.CoreScreenLayer) DependencyResolver(org.terasology.module.DependencyResolver) LoggerFactory(org.slf4j.LoggerFactory) Config(org.terasology.config.Config) Future(java.util.concurrent.Future) ModuleMetadata(org.terasology.module.ModuleMetadata) Map(java.util.Map) SimpleUri(org.terasology.engine.SimpleUri) TerasologyConstants(org.terasology.engine.TerasologyConstants) MessagePopup(org.terasology.rendering.nui.layers.mainMenu.MessagePopup) ImmutableSet(com.google.common.collect.ImmutableSet) WidgetUtil(org.terasology.rendering.nui.WidgetUtil) CancellationException(java.util.concurrent.CancellationException) DependencyInfo(org.terasology.module.DependencyInfo) Set(java.util.Set) ConfirmPopup(org.terasology.rendering.nui.layers.mainMenu.ConfirmPopup) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) ResourceUrn(org.terasology.assets.ResourceUrn) In(org.terasology.registry.In) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) MenuAnimationSystems(org.terasology.rendering.nui.animation.MenuAnimationSystems) AbstractItemRenderer(org.terasology.rendering.nui.itemRendering.AbstractItemRenderer) ArrayList(java.util.ArrayList) DependencyResolutionFailedException(org.terasology.engine.module.DependencyResolutionFailedException) Lists(com.google.common.collect.Lists) ResolutionResult(org.terasology.module.ResolutionResult) TranslationSystem(org.terasology.i18n.TranslationSystem) TextChangeEventListener(org.terasology.rendering.nui.widgets.TextChangeEventListener) UIList(org.terasology.rendering.nui.widgets.UIList) Name(org.terasology.naming.Name) ModuleManager(org.terasology.engine.module.ModuleManager) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Vector2i(org.terasology.math.geom.Vector2i) Maps(com.google.common.collect.Maps) UILabel(org.terasology.rendering.nui.widgets.UILabel) WorldGeneratorManager(org.terasology.world.generator.internal.WorldGeneratorManager) ExecutionException(java.util.concurrent.ExecutionException) UICheckbox(org.terasology.rendering.nui.widgets.UICheckbox) Binding(org.terasology.rendering.nui.databinding.Binding) Canvas(org.terasology.rendering.nui.Canvas) UIButton(org.terasology.rendering.nui.widgets.UIButton) ModuleInstaller(org.terasology.engine.module.ModuleInstaller) ModuleConfig(org.terasology.config.ModuleConfig) WaitPopup(org.terasology.rendering.nui.layers.mainMenu.WaitPopup) ResettableUIText(org.terasology.rendering.nui.widgets.ResettableUIText) Comparator(java.util.Comparator) Collections(java.util.Collections) Module(org.terasology.module.Module) SimpleUri(org.terasology.engine.SimpleUri) ModuleConfig(org.terasology.config.ModuleConfig)

Example 5 with Module

use of org.terasology.module.Module in project Terasology by MovingBlocks.

the class SelectModulesScreen method updateModuleInformation.

private void updateModuleInformation() {
    Iterable<Module> remoteModuleRegistry = moduleManager.getInstallManager().getRemoteRegistry();
    Set<Name> filtered = ImmutableSet.of(TerasologyConstants.ENGINE_MODULE, new Name("engine-test"));
    for (Module remote : remoteModuleRegistry) {
        ModuleSelectionInfo info = modulesLookup.get(remote.getId());
        if (!filtered.contains(remote.getId())) {
            if (info == null) {
                info = ModuleSelectionInfo.remote(remote);
                modulesLookup.put(remote.getId(), info);
                int pos = Collections.binarySearch(sortedModules, info, moduleInfoComparator);
                if (pos < 0) {
                    // not yet in the (sorted) list
                    // use "insertion point" to keep the list sorted
                    sortedModules.add(-pos - 1, info);
                    allSortedModules.clear();
                    allSortedModules.addAll(sortedModules);
                }
            }
            info.setOnlineVersion(remote);
        }
    }
}
Also used : Module(org.terasology.module.Module) Name(org.terasology.naming.Name)

Aggregations

Module (org.terasology.module.Module)32 Name (org.terasology.naming.Name)15 ModuleManager (org.terasology.engine.module.ModuleManager)11 DependencyResolver (org.terasology.module.DependencyResolver)11 ModuleEnvironment (org.terasology.module.ModuleEnvironment)9 ResolutionResult (org.terasology.module.ResolutionResult)8 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ModuleMetadata (org.terasology.module.ModuleMetadata)4 IOException (java.io.IOException)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 SimpleUri (org.terasology.engine.SimpleUri)3 GameManifest (org.terasology.game.GameManifest)3 Vector2i (org.terasology.math.geom.Vector2i)3 DependencyInfo (org.terasology.module.DependencyInfo)3 ModuleRegistry (org.terasology.module.ModuleRegistry)3