Search in sources :

Example 41 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SetCategoryCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    String warpName = args.<Warp>getOne(warpKey).get().getName();
    if (args.hasAny("r")) {
        // Remove the category.
        if (handler.setWarpCategory(warpName, null)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.category.removed", warpName));
            return CommandResult.success();
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.category.noremove", warpName));
    }
    Optional<Tuple<String, Boolean>> categoryOp = args.getOne(categoryKey);
    if (!categoryOp.isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.category.required"));
    }
    Tuple<String, Boolean> category = categoryOp.get();
    if (!args.hasAny("n") && !category.getSecond()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.category.requirenew", category.getFirst()).toBuilder().onClick(TextActions.runCommand("/warp setcategory -n " + warpName + " " + category.getFirst())).build());
        return CommandResult.empty();
    }
    // Add the category.
    if (handler.setWarpCategory(warpName, category.getFirst())) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.category.added", category.getFirst(), warpName));
        return CommandResult.success();
    }
    WarpCategory c = handler.getWarpCategoryOrDefault(category.getFirst());
    throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.warp.category.couldnotadd", c.getDisplayName(), Text.of(warpName)));
}
Also used : Warp(io.github.nucleuspowered.nucleus.api.nucleusdata.Warp) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Tuple(org.spongepowered.api.util.Tuple) WarpCategory(io.github.nucleuspowered.nucleus.api.nucleusdata.WarpCategory)

Example 42 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SetWarpCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    String warp = args.<String>getOne(WarpCommand.warpNameArg).get();
    // Needs to match the name...
    if (!warpRegex.matcher(warp).matches()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.invalidname"));
        return CommandResult.empty();
    }
    // Get the service, does the warp exist?
    if (qs.getWarp(warp).isPresent()) {
        // You have to delete to set the same name
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.nooverwrite"));
        return CommandResult.empty();
    }
    CreateWarpEvent event = CauseStackHelper.createFrameWithCausesWithReturn(c -> new CreateWarpEvent(c, warp, src.getLocation()), src);
    if (Sponge.getEventManager().post(event)) {
        throw new ReturnMessageException(event.getCancelMessage().orElseGet(() -> plugin.getMessageProvider().getTextMessageWithFormat("nucleus.eventcancelled")));
    }
    // OK! Set it.
    if (qs.setWarp(warp, src.getLocation(), src.getRotation())) {
        // Worked. Tell them.
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.set", warp));
        return CommandResult.success();
    }
    // Didn't work. Tell them.
    throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.warps.seterror"));
}
Also used : ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CreateWarpEvent(io.github.nucleuspowered.nucleus.modules.warp.event.CreateWarpEvent)

Example 43 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException 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)

Example 44 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class DeleteWorldCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties properties = args.<WorldProperties>getOne(world).get();
    if (!properties.isEnabled()) {
        throw ReturnMessageException.fromKey("args.worldproperties.noexistdisabled", properties.getWorldName());
    }
    if (confirm != null && confirm.getFirst().isAfter(Instant.now()) && confirm.getSecond().equals(Util.getUUID(src)) && confirm.getThird().getUniqueId().equals(properties.getUniqueId())) {
        try {
            completeDeletion(src);
        } finally {
            confirm = null;
        }
        return CommandResult.success();
    }
    confirm = null;
    runChecks(properties);
    // Scary warning.
    Path path = plugin.getDataPath().getParent().resolve("world").resolve(properties.getWorldName());
    if (Files.exists(path)) {
        confirm = Tuples.of(Instant.now().plus(30, ChronoUnit.SECONDS), Util.getUUID(src), properties, path);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.delete.warning1", properties.getWorldName()));
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.delete.warning2", path.toAbsolutePath().toString()));
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.delete.warning3", properties.getWorldName()));
        return CommandResult.success();
    } else {
        throw new ReturnMessageException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.world.delete.notfound", properties.getWorldName()));
    }
}
Also used : Path(java.nio.file.Path) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 45 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class DisableWorldCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties worldProperties = args.<WorldProperties>getOne(worldKey).get();
    if (!worldProperties.isEnabled()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.disable.alreadydisabled", worldProperties.getWorldName()));
    }
    if (Sponge.getServer().getWorld(worldProperties.getUniqueId()).isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.disable.warnloaded", worldProperties.getWorldName()));
    }
    worldProperties.setEnabled(false);
    if (worldProperties.isEnabled()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.disable.couldnotdisable", worldProperties.getWorldName()));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.disable.success", worldProperties.getWorldName()));
    return CommandResult.success();
}
Also used : ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)45 Player (org.spongepowered.api.entity.living.player.Player)14 World (org.spongepowered.api.world.World)14 Text (org.spongepowered.api.text.Text)11 User (org.spongepowered.api.entity.living.player.User)9 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)9 CommandResult (org.spongepowered.api.command.CommandResult)7 Inventory (org.spongepowered.api.item.inventory.Inventory)6 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)5 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)5 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)5 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)5 Optional (java.util.Optional)5 CommandContext (org.spongepowered.api.command.args.CommandContext)5 CommandElement (org.spongepowered.api.command.args.CommandElement)5 GenericArguments (org.spongepowered.api.command.args.GenericArguments)5 Home (io.github.nucleuspowered.nucleus.api.nucleusdata.Home)4 Warp (io.github.nucleuspowered.nucleus.api.nucleusdata.Warp)4 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)4 NucleusTeleportHandler (io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler)4