Search in sources :

Example 31 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Skree by Skelril.

the class RegionListMembersCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    UserStorageService userService = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    List<Text> result = ref.getMembers().stream().map(userService::get).filter(Optional::isPresent).map(Optional::get).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).map(a -> Text.of((a.isOnline() ? TextColors.GREEN : TextColors.RED), a.getName())).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Members")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) UserStorageService(org.spongepowered.api.service.user.UserStorageService) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Example 32 with CommandResult

use of org.spongepowered.api.command.CommandResult in project SpongeCommon by SpongePowered.

the class MixinServerCommandManager method executeCommand.

/**
 * @author zml
 *
 * Purpose: Reroute MC command handling through Sponge
 * Reasoning: All commands should go through one system -- we need none of the MC handling code
 */
@Override
public int executeCommand(ICommandSender sender, String command) {
    command = command.trim();
    if (command.startsWith("/")) {
        command = command.substring(1);
    }
    CommandSource source = WrapperCommandSource.of(sender);
    CommandResult result = SpongeImpl.getGame().getCommandManager().process(source, command);
    updateStat(sender, CommandResultStats.Type.AFFECTED_BLOCKS, result.getAffectedBlocks());
    updateStat(sender, CommandResultStats.Type.AFFECTED_ENTITIES, result.getAffectedEntities());
    updateStat(sender, CommandResultStats.Type.AFFECTED_ITEMS, result.getAffectedItems());
    updateStat(sender, CommandResultStats.Type.QUERY_RESULT, result.getQueryResult());
    updateStat(sender, CommandResultStats.Type.SUCCESS_COUNT, result.getSuccessCount());
    return result.getSuccessCount().orElse(0);
// return super.executeCommand(sender, command); // Try Vanilla instead
}
Also used : WrapperCommandSource(org.spongepowered.common.command.WrapperCommandSource) CommandSource(org.spongepowered.api.command.CommandSource) CommandResult(org.spongepowered.api.command.CommandResult)

Example 33 with CommandResult

use of org.spongepowered.api.command.CommandResult in project AtherysCore by Atherys-Horizons.

the class PartyInviteCommand method execute.

@Nonnull
@Override
public CommandResult execute(@Nonnull User user, @Nonnull CommandContext args) throws CommandException {
    Optional<Player> invitee = args.getOne("invitedPlayer");
    if (!invitee.isPresent())
        return CommandResult.empty();
    // if invitee is already in another party, don't invite
    if (PartyManager.getInstance().hasPlayerParty(invitee.get())) {
        PartyMsg.error(user, "That player is already in another party.");
        return CommandResult.success();
    }
    Optional<Party> playerParty = PartyManager.getInstance().getPlayerParty(user);
    if (playerParty.isPresent() && playerParty.get().isLeader(user)) {
        // If player has party and is leader of that party, invitee will be invited to player's party
        Party party = playerParty.get();
        Question q = Question.of(Text.of(user.getName(), " has invited you to their party. Would you like to join?")).addAnswer(Question.Answer.of(Text.of(TextStyles.BOLD, TextColors.DARK_GREEN, "Yes"), (respondent) -> {
            party.addPlayer(respondent);
            PartyMsg.info(party, respondent.getName(), " has joined your party!");
        })).addAnswer(Question.Answer.of(Text.of(TextStyles.BOLD, TextColors.DARK_RED, "No"), (respondent) -> PartyMsg.error(user, respondent.getName(), " has refused to join your party."))).build();
        q.pollChat(invitee.get());
    } else {
        // If player does not have a party, and neither does invitee, then the party will be created when invitee accepts party invite
        Question q = Question.of(Text.of(user.getName(), " has invited you to their party. Would you like to join?")).addAnswer(Question.Answer.of(Text.of(TextStyles.BOLD, TextColors.DARK_GREEN, "Yes"), (respondent) -> {
            if (!user.isOnline()) {
                // If when party invite is accepted, player is offline, party will not be created.
                PartyMsg.error(respondent, user.getName(), " has gone offline since sending you that invite. Party could not be created. Sorry!");
                return;
            }
            Party party = Party.of(user, invitee.get());
            PartyMsg.info(party, "Your party has been created! Do ", TextColors.GREEN, TextStyles.BOLD, "/party", TextColors.DARK_AQUA, TextStyles.RESET, " for a list of members.");
        })).addAnswer(Question.Answer.of(Text.of(TextStyles.BOLD, TextColors.DARK_RED, "No"), (respondent) -> PartyMsg.error(user, respondent.getName(), " has refused to join your party."))).build();
        q.pollChat(invitee.get());
    }
    return CommandResult.success();
}
Also used : Party(com.atherys.core.party.Party) CommandResult(org.spongepowered.api.command.CommandResult) User(org.spongepowered.api.entity.living.player.User) TextStyles(org.spongepowered.api.text.format.TextStyles) Question(com.atherys.core.utils.Question) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) PartyManager(com.atherys.core.party.PartyManager) UserCommand(com.atherys.core.commands.UserCommand) Optional(java.util.Optional) PartyMsg(com.atherys.core.party.PartyMsg) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Nonnull(javax.annotation.Nonnull) Player(org.spongepowered.api.entity.living.player.Player) Party(com.atherys.core.party.Party) Question(com.atherys.core.utils.Question) Nonnull(javax.annotation.Nonnull)

Example 34 with CommandResult

use of org.spongepowered.api.command.CommandResult in project ProjectWorlds by trentech.

the class CommandLoad method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("load")) {
        throw new CommandException(getHelp().getUsageText());
    }
    if (arguments.equalsIgnoreCase("--help")) {
        help.execute(source);
        return CommandResult.success();
    }
    Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(arguments);
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, arguments, " does not exist"), false);
    }
    WorldProperties world = optionalWorld.get();
    if (Sponge.getServer().getWorld(world.getUniqueId()).isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " is already loaded"), false);
    }
    WorldData worldData = new WorldData(world.getWorldName());
    if (!worldData.exists()) {
        throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " does not exist"), false);
    }
    SpongeData spongeData = new SpongeData(world.getWorldName());
    if (!spongeData.exists()) {
        source.sendMessage(Text.of(TextColors.RED, "Foriegn world detected"));
        source.sendMessage(Text.builder().color(TextColors.YELLOW).onHover(TextActions.showText(Text.of("Click command for more information "))).onClick(TextActions.runCommand("/pjw:world import")).append(Text.of(" /world import")).build());
        return CommandResult.success();
    }
    source.sendMessage(Text.of(TextColors.YELLOW, "Preparing spawn area. This may take a minute."));
    Task.builder().delayTicks(20).execute(c -> {
        Optional<World> load = Sponge.getServer().loadWorld(world);
        if (!load.isPresent()) {
            source.sendMessage(Text.of(TextColors.RED, "Could not load ", world.getWorldName()));
            return;
        }
        if (CommandCreate.worlds.contains(world.getWorldName())) {
            Utils.createPlatform(load.get().getSpawnLocation().getRelative(Direction.DOWN));
            CommandCreate.worlds.remove(world.getWorldName());
        }
        source.sendMessage(Text.of(TextColors.DARK_GREEN, world.getWorldName(), " loaded successfully"));
    }).submit(Main.getPlugin());
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Location(org.spongepowered.api.world.Location) Utils(com.gmail.trentech.pjw.utils.Utils) CommandSource(org.spongepowered.api.command.CommandSource) WorldData(com.gmail.trentech.pjw.io.WorldData) CommandCallable(org.spongepowered.api.command.CommandCallable) Sponge(org.spongepowered.api.Sponge) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) Direction(org.spongepowered.api.util.Direction) List(java.util.List) Text(org.spongepowered.api.text.Text) Main(com.gmail.trentech.pjw.Main) World(org.spongepowered.api.world.World) Task(org.spongepowered.api.scheduler.Task) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Help(com.gmail.trentech.pjc.help.Help) Optional(java.util.Optional) SpongeData(com.gmail.trentech.pjw.io.SpongeData) TextColors(org.spongepowered.api.text.format.TextColors) Optional(java.util.Optional) SpongeData(com.gmail.trentech.pjw.io.SpongeData) CommandException(org.spongepowered.api.command.CommandException) WorldData(com.gmail.trentech.pjw.io.WorldData) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 35 with CommandResult

use of org.spongepowered.api.command.CommandResult in project ProjectWorlds by trentech.

the class CommandRegen method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("regen")) {
        throw new CommandException(getHelp().getUsageText());
    }
    String[] args = arguments.split(" ");
    if (args[args.length - 1].equalsIgnoreCase("--help")) {
        getHelp().execute(source);
        return CommandResult.success();
    }
    String worldName;
    try {
        worldName = args[0];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<WorldProperties> optionalProperties = Sponge.getServer().getWorldProperties(worldName);
    if (!optionalProperties.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
    }
    WorldProperties properties = optionalProperties.get();
    if (Sponge.getServer().getWorld(properties.getWorldName()).isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, properties.getWorldName(), " must be unloaded before you can regenerate"), false);
    }
    WorldArchetype.Builder builder = WorldArchetype.builder().from(properties);
    String bool;
    try {
        bool = args[1];
        if (!bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("false")) {
            source.sendMessage(Text.of(TextColors.YELLOW, bool, " is not a valid Boolean"));
            throw new CommandException(getHelp().getUsageText());
        }
        String seed;
        try {
            seed = args[2];
            try {
                Long s = Long.parseLong(seed);
                builder.seed(s);
            } catch (Exception e) {
                builder.seed(seed.hashCode());
            }
        } catch (Exception e) {
            builder.randomSeed();
        }
    } catch (Exception e) {
    }
    try {
        CompletableFuture<Boolean> delete = Sponge.getServer().deleteWorld(properties);
        while (!delete.isDone()) {
        }
        if (!delete.get()) {
            throw new CommandException(Text.of(TextColors.RED, "Could not delete ", properties.getWorldName()), false);
        }
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    source.sendMessage(Text.of(TextColors.DARK_GREEN, "Regenerating world.."));
    WorldArchetype settings = builder.enabled(true).loadsOnStartup(true).build(properties.getWorldName() + "_" + (ThreadLocalRandom.current().nextInt(100) + 1), properties.getWorldName());
    WorldProperties newProperties;
    try {
        newProperties = Sponge.getServer().createWorldProperties(properties.getWorldName(), settings);
    } catch (IOException e) {
        e.printStackTrace();
        throw new CommandException(Text.of(TextColors.RED, "Something went wrong. Check server log for details"), false);
    }
    Task.builder().delayTicks(20).execute(c -> {
        Optional<World> load = Sponge.getServer().loadWorld(newProperties);
        if (!load.isPresent()) {
            source.sendMessage(Text.of(TextColors.RED, "Could not load ", properties.getWorldName()));
            return;
        }
        Utils.createPlatform(load.get().getSpawnLocation().getRelative(Direction.DOWN));
        source.sendMessage(Text.of(TextColors.DARK_GREEN, properties.getWorldName(), " regenerated successfully"));
    }).submit(Main.getPlugin());
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Location(org.spongepowered.api.world.Location) Utils(com.gmail.trentech.pjw.utils.Utils) CommandSource(org.spongepowered.api.command.CommandSource) CommandCallable(org.spongepowered.api.command.CommandCallable) Sponge(org.spongepowered.api.Sponge) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) CommandException(org.spongepowered.api.command.CommandException) Direction(org.spongepowered.api.util.Direction) WorldArchetype(org.spongepowered.api.world.WorldArchetype) List(java.util.List) Text(org.spongepowered.api.text.Text) Main(com.gmail.trentech.pjw.Main) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) World(org.spongepowered.api.world.World) Task(org.spongepowered.api.scheduler.Task) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Help(com.gmail.trentech.pjc.help.Help) Optional(java.util.Optional) TextColors(org.spongepowered.api.text.format.TextColors) Optional(java.util.Optional) CommandException(org.spongepowered.api.command.CommandException) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CommandException(org.spongepowered.api.command.CommandException) WorldArchetype(org.spongepowered.api.world.WorldArchetype) ExecutionException(java.util.concurrent.ExecutionException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

CommandResult (org.spongepowered.api.command.CommandResult)62 Text (org.spongepowered.api.text.Text)46 CommandContext (org.spongepowered.api.command.args.CommandContext)40 List (java.util.List)39 CommandSource (org.spongepowered.api.command.CommandSource)37 Player (org.spongepowered.api.entity.living.player.Player)36 Sponge (org.spongepowered.api.Sponge)34 Optional (java.util.Optional)33 Collectors (java.util.stream.Collectors)33 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)26 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)26 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)26 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)26 TextColors (org.spongepowered.api.text.format.TextColors)25 CommandElement (org.spongepowered.api.command.args.CommandElement)21 Util (io.github.nucleuspowered.nucleus.Util)20 GenericArguments (org.spongepowered.api.command.args.GenericArguments)20 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)18 CommandException (org.spongepowered.api.command.CommandException)18