Search in sources :

Example 1 with QueryMode

use of com.skelril.skree.content.market.MarketImplUtil.QueryMode in project Skree by Skelril.

the class MarketSellCommand 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!"));
        return CommandResult.empty();
    }
    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();
    Player player = (Player) src;
    if (!canBuyOrSell(player)) {
        player.sendMessage(Text.of(TextColors.DARK_RED, "You cannot buy or sell from this area."));
        return CommandResult.empty();
    }
    Optional<ItemStack> filter = Optional.empty();
    QueryMode mode = QueryMode.HELD;
    if (args.hasAny("a")) {
        mode = QueryMode.EVERYTHING;
    } else if (args.hasAny("h")) {
        mode = QueryMode.HOT_BAR;
    }
    if (mode != QueryMode.HELD && !args.hasAny("u")) {
        filter = player.getItemInHand(HandTypes.MAIN_HAND);
        if (!filter.isPresent()) {
            src.sendMessage(Text.of(TextColors.RED, "You're not holding an item to filter with!"));
            return CommandResult.empty();
        }
    }
    Clause<BigDecimal, List<Integer>> changes = MarketImplUtil.getChanges(player, service, mode, filter.orElse(null));
    if (changes.getValue().isEmpty()) {
        src.sendMessage(Text.of(TextColors.RED, "No sellable items found" + (filter.isPresent() ? " that matched the filter" : "") + "!"));
        return CommandResult.empty();
    }
    List<Clause<ItemStack, Integer>> transactions = MarketImplUtil.removeAtPos(player, changes.getValue());
    if (!service.logTransactionByStack(player.getUniqueId(), transactions)) {
        // TODO Auto reporting
        // Not critical, continue
        src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to log transactions, please report this!"));
    }
    BigDecimal newBalance = changes.getKey().add(MarketImplUtil.getMoney(player));
    if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to adjust your balance, please report this!"));
        return CommandResult.empty();
    }
    // Process new stocks
    for (Clause<ItemStack, Integer> transaction : transactions) {
        service.setStock(transaction.getKey(), service.getStock(transaction.getKey()).orElse(0) + -(transaction.getValue()));
    }
    player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) sold for: ", TextColors.WHITE, MarketImplUtil.format(changes.getKey()), TextColors.YELLOW, "!"));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) BigDecimal(java.math.BigDecimal) QueryMode(com.skelril.skree.content.market.MarketImplUtil.QueryMode) List(java.util.List) Clause(com.skelril.nitro.Clause) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MarketService(com.skelril.skree.service.MarketService)

Aggregations

Clause (com.skelril.nitro.Clause)1 QueryMode (com.skelril.skree.content.market.MarketImplUtil.QueryMode)1 MarketService (com.skelril.skree.service.MarketService)1 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 Player (org.spongepowered.api.entity.living.player.Player)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1