Search in sources :

Example 11 with CommandException

use of org.spongepowered.api.command.CommandException 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)

Aggregations

CommandException (org.spongepowered.api.command.CommandException)11 Text (org.spongepowered.api.text.Text)9 Collectors (java.util.stream.Collectors)8 Sponge (org.spongepowered.api.Sponge)8 CommandResult (org.spongepowered.api.command.CommandResult)8 CommandSource (org.spongepowered.api.command.CommandSource)8 CommandContext (org.spongepowered.api.command.args.CommandContext)8 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)8 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)8 TextColors (org.spongepowered.api.text.format.TextColors)8 List (java.util.List)7 Player (org.spongepowered.api.entity.living.player.Player)7 Optional (java.util.Optional)5 PaginationService (org.spongepowered.api.service.pagination.PaginationService)5 RegionService (com.skelril.skree.service.RegionService)4 Region (com.skelril.skree.service.internal.region.Region)4 BigDecimal (java.math.BigDecimal)4 TEAccount (com.erigitic.config.TEAccount)2 MarketService (com.skelril.skree.service.MarketService)2 WorldService (com.skelril.skree.service.WorldService)2