Update for full bank support.

Nearly finished.
This commit is contained in:
Stephanie Gardner 2013-01-09 23:58:53 -05:00
parent d0200930dd
commit b4c3a6df9b
2 changed files with 309 additions and 246 deletions

Binary file not shown.

View File

@ -31,6 +31,9 @@ import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin;
import com.gmail.mirelatrue.xpbank.API;
import com.gmail.mirelatrue.xpbank.Account;
import com.gmail.mirelatrue.xpbank.GroupBank;
import com.gmail.mirelatrue.xpbank.XPBank;
public class Economy_XPBank implements Economy {
@ -39,8 +42,9 @@ public class Economy_XPBank implements Economy {
private final String name = "XPBank";
private Plugin plugin = null;
private XPBank XPB = null;
private API api = null;
public Economy_XPBank(Plugin plugin) {
public Economy_XPBank (Plugin plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
@ -49,6 +53,7 @@ public class Economy_XPBank implements Economy {
Plugin economy = plugin.getServer().getPluginManager().getPlugin("XPBank");
if (economy != null && economy.isEnabled()) {
XPB = (XPBank) economy;
api = XPB.getAPI();
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
}
}
@ -57,25 +62,25 @@ public class Economy_XPBank implements Economy {
public class EconomyServerListener implements Listener {
Economy_XPBank economy = null;
public EconomyServerListener(Economy_XPBank economy_XPBank) {
public EconomyServerListener (Economy_XPBank economy_XPBank) {
this.economy = economy_XPBank;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPluginEnable(PluginEnableEvent event) {
@EventHandler (priority = EventPriority.MONITOR)
public void onPluginEnable (PluginEnableEvent event) {
if (economy.XPB == null) {
Plugin eco = plugin.getServer().getPluginManager().getPlugin("XPBank");
if (eco != null && eco.isEnabled()) {
economy.XPB = (XPBank) eco;
api = XPB.getAPI();
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPluginDisable(PluginDisableEvent event) {
@EventHandler (priority = EventPriority.MONITOR)
public void onPluginDisable (PluginDisableEvent event) {
if (economy.XPB != null) {
if (event.getPlugin().getDescription().getName().equals("XPBank")) {
economy.XPB = null;
@ -86,209 +91,267 @@ public class Economy_XPBank implements Economy {
}
@Override
public boolean isEnabled() {
public boolean isEnabled () {
return this.XPB != null;
}
@Override
public String getName() {
public String getName () {
return name;
}
@Override
public String format(double amount) {
return String.format("%d %s", (int) amount, XPBank.currencyName);
public boolean hasBankSupport () {
return true;
}
@Override
public String currencyNamePlural() {
return XPBank.currencyName;
public int fractionalDigits () {
return 0;
}
@Override
public String currencyNameSingular() {
return XPBank.currencyName;
public String format (double amount) {
return String.format("%d %s", (int) amount, api.currencyName((int) amount));
}
@Override
public double getBalance(String playerName) {
return XPB.getBalance(playerName);
public String currencyNamePlural () {
return api.getMsg("CurrencyNamePlural");
}
@Override
public boolean has(String playerName, double amount) {
if (XPB.getBalance(playerName) >= (int) amount) { return true; }
public String currencyNameSingular () {
return api.getMsg("currencyName");
}
@Override
public boolean hasAccount (String playerName) {
Account account = api.getAccount(playerName);
if (account != null) {
return true;
}
return false;
}
@Override
public EconomyResponse withdrawPlayer(String playerName, double amount) {
if (!XPB.playerExists(playerName)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "Player doesn't exist");
public double getBalance (String playerName) {
Account account = api.getAccount(playerName);
return account.getBalance();
}
@Override
public boolean has (String playerName, double amount) {
Account account = api.getAccount(playerName);
if (account.getBalance() >= (int) amount) {
return true;
}
return false;
}
@Override
public EconomyResponse withdrawPlayer (String playerName, double amount) {
Account account = api.getAccount(playerName);
if (account == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("Player doesn't exist."));
}
int value = (int) amount;
int balance = XPB.getBalance(playerName);
int balance = account.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format("Insufficient %s", XPBank.currencyName));
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
}
XPB.addToBalance(playerName, -value);
account.modifyBalance(-value);
return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
if (!XPB.playerExists(playerName)) {
public EconomyResponse depositPlayer (String playerName, double amount) {
Account account = api.getAccount(playerName);
if (account == null) {
// Stupid plugins that use fake players without creating them first...
// return new EconomyResponse(0, 0, ResponseType.FAILURE, "Player doesn't exist");
this.createPlayerAccount(playerName);
}
int value = (int) amount;
int balance = XPB.getBalance(playerName);
int balance = account.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
XPB.addToBalance(playerName, value);
account.addTaxableIncome(value);
return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse createBank(String name, String player) {
if (XPB.bankExists(name)) {
return new EconomyResponse(0, XPB.getBankBalance(name), ResponseType.FAILURE, "That account already exists");
public EconomyResponse createBank (String name, String player) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank != null) {
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("GroupBankExists"), name));
}
Boolean created = XPB.addBank(name);
Account account = api.getAccount(player);
if (!created) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There was an error creating the account");
groupBank = api.createGroupBank(name, account);
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse deleteBank (String name) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
api.deleteGroupBank(groupBank, String.format(api.getMsg("Disbanded"), groupBank.getName()));
return new EconomyResponse(0, 0, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse deleteBank(String name) {
if (!XPB.bankExists(name)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no account with that name");
public EconomyResponse bankBalance (String name) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
Boolean deleted = XPB.deleteBank(name);
if (!deleted) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There was an error deleting the account");
}
return new EconomyResponse(0, 0, ResponseType.SUCCESS, null);
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse bankHas(String name, double amount) {
if (!XPB.bankExists(name)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no account with that name");
}
public EconomyResponse bankHas (String name, double amount) {
GroupBank groupBank = api.getGroupBank(name);
int balance = XPB.getBankBalance(name);
if (balance >= (int) amount) {
return new EconomyResponse(0, balance, ResponseType.SUCCESS, null);
}
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format("The account does not have that much %s", XPBank.currencyName));
}
@Override
public EconomyResponse bankWithdraw(String name, double amount) {
if (!XPB.bankExists(name)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no account with that name");
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
int value = (int) amount;
int balance = XPB.getBankBalance(name);
int balance = groupBank.getBalance();
if (balance >= value) {
return new EconomyResponse(0, balance, ResponseType.SUCCESS, null);
}
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
}
@Override
public EconomyResponse bankWithdraw (String name, double amount) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
int value = (int) amount;
int balance = groupBank.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format("Insufficient %s", XPBank.currencyName));
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
}
XPB.addToBankBalance(name, -value);
groupBank.modifyBalance(-value);
return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse bankDeposit(String name, double amount) {
if (!XPB.bankExists(name)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no account with that name");
public EconomyResponse bankDeposit (String name, double amount) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
int value = (int) amount;
int balance = XPB.getBankBalance(name);
int balance = groupBank.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
XPB.addToBankBalance(name, value);
groupBank.modifyBalance(value);
return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse isBankOwner(String name, String playerName) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "XPBank does not support assigning bank owners");
public EconomyResponse isBankOwner (String name, String playerName) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
Account account = api.getAccount(name);
if (account == null) {
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, api.getMsg("PlayerNotExist"));
}
if (groupBank.getOwner().equalsIgnoreCase(name)) {
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
}
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("PlayerNotOwner"), account.getName(), groupBank.getName()));
}
@Override
public EconomyResponse isBankMember(String name, String playerName) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "XPBank does not support assigning bank members");
public EconomyResponse isBankMember (String name, String playerName) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
Account account = api.getAccount(name);
if (account == null) {
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, api.getMsg("PlayerNotExist"));
}
if (groupBank.groupMembers.getMembers().containsKey(playerName)) {
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
}
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.FAILURE, String.format(api.getMsg("NotAMemberOf"), groupBank.getName(), account.getName()));
}
@Override
public EconomyResponse bankBalance(String name) {
if (!XPB.bankExists(name)) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There is no account with that name");
}
return new EconomyResponse(0, XPB.getBankBalance(name), ResponseType.SUCCESS, null);
public List<String> getBanks () {
return api.getAllGroupBanks();
}
@Override
public List<String> getBanks() {
return XPB.getBanks();
}
public boolean createPlayerAccount (String playerName) {
api.createAccount(playerName);
@Override
public boolean hasBankSupport() {
return true;
}
@Override
public boolean hasAccount(String playerName) {
return XPB.playerExists(playerName);
}
@Override
public boolean createPlayerAccount(String playerName) {
return XPB.addPlayer(playerName);
}
@Override
public int fractionalDigits() {
return 0;
}
}