2012-01-25 16:32:34 +01:00
|
|
|
package com.Acrobot.ChestShop.Economy;
|
|
|
|
|
2012-09-02 22:41:24 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.NumberUtil;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2012-09-02 22:41:24 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
2012-09-15 20:32:22 +02:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2012-01-25 16:32:34 +01:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp;
|
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
* Economy management
|
|
|
|
*/
|
|
|
|
public class Economy {
|
2012-11-25 23:34:21 +01:00
|
|
|
private static EconomyManager manager = new EconomyManager();
|
2012-01-25 16:32:34 +01:00
|
|
|
|
2012-09-15 20:32:22 +02:00
|
|
|
public static boolean isOwnerEconomicallyActive(Inventory inventory) {
|
2012-09-20 22:38:11 +02:00
|
|
|
return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty();
|
2012-09-15 20:32:22 +02:00
|
|
|
}
|
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
public static boolean hasAccount(String player) {
|
|
|
|
return !player.isEmpty() && manager.hasAccount(uName.getName(player));
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
2012-08-24 17:36:35 +02:00
|
|
|
public static String getServerAccountName() {
|
2012-11-23 21:04:13 +01:00
|
|
|
return Properties.SERVER_ECONOMY_ACCOUNT;
|
2012-08-18 21:53:01 +02:00
|
|
|
}
|
2012-09-02 22:41:24 +02:00
|
|
|
|
2012-08-18 21:53:01 +02:00
|
|
|
public static boolean isServerAccount(String acc) {
|
2012-09-02 22:41:24 +02:00
|
|
|
return ChestShopSign.isAdminShop(acc);
|
2012-08-18 21:53:01 +02:00
|
|
|
}
|
2012-01-25 16:32:34 +01:00
|
|
|
|
2013-01-09 01:50:44 +01:00
|
|
|
public static boolean add(String name, double amount) {
|
2012-11-03 20:40:30 +01:00
|
|
|
if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
|
|
|
|
name = getServerAccountName();
|
|
|
|
}
|
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
float taxAmount = isServerAccount(name) ? Properties.SERVER_TAX_AMOUNT : Properties.TAX_AMOUNT;
|
2012-01-25 16:32:34 +01:00
|
|
|
|
2012-08-18 21:53:01 +02:00
|
|
|
double tax = getTax(taxAmount, amount);
|
2012-08-24 17:36:35 +02:00
|
|
|
if (tax != 0) {
|
2012-09-02 22:41:24 +02:00
|
|
|
if (!getServerAccountName().isEmpty()) {
|
2012-11-25 23:34:21 +01:00
|
|
|
manager.add(getServerAccountName(), tax);
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
amount -= tax;
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
2012-09-02 22:41:24 +02:00
|
|
|
|
2013-01-09 01:50:44 +01:00
|
|
|
return manager.add(uName.getName(name), amount);
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
public static double getTax(float tax, double price) {
|
|
|
|
return NumberUtil.roundDown((tax / 100F) * price);
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
2013-01-09 01:50:44 +01:00
|
|
|
public static boolean subtract(String name, double amount) {
|
2012-11-03 20:40:30 +01:00
|
|
|
if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
|
|
|
|
name = getServerAccountName();
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:50:44 +01:00
|
|
|
return manager.subtract(uName.getName(name), roundUp(amount));
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 15:49:48 +02:00
|
|
|
public static boolean hasEnough(String name, double amount) {
|
2012-09-12 12:35:48 +02:00
|
|
|
if (amount <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-25 23:34:21 +01:00
|
|
|
|
2012-11-03 20:40:30 +01:00
|
|
|
if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
|
|
|
|
name = getServerAccountName();
|
|
|
|
}
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
return manager.hasEnough(uName.getName(name), roundUp(amount));
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
2012-08-10 19:01:04 +02:00
|
|
|
public static double getBalance(String name) {
|
2012-11-03 20:40:30 +01:00
|
|
|
if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
|
|
|
|
name = getServerAccountName();
|
|
|
|
}
|
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
return manager.balance(uName.getName(name));
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatBalance(double amount) {
|
2012-11-25 23:34:21 +01:00
|
|
|
return manager.format(roundUp(amount));
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|
2012-06-25 17:16:24 +02:00
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
public static void setPlugin(EconomyManager plugin) {
|
|
|
|
manager = plugin;
|
2012-06-25 17:16:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
public static EconomyManager getManager() {
|
|
|
|
return manager;
|
2012-06-25 17:16:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isLoaded() {
|
2012-11-25 23:34:21 +01:00
|
|
|
return manager.getClass() != EconomyManager.class;
|
2012-06-25 17:16:24 +02:00
|
|
|
}
|
2012-01-25 16:32:34 +01:00
|
|
|
}
|