Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added payPerRegen setting #747

Merged
merged 12 commits into from
Oct 21, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ 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().buy(player, schematicConfig)) {
return false;
}
}

IridiumSkyblock.getInstance().getIslandManager().getTeamMembers(island).stream()
.map(User::getPlayer)
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Messages() {
public String regeneratingIsland = "%prefix% &7Regenerating Island...";
public String cannotRegenIsland = "%prefix% &7You cannot regenerate your Island.";
public String unknownSchematic = "%prefix% &7No schematic with that name exists.";
public String paidForRegen = "%prefix% &7%player% successfully regenerated the island (%schematic%&7) for %vault_cost%.";
public String noSafeLocation = "%prefix% &7Could not find a safe location to teleport to.";
public String cannotHurtPlayers = "%prefix% &7You cannot hurt players on your Island.";
public String creatingIsland = "%prefix% &7Creating Island, please wait...";
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/com/iridium/iridiumskyblock/configs/Schematics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,54 @@

import com.google.common.collect.ImmutableMap;
import com.iridium.iridiumcore.Item;
import com.iridium.iridiumcore.dependencies.fasterxml.annotation.JsonIgnore;
import com.iridium.iridiumcore.dependencies.fasterxml.annotation.JsonIgnoreProperties;
import com.iridium.iridiumcore.dependencies.xseries.XMaterial;
import com.iridium.iridiumcore.dependencies.xseries.XSound;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.bukkit.block.Biome;

import java.util.Collections;
import java.util.Map;
import java.util.*;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Schematics {

public Map<String, SchematicConfig> schematics = ImmutableMap.<String, SchematicConfig>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
), new SchematicWorld(Biome.THE_END,
"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;
Expand All @@ -69,4 +74,16 @@ public SchematicWorld(Biome biome, String schematicID, Double islandHeight, Bool
}
}

@AllArgsConstructor
@NoArgsConstructor
public static class Cost {
public double money;
public Map<String, Double> bankItems;

@JsonIgnore
public boolean canPurchase() {
return money > 0 || bankItems.values().stream().anyMatch(value -> value > 0);
}
}

}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -115,4 +129,80 @@ private CompletableFuture<Void> 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<Placeholder> 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<Island> 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);
}
}
Loading