Skip to content

Commit

Permalink
Updated SchematicManager.java
Browse files Browse the repository at this point in the history
copied the shopmanager and created this, parity is important and tbh i prefer this anyway
  • Loading branch information
sh0inx committed Oct 11, 2023
1 parent 69aa68e commit 3d65970
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams<Isl
Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic.get());

if(schematicConfig.regenCost.money != 0 && !schematicConfig.regenCost.bankItems.isEmpty()) {
if(!IridiumSkyblock.getInstance().getSchematicManager().processTransaction(player, schematicConfig)) {
if(!IridiumSkyblock.getInstance().getSchematicManager().buy(player, schematicConfig)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iridium.iridiumskyblock.managers;

import com.iridium.iridiumcore.utils.InventoryUtils;
import com.iridium.iridiumcore.utils.Placeholder;
import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
Expand All @@ -11,14 +12,17 @@
import com.iridium.iridiumskyblock.schematics.SchematicPaster;
import com.iridium.iridiumskyblock.schematics.WorldEdit;
import com.iridium.iridiumteams.bank.BankItem;
import com.iridium.iridiumteams.configs.Shop;
import com.iridium.iridiumteams.database.TeamBank;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import javax.xml.crypto.URIReference;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -125,63 +129,69 @@ private CompletableFuture<Void> pasteSchematic(Island island, Schematics.Schemat
return completableFuture;
}

public boolean processTransaction(Player player, Schematics.SchematicConfig schematic) {

User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> 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<Placeholder> 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<Placeholder> 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<Island> 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) {
Expand Down

0 comments on commit 3d65970

Please sign in to comment.