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,264 +31,327 @@ import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin; 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; import com.gmail.mirelatrue.xpbank.XPBank;
public class Economy_XPBank implements Economy { public class Economy_XPBank implements Economy {
private static final Logger log = Logger.getLogger("Minecraft"); private static final Logger log = Logger.getLogger("Minecraft");
private final String name = "XPBank"; private final String name = "XPBank";
private Plugin plugin = null; private Plugin plugin = null;
private XPBank XPB = null; private XPBank XPB = null;
private API api = null;
public Economy_XPBank(Plugin plugin) { public Economy_XPBank (Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin); Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
// Load Plugin in case it was loaded before
if (XPB == null) {
Plugin economy = plugin.getServer().getPluginManager().getPlugin("XPBank");
if (economy != null && economy.isEnabled()) {
XPB = (XPBank) economy;
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
}
}
}
public class EconomyServerListener implements Listener { // Load Plugin in case it was loaded before
Economy_XPBank economy = null; if (XPB == null) {
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));
}
}
}
public EconomyServerListener(Economy_XPBank economy_XPBank) { public class EconomyServerListener implements Listener {
this.economy = economy_XPBank; Economy_XPBank economy = null;
}
@EventHandler(priority = EventPriority.MONITOR) public EconomyServerListener (Economy_XPBank economy_XPBank) {
public void onPluginEnable(PluginEnableEvent event) { this.economy = economy_XPBank;
if (economy.XPB == null) { }
Plugin eco = plugin.getServer().getPluginManager().getPlugin("XPBank");
if (eco != null && eco.isEnabled()) { @EventHandler (priority = EventPriority.MONITOR)
economy.XPB = (XPBank) eco; public void onPluginEnable (PluginEnableEvent event) {
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name)); if (economy.XPB == null) {
} Plugin eco = plugin.getServer().getPluginManager().getPlugin("XPBank");
}
}
@EventHandler(priority = EventPriority.MONITOR) if (eco != null && eco.isEnabled()) {
public void onPluginDisable(PluginDisableEvent event) { economy.XPB = (XPBank) eco;
if (economy.XPB != null) { api = XPB.getAPI();
if (event.getPlugin().getDescription().getName().equals("XPBank")) { log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
economy.XPB = null; }
log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name)); }
} }
}
}
}
@Override @EventHandler (priority = EventPriority.MONITOR)
public boolean isEnabled() { public void onPluginDisable (PluginDisableEvent event) {
return this.XPB != null; if (economy.XPB != null) {
} if (event.getPlugin().getDescription().getName().equals("XPBank")) {
economy.XPB = null;
@Override log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
public String getName() { }
return name; }
} }
}
@Override
public String format(double amount) {
return String.format("%d %s", (int) amount, XPBank.currencyName);
}
@Override
public String currencyNamePlural() {
return XPBank.currencyName;
}
@Override
public String currencyNameSingular() {
return XPBank.currencyName;
}
@Override
public double getBalance(String playerName) {
return XPB.getBalance(playerName);
}
@Override
public boolean has(String playerName, double amount) {
if (XPB.getBalance(playerName) >= (int) amount) { 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");
}
int value = (int) amount;
int balance = XPB.getBalance(playerName);
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format("Insufficient %s", XPBank.currencyName));
}
XPB.addToBalance(playerName, -value);
return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
if (!XPB.playerExists(playerName)) {
// 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);
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
}
XPB.addToBalance(playerName, 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");
}
Boolean created = XPB.addBank(name);
if (!created) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, "There was an error creating the account");
}
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");
}
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);
}
@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");
}
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");
}
int value = (int) amount;
int balance = XPB.getBankBalance(name);
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format("Insufficient %s", XPBank.currencyName));
}
XPB.addToBankBalance(name, -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");
}
int value = (int) amount;
int balance = XPB.getBankBalance(name);
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, "Amount must be greater than zero");
}
XPB.addToBankBalance(name, 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");
}
@Override
public EconomyResponse isBankMember(String name, String playerName) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "XPBank does not support assigning bank members");
}
@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);
}
@Override
public List<String> getBanks() {
return XPB.getBanks();
}
@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 @Override
public int fractionalDigits() { public boolean isEnabled () {
return this.XPB != null;
}
@Override
public String getName () {
return name;
}
@Override
public boolean hasBankSupport () {
return true;
}
@Override
public int fractionalDigits () {
return 0; return 0;
} }
}
@Override
public String format (double amount) {
return String.format("%d %s", (int) amount, api.currencyName((int) amount));
}
@Override
public String currencyNamePlural () {
return api.getMsg("CurrencyNamePlural");
}
@Override
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 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 = account.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
}
account.modifyBalance(-value);
return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
}
@Override
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 = account.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
account.addTaxableIncome(value);
return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
}
@Override
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));
}
Account account = api.getAccount(player);
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 bankBalance (String name) {
GroupBank groupBank = api.getGroupBank(name);
if (groupBank == null) {
return new EconomyResponse(0, 0, ResponseType.FAILURE, api.getMsg("GroupBankNotExists"));
}
return new EconomyResponse(0, groupBank.getBalance(), ResponseType.SUCCESS, null);
}
@Override
public EconomyResponse bankHas (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 (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, api.getMsg("LessThanZero"));
}
if (value > balance) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, String.format(api.getMsg("InsufficientXP"), api.currencyName(value)));
}
groupBank.modifyBalance(-value);
return new EconomyResponse(value, balance - value, ResponseType.SUCCESS, null);
}
@Override
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 = groupBank.getBalance();
if (value < 1) {
return new EconomyResponse(0, balance, ResponseType.FAILURE, api.getMsg("LessThanZero"));
}
groupBank.modifyBalance(value);
return new EconomyResponse(value, balance + value, ResponseType.SUCCESS, null);
}
@Override
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) {
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 List<String> getBanks () {
return api.getAllGroupBanks();
}
@Override
public boolean createPlayerAccount (String playerName) {
api.createAccount(playerName);
return true;
}
}