Search in sources :

Example 6 with Difficulty

use of org.spongepowered.api.world.difficulty.Difficulty in project ProjectWorlds by trentech.

the class CommandDifficulty method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
    List<String> list = new ArrayList<>();
    if (arguments.equalsIgnoreCase("difficulty")) {
        return list;
    }
    String[] args = arguments.split(" ");
    if (args.length == 1) {
        for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
            if (world.getWorldName().equalsIgnoreCase(args[0])) {
                for (Difficulty difficulty : Sponge.getRegistry().getAllOf(Difficulty.class)) {
                    list.add(difficulty.getId());
                }
                return list;
            }
            if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
                list.add(world.getWorldName());
            }
        }
    }
    if (args.length == 2) {
        for (Difficulty difficulty : Sponge.getRegistry().getAllOf(Difficulty.class)) {
            if (difficulty.getId().equalsIgnoreCase(args[1])) {
                return list;
            }
            if (difficulty.getId().toLowerCase().startsWith(args[1].toLowerCase())) {
                list.add(difficulty.getId());
            }
        }
    }
    return list;
}
Also used : Difficulty(org.spongepowered.api.world.difficulty.Difficulty) ArrayList(java.util.ArrayList) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 7 with Difficulty

use of org.spongepowered.api.world.difficulty.Difficulty in project ProjectWorlds by trentech.

the class CommandDifficulty method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("difficulty")) {
        throw new CommandException(getHelp().getUsageText());
    }
    String[] args = arguments.split(" ");
    if (args[args.length - 1].equalsIgnoreCase("--help")) {
        help.execute(source);
        return CommandResult.success();
    }
    String worldName;
    String diff;
    try {
        worldName = args[0];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    try {
        diff = args[1];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(worldName);
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
    }
    WorldProperties world = optionalWorld.get();
    Optional<Difficulty> optionalDifficulty = Sponge.getRegistry().getType(Difficulty.class, diff);
    if (!optionalDifficulty.isPresent()) {
        source.sendMessage(Text.of(TextColors.YELLOW, diff, " is not a valid Difficulty"));
        throw new CommandException(getHelp().getUsageText());
    }
    Difficulty difficulty = optionalDifficulty.get();
    world.setDifficulty(difficulty);
    Sponge.getServer().saveWorldProperties(world);
    source.sendMessage(Text.of(TextColors.DARK_GREEN, "Set difficulty of ", world.getWorldName(), " to ", TextColors.YELLOW, difficulty.getTranslation().get().toUpperCase()));
    return CommandResult.success();
}
Also used : Difficulty(org.spongepowered.api.world.difficulty.Difficulty) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 8 with Difficulty

use of org.spongepowered.api.world.difficulty.Difficulty in project core by CubeEngine.

the class DifficultyParser method parse.

@Override
public Difficulty parse(Class type, CommandInvocation invocation) throws ParserException {
    String token = invocation.currentToken();
    Locale locale = invocation.getContext(Locale.class);
    try {
        Difficulty difficulty = difficultyMap.get(Integer.valueOf(token));
        if (difficulty == null) {
            throw new TranslatedParserException(i18n.translate(locale, MessageType.NEGATIVE, "The given difficulty level is unknown!"));
        }
        invocation.consume(1);
        return difficulty;
    } catch (NumberFormatException e) {
        return super.parse(type, invocation);
    }
}
Also used : Locale(java.util.Locale) Difficulty(org.spongepowered.api.world.difficulty.Difficulty) TranslatedParserException(org.cubeengine.libcube.service.command.TranslatedParserException)

Example 9 with Difficulty

use of org.spongepowered.api.world.difficulty.Difficulty in project LanternServer by LanternPowered.

the class CommandDifficulty method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    final ImmutableMap.Builder<String, Object> baseBuilder = ImmutableMap.builder();
    final ImmutableMap.Builder<String, Object> aliasesBuilder = ImmutableMap.builder();
    for (Difficulty difficulty : Sponge.getRegistry().getAllOf(Difficulty.class)) {
        baseBuilder.put(difficulty.getName(), difficulty);
        aliasesBuilder.put(difficulty.getName().substring(0, 1), difficulty);
        aliasesBuilder.put(((LanternDifficulty) difficulty).getInternalId() + "", difficulty);
    }
    specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), ChoicesElement.of(Text.of("difficulty"), baseBuilder.build(), aliasesBuilder.build(), false, true)).executor((src, args) -> {
        WorldProperties world = CommandHelper.getWorldProperties(src, args);
        Difficulty difficulty = args.<Difficulty>getOne("difficulty").get();
        world.setDifficulty(difficulty);
        src.sendMessage(t("commands.difficulty.success", difficulty.getName()));
        return CommandResult.success();
    });
}
Also used : Difficulty(org.spongepowered.api.world.difficulty.Difficulty) LanternDifficulty(org.lanternpowered.server.world.difficulty.LanternDifficulty) LanternDifficulty(org.lanternpowered.server.world.difficulty.LanternDifficulty) ImmutableMap(com.google.common.collect.ImmutableMap) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 10 with Difficulty

use of org.spongepowered.api.world.difficulty.Difficulty in project Nucleus by NucleusPowered.

the class CreateWorldCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    String nameInput = args.<String>getOne(name).get();
    Optional<DimensionType> dimensionInput = args.getOne(dimension);
    Optional<GeneratorType> generatorInput = args.getOne(generator);
    Optional<GameMode> gamemodeInput = args.getOne(gamemode);
    Optional<Difficulty> difficultyInput = args.getOne(difficulty);
    Collection<WorldGeneratorModifier> modifiers = args.getAll(modifier);
    Optional<Long> seedInput = args.getOne(seed);
    boolean genStructures = !args.hasAny("n");
    boolean loadOnStartup = !args.hasAny("l") || args.<Boolean>getOne("l").orElse(true);
    boolean keepSpawnLoaded = !args.hasAny("k") || args.<Boolean>getOne("k").orElse(true);
    boolean allowCommands = !args.hasAny("c") || args.<Boolean>getOne("c").orElse(true);
    boolean bonusChest = !args.hasAny("b") || args.<Boolean>getOne("b").orElse(true);
    if (Sponge.getServer().getAllWorldProperties().stream().anyMatch(x -> x.getWorldName().equalsIgnoreCase(nameInput))) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.create.exists", nameInput));
    }
    // Does the world exist?
    Path worldPath = Sponge.getGame().getGameDirectory().resolve("world");
    Path worldDir = worldPath.resolve(nameInput);
    if (!args.hasAny("i") && Files.exists(worldDir)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.import.noexist", nameInput));
    }
    if (args.hasAny("i") && Files.exists(worldDir)) {
        onImport(worldDir, nameInput);
    }
    WorldArchetype.Builder worldSettingsBuilder = WorldArchetype.builder().enabled(true);
    if (args.hasAny(preset)) {
        WorldArchetype preset1 = args.<WorldArchetype>getOne(preset).get();
        worldSettingsBuilder.from(preset1);
        dimensionInput.ifPresent(worldSettingsBuilder::dimension);
        generatorInput.ifPresent(worldSettingsBuilder::generator);
        gamemodeInput.ifPresent(worldSettingsBuilder::gameMode);
        difficultyInput.ifPresent(worldSettingsBuilder::difficulty);
        if (!modifiers.isEmpty()) {
            modifiers.addAll(preset1.getGeneratorModifiers());
            worldSettingsBuilder.generatorModifiers(modifiers.toArray(new WorldGeneratorModifier[modifiers.size()]));
        }
    } else {
        worldSettingsBuilder.dimension(dimensionInput.orElse(DimensionTypes.OVERWORLD)).generator(generatorInput.orElse(GeneratorTypes.DEFAULT)).gameMode(gamemodeInput.orElse(GameModes.SURVIVAL)).difficulty(difficultyInput.orElse(Difficulties.NORMAL));
        if (!modifiers.isEmpty()) {
            worldSettingsBuilder.generatorModifiers(modifiers.toArray(new WorldGeneratorModifier[modifiers.size()]));
        }
    }
    worldSettingsBuilder.loadsOnStartup(loadOnStartup).keepsSpawnLoaded(keepSpawnLoaded).usesMapFeatures(genStructures).commandsAllowed(allowCommands).generateBonusChest(bonusChest);
    seedInput.ifPresent(worldSettingsBuilder::seed);
    WorldArchetype wa = worldSettingsBuilder.build(nameInput.toLowerCase(), nameInput);
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.create.begin", nameInput));
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.create.newparams", wa.getDimensionType().getName(), wa.getGeneratorType().getName(), modifierString(modifiers), wa.getGameMode().getName(), wa.getDifficulty().getName()));
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.create.newparams2", String.valueOf(loadOnStartup), String.valueOf(keepSpawnLoaded), String.valueOf(genStructures), String.valueOf(allowCommands), String.valueOf(bonusChest)));
    WorldProperties worldProperties = Sponge.getGame().getServer().createWorldProperties(nameInput, wa);
    if (this.worldBorderDefault != null && this.worldBorderDefault > 0) {
        worldProperties.setWorldBorderDiameter(this.worldBorderDefault);
    }
    worldProperties.setDifficulty(wa.getDifficulty());
    if (!Sponge.getServer().saveWorldProperties(worldProperties)) {
        throw ReturnMessageException.fromKey("command.world.create.couldnotsave", nameInput);
    }
    Optional<World> world = Sponge.getGame().getServer().loadWorld(worldProperties);
    if (world.isPresent()) {
        world.get().getProperties().setDifficulty(wa.getDifficulty());
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.create.success", nameInput));
        return CommandResult.success();
    } else {
        throw ReturnMessageException.fromKey("command.world.create.worldfailedtoload", nameInput);
    }
}
Also used : Path(java.nio.file.Path) DimensionType(org.spongepowered.api.world.DimensionType) Difficulty(org.spongepowered.api.world.difficulty.Difficulty) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) GeneratorType(org.spongepowered.api.world.GeneratorType) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) WorldGeneratorModifier(org.spongepowered.api.world.gen.WorldGeneratorModifier) WorldArchetype(org.spongepowered.api.world.WorldArchetype) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

Difficulty (org.spongepowered.api.world.difficulty.Difficulty)11 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)6 DimensionType (org.spongepowered.api.world.DimensionType)4 GeneratorType (org.spongepowered.api.world.GeneratorType)4 WorldGeneratorModifier (org.spongepowered.api.world.gen.WorldGeneratorModifier)4 ArrayList (java.util.ArrayList)3 GameMode (org.spongepowered.api.entity.living.player.gamemode.GameMode)3 Gamemode (com.gmail.trentech.pjw.utils.Gamemode)2 CommandException (org.spongepowered.api.command.CommandException)2 WorldArchetype (org.spongepowered.api.world.WorldArchetype)2 Help (com.gmail.trentech.pjc.help.Help)1 Usage (com.gmail.trentech.pjc.help.Usage)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Locale (java.util.Locale)1 Optional (java.util.Optional)1 TranslatedParserException (org.cubeengine.libcube.service.command.TranslatedParserException)1 LanternDifficulty (org.lanternpowered.server.world.difficulty.LanternDifficulty)1