2016-06-14 05:06:58 +02:00
|
|
|
package org.appledash.saneeconomy.economy;
|
|
|
|
|
2016-09-18 10:37:44 +02:00
|
|
|
import org.appledash.saneeconomy.ISaneEconomy;
|
2016-06-14 05:06:58 +02:00
|
|
|
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
2016-07-20 03:20:54 +02:00
|
|
|
import org.appledash.saneeconomy.economy.economable.Economable;
|
2016-06-15 01:47:14 +02:00
|
|
|
import org.appledash.saneeconomy.utils.NumberUtils;
|
2016-07-12 02:22:07 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2016-06-15 01:19:00 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2016-06-14 05:06:58 +02:00
|
|
|
|
2016-07-12 02:22:07 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
/**
|
|
|
|
* Created by AppleDash on 6/13/2016.
|
|
|
|
* Blackjack is still best pony.
|
|
|
|
*
|
|
|
|
* Represents our EconomyManager, which manages players' balances.
|
|
|
|
*/
|
|
|
|
public class EconomyManager {
|
2016-09-18 11:11:22 +02:00
|
|
|
private final ISaneEconomy saneEconomy;
|
2016-06-14 10:59:16 +02:00
|
|
|
private final Currency currency;
|
|
|
|
private final EconomyStorageBackend backend;
|
2016-06-14 05:06:58 +02:00
|
|
|
|
2016-09-18 10:37:44 +02:00
|
|
|
public EconomyManager(ISaneEconomy saneEconomy, Currency currency, EconomyStorageBackend backend) {
|
|
|
|
this.saneEconomy = saneEconomy;
|
2016-06-14 05:06:58 +02:00
|
|
|
this.currency = currency;
|
|
|
|
this.backend = backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Currency we're using/
|
|
|
|
* @return Currency
|
|
|
|
*/
|
|
|
|
public Currency getCurrency() {
|
|
|
|
return currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the balance of a player, formatted according to our Currency's format.
|
|
|
|
* @param player Player
|
|
|
|
* @return Formatted balance
|
|
|
|
*/
|
2016-07-20 03:20:54 +02:00
|
|
|
public String getFormattedBalance(Economable player) {
|
2016-06-14 05:06:58 +02:00
|
|
|
return currency.formatAmount(backend.getBalance(player));
|
|
|
|
}
|
|
|
|
|
2016-06-14 05:29:26 +02:00
|
|
|
/**
|
|
|
|
* Check whether a player has used the economy system before.
|
|
|
|
* @param player Player to check
|
|
|
|
* @return True if they have used the economy system before, false otherwise
|
|
|
|
*/
|
2016-07-20 03:20:54 +02:00
|
|
|
public boolean accountExists(Economable player) {
|
2016-06-14 05:29:26 +02:00
|
|
|
return backend.accountExists(player);
|
|
|
|
}
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
/**
|
|
|
|
* Get a player's balance.
|
|
|
|
* @param targetPlayer Player to get balance of
|
|
|
|
* @return Player's balance
|
|
|
|
*/
|
2016-07-20 03:20:54 +02:00
|
|
|
public double getBalance(Economable targetPlayer) {
|
2016-06-14 05:06:58 +02:00
|
|
|
return backend.getBalance(targetPlayer);
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:19:00 +02:00
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
/**
|
|
|
|
* Check if a player has a certain amount of money.
|
|
|
|
* @param targetPlayer Player to check balance of
|
|
|
|
* @param requiredBalance How much money we're checking for
|
|
|
|
* @return True if they have requiredBalance or more, false otherwise
|
|
|
|
*/
|
2016-07-20 03:20:54 +02:00
|
|
|
public boolean hasBalance(Economable targetPlayer, double requiredBalance) {
|
2016-06-14 05:06:58 +02:00
|
|
|
return getBalance(targetPlayer) >= requiredBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to a player's balance.
|
|
|
|
* @param targetPlayer Player to add to
|
|
|
|
* @param amount Amount to add
|
|
|
|
* @return Player's new balance
|
|
|
|
* @throws IllegalArgumentException If amount is negative
|
|
|
|
*/
|
2016-08-15 08:25:33 +02:00
|
|
|
public double addBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
2016-07-29 13:16:11 +02:00
|
|
|
amount = NumberUtils.filterAmount(currency, amount);
|
2016-06-15 01:47:14 +02:00
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
if (amount < 0) {
|
|
|
|
throw new IllegalArgumentException("Cannot add a negative amount!");
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:22:12 +02:00
|
|
|
double newAmount = backend.getBalance(targetPlayer) + amount;
|
|
|
|
|
2016-08-15 08:25:33 +02:00
|
|
|
setBalance(targetPlayer, newAmount, reason);
|
2016-07-12 18:22:12 +02:00
|
|
|
|
|
|
|
return newAmount;
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract from a player's balance.
|
|
|
|
* If the subtraction would result in a negative balance, the balance is instead set to 0.
|
|
|
|
* @param targetPlayer Player to subtract from
|
|
|
|
* @param amount Amount to subtract
|
|
|
|
* @return Player's new balance
|
|
|
|
* @throws IllegalArgumentException If amount is negative
|
|
|
|
*/
|
2016-08-15 08:25:33 +02:00
|
|
|
public double subtractBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
2016-07-29 13:16:11 +02:00
|
|
|
amount = NumberUtils.filterAmount(currency, amount);
|
2016-06-15 01:47:14 +02:00
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
if (amount < 0) {
|
|
|
|
throw new IllegalArgumentException("Cannot subtract a negative amount!");
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:22:12 +02:00
|
|
|
double newAmount = backend.getBalance(targetPlayer) - amount;
|
|
|
|
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
/* Subtracting that much would result in a negative balance - don't do that */
|
2016-07-12 18:22:12 +02:00
|
|
|
if (newAmount <= 0.0D) {
|
|
|
|
newAmount = 0.0D;
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 08:25:33 +02:00
|
|
|
setBalance(targetPlayer, newAmount, reason);
|
2016-07-12 18:22:12 +02:00
|
|
|
|
|
|
|
return newAmount;
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a player's balance.
|
|
|
|
* @param targetPlayer Player to set balance of
|
|
|
|
* @param amount Amount to set balance to
|
|
|
|
* @throws IllegalArgumentException If amount is negative
|
|
|
|
*/
|
2016-08-15 08:25:33 +02:00
|
|
|
public void setBalance(Economable targetPlayer, double amount, TransactionReason reason) {
|
2016-07-29 13:16:11 +02:00
|
|
|
amount = NumberUtils.filterAmount(currency, amount);
|
2016-06-15 01:47:14 +02:00
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
if (amount < 0) {
|
|
|
|
throw new IllegalArgumentException("Cannot set balance to a negative value!");
|
|
|
|
}
|
|
|
|
|
2016-08-15 08:34:55 +02:00
|
|
|
double oldAmount = backend.getBalance(targetPlayer);
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
backend.setBalance(targetPlayer, amount);
|
2016-08-15 08:34:55 +02:00
|
|
|
|
2016-09-18 10:37:44 +02:00
|
|
|
if (saneEconomy.shouldLogTransactions() && reason != TransactionReason.PLAYER_PAY) { // Player pay is handled in the transfer() method.
|
2016-08-15 08:34:55 +02:00
|
|
|
if (oldAmount > amount) { // Lower amount now
|
2016-09-21 08:23:38 +02:00
|
|
|
saneEconomy.getTransactionLogger().logSubtraction(targetPlayer, oldAmount - amount, reason);
|
2016-08-15 08:34:55 +02:00
|
|
|
} else if (oldAmount < amount) { // Higher amount now
|
2016-09-21 08:23:38 +02:00
|
|
|
saneEconomy.getTransactionLogger().logAddition(targetPlayer, amount - oldAmount, reason);
|
2016-08-15 08:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-15 08:25:33 +02:00
|
|
|
* Transfer money from one Economable to another.
|
|
|
|
* @param from Economable to transfer from
|
|
|
|
* @param to Economable to transfer to
|
2016-06-14 05:06:58 +02:00
|
|
|
* @param amount Amount to transfer
|
2016-08-15 08:25:33 +02:00
|
|
|
* @return True if success, false if from has insufficient funds.
|
2016-06-14 05:06:58 +02:00
|
|
|
* @throws IllegalArgumentException If amount is negative
|
|
|
|
*/
|
2016-08-15 08:25:33 +02:00
|
|
|
public boolean transfer(Economable from, Economable to, double amount) {
|
2016-07-29 13:16:11 +02:00
|
|
|
amount = NumberUtils.filterAmount(currency, amount);
|
2016-06-15 01:47:14 +02:00
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
if (amount < 0) {
|
|
|
|
throw new IllegalArgumentException("Cannot transfer a negative amount!");
|
|
|
|
}
|
|
|
|
|
2016-08-15 08:25:33 +02:00
|
|
|
if (!hasBalance(from, amount)) {
|
2016-06-14 05:06:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the actual transfer. TODO: Maybe return their new balances in some way? */
|
2016-08-15 08:25:33 +02:00
|
|
|
subtractBalance(from, amount, TransactionReason.PLAYER_PAY);
|
|
|
|
addBalance(to, amount, TransactionReason.PLAYER_PAY);
|
2016-06-14 05:06:58 +02:00
|
|
|
|
2016-09-18 10:37:44 +02:00
|
|
|
if (saneEconomy.shouldLogTransactions()) {
|
2016-09-21 04:47:25 +02:00
|
|
|
saneEconomy.getTransactionLogger().logTransfer(from, to, amount);
|
2016-08-15 08:34:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-14 14:31:52 +02:00
|
|
|
|
2016-07-12 02:22:07 +02:00
|
|
|
/**
|
|
|
|
* Get the players who have the most money.
|
|
|
|
* @param amount Maximum number of players to show.
|
|
|
|
* @return Map of OfflinePlayer to Double
|
|
|
|
*/
|
2016-07-20 03:20:54 +02:00
|
|
|
public Map<OfflinePlayer, Double> getTopPlayerBalances(int amount) {
|
|
|
|
Map<UUID, Double> uuidBalances = backend.getTopPlayerBalances(amount);
|
2016-07-12 02:22:07 +02:00
|
|
|
Map<OfflinePlayer, Double> playerBalances = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
uuidBalances.forEach((uuid, balance) -> playerBalances.put(Bukkit.getServer().getOfflinePlayer(uuid), balance));
|
|
|
|
|
|
|
|
return playerBalances;
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:52 +02:00
|
|
|
public EconomyStorageBackend getBackend() {
|
|
|
|
return backend;
|
|
|
|
}
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|