Search in sources :

Example 11 with GeneratorType

use of org.spongepowered.api.world.GeneratorType 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

GeneratorType (org.spongepowered.api.world.GeneratorType)11 DimensionType (org.spongepowered.api.world.DimensionType)8 WorldGeneratorModifier (org.spongepowered.api.world.gen.WorldGeneratorModifier)7 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 WorldArchetype (org.spongepowered.api.world.WorldArchetype)4 Difficulty (org.spongepowered.api.world.difficulty.Difficulty)4 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)3 Gamemode (com.gmail.trentech.pjw.utils.Gamemode)2 List (java.util.List)2 Optional (java.util.Optional)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 Help (com.gmail.trentech.pjc.help.Help)1 Usage (com.gmail.trentech.pjc.help.Usage)1 SpongeData (com.gmail.trentech.pjw.io.SpongeData)1 WorldData (com.gmail.trentech.pjw.io.WorldData)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1