2011-06-29 02:18:20 +02:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Morgan Humes <morgan@lanaddict.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.milkbowl.vault.economy.plugins;
|
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
import java.util.List;
|
2011-06-30 01:11:03 +02:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
import net.milkbowl.vault.economy.EconomyResponse;
|
2011-10-30 20:00:12 +01:00
|
|
|
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
|
2011-06-29 02:18:20 +02:00
|
|
|
|
|
|
|
import org.bukkit.event.Event.Priority;
|
|
|
|
import org.bukkit.event.Event.Type;
|
|
|
|
import org.bukkit.event.server.PluginDisableEvent;
|
|
|
|
import org.bukkit.event.server.PluginEnableEvent;
|
|
|
|
import org.bukkit.event.server.ServerListener;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
|
|
|
|
import cosine.boseconomy.BOSEconomy;
|
|
|
|
|
2011-10-10 08:02:05 +02:00
|
|
|
public class Economy_BOSE7 implements Economy {
|
2011-06-30 01:11:03 +02:00
|
|
|
private static final Logger log = Logger.getLogger("Minecraft");
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
private String name = "BOSEconomy";
|
|
|
|
private Plugin plugin = null;
|
|
|
|
private PluginManager pluginManager = null;
|
|
|
|
private BOSEconomy economy = null;
|
|
|
|
private EconomyServerListener economyServerListener = null;
|
|
|
|
|
2011-10-10 08:02:05 +02:00
|
|
|
public Economy_BOSE7(Plugin plugin) {
|
2011-06-29 02:18:20 +02:00
|
|
|
this.plugin = plugin;
|
|
|
|
pluginManager = this.plugin.getServer().getPluginManager();
|
|
|
|
|
|
|
|
economyServerListener = new EconomyServerListener(this);
|
|
|
|
|
|
|
|
this.pluginManager.registerEvent(Type.PLUGIN_ENABLE, economyServerListener, Priority.Monitor, plugin);
|
|
|
|
this.pluginManager.registerEvent(Type.PLUGIN_DISABLE, economyServerListener, Priority.Monitor, plugin);
|
|
|
|
|
|
|
|
// Load Plugin in case it was loaded before
|
|
|
|
if (economy == null) {
|
|
|
|
Plugin bose = plugin.getServer().getPluginManager().getPlugin("BOSEconomy");
|
2011-10-10 08:02:05 +02:00
|
|
|
if (bose != null && bose.isEnabled() && bose.getDescription().getVersion().startsWith("0.7")) {
|
2011-06-29 02:18:20 +02:00
|
|
|
economy = (BOSEconomy) bose;
|
|
|
|
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
2011-08-12 19:11:25 +02:00
|
|
|
if (economy == null) {
|
2011-06-29 02:18:20 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return economy.isEnabled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-07-18 20:31:29 +02:00
|
|
|
public double getBalance(String playerName) {
|
|
|
|
final double balance;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-06-29 02:18:20 +02:00
|
|
|
|
2011-07-18 20:31:29 +02:00
|
|
|
final double fBalance = balance;
|
|
|
|
return fBalance;
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
|
|
|
double balance;
|
|
|
|
EconomyResponse.ResponseType type;
|
|
|
|
String errorMessage = null;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
|
|
|
if (amount < 0) {
|
2011-06-29 02:18:20 +02:00
|
|
|
errorMessage = "Cannot withdraw negative funds";
|
|
|
|
type = EconomyResponse.ResponseType.FAILURE;
|
|
|
|
amount = 0;
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-10-30 20:00:12 +01:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
if (balance - amount < 0) {
|
2011-06-29 02:18:20 +02:00
|
|
|
errorMessage = "Insufficient funds";
|
|
|
|
type = EconomyResponse.ResponseType.FAILURE;
|
|
|
|
amount = 0;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-10-30 20:00:12 +01:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
2011-12-22 22:32:38 +01:00
|
|
|
if (economy.setPlayerMoney(playerName, balance - amount, false)) {
|
2011-06-29 02:18:20 +02:00
|
|
|
type = EconomyResponse.ResponseType.SUCCESS;
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
|
|
|
} else {
|
|
|
|
errorMessage = "Error withdrawing funds";
|
|
|
|
type = EconomyResponse.ResponseType.FAILURE;
|
|
|
|
amount = 0;
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse depositPlayer(String playerName, double amount) {
|
|
|
|
double balance;
|
|
|
|
EconomyResponse.ResponseType type;
|
|
|
|
String errorMessage = null;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
|
|
|
if (amount < 0) {
|
2011-06-29 02:18:20 +02:00
|
|
|
errorMessage = "Cannot deposit negative funds";
|
|
|
|
type = EconomyResponse.ResponseType.FAILURE;
|
|
|
|
amount = 0;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
return new EconomyResponse(amount, economy.getPlayerMoneyDouble(playerName), type, errorMessage);
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
|
|
|
if (economy.setPlayerMoney(playerName, balance + amount, false)) {
|
2011-06-29 02:18:20 +02:00
|
|
|
type = EconomyResponse.ResponseType.SUCCESS;
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
|
|
|
} else {
|
|
|
|
errorMessage = "Error withdrawing funds";
|
|
|
|
type = EconomyResponse.ResponseType.FAILURE;
|
|
|
|
amount = 0;
|
2011-12-22 22:32:38 +01:00
|
|
|
balance = economy.getPlayerMoneyDouble(playerName);
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-10-30 20:00:12 +01:00
|
|
|
return new EconomyResponse(amount, balance, type, errorMessage);
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMoneyNamePlural() {
|
|
|
|
return economy.getMoneyNamePlural();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMoneyNameSingular() {
|
|
|
|
return economy.getMoneyName();
|
|
|
|
}
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
private class EconomyServerListener extends ServerListener {
|
2011-10-10 08:02:05 +02:00
|
|
|
Economy_BOSE7 economy = null;
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-10-10 08:02:05 +02:00
|
|
|
public EconomyServerListener(Economy_BOSE7 economy) {
|
2011-06-29 02:18:20 +02:00
|
|
|
this.economy = economy;
|
|
|
|
}
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
public void onPluginEnable(PluginEnableEvent event) {
|
|
|
|
if (economy.economy == null) {
|
|
|
|
Plugin bose = plugin.getServer().getPluginManager().getPlugin("BOSEconomy");
|
|
|
|
|
2011-10-10 08:02:05 +02:00
|
|
|
if (bose != null && bose.isEnabled() && bose.getDescription().getVersion().startsWith("0.7")) {
|
2011-06-29 02:18:20 +02:00
|
|
|
economy.economy = (BOSEconomy) bose;
|
|
|
|
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-12 19:11:25 +02:00
|
|
|
|
2011-06-29 02:18:20 +02:00
|
|
|
public void onPluginDisable(PluginDisableEvent event) {
|
|
|
|
if (economy.economy != null) {
|
2011-10-10 08:02:05 +02:00
|
|
|
if (event.getPlugin().getDescription().getName().equals("BOSEconomy") && event.getPlugin().getDescription().getVersion().startsWith("0.7")) {
|
2011-06-29 02:18:20 +02:00
|
|
|
economy.economy = null;
|
|
|
|
log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String format(double amount) {
|
|
|
|
if (amount == 1) {
|
2011-10-10 08:40:03 +02:00
|
|
|
return String.format("%.0f %s", amount, getMoneyNameSingular());
|
2011-06-29 02:18:20 +02:00
|
|
|
} else {
|
2011-10-10 08:40:03 +02:00
|
|
|
return String.format("%.2f %s", amount, getMoneyNamePlural());
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-22 22:32:38 +01:00
|
|
|
|
2011-10-30 20:00:12 +01:00
|
|
|
@Override
|
|
|
|
public EconomyResponse createBank(String name, String player) {
|
|
|
|
boolean success = economy.addBankOwner(name, player, false);
|
|
|
|
if (success)
|
2011-12-22 22:32:38 +01:00
|
|
|
return new EconomyResponse(0, economy.getBankMoneyDouble(name), ResponseType.SUCCESS, "");
|
2011-10-30 20:00:12 +01:00
|
|
|
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "Unable to create that bank account.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse bankHas(String name, double amount) {
|
|
|
|
if (!economy.bankExists(name))
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
|
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
double bankMoney = economy.getBankMoneyDouble(name);
|
2011-10-30 20:00:12 +01:00
|
|
|
if (bankMoney < amount)
|
|
|
|
return new EconomyResponse(0, bankMoney, ResponseType.FAILURE, "The bank does not have enough money!");
|
|
|
|
else
|
|
|
|
return new EconomyResponse(0, bankMoney, ResponseType.SUCCESS, "");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse bankWithdraw(String name, double amount) {
|
|
|
|
EconomyResponse er = bankHas(name, amount);
|
|
|
|
if (!er.transactionSuccess())
|
|
|
|
return er;
|
|
|
|
else {
|
2011-12-22 22:32:38 +01:00
|
|
|
economy.addBankMoney(name, -amount, true);
|
|
|
|
return new EconomyResponse(amount, economy.getBankMoneyDouble(name), ResponseType.SUCCESS, "");
|
2011-10-30 20:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse bankDeposit(String name, double amount) {
|
|
|
|
if (!economy.bankExists(name))
|
|
|
|
return new EconomyResponse(amount, 0, ResponseType.FAILURE, "That bank does not exist!");
|
|
|
|
else {
|
2011-12-22 22:32:38 +01:00
|
|
|
economy.addBankMoney(name, amount, true);
|
|
|
|
return new EconomyResponse(amount, economy.getBankMoneyDouble(name), ResponseType.SUCCESS, "");
|
2011-10-30 20:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse isBankOwner(String name, String playerName) {
|
|
|
|
if (!economy.bankExists(name))
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
|
|
|
|
else if (economy.isBankOwner(name, playerName)) {
|
2011-12-22 22:32:38 +01:00
|
|
|
return new EconomyResponse(0, economy.getBankMoneyDouble(name), ResponseType.SUCCESS, "");
|
2011-10-30 20:00:12 +01:00
|
|
|
} else
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player is not a bank owner!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse isBankMember(String name, String playerName) {
|
|
|
|
if (!economy.bankExists(name))
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
|
|
|
|
else if (economy.isBankMember(name, playerName)) {
|
2011-12-22 22:32:38 +01:00
|
|
|
return new EconomyResponse(0, economy.getBankMoneyDouble(name), ResponseType.SUCCESS, "");
|
2011-10-30 20:00:12 +01:00
|
|
|
} else
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That player is not a bank member!");
|
|
|
|
}
|
2011-11-29 17:51:44 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public EconomyResponse bankBalance(String name) {
|
|
|
|
if (!economy.bankExists(name))
|
|
|
|
return new EconomyResponse(0, 0, ResponseType.FAILURE, "That bank does not exist!");
|
2011-10-30 20:00:12 +01:00
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
double bankMoney = economy.getBankMoneyDouble(name);
|
2011-11-29 17:51:44 +01:00
|
|
|
return new EconomyResponse(0, bankMoney, ResponseType.SUCCESS, null);
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:32:38 +01:00
|
|
|
@Override
|
|
|
|
public List<String> getBanks() {
|
|
|
|
return economy.getBankList();
|
|
|
|
}
|
|
|
|
|
2011-10-30 20:00:12 +01:00
|
|
|
@Override
|
|
|
|
public boolean has(String playerName, double amount) {
|
|
|
|
return getBalance(playerName) >= amount;
|
|
|
|
}
|
2012-01-02 19:19:29 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasBankSupport() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-02 19:50:16 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasAccount(String playerName) {
|
|
|
|
return economy.playerRegistered(playerName, false);
|
|
|
|
}
|
2011-06-29 02:18:20 +02:00
|
|
|
}
|