diff --git a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java index efdd82db8..f54c148b4 100644 --- a/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java +++ b/src/main/java/com/iridium/iridiumskyblock/commands/RegenCommand.java @@ -47,6 +47,12 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams schematics = ImmutableMap.builder() - .put("desert", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 11, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGY0OTNkZDgwNjUzM2Q5ZDIwZTg0OTUzOTU0MzY1ZjRkMzY5NzA5Y2ViYzlkZGVmMDIyZDFmZDQwZDg2YTY4ZiJ9fX0=", 1, "&9&lDesert Island", Collections.singletonList("&7A starter desert island.")), - -0.5, 89, -0.5, 90, new SchematicWorld(Biome.DESERT, + .put("desert", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 11, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGY0OTNkZDgwNjUzM2Q5ZDIwZTg0OTUzOTU0MzY1ZjRkMzY5NzA5Y2ViYzlkZGVmMDIyZDFmZDQwZDg2YTY4ZiJ9fX0=", 1, "&9&lDesert Island", Arrays.asList("&7A starter desert island.", "", "&9&l[!] &7Costs $1000")), + new Schematics.Cost(1000, new HashMap<>()), -0.5, 89, -0.5, 90, new SchematicWorld(Biome.DESERT, "desert.schem", 90.0, true ), new SchematicWorld(Biome.NETHER_WASTES, "desert_nether.schem", 90.0, true ), new SchematicWorld(Biome.THE_END, "desert_end.schem", 90.0, true ))) - .put("jungle", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 13, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjgzYWRmNDU2MGRlNDc0MTQwNDA5M2FjNjFjMzNmYjU1NmIzZDllZTUxNDBmNjIwMzYyNTg5ZmRkZWRlZmEyZCJ9fX0=", 1, "&9&lJungle Island", Collections.singletonList("&7A starter jungle island.")), - 1.5, 83, 1.5, 90, new SchematicWorld(Biome.JUNGLE, + .put("jungle", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 13, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjgzYWRmNDU2MGRlNDc0MTQwNDA5M2FjNjFjMzNmYjU1NmIzZDllZTUxNDBmNjIwMzYyNTg5ZmRkZWRlZmEyZCJ9fX0=", 1, "&9&lJungle Island", Arrays.asList("&7A starter jungle island.", "", "&9&l[!] &7Costs $1000")), + new Schematics.Cost(1000, new HashMap<>()), 1.5, 83, 1.5, 90, new SchematicWorld(Biome.JUNGLE, "jungle.schem", 90.0, true ), new SchematicWorld(Biome.NETHER_WASTES, "jungle_nether.schem", 90.0, true ), new SchematicWorld(Biome.THE_END, "jungle_end.schem", 90.0, true ))) - .put("mushroom", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 15, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZWE0NWQxYjQxN2NiZGRjMjE3NjdiMDYwNDRlODk5YjI2NmJmNzhhNjZlMjE4NzZiZTNjMDUxNWFiNTVkNzEifX19", 1, "&9&lMushroom Island", Collections.singletonList("&7A starter mushroom island.")), - 0.5, 89, -0.5, 90, new SchematicWorld(Biome.MUSHROOM_FIELDS, + .put("mushroom", new SchematicConfig(new Item(XMaterial.PLAYER_HEAD, 15, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZWE0NWQxYjQxN2NiZGRjMjE3NjdiMDYwNDRlODk5YjI2NmJmNzhhNjZlMjE4NzZiZTNjMDUxNWFiNTVkNzEifX19", 1, "&9&lMushroom Island", Arrays.asList("&7A starter mushroom island.", "", "&9&l[!] &7Costs $1000")), + new Schematics.Cost(1000, new HashMap<>()), 0.5, 89, -0.5, 90, new SchematicWorld(Biome.MUSHROOM_FIELDS, "mushroom.schem", 90.0, true ), new SchematicWorld(Biome.NETHER_WASTES, "mushroom_nether.schem", 90.0, true @@ -40,11 +41,15 @@ public class Schematics { "mushroom_end.schem", 90.0, true ))) .build(); + public boolean abbreviatePrices = true; + public XSound failSound = XSound.BLOCK_ANVIL_LAND; + public XSound successSound = XSound.ENTITY_PLAYER_LEVELUP; @NoArgsConstructor @AllArgsConstructor public static class SchematicConfig { public Item item; + public Cost regenCost; public double xHome; public double yHome; public double zHome; @@ -69,4 +74,16 @@ public SchematicWorld(Biome biome, String schematicID, Double islandHeight, Bool } } + @AllArgsConstructor + @NoArgsConstructor + public static class Cost { + public double money; + public Map bankItems; + + @JsonIgnore + public boolean canPurchase() { + return money > 0 || bankItems.values().stream().anyMatch(value -> value > 0); + } + } + } diff --git a/src/main/java/com/iridium/iridiumskyblock/managers/SchematicManager.java b/src/main/java/com/iridium/iridiumskyblock/managers/SchematicManager.java index 71476c898..ffe594ce0 100644 --- a/src/main/java/com/iridium/iridiumskyblock/managers/SchematicManager.java +++ b/src/main/java/com/iridium/iridiumskyblock/managers/SchematicManager.java @@ -1,20 +1,34 @@ 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; import com.iridium.iridiumskyblock.configs.Schematics; import com.iridium.iridiumskyblock.database.Island; +import com.iridium.iridiumskyblock.database.User; import com.iridium.iridiumskyblock.schematics.FastAsyncWorldEdit; import com.iridium.iridiumskyblock.schematics.SchematicAsync; 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; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; public class SchematicManager { @@ -115,4 +129,80 @@ private CompletableFuture pasteSchematic(Island island, Schematics.Schemat return completableFuture; } + 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; + } + + purchase(player, schematic); + + List bankPlaceholders = IridiumSkyblock.getInstance().getBankItemList().stream() + .map(BankItem::getName) + .map(name -> new Placeholder(name + "_cost", formatPrice(getBankBalance(player, name)))) + .collect(Collectors.toList()); + + double moneyCost = round(schematic.regenCost.money, 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; + } + + 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); + } + + 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); + } + + 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; + } + + return moneyCost == 0 || economy != null && economy.getBalance(player) >= moneyCost; + } + + private void purchase(Player player, Schematics.SchematicConfig schematic) { + double moneyCost = round(schematic.regenCost.money, 2); + IridiumSkyblock.getInstance().getEconomy().withdrawPlayer(player, moneyCost); + + for (String bankItem : schematic.regenCost.bankItems.keySet()) { + double cost = round(schematic.regenCost.bankItems.get(bankItem), 2); + setBankBalance(player, bankItem, getBankBalance(player, bankItem) - cost); + } + IridiumSkyblock.getInstance().getSchematics().successSound.play(player); + } + + private double round(double value, int places) { + BigDecimal bigDecimal = BigDecimal.valueOf(value); + bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP); + return bigDecimal.doubleValue(); + } + + public String formatPrice(double value) { + if (IridiumSkyblock.getInstance().getSchematics().abbreviatePrices) { + return IridiumSkyblock.getInstance().getConfiguration().numberFormatter.format(value); + } + return String.valueOf(value); + } }