Search in sources :

Example 1 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketVerifyCommand 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();
    Task.builder().async().execute(() -> {
        PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
        List<Clause<String, BigDecimal>> profitMargins = new ArrayList<>();
        for (IRecipe recipe : CraftingManager.getInstance().getRecipeList()) {
            ItemStack output = recipe.getRecipeOutput();
            if (output == null) {
                continue;
            }
            Optional<BigDecimal> optResultPrice = service.getPrice(tf(output));
            if (!optResultPrice.isPresent()) {
                continue;
            }
            String name = service.getAlias(tf(output)).orElse(output.getItem().getRegistryName().toString());
            Collection<ItemStack> items = new ArrayList<>();
            if (recipe instanceof ShapedRecipes) {
                items.addAll(Lists.newArrayList(((ShapedRecipes) recipe).recipeItems));
            } else if (recipe instanceof ShapelessRecipes) {
                items.addAll(((ShapelessRecipes) recipe).recipeItems);
            } else {
                src.sendMessage(Text.of(TextColors.RED, "Unsupported recipe for " + name));
                continue;
            }
            items.removeAll(Collections.singleton(null));
            BigDecimal creationCost = BigDecimal.ZERO;
            try {
                for (ItemStack stack : items) {
                    creationCost = creationCost.add(service.getPrice(tf(stack)).orElse(BigDecimal.ZERO));
                }
            } catch (Exception ex) {
                src.sendMessage(Text.of(TextColors.RED, "Couldn't complete checks for " + name));
                continue;
            }
            if (creationCost.equals(BigDecimal.ZERO)) {
                src.sendMessage(Text.of(TextColors.RED, "No ingredients found on market for " + name));
                continue;
            }
            BigDecimal sellPrice = optResultPrice.get();
            sellPrice = sellPrice.multiply(service.getSellFactor(sellPrice));
            profitMargins.add(new Clause<>(name, sellPrice.subtract(creationCost)));
        }
        List<Text> result = profitMargins.stream().sorted((a, b) -> b.getValue().subtract(a.getValue()).intValue()).map(a -> {
            boolean profitable = a.getValue().compareTo(BigDecimal.ZERO) >= 0;
            return Text.of(profitable ? TextColors.RED : TextColors.GREEN, a.getKey().toUpperCase(), " has a profit margin of ", profitable ? "+" : "", MarketImplUtil.format(a.getValue()));
        }).collect(Collectors.toList());
        pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Profit Margin Report")).padding(Text.of(" ")).sendTo(src);
    }).submit(SkreePlugin.inst());
    src.sendMessage(Text.of(TextColors.YELLOW, "Verification in progress..."));
    return CommandResult.success();
}
Also used : java.util(java.util) ItemStack(net.minecraft.item.ItemStack) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) SkreePlugin(com.skelril.skree.SkreePlugin) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Task(org.spongepowered.api.scheduler.Task) ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) TextColors(org.spongepowered.api.text.format.TextColors) ForgeTransformer.tf(com.skelril.nitro.transformer.ForgeTransformer.tf) MarketImplUtil(com.skelril.skree.content.market.MarketImplUtil) CommandResult(org.spongepowered.api.command.CommandResult) IRecipe(net.minecraft.item.crafting.IRecipe) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) MarketService(com.skelril.skree.service.MarketService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) CraftingManager(net.minecraft.item.crafting.CraftingManager) Clause(com.skelril.nitro.Clause) ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) IRecipe(net.minecraft.item.crafting.IRecipe) BigDecimal(java.math.BigDecimal) CommandException(org.spongepowered.api.command.CommandException) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Clause(com.skelril.nitro.Clause) ItemStack(net.minecraft.item.ItemStack) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes) MarketService(com.skelril.skree.service.MarketService)

Example 2 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketSimulateCommand 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();
    src.sendMessage(Text.of(TextColors.YELLOW, "Starting market update..."));
    service.updatePrices();
    src.sendMessage(Text.of(TextColors.YELLOW, "Update completed!"));
    return CommandResult.success();
}
Also used : MarketService(com.skelril.skree.service.MarketService)

Example 3 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketTrackItemCommand 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();
    Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
    if (!held.isPresent()) {
        src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
        return CommandResult.empty();
    }
    ItemStack item = held.get();
    if (service.addItem(item)) {
        src.sendMessage(Text.of(TextColors.YELLOW, "Your held item is now being tracked."));
    } else {
        src.sendMessage(Text.of(TextColors.RED, "Your held item is already tracked."));
        return CommandResult.empty();
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MarketService(com.skelril.skree.service.MarketService)

Example 4 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketBuyCommand 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.RED, "You cannot buy or sell from this area."));
        return CommandResult.empty();
    }
    String itemName = args.<String>getOne("item").get();
    List<String> targetItems;
    if (itemName.endsWith("#armor")) {
        String armorType = itemName.replace("#armor", " ");
        targetItems = Lists.newArrayList(armorType + "helmet", armorType + "chestplate", armorType + "leggings", armorType + "boots");
    } else {
        targetItems = Lists.newArrayList(itemName);
    }
    BigDecimal price = BigDecimal.ZERO;
    for (String anItem : targetItems) {
        Optional<BigDecimal> optPrice = service.getPrice(anItem);
        if (!optPrice.isPresent()) {
            src.sendMessage(Text.of(TextColors.RED, "That item is not available for purchase."));
            return CommandResult.empty();
        }
        price = price.add(optPrice.get());
    }
    Optional<Integer> optAmt = args.getOne("amount");
    int amt = Math.max(1, optAmt.isPresent() ? optAmt.get() : 0);
    price = price.multiply(BigDecimal.valueOf(amt));
    BigDecimal funds = MarketImplUtil.getMoney(player);
    BigDecimal newBalance = funds.subtract(price);
    if (newBalance.compareTo(BigDecimal.ZERO) < 0) {
        BigDecimal neededAmt = newBalance.multiply(BigDecimal.valueOf(-1));
        src.sendMessage(Text.of(TextColors.RED, "You do not have enough money to purchase that item(s)."));
        src.sendMessage(Text.of(TextColors.RED, "You need an additional ", TextColors.WHITE, format(neededAmt), TextColors.RED, "."));
        return CommandResult.empty();
    }
    List<Clause<String, Integer>> newStocks = new ArrayList<>();
    for (String anItem : targetItems) {
        Optional<Integer> optStock = service.getStock(anItem);
        if (optStock.orElse(0) < amt) {
            src.sendMessage(Text.of(TextColors.RED, "There is not enough stock to satisfy your order."));
            return CommandResult.empty();
        }
        newStocks.add(new Clause<>(anItem, optStock.get() - amt));
    }
    // Accumulate items
    List<ItemStack> itemStacks = new ArrayList<>(targetItems.size());
    for (String anItem : targetItems) {
        Optional<ItemStack> stack = service.getItem(anItem);
        if (!stack.isPresent()) {
            // TODO Auto reporting
            src.sendMessage(Text.of(TextColors.RED, "An item stack could not be resolved, please report this!"));
            return CommandResult.empty();
        }
        int total = amt;
        while (total > 0) {
            int increment = Math.min(total, stack.get().getMaxStackQuantity());
            total -= increment;
            itemStacks.add(newItemStack(stack.get(), increment));
        }
    }
    // Alright, all items have been found
    if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.RED, "Failed to adjust your balance, please report this!"));
        return CommandResult.empty();
    }
    Clause<Boolean, List<Clause<ItemStack, Integer>>> transactions = MarketImplUtil.giveItems(player, itemStacks, Cause.source(SkreePlugin.container()).build());
    if (!transactions.getKey()) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.RED, "Failed to give all items, please report this!"));
        return CommandResult.empty();
    }
    // Items have been given process stocks
    for (Clause<String, Integer> stock : newStocks) {
        service.setStock(stock.getKey(), stock.getValue());
    }
    if (!service.logTransactionByStack(player.getUniqueId(), transactions.getValue())) {
        // TODO Auto reporting
        // Not critical, continue
        src.sendMessage(Text.of(TextColors.RED, "Failed to log transactions, please report this!"));
    }
    player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) purchased for ", TextColors.WHITE, format(price), TextColors.YELLOW, "!"));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ArrayList(java.util.ArrayList) List(java.util.List) Clause(com.skelril.nitro.Clause) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) MarketService(com.skelril.skree.service.MarketService)

Example 5 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketAddAliasCommand 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();
    Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
    if (!held.isPresent()) {
        src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
        return CommandResult.empty();
    }
    ItemStack item = held.get();
    String alias = args.<String>getOne("alias").get();
    if (service.addAlias(alias, item)) {
        src.sendMessage(Text.of(TextColors.YELLOW, alias + " added to the market."));
    } else {
        src.sendMessage(Text.of(TextColors.RED, "Your held item is not currently tracked, or the alias is already in use."));
        return CommandResult.empty();
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MarketService(com.skelril.skree.service.MarketService)

Aggregations

MarketService (com.skelril.skree.service.MarketService)14 ItemStack (org.spongepowered.api.item.inventory.ItemStack)9 BigDecimal (java.math.BigDecimal)8 Player (org.spongepowered.api.entity.living.player.Player)8 Clause (com.skelril.nitro.Clause)3 List (java.util.List)3 Text (org.spongepowered.api.text.Text)3 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)2 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Sponge (org.spongepowered.api.Sponge)2 CommandException (org.spongepowered.api.command.CommandException)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)2 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)2 PaginationService (org.spongepowered.api.service.pagination.PaginationService)2 TextColors (org.spongepowered.api.text.format.TextColors)2 Lists (com.google.common.collect.Lists)1