Vault/src/net/milkbowl/vault/economy/plugins/Economy_Essentials.java

265 lines
9.3 KiB
Java
Raw Normal View History

2011-06-29 02:18:20 +02:00
/**
2012-01-16 17:39:28 +01:00
* Copyright (C) 2011 Morgan Humes <morgan@lanaddict.com>
2011-06-29 02:18:20 +02:00
*
* 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;
import java.util.ArrayList;
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;
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
2011-06-29 02:18:20 +02:00
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
2011-06-29 02:18:20 +02:00
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;
public class Economy_Essentials 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
2012-02-08 23:26:00 +01:00
private final String name = "Essentials Economy";
2011-06-29 02:18:20 +02:00
private Plugin plugin = null;
private Essentials ess = null;
2011-08-12 19:11:25 +02:00
2011-06-29 02:18:20 +02:00
public Economy_Essentials(Plugin plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(new EconomyServerListener(this), plugin);
2011-08-12 19:11:25 +02:00
2011-06-29 02:18:20 +02:00
// Load Plugin in case it was loaded before
if (ess == null) {
Plugin essentials = plugin.getServer().getPluginManager().getPlugin("Essentials");
if (essentials != null && essentials.isEnabled()) {
ess = (Essentials) essentials;
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), name));
}
}
2011-08-12 19:11:25 +02:00
}
2011-06-29 02:18:20 +02:00
@Override
public boolean isEnabled() {
2011-08-12 19:11:25 +02:00
if (ess == null) {
2011-06-29 02:18:20 +02:00
return false;
} else {
return ess.isEnabled();
}
}
2011-08-12 19:11:25 +02:00
2011-06-29 02:18:20 +02:00
@Override
public String getName() {
return name;
}
@Override
public double getBalance(String playerName) {
2011-06-29 02:18:20 +02:00
double balance;
2011-06-29 02:18:20 +02:00
try {
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
} catch (UserDoesNotExistException e) {
createPlayerAccount(playerName);
balance = 0;
2011-06-29 02:18:20 +02:00
}
2011-08-12 19:11:25 +02:00
return balance;
2011-06-29 02:18:20 +02:00
}
2011-08-12 19:11:25 +02:00
2012-01-15 20:55:14 +01:00
@Override
public boolean createPlayerAccount(String playerName) {
if (hasAccount(playerName)) {
2011-06-29 02:18:20 +02:00
return false;
}
return com.earth2me.essentials.api.Economy.createNPC(playerName);
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
2011-06-29 02:18:20 +02:00
try {
com.earth2me.essentials.api.Economy.subtract(playerName, amount);
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
type = EconomyResponse.ResponseType.SUCCESS;
} catch (UserDoesNotExistException e) {
if (createPlayerAccount(playerName)) {
return withdrawPlayer(playerName, amount);
} else {
amount = 0;
balance = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "User does not exist";
}
} catch (NoLoanPermittedException e) {
try {
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
amount = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "Loan was not permitted";
} catch (UserDoesNotExistException e1) {
amount = 0;
balance = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "User does not exist";
}
}
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
2011-06-29 02:18:20 +02:00
try {
com.earth2me.essentials.api.Economy.add(playerName, amount);
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
type = EconomyResponse.ResponseType.SUCCESS;
} catch (UserDoesNotExistException e) {
2011-08-12 19:11:25 +02:00
if (createPlayerAccount(playerName)) {
2011-06-29 02:18:20 +02:00
return depositPlayer(playerName, amount);
} else {
amount = 0;
balance = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "User does not exist";
}
} catch (NoLoanPermittedException e) {
try {
balance = com.earth2me.essentials.api.Economy.getMoney(playerName);
amount = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "Loan was not permitted";
} catch (UserDoesNotExistException e1) {
balance = 0;
amount = 0;
type = EconomyResponse.ResponseType.FAILURE;
errorMessage = "Loan was not permitted";
}
}
2011-08-12 19:11:25 +02:00
2011-06-29 02:18:20 +02:00
return new EconomyResponse(amount, balance, type, errorMessage);
}
2011-08-12 19:11:25 +02:00
public class EconomyServerListener implements Listener {
2011-06-29 02:18:20 +02:00
Economy_Essentials economy = null;
2011-08-12 19:11:25 +02:00
2011-06-29 02:18:20 +02:00
public EconomyServerListener(Economy_Essentials economy) {
this.economy = economy;
}
2011-08-12 19:11:25 +02:00
@EventHandler(priority = EventPriority.MONITOR)
2011-06-29 02:18:20 +02:00
public void onPluginEnable(PluginEnableEvent event) {
if (economy.ess == null) {
Plugin essentials = plugin.getServer().getPluginManager().getPlugin("Essentials");
if (essentials != null && essentials.isEnabled()) {
economy.ess = (Essentials) essentials;
log.info(String.format("[%s][Economy] %s hooked.", plugin.getDescription().getName(), economy.name));
}
}
}
2011-08-12 19:11:25 +02:00
@EventHandler(priority = EventPriority.MONITOR)
2011-06-29 02:18:20 +02:00
public void onPluginDisable(PluginDisableEvent event) {
if (economy.ess != null) {
if (event.getPlugin().getDescription().getName().equals("Essentials")) {
economy.ess = null;
log.info(String.format("[%s][Economy] %s unhooked.", plugin.getDescription().getName(), economy.name));
}
}
}
}
@Override
public String format(double amount) {
return com.earth2me.essentials.api.Economy.format(amount);
}
@Override
public boolean has(String playerName, double amount) {
return getBalance(playerName) >= amount;
}
@Override
public EconomyResponse createBank(String name, String player) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse deleteBank(String name) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse bankHas(String name, double amount) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse bankWithdraw(String name, double amount) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse bankDeposit(String name, double amount) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse isBankOwner(String name, String playerName) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse isBankMember(String name, String playerName) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public EconomyResponse bankBalance(String name) {
return new EconomyResponse(0, 0, ResponseType.NOT_IMPLEMENTED, "Essentials Eco does not support bank accounts!");
}
@Override
public List<String> getBanks() {
return new ArrayList<String>();
}
@Override
public boolean hasBankSupport() {
return false;
}
@Override
public boolean hasAccount(String playerName) {
return com.earth2me.essentials.api.Economy.playerExists(playerName);
}
2011-06-29 02:18:20 +02:00
}