Search in sources :

Example 1 with ItemDescriptor

use of com.skelril.skree.service.internal.market.ItemDescriptor in project Skree by Skelril.

the class MarketListCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
        return CommandResult.empty();
    }
    MarketService service = optService.get();
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    Optional<String> optFilter = args.getOne("name");
    String filter = optFilter.isPresent() ? optFilter.get() : "";
    if (!filter.matches(MarketService.VALID_ALIAS_REGEX)) {
        src.sendMessage(Text.of(TextColors.RED, "Invalid filter supplied."));
        return CommandResult.empty();
    }
    List<ItemDescriptor> prices = filter.isEmpty() ? service.getPrices() : service.getPrices(filter + "%");
    if (prices.isEmpty()) {
        src.sendMessage(Text.of(TextColors.YELLOW, "No items matched."));
        return CommandResult.success();
    }
    List<Text> result = prices.stream().filter(a -> filter.isEmpty() || a.getName().startsWith(filter)).sorted(Comparator.comparing(ItemDescriptor::getCurrentValue)).map(a -> createLine(a, service)).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Item List")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) MarketImplUtil.format(com.skelril.skree.content.market.MarketImplUtil.format) GenericArguments.remainingJoinedStrings(org.spongepowered.api.command.args.GenericArguments.remainingJoinedStrings) MarketService(com.skelril.skree.service.MarketService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) BigDecimal(java.math.BigDecimal) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Optional(java.util.Optional) Comparator(java.util.Comparator) TextColors(org.spongepowered.api.text.format.TextColors) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) MarketService(com.skelril.skree.service.MarketService)

Aggregations

MarketImplUtil.format (com.skelril.skree.content.market.MarketImplUtil.format)1 MarketService (com.skelril.skree.service.MarketService)1 ItemDescriptor (com.skelril.skree.service.internal.market.ItemDescriptor)1 BigDecimal (java.math.BigDecimal)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Sponge (org.spongepowered.api.Sponge)1 CommandException (org.spongepowered.api.command.CommandException)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandSource (org.spongepowered.api.command.CommandSource)1 CommandContext (org.spongepowered.api.command.args.CommandContext)1 GenericArguments.optional (org.spongepowered.api.command.args.GenericArguments.optional)1 GenericArguments.remainingJoinedStrings (org.spongepowered.api.command.args.GenericArguments.remainingJoinedStrings)1 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 PaginationService (org.spongepowered.api.service.pagination.PaginationService)1 Text (org.spongepowered.api.text.Text)1 TextActions (org.spongepowered.api.text.action.TextActions)1