FabledSkyBlock/src/main/java/com/craftaro/skyblock/bank/BankManager.java

198 lines
7.1 KiB
Java
Raw Normal View History

package com.craftaro.skyblock.bank;
2020-05-27 15:52:13 +02:00
import com.craftaro.core.hooks.EconomyManager;
import com.craftaro.core.hooks.economies.Economy;
import com.craftaro.skyblock.SkyBlock;
import com.craftaro.skyblock.config.FileManager;
import com.craftaro.skyblock.island.Island;
import com.craftaro.skyblock.playerdata.PlayerData;
2020-05-27 15:52:13 +02:00
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
2020-05-27 15:52:13 +02:00
public class BankManager {
2020-06-20 21:38:11 +02:00
private final HashMap<UUID, List<Transaction>> log;
2020-07-16 11:07:08 +02:00
private final SkyBlock plugin;
2020-05-27 15:52:13 +02:00
public FileConfiguration lang;
2020-07-16 11:07:08 +02:00
public BankManager(SkyBlock plugin) {
this.plugin = plugin;
this.lang = this.plugin.getLanguage();
this.log = new HashMap<>();
2020-05-27 15:52:13 +02:00
loadTransactions();
}
/*public List<String> getTransactions(Player player) {
2020-05-27 15:52:13 +02:00
if (log.containsKey(player.getUniqueId())&&log.get(player.getUniqueId())!=null&&!log.get(player.getUniqueId()).isEmpty()) {
List<String> lore = new ArrayList<>();
List<Transaction> transactions = log.get(player.getUniqueId());
2020-06-02 16:31:44 +02:00
int size = transactions.size()>10 ? 10 : transactions.size();
for (int i = 0;i<size;i++) {
Transaction t = transactions.get((transactions.size()-1)-i);
2020-06-17 17:55:05 +02:00
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy HH:mm");
lore.add("#" + (i+1) + " " + formatDate.format(t.timestamp) +" " + t.player.getPlayer().getDisplayName() + " " + t.action.name().toLowerCase() + " " + EconomyManager.formatEconomy(t.ammount));
2020-05-27 15:52:13 +02:00
}
return lore;
}else {
List<String> lore = new ArrayList<>();
lore.add(lang.getString("Menu.Bank.Item.Log.Empty"));
return lore;
}
}*/
public List<Transaction> getTransactions(Player player) {
return getTransactions(player.getUniqueId());
}
public List<Transaction> getTransactions(UUID uuid) {
if (this.log.containsKey(uuid)
&& this.log.get(uuid) != null
&& !this.log.get(uuid).isEmpty()) {
return new ArrayList<>(this.log.get(uuid));
} else {
return new ArrayList<>();
}
2020-05-27 15:52:13 +02:00
}
public void addTransaction(Player p, Transaction transaction) {
if (this.log.containsKey(p.getUniqueId())) {
this.log.get(p.getUniqueId()).add(transaction);
} else {
2020-05-27 15:52:13 +02:00
List<Transaction> t = new ArrayList<>();
t.add(transaction);
this.log.put(p.getUniqueId(), t);
2020-05-27 15:52:13 +02:00
}
}
private void loadTransactions() {
Map<UUID, PlayerData> playerDataStorage = SkyBlock.getInstance().getPlayerDataManager().getPlayerData();
synchronized (playerDataStorage) {
for (UUID uid : playerDataStorage.keySet()) {
this.log.put(uid, playerDataStorage.get(uid).getTransactions());
}
2020-05-27 15:52:13 +02:00
}
}
public List<String> getBalanceLore(Player player) {
Economy economy = this.plugin.getEconomyManager().getEconomy();
2020-05-27 15:52:13 +02:00
List<String> result = new ArrayList<>();
2020-06-02 16:31:44 +02:00
result.add("Some error occurred while loading your balance!");
Island island = SkyBlock.getPlugin(SkyBlock.class).getIslandManager().getIsland(player);
result.add("If this is null then its a easy to fix bug: " + island.toString());
2020-05-27 15:52:13 +02:00
if (island != null) {
double accountBalance = 0;
if (economy != null) {
accountBalance = economy.getBalance(player);
}
2020-05-27 15:52:13 +02:00
result.clear();
result.add(player.getDisplayName() + "'s balance is " + EconomyManager.formatEconomy(accountBalance));
result.add(player.getDisplayName() + "'s island has " + EconomyManager.formatEconomy(island.getBankBalance()));
2020-05-27 15:52:13 +02:00
}
return result;
}
public List<Transaction> getTransactionList(Player player) {
return getTransactionList(player.getUniqueId());
}
public List<Transaction> getTransactionList(UUID uuid) {
return this.log.get(uuid);
2020-05-27 15:52:13 +02:00
}
2020-06-20 21:38:11 +02:00
public BankResponse deposit(Player player, Island island, double amt, boolean admin) {
Economy economy = this.plugin.getEconomyManager().getEconomy();
FileManager fileManager = this.plugin.getFileManager();
2020-06-20 21:38:11 +02:00
// Make sure the amount is positive
if (amt <= 0) {
return BankResponse.NEGATIVE_AMOUNT;
}
// If decimals aren't allowed, check for them
2020-09-01 21:05:37 +02:00
if (!this.plugin.getConfiguration().getBoolean("Island.Bank.AllowDecimals")) {
2020-06-20 21:38:11 +02:00
int intAmt = (int) amt;
if (intAmt != amt) {
return BankResponse.DECIMALS_NOT_ALLOWED;
}
}
if (!admin) {
if (economy == null || !economy.hasBalance(player, amt)) {
if (economy == null) {
this.plugin.getLogger().warning("No compatible economy plugin found Please check your configuration");
}
2020-06-20 21:38:11 +02:00
return BankResponse.NOT_ENOUGH_MONEY;
}
2020-07-16 11:07:08 +02:00
economy.withdrawBalance(player, amt);
2020-06-20 21:38:11 +02:00
}
island.addToBank(amt);
Transaction t = new Transaction();
t.player = player;
t.amount = (float) amt;
t.timestamp = Calendar.getInstance().getTime();
t.action = Transaction.Type.DEPOSIT;
t.visibility = admin ? Transaction.Visibility.ADMIN : Transaction.Visibility.USER;
this.addTransaction(player, t);
return BankResponse.SUCCESS;
}
public BankResponse withdraw(Player player, Island island, double amt, boolean admin) {
Economy economy = this.plugin.getEconomyManager().getEconomy();
2020-06-20 21:38:11 +02:00
// Make sure the amount is positive
if (amt <= 0) {
return BankResponse.NEGATIVE_AMOUNT;
}
// If decimals aren't allowed, check for them
2020-09-01 21:05:37 +02:00
if (!this.plugin.getConfiguration().getBoolean("Island.Bank.AllowDecimals")) {
2020-06-20 21:38:11 +02:00
int intAmt = (int) amt;
if (intAmt != amt) {
return BankResponse.DECIMALS_NOT_ALLOWED;
}
}
if (!admin) {
if (economy == null || amt > island.getBankBalance()) {
if (economy == null) {
this.plugin.getLogger().warning("No compatible economy plugin found Please check your configuration");
}
2020-06-20 21:38:11 +02:00
return BankResponse.NOT_ENOUGH_MONEY;
}
2020-07-16 11:07:08 +02:00
economy.deposit(player, amt);
2020-06-20 21:38:11 +02:00
}
island.removeFromBank(amt);
Transaction t = new Transaction();
t.player = player;
t.amount = (float) amt;
t.timestamp = Calendar.getInstance().getTime();
t.action = Transaction.Type.WITHDRAW;
t.visibility = admin ? Transaction.Visibility.ADMIN : Transaction.Visibility.USER;
this.addTransaction(player, t);
return BankResponse.SUCCESS;
}
public enum BankResponse {
2020-06-20 21:38:11 +02:00
NOT_ENOUGH_MONEY,
DECIMALS_NOT_ALLOWED,
NEGATIVE_AMOUNT,
SUCCESS
}
2020-05-27 15:52:13 +02:00
}