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,13 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams<Isl

Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic.get());

if(IridiumSkyblock.getInstance().getConfiguration().payPerRegen) {
boolean purchased = IridiumSkyblock.getInstance().getSchematicManager().buy(player, schematicConfig);
if(!purchased) {
sh0inx marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -53,6 +53,7 @@ public Configuration() {
public boolean removeIslandBlocksOnDelete = false;
public boolean clearInventoryOnRegen = false;
public boolean clearEnderChestOnRegen = false;
public boolean payPerRegen = false;
sh0inx marked this conversation as resolved.
Show resolved Hide resolved
public boolean allowPvPOnIslands = false;
public int distance = 151;
public int netherUnlockLevel = 10;
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
98 changes: 70 additions & 28 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,79 @@

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,
"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,
"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,
"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 Map<String, SchematicConfig> schematics;
public String buyPriceLore;
public String notPurchasableLore;
public boolean abbreviatePrices;
public XSound failSound;
public XSound successSound;
public List<String> schematicPurchaseLore;

public Schematics() {
this("&c");
}

public Schematics(String color) {
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.")),
new Schematics.Cost(100, 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.")),
new Schematics.Cost(100, 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.")),
new Schematics.Cost(100, 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();

buyPriceLore = "&aBuy Price: $%vault_cost%";
notPurchasableLore = "&cThis item cannot be purchased!";

abbreviatePrices = true;

failSound = XSound.BLOCK_ANVIL_LAND;
successSound = XSound.ENTITY_PLAYER_LEVELUP;

schematicPurchaseLore = Arrays.asList(
" ",
color + "&l[!] " + color + "Left-Click to Purchase %amount%, Shift for 64",
color + "&l[!] " + color + "Right Click to Sell %amount%, Shift for 64"
);
}

@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 +99,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,30 @@
package com.iridium.iridiumskyblock.managers;

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.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 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 +125,78 @@ 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);

IridiumSkyblock.getInstance().getSchematics().successSound.play(player);

List<Placeholder> bankPlaceholders = IridiumSkyblock.getInstance().getBankItemList().stream()
.map(BankItem::getName)
.map(name -> new Placeholder(name + "_cost", formatPrice(getBankBalance(player, name))))
.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("%vault_cost%", formatPrice(schematic.regenCost.money)),
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> island = IridiumSkyblock.getInstance().getTeamManager().getTeamViaID(user.getTeamID());
if (!island.isPresent()) return;
IridiumSkyblock.getInstance().getTeamManager().getTeamBank(island.get(), bankItem).setNumber(amount);
}

private boolean canPurchase(Player player, Schematics.SchematicConfig schematic) {
double moneyCost = schematic.regenCost.money;
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) >= schematic.regenCost.money;
}

private void purchase(Player player, Schematics.SchematicConfig schematic) {
IridiumSkyblock.getInstance().getEconomy().withdrawPlayer(player, schematic.regenCost.money);

for (String bankItem : schematic.regenCost.bankItems.keySet()) {
setBankBalance(player, bankItem, getBankBalance(player, bankItem) - round(schematic.regenCost.bankItems.get(bankItem), 2));
PeachesMLG marked this conversation as resolved.
Show resolved Hide resolved
}
}

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