diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java index c448ddb9b..777072bc7 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java @@ -48,7 +48,7 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams pasteSchematic(Island island, Schematics.Schemat return completableFuture; } - public boolean processTransaction(Player player, Schematics.SchematicConfig schematic) { - - User user = IridiumSkyblock.getInstance().getUserManager().getUser(player); - Optional island = IridiumSkyblock.getInstance().getTeamManager().getTeamViaID(user.getTeamID()); - - if(!island.isPresent()){ - //NO ISLAND + public boolean buy(Player player, Schematics.SchematicConfig schematic) { + if (!canPurchase(player, schematic)) { + player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotAfford + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); + IridiumSkyblock.getInstance().getSchematics().failSound.play(player); return false; } - List bankPlaceholders; - double moneyCost = schematic.regenCost.money; - Economy economy = IridiumSkyblock.getInstance().getEconomy(); + purchase(player, schematic); - if(moneyCost != 0 && economy != null && economy.getBalance(player) < moneyCost) { - //Cannot Afford - return false; - } + IridiumSkyblock.getInstance().getShop().successSound.play(player); - for (String schematicBankItem : schematic.regenCost.bankItems.keySet()) { + List bankPlaceholders = IridiumSkyblock.getInstance().getBankItemList().stream() + .map(BankItem::getName) + .map(name -> new Placeholder(name + "_cost", formatPrice(getBankBalance(player, name)))) + .collect(Collectors.toList()); - TeamBank bank = IridiumSkyblock.getInstance().getTeamManager().getTeamBank(island.get(), schematicBankItem); - double bankBalance = bank.getNumber(); + double moneyCost = round(schematic.regenCost.money, 2); - double cost = round(schematic.regenCost.bankItems.get(schematicBankItem), 2); + player.sendMessage(StringUtils.color(StringUtils.processMultiplePlaceholders(IridiumSkyblock.getInstance().getMessages().paidForRegen + .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) + .replace("%player%", player.getName()) + .replace("%schematic%", StringUtils.color(schematic.item.displayName)) + .replace ("%vault_cost%", formatPrice(moneyCost)), + bankPlaceholders) + )); + return true; + } - if (bank.getNumber() < cost) { - player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotAfford - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))); - IridiumSkyblock.getInstance().getSchematics().failSound.play(player); + private double getBankBalance(Player player, String bankItem) { + User user = IridiumSkyblock.getInstance().getUserManager().getUser(player); + return IridiumSkyblock.getInstance().getTeamManager().getTeamViaID(user.getTeamID()) + .map(team -> IridiumSkyblock.getInstance().getTeamManager().getTeamBank(team, bankItem)) + .map(TeamBank::getNumber) + .orElse(0.0); + } - return false; - } + private void setBankBalance(Player player, String bankItem, double amount) { + User user = IridiumSkyblock.getInstance().getUserManager().getUser(player); + Optional team = IridiumSkyblock.getInstance().getTeamManager().getTeamViaID(user.getTeamID()); + if (!team.isPresent()) return; + IridiumSkyblock.getInstance().getTeamManager().getTeamBank(team.get(), bankItem).setNumber(amount); + } - double finalBankBalance = bankBalance; - bankPlaceholders = IridiumSkyblock.getInstance().getBankItemList().stream() - .map(BankItem::getName) - .map(name -> new Placeholder(name + "_cost", formatPrice(finalBankBalance))) - .collect(Collectors.toList()); - - player.sendMessage(StringUtils.color(StringUtils.processMultiplePlaceholders(IridiumSkyblock.getInstance().getMessages().paidForRegen - .replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix) - .replace("%player%", player.getName()) - .replace("%schematic%", StringUtils.color(schematic.item.displayName)) - .replace("%bank_cost%", formatPrice(cost)) - .replace ("%vault_cost%", formatPrice(moneyCost)), - bankPlaceholders) - )); - - bank.setNumber(bankBalance - cost); + private boolean canPurchase(Player player, Schematics.SchematicConfig schematic) { + double moneyCost = round(schematic.regenCost.money, 2); + Economy economy = IridiumSkyblock.getInstance().getEconomy(); + for (String bankItem : schematic.regenCost.bankItems.keySet()) { + double cost = round(schematic.regenCost.bankItems.get(bankItem), 2); + if (getBankBalance(player, bankItem) < cost) return false; } - economy.withdrawPlayer(player, moneyCost); + return moneyCost == 0 || economy != null && economy.getBalance(player) >= moneyCost; + } - IridiumSkyblock.getInstance().getSchematics().successSound.play(player); + private void purchase(Player player, Schematics.SchematicConfig schematic) { + double moneyCost = round(schematic.regenCost.money, 2); + IridiumSkyblock.getInstance().getEconomy().withdrawPlayer(player, moneyCost); - return true; + for (String bankItem : schematic.regenCost.bankItems.keySet()) { + double cost = round(schematic.regenCost.bankItems.get(bankItem), 2); + setBankBalance(player, bankItem, getBankBalance(player, bankItem) - cost); + } } private double round(double value, int places) {