SaneEconomy/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/EconomyManager.java

183 lines
5.8 KiB
Java
Raw Normal View History

2016-06-14 05:06:58 +02:00
package org.appledash.saneeconomy.economy;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
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-06-14 10:59:16 +02:00
private final Currency currency;
private final EconomyStorageBackend backend;
2016-06-14 05:06:58 +02:00
public EconomyManager(Currency currency, EconomyStorageBackend backend) {
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
*/
public String getFormattedBalance(Economable player) {
2016-06-14 05:06:58 +02:00
return currency.formatAmount(backend.getBalance(player));
}
/**
* 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
*/
public boolean accountExists(Economable player) {
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
*/
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
*/
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
*/
public double addBalance(Economable targetPlayer, double amount, TransactionReason reason) {
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!");
}
double newAmount = backend.getBalance(targetPlayer) + amount;
setBalance(targetPlayer, newAmount, reason);
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
*/
public double subtractBalance(Economable targetPlayer, double amount, TransactionReason reason) {
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!");
}
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 */
if (newAmount <= 0.0D) {
newAmount = 0.0D;
2016-06-14 05:06:58 +02:00
}
setBalance(targetPlayer, newAmount, reason);
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
*/
public void setBalance(Economable targetPlayer, double amount, TransactionReason reason) {
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!");
}
backend.setBalance(targetPlayer, amount);
}
/**
* 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
* @return True if success, false if from has insufficient funds.
2016-06-14 05:06:58 +02:00
* @throws IllegalArgumentException If amount is negative
*/
public boolean transfer(Economable from, Economable to, double amount) {
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!");
}
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? */
subtractBalance(from, amount, TransactionReason.PLAYER_PAY);
addBalance(to, amount, TransactionReason.PLAYER_PAY);
2016-06-14 05:06:58 +02:00
return true;
}
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
*/
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;
}
public EconomyStorageBackend getBackend() {
return backend;
}
2016-06-14 05:06:58 +02:00
}