Updated economy support

This commit is contained in:
Garbage Mule 2011-09-22 11:50:01 +02:00
parent 0badeaaf90
commit c126bf36e9
16 changed files with 260 additions and 126 deletions

Binary file not shown.

View File

@ -1292,7 +1292,7 @@ public class Arena
if (stack.getTypeId() == MobArena.ECONOMY_MONEY_ID)
{
//if (plugin.Methods.hasMethod() && !plugin.Method.getAccount(p.getName()).hasEnough(stack.getAmount()))
if (plugin.Methods.hasMethod() && !(plugin.Method.getAccount(p.getName()).balance() >= stack.getAmount()))
if (plugin.Method != null && !(plugin.Method.getAccount(p.getName()).balance() >= stack.getAmount()))
return false;
}
// Normal stack
@ -1317,7 +1317,7 @@ public class Arena
// Take some economy money
for (InventoryItem item : InventoryItem.extractAllFromArray(MobArena.ECONOMY_MONEY_ID, fee))
if (plugin.Methods.hasMethod())
if (plugin.Method != null)
plugin.Method.getAccount(p.getName()).subtract(item.getAmount());
// Take any other items

View File

@ -1,6 +1,8 @@
package com.garbagemule.MobArena;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EndermanPickupEvent;
import org.bukkit.event.entity.EndermanPlaceEvent;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
@ -62,4 +64,16 @@ public class MAEntityListener extends EntityListener
for (Arena arena : am.arenas)
arena.eventListener.onEntityTarget(event);
}
public void onEndermanPickup(EndermanPickupEvent event)
{
for (Arena arena : am.arenas)
arena.eventListener.onEndermanPickup(event);
}
public void onEndermanPickup(EndermanPlaceEvent event)
{
for (Arena arena : am.arenas)
arena.eventListener.onEndermanPlace(event);
}
}

View File

@ -22,6 +22,8 @@ import org.bukkit.event.block.BlockEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EndermanPickupEvent;
import org.bukkit.event.entity.EndermanPlaceEvent;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
@ -435,6 +437,18 @@ public class MAListener implements ArenaListener
}
}
public void onEndermanPickup(EndermanPickupEvent event)
{
if (arena.inRegion(event.getBlock().getLocation()))
event.setCancelled(true);
}
public void onEndermanPlace(EndermanPlaceEvent event)
{
if (arena.inRegion(event.getLocation()))
event.setCancelled(true);
}
public void onEntityRegainHealth(EntityRegainHealthEvent event)
{
if (!arena.running) return;

View File

@ -199,7 +199,7 @@ public class MASpawnThread implements Runnable
}
else if (reward.getTypeId() == MobArena.ECONOMY_MONEY_ID)
{
if (plugin.Methods.hasMethod())
if (plugin.Method != null)
MAUtils.tellPlayer(p, Msg.WAVE_REWARD, plugin.Method.format(reward.getAmount()));
else MobArena.warning("Tried to add money, but no economy plugin detected!");
}

View File

@ -479,7 +479,7 @@ public class MAUtils
// If this is money, don't add to inventory.
if (stack.getTypeId() == MobArena.ECONOMY_MONEY_ID)
{
if (plugin != null && plugin.Methods.hasMethod())
if (plugin != null && plugin.Method != null)
plugin.Method.getAccount(p.getName()).add(stack.getAmount());
continue;
@ -1170,11 +1170,16 @@ public class MAUtils
stack = (ItemStack) e;
if (stack.getTypeId() == MobArena.ECONOMY_MONEY_ID)
{
if (plugin.Methods.hasMethod())
if (plugin.Method != null)
{
buffy.append(plugin.Method.format(stack.getAmount()));
buffy.append(", ");
}
else
{
MobArena.warning("Tried to do some money stuff, but no economy plugin was detected!");
return buffy.toString();
}
continue;
}

View File

@ -36,7 +36,6 @@ public class MobArena extends JavaPlugin
private ArenaMaster am;
// Economy stuff
protected Methods Methods;
protected Method Method;
// Spout stuff
@ -141,6 +140,8 @@ public class MobArena extends JavaPlugin
pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this);
pm.registerEvent(Event.Type.ENTITY_COMBUST, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.ENTITY_TARGET, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.ENDERMAN_PICKUP, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.ENDERMAN_PLACE, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Highest, this);
pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Monitor, this); // I know Monitor is bad, but other plugins suck! :(
}
@ -163,12 +164,13 @@ public class MobArena extends JavaPlugin
private void setupRegister()
{
Methods = new Methods();
if (!Methods.hasMethod() && Methods.setMethod(this))
{
Method = Methods.getMethod();
Methods.setMethod(getServer().getPluginManager());
Method = Methods.getMethod();
if (Method != null)
info("Payment method found (" + Method.getName() + " version: " + Method.getVersion() + ")");
}
else
info("No payment method found!");
}
private void setupSpout()

View File

@ -39,6 +39,13 @@ public interface Method {
*/
public String getVersion();
/**
* Returns the amount of decimal places that get stored
* NOTE: it will return -1 if there is no rounding
*
* @return <code>int</code> for each decimal place
*/
public int fractionalDigits();
/**
* Formats amounts into this payment methods style of currency display.

View File

@ -12,15 +12,13 @@ import org.bukkit.plugin.PluginManager;
*
* Allowing you to check whether a payment method exists or not.
*
* <blockquote><pre>
* Methods methods = new Methods();
* </pre></blockquote>
*
* Methods also allows you to set a preferred method of payment before it captures
* payment plugins in the initialization process.
*
* in <code>bukkit.yml</code>:
* <blockquote><pre>
* Methods methods = new Methods("iConomy");
* economy:
* preferred: "iConomy"
* </pre></blockquote>
*
* @author: Nijikokun <nijikokun@shortmail.com> (@nijikokun)
@ -28,48 +26,58 @@ import org.bukkit.plugin.PluginManager;
* @license: AOL license <http://aol.nexua.org>
*/
public class Methods {
private boolean self = false;
private Method Method = null;
private String preferred = "";
private Set<Method> Methods = new HashSet<Method>();
private Set<String> Dependencies = new HashSet<String>();
private Set<Method> Attachables = new HashSet<Method>();
private static String version = null;
private static boolean self = false;
private static Method Method = null;
private static String preferred = "";
private static Set<Method> Methods = new HashSet<Method>();
private static Set<String> Dependencies = new HashSet<String>();
private static Set<Method> Attachables = new HashSet<Method>();
/**
* Initialize Method class
*/
public Methods() {
this._init();
}
/**
* Initializes <code>Methods</code> class utilizing a "preferred" payment method check before
* returning the first method that was initialized.
*
* @param preferred Payment method that is most preferred for this setup.
*/
public Methods(String preferred) {
this._init();
if(this.Dependencies.contains(preferred)) {
this.preferred = preferred;
}
static {
_init();
}
/**
* Implement all methods along with their respective name & class.
*
* @see #Methods()
* @see #Methods(java.lang.String)
*/
private void _init() {
this.addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo6());
this.addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo5());
this.addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo4());
this.addMethod("BOSEconomy", new com.garbagemule.register.payment.methods.BOSE6());
this.addMethod("BOSEconomy", new com.garbagemule.register.payment.methods.BOSE7());
this.addMethod("Essentials", new com.garbagemule.register.payment.methods.EE17());
this.addMethod("MultiCurrency", new com.garbagemule.register.payment.methods.MCUR());
private static void _init() {
addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo6());
addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo5());
addMethod("iConomy", new com.garbagemule.register.payment.methods.iCo4());
addMethod("BOSEconomy", new com.garbagemule.register.payment.methods.BOSE6());
addMethod("BOSEconomy", new com.garbagemule.register.payment.methods.BOSE7());
addMethod("Essentials", new com.garbagemule.register.payment.methods.EE17());
addMethod("Currency", new com.garbagemule.register.payment.methods.MCUR());
Dependencies.add("MultiCurrency");
}
/**
* Used by the plugin to setup version
*
* @param v version
*/
public static void setVersion(String v) {
version = v;
}
/**
* Use to reset methods during disable
*/
public static void reset() {
version = null;
self = false;
Method = null;
preferred = "";
Attachables.clear();
}
/**
* Use to get version of Register plugin
* @return version
*/
public static String getVersion() {
return version;
}
/**
@ -79,7 +87,7 @@ public class Methods {
* @return <code>Set<String></code> - Array of payment methods that are loaded.
* @see #setMethod(org.bukkit.plugin.Plugin)
*/
public Set<String> getDependencies() {
public static Set<String> getDependencies() {
return Dependencies;
}
@ -90,18 +98,17 @@ public class Methods {
* @param plugin Plugin data from bukkit, Internal Class file.
* @return Method <em>or</em> Null
*/
public Method createMethod(Plugin plugin) {
for (Method method: Methods) {
public static Method createMethod(Plugin plugin) {
for (Method method: Methods)
if (method.isCompatible(plugin)) {
method.setPlugin(plugin);
return method;
}
}
return null;
}
private void addMethod(String name, Method method) {
private static void addMethod(String name, Method method) {
Dependencies.add(name);
Methods.add(method);
}
@ -113,7 +120,7 @@ public class Methods {
* @see #setMethod(org.bukkit.plugin.Plugin)
* @see #checkDisabled(org.bukkit.plugin.Plugin)
*/
public boolean hasMethod() {
public static boolean hasMethod() {
return (Method != null);
}
@ -121,70 +128,93 @@ public class Methods {
* Checks Plugin Class against a multitude of checks to verify it's usability
* as a payment method.
*
* @param method Plugin data from bukkit, Internal Class file.
* @param <code>PluginManager</code> the plugin manager for the server
* @return <code>boolean</code> True on success, False on failure.
*/
public boolean setMethod(Plugin method) {
if(hasMethod()) return true;
if(self) { self = false; return false; }
public static boolean setMethod(PluginManager manager) {
if (hasMethod())
return true;
if (self) {
self = false;
return false;
}
int count = 0;
boolean match = false;
Plugin plugin = null;
PluginManager manager = method.getServer().getPluginManager();
for(String name: this.getDependencies()) {
if(hasMethod()) break;
if(method.getDescription().getName().equals(name)) plugin = method; else plugin = manager.getPlugin(name);
if(plugin == null) continue;
for (String name : getDependencies()) {
if (hasMethod())
break;
Method current = this.createMethod(plugin);
if(current == null) continue;
plugin = manager.getPlugin(name);
if (plugin == null)
continue;
if(this.preferred.isEmpty())
this.Method = current;
else {
this.Attachables.add(current);
}
Method current = createMethod(plugin);
if (current == null)
continue;
if (preferred.isEmpty())
Method = current;
else
Attachables.add(current);
}
if(!this.preferred.isEmpty()) {
if (!preferred.isEmpty()) {
do {
if(hasMethod()) {
if (hasMethod())
match = true;
} else {
for(Method attached: this.Attachables) {
if(attached == null) continue;
else {
for (Method attached : Attachables) {
if (attached == null)
continue;
if(hasMethod()) {
if (hasMethod()) {
match = true;
break;
}
if(this.preferred.isEmpty()) this.Method = attached;
if (preferred.isEmpty())
Method = attached;
if(count == 0) {
if(this.preferred.equalsIgnoreCase(attached.getName()))
this.Method = attached;
} else {
this.Method = attached;
}
if (count == 0)
if (preferred.equalsIgnoreCase(attached.getName()))
Method = attached;
else
Method = attached;
}
count++;
}
} while(!match);
} while (!match);
}
return hasMethod();
}
/**
* Sets the preferred economy
*
* @return <code>boolean</code>
*/
public static boolean setPreferred(String check) {
if (getDependencies().contains(check)) {
preferred = check;
return true;
}
return false;
}
/**
* Grab the existing and initialized (hopefully) Method Class.
*
* @return <code>Method</code> <em>or</em> <code>Null</code>
*/
public Method getMethod() {
public static Method getMethod() {
return Method;
}
@ -195,9 +225,13 @@ public class Methods {
* @param method Plugin data from bukkit, Internal Class file.
* @return <code>boolean</code>
*/
public boolean checkDisabled(Plugin method) {
if(!hasMethod()) return true;
if (Method.isCompatible(method)) Method = null;
public static boolean checkDisabled(Plugin method) {
if(!hasMethod())
return true;
if (Method.isCompatible(method))
Method = null;
return (Method == null);
}
}

View File

@ -1,6 +1,7 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import cosine.boseconomy.BOSEconomy;
import org.bukkit.plugin.Plugin;
@ -11,6 +12,7 @@ import org.bukkit.plugin.Plugin;
* @copyright (c) 2011
* @license AOL license <http://aol.nexua.org>
*/
@SuppressWarnings("deprecation")
public class BOSE6 implements Method {
private BOSEconomy BOSEconomy;
@ -26,9 +28,16 @@ public class BOSE6 implements Method {
return "0.6.2";
}
public int fractionalDigits() {
return 0;
}
public String format(double amount) {
String currency = this.BOSEconomy.getMoneyNamePlural();
if(amount == 1) currency = this.BOSEconomy.getMoneyName();
if(amount == 1)
currency = this.BOSEconomy.getMoneyName();
return amount + " " + currency;
}
@ -45,25 +54,32 @@ public class BOSE6 implements Method {
}
public boolean hasBankAccount(String bank, String name) {
return this.BOSEconomy.isBankOwner(bank, name) || this.BOSEconomy.isBankMember(bank, name);
return this.BOSEconomy.isBankOwner(bank, name)
|| this.BOSEconomy.isBankMember(bank, name);
}
public MethodAccount getAccount(String name) {
if(!hasAccount(name)) return null;
if(!hasAccount(name))
return null;
return new BOSEAccount(name, this.BOSEconomy);
}
public MethodBankAccount getBankAccount(String bank, String name) {
if(!hasBankAccount(bank, name)) return null;
if(!hasBankAccount(bank, name))
return null;
return new BOSEBankAccount(bank, BOSEconomy);
}
public boolean isCompatible(Plugin plugin) {
return plugin.getDescription().getName().equalsIgnoreCase("boseconomy") && plugin instanceof BOSEconomy && plugin.getDescription().getVersion().equals("0.6.2");
return plugin.getDescription().getName().equalsIgnoreCase("boseconomy")
&& plugin instanceof BOSEconomy
&& plugin.getDescription().getVersion().equals("0.6.2");
}
public void setPlugin(Plugin plugin) {
BOSEconomy = (BOSEconomy)plugin;
BOSEconomy = (BOSEconomy) plugin;
}
public class BOSEAccount implements MethodAccount {

View File

@ -1,6 +1,7 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import cosine.boseconomy.BOSEconomy;
import org.bukkit.plugin.Plugin;
@ -27,9 +28,16 @@ public class BOSE7 implements Method {
return "0.7.0";
}
public int fractionalDigits() {
return this.BOSEconomy.getFractionalDigits();
}
public String format(double amount) {
String currency = this.BOSEconomy.getMoneyNamePlural();
if(amount == 1) currency = this.BOSEconomy.getMoneyName();
if(amount == 1)
currency = this.BOSEconomy.getMoneyName();
return amount + " " + currency;
}
@ -50,17 +58,23 @@ public class BOSE7 implements Method {
}
public MethodAccount getAccount(String name) {
if(!hasAccount(name)) return null;
if(!hasAccount(name))
return null;
return new BOSEAccount(name, this.BOSEconomy);
}
public MethodBankAccount getBankAccount(String bank, String name) {
if(!hasBankAccount(bank, name)) return null;
if(!hasBankAccount(bank, name))
return null;
return new BOSEBankAccount(bank, BOSEconomy);
}
public boolean isCompatible(Plugin plugin) {
return plugin.getDescription().getName().equalsIgnoreCase("boseconomy") && plugin instanceof BOSEconomy && !plugin.getDescription().getVersion().equals("0.6.2");
return plugin.getDescription().getName().equalsIgnoreCase("boseconomy")
&& plugin instanceof BOSEconomy
&& !plugin.getDescription().getVersion().equals("0.6.2");
}
public void setPlugin(Plugin plugin) {

View File

@ -1,11 +1,11 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;
import com.garbagemule.register.payment.Method;
import org.bukkit.plugin.Plugin;
@ -34,6 +34,10 @@ public class EE17 implements Method {
return "2.2";
}
public int fractionalDigits() {
return -1;
}
public String format(double amount) {
return Economy.format(amount);
}
@ -55,7 +59,9 @@ public class EE17 implements Method {
}
public MethodAccount getAccount(String name) {
if(!hasAccount(name)) return null;
if(!hasAccount(name))
return null;
return new EEcoAccount(name);
}
@ -67,7 +73,8 @@ public class EE17 implements Method {
try { Class.forName("com.earth2me.essentials.api.Economy"); }
catch(Exception e) { return false; }
return plugin.getDescription().getName().equalsIgnoreCase("essentials") && plugin instanceof Essentials;
return plugin.getDescription().getName().equalsIgnoreCase("essentials")
&& plugin instanceof Essentials;
}
public void setPlugin(Plugin plugin) {

View File

@ -29,6 +29,10 @@ public class MCUR implements Method {
return "0.09";
}
public int fractionalDigits() {
return -1;
}
public String format(double amount) {
return amount + " Currency";
}
@ -58,7 +62,9 @@ public class MCUR implements Method {
}
public boolean isCompatible(Plugin plugin) {
return plugin.getDescription().getName().equalsIgnoreCase(getName()) && plugin instanceof Currency;
return (plugin.getDescription().getName().equalsIgnoreCase("Currency")
|| plugin.getDescription().getName().equalsIgnoreCase("MultiCurrency"))
&& plugin instanceof Currency;
}
public void setPlugin(Plugin plugin) {

View File

@ -1,9 +1,9 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import com.nijiko.coelho.iConomy.iConomy;
import com.nijiko.coelho.iConomy.system.Account;
import com.garbagemule.register.payment.Method;
import org.bukkit.plugin.Plugin;
@ -29,8 +29,12 @@ public class iCo4 implements Method {
return "4";
}
public int fractionalDigits() {
return 2;
}
public String format(double amount) {
return this.iConomy.getBank().format(amount);
return com.nijiko.coelho.iConomy.iConomy.getBank().format(amount);
}
public boolean hasBanks() {
@ -42,7 +46,7 @@ public class iCo4 implements Method {
}
public boolean hasAccount(String name) {
return this.iConomy.getBank().hasAccount(name);
return com.nijiko.coelho.iConomy.iConomy.getBank().hasAccount(name);
}
public boolean hasBankAccount(String bank, String name) {
@ -50,7 +54,7 @@ public class iCo4 implements Method {
}
public MethodAccount getAccount(String name) {
return new iCoAccount(this.iConomy.getBank().getAccount(name));
return new iCoAccount(com.nijiko.coelho.iConomy.iConomy.getBank().getAccount(name));
}
public MethodBankAccount getBankAccount(String bank, String name) {
@ -58,7 +62,9 @@ public class iCo4 implements Method {
}
public boolean isCompatible(Plugin plugin) {
return plugin.getDescription().getName().equalsIgnoreCase("iconomy") && plugin.getClass().getName().equals("com.nijiko.coelho.iConomy.iConomy") && plugin instanceof iConomy;
return plugin.getDescription().getName().equalsIgnoreCase("iconomy")
&& plugin.getClass().getName().equals("com.nijiko.coelho.iConomy.iConomy")
&& plugin instanceof iConomy;
}
public void setPlugin(Plugin plugin) {

View File

@ -1,12 +1,12 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import com.iConomy.iConomy;
import com.iConomy.system.Account;
import com.iConomy.system.BankAccount;
import com.iConomy.system.Holdings;
import com.iConomy.util.Constants;
import com.garbagemule.register.payment.Method;
import org.bukkit.plugin.Plugin;
@ -32,8 +32,12 @@ public class iCo5 implements Method {
return "5";
}
public int fractionalDigits() {
return 2;
}
public String format(double amount) {
return this.iConomy.format(amount);
return com.iConomy.iConomy.format(amount);
}
public boolean hasBanks() {
@ -41,27 +45,29 @@ public class iCo5 implements Method {
}
public boolean hasBank(String bank) {
return (hasBanks()) && this.iConomy.Banks.exists(bank);
return (hasBanks()) && com.iConomy.iConomy.Banks.exists(bank);
}
public boolean hasAccount(String name) {
return this.iConomy.hasAccount(name);
return com.iConomy.iConomy.hasAccount(name);
}
public boolean hasBankAccount(String bank, String name) {
return (hasBank(bank)) && this.iConomy.getBank(bank).hasAccount(name);
return (hasBank(bank)) && com.iConomy.iConomy.getBank(bank).hasAccount(name);
}
public MethodAccount getAccount(String name) {
return new iCoAccount(this.iConomy.getAccount(name));
return new iCoAccount(com.iConomy.iConomy.getAccount(name));
}
public MethodBankAccount getBankAccount(String bank, String name) {
return new iCoBankAccount(this.iConomy.getBank(bank).getAccount(name));
return new iCoBankAccount(com.iConomy.iConomy.getBank(bank).getAccount(name));
}
public boolean isCompatible(Plugin plugin) {
return plugin.getDescription().getName().equalsIgnoreCase("iconomy") && plugin.getClass().getName().equals("com.iConomy.iConomy") && plugin instanceof iConomy;
return plugin.getDescription().getName().equalsIgnoreCase("iconomy")
&& plugin.getClass().getName().equals("com.iConomy.iConomy")
&& plugin instanceof iConomy;
}
public void setPlugin(Plugin plugin) {

View File

@ -1,11 +1,11 @@
package com.garbagemule.register.payment.methods;
import com.garbagemule.register.payment.Method;
import com.iCo6.iConomy;
import com.iCo6.system.Account;
import com.iCo6.system.Accounts;
import com.iCo6.system.Holdings;
import com.garbagemule.register.payment.Method;
import org.bukkit.plugin.Plugin;
@ -31,8 +31,12 @@ public class iCo6 implements Method {
return "6";
}
public int fractionalDigits() {
return 2;
}
public String format(double amount) {
return this.iConomy.format(amount);
return com.iCo6.iConomy.format(amount);
}
public boolean hasBanks() {
@ -60,10 +64,9 @@ public class iCo6 implements Method {
}
public boolean isCompatible(Plugin plugin) {
try { Class.forName("com.iCo6.IO"); }
catch(Exception e) { return false; }
return plugin.getDescription().getName().equalsIgnoreCase("iconomy") && plugin.getClass().getName().equals("com.iCo6.iConomy") && plugin instanceof iConomy;
return plugin.getDescription().getName().equalsIgnoreCase("iconomy")
&& plugin.getClass().getName().equals("com.iCo6.iConomy")
&& plugin instanceof iConomy;
}
public void setPlugin(Plugin plugin) {