From 614b7b84f779a725fdebb64618764cab01a53844 Mon Sep 17 00:00:00 2001 From: KHobbits Date: Mon, 8 Aug 2011 10:17:13 +0100 Subject: [PATCH] Adding new register methods. --- Essentials/nbproject/project.properties | 4 + .../essentials/register/payment/Methods.java | 338 ++++++++++++------ .../register/payment/methods/MCUR.java | 120 +++++++ .../register/payment/methods/iCo6.java | 142 ++++++++ 4 files changed, 502 insertions(+), 102 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/register/payment/methods/MCUR.java create mode 100644 Essentials/src/com/earth2me/essentials/register/payment/methods/iCo6.java diff --git a/Essentials/nbproject/project.properties b/Essentials/nbproject/project.properties index f6fb2770f..8df8ef4d2 100644 --- a/Essentials/nbproject/project.properties +++ b/Essentials/nbproject/project.properties @@ -65,7 +65,9 @@ file.reference.BOSEconomy7.jar=../lib/BOSEconomy7.jar file.reference.craftbukkit-0.0.1-SNAPSHOT.jar=..\\lib\\craftbukkit-0.0.1-SNAPSHOT.jar file.reference.iCo4.jar=../lib/iCo4.jar file.reference.iCo5.jar=../lib/iCo5.jar +file.reference.iCo6.jar=../lib/iCo6.jar file.reference.junit-4.5.jar=..\\lib\\junit_4\\junit-4.5.jar +file.reference.MultiCurrency.jar=../lib/MultiCurrency.jar file.reference.Permissions3.jar=../lib/Permissions3.jar file.reference.PermissionsEx.jar=../lib/PermissionsEx.jar includes=** @@ -77,6 +79,8 @@ javac.classpath=\ ${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar}:\ ${file.reference.iCo4.jar}:\ ${file.reference.iCo5.jar}:\ + ${file.reference.iCo6.jar}:\ + ${file.reference.MultiCurrency.jar}:\ ${file.reference.BOSEconomy7.jar}:\ ${file.reference.PermissionsEx.jar} # Space-separated list of extra javac options diff --git a/Essentials/src/com/earth2me/essentials/register/payment/Methods.java b/Essentials/src/com/earth2me/essentials/register/payment/Methods.java index 0ebbcfb84..3dc7a2c63 100644 --- a/Essentials/src/com/earth2me/essentials/register/payment/Methods.java +++ b/Essentials/src/com/earth2me/essentials/register/payment/Methods.java @@ -1,137 +1,271 @@ package com.earth2me.essentials.register.payment; +import java.util.HashSet; +import java.util.Set; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import java.util.HashSet; import java.util.Set; + /** - * Methods.java - * Controls the getting / setting of methods & the method of payment used. + * The Methods initializes Methods that utilize the Method interface + * based on a "first come, first served" basis. * - * @author: Nijikokun (@nijikokun) + * Allowing you to check whether a payment method exists or not. + * + *
+ *  Methods methods = new Methods();
+ * 
+ * + * Methods also allows you to set a preferred method of payment before it captures + * payment plugins in the initialization process. + * + *
+ *  Methods methods = new Methods("iConomy");
+ * 
+ * + * @author: Nijikokun (@nijikokun) * @copyright: Copyright (C) 2011 - * @license: GNUv3 Affero License + * @license: AOL license */ -public class Methods { - private boolean self = false; - private Method Method = null; - private String preferred = ""; - private Set Methods = new HashSet(); - private Set Dependencies = new HashSet(); - private Set Attachables = new HashSet(); +public class Methods +{ + private boolean self = false; + private Method Method = null; + private String preferred = ""; + private Set Methods = new HashSet(); + private Set Dependencies = new HashSet(); + private Set Attachables = new HashSet(); - public Methods() { - this._init(); - } + /** + * Initialize Method class + */ + public Methods() + { + this._init(); + } - /** - * Allows you to set which economy plugin is most preferred. - * - * @param preferred - preferred economy plugin - */ - public Methods(String preferred) { - this._init(); + /** + * Initializes Methods 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; - } - } + if (this.Dependencies.contains(preferred)) + { + this.preferred = preferred; + } + } - private void _init() { - this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo4()); - this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo5()); - this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE6()); - this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE7()); - } + /** + * Implement all methods along with their respective name & class. + * + * @see #Methods() + * @see #Methods(java.lang.String) + */ + private void _init() + { + this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo4()); + this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo5()); + this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo6()); + this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE6()); + this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE7()); + this.addMethod("MultiCurrency", new com.earth2me.essentials.register.payment.methods.MCUR()); + } - public Set getDependencies() { - return Dependencies; - } + /** + * Returns an array of payment method names that have been loaded + * through the _init method. + * + * @return Set - Array of payment methods that are loaded. + * @see #setMethod(org.bukkit.plugin.Plugin) + */ + public Set getDependencies() + { + return Dependencies; + } - public Method createMethod(Plugin plugin) { - for (Method method: Methods) { - if (method.isCompatible(plugin)) { - method.setPlugin(plugin); - return method; - } - } + /** + * Interprets Plugin class data to verify whether it is compatible with an existing payment + * method to use for payments and other various economic activity. + * + * @param plugin Plugin data from bukkit, Internal Class file. + * @return Method or Null + */ + public Method createMethod(Plugin plugin) + { + for (Method method : Methods) + { + if (method.isCompatible(plugin)) + { + method.setPlugin(plugin); + return method; + } + } - return null; - } + return null; + } - private void addMethod(String name, Method method) { - Dependencies.add(name); - Methods.add(method); - } + private void addMethod(String name, Method method) + { + Dependencies.add(name); + Methods.add(method); + } - public boolean hasMethod() { - return (Method != null); - } + /** + * Verifies if Register has set a payment method for usage yet. + * + * @return boolean + * @see #setMethod(org.bukkit.plugin.Plugin) + * @see #checkDisabled(org.bukkit.plugin.Plugin) + */ + public boolean hasMethod() + { + return (Method != null); + } - public boolean setMethod(Plugin method) { - if(hasMethod()) return true; - if(self) { self = false; return false; } + /** + * 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. + * @return boolean True on success, False on failure. + */ + public boolean setMethod(Plugin method) + { + if (hasMethod()) + { + return true; + } + if (self) + { + self = false; + return false; + } - int count = 0; - boolean match = false; - Plugin plugin; - PluginManager manager = method.getServer().getPluginManager(); + 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 : this.getDependencies()) + { + if (hasMethod()) + { + break; + } + if (method.getDescription().getName().equals(name)) + { + plugin = method; + } + else + { + plugin = manager.getPlugin(name); + } + if (plugin == null) + { + continue; + } - Method current = this.createMethod(plugin); - if(current == null) continue; + Method current = this.createMethod(plugin); + if (current == null) + { + continue; + } - if(this.preferred.isEmpty()) - this.Method = current; - else { - this.Attachables.add(current); - } - } + if (this.preferred.isEmpty()) + { + this.Method = current; + } + else + { + this.Attachables.add(current); + } + } - if(!this.preferred.isEmpty()) { - do { - if(hasMethod()) { - match = true; - } else { - for(Method attached: this.Attachables) { - if(attached == null) continue; + if (!this.preferred.isEmpty()) + { + do + { + if (hasMethod()) + { + match = true; + } + else + { + for (Method attached : this.Attachables) + { + if (attached == null) + { + continue; + } - if(hasMethod()) { - match = true; - break; - } + if (hasMethod()) + { + match = true; + break; + } - if(this.preferred.isEmpty()) this.Method = attached; + if (this.preferred.isEmpty()) + { + this.Method = attached; + } - if(count == 0) { - if(this.preferred.equalsIgnoreCase(attached.getName())) - this.Method = attached; - } else { - this.Method = attached; - } - } + if (count == 0) + { + if (this.preferred.equalsIgnoreCase(attached.getName())) + { + this.Method = attached; + } + } + else + { + this.Method = attached; + } + } - count++; - } - } while(!match); - } + count++; + } + } + while (!match); + } - return hasMethod(); - } + return hasMethod(); + } - public Method getMethod() { - return Method; - } + /** + * Grab the existing and initialized (hopefully) Method Class. + * + * @return Method or Null + */ + public Method getMethod() + { + return Method; + } - public boolean checkDisabled(Plugin method) { - if(!hasMethod()) return true; - if (Method.isCompatible(method)) Method = null; - return (Method == null); - } + /** + * Verify is a plugin is disabled, only does this if we there is an existing payment + * method initialized in Register. + * + * @param method Plugin data from bukkit, Internal Class file. + * @return boolean + */ + public boolean checkDisabled(Plugin method) + { + if (!hasMethod()) + { + return true; + } + if (Method.isCompatible(method)) + { + Method = null; + } + return (Method == null); + } } diff --git a/Essentials/src/com/earth2me/essentials/register/payment/methods/MCUR.java b/Essentials/src/com/earth2me/essentials/register/payment/methods/MCUR.java new file mode 100644 index 000000000..8ea709c19 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/register/payment/methods/MCUR.java @@ -0,0 +1,120 @@ +package com.earth2me.essentials.register.payment.methods; + +import com.earth2me.essentials.register.payment.Method; + +import me.ashtheking.currency.Currency; +import me.ashtheking.currency.CurrencyList; + +import org.bukkit.plugin.Plugin; + +/** + * MultiCurrency Method implementation. + * + * @author Acrobot + * @copyright (c) 2011 + * @license AOL license + */ +public class MCUR implements Method { + private Currency currencyList; + + public Object getPlugin() { + return this.currencyList; + } + + public String getName() { + return "MultiCurrency"; + } + + public String getVersion() { + return "0.09"; + } + + public String format(double amount) { + return amount + " Currency"; + } + + public boolean hasBanks() { + return false; + } + + public boolean hasBank(String bank) { + return false; + } + + public boolean hasAccount(String name) { + return true; + } + + public boolean hasBankAccount(String bank, String name) { + return false; + } + + public MethodAccount getAccount(String name) { + return new MCurrencyAccount(name); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return null; + } + + public boolean isCompatible(Plugin plugin) { + return plugin.getDescription().getName().equalsIgnoreCase(getName()) && plugin instanceof Currency; + } + + public void setPlugin(Plugin plugin) { + currencyList = (Currency) plugin; + } + + public class MCurrencyAccount implements MethodAccount{ + private String name; + + public MCurrencyAccount(String name) { + this.name = name; + } + + public double balance() { + return CurrencyList.getValue((String) CurrencyList.maxCurrency(name)[0], name); + } + + public boolean set(double amount) { + CurrencyList.setValue((String) CurrencyList.maxCurrency(name)[0], name, amount); + return true; + } + + public boolean add(double amount) { + return CurrencyList.add(name, amount); + } + + public boolean subtract(double amount) { + return CurrencyList.subtract(name, amount); + } + + public boolean multiply(double amount) { + return CurrencyList.multiply(name, amount); + } + + public boolean divide(double amount) { + return CurrencyList.divide(name, amount); + } + + public boolean hasEnough(double amount) { + return CurrencyList.hasEnough(name, amount); + } + + public boolean hasOver(double amount) { + return CurrencyList.hasOver(name, amount); + } + + public boolean hasUnder(double amount) { + return CurrencyList.hasUnder(name, amount); + } + + public boolean isNegative() { + return CurrencyList.isNegative(name); + } + + public boolean remove() { + return CurrencyList.remove(name); + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/register/payment/methods/iCo6.java b/Essentials/src/com/earth2me/essentials/register/payment/methods/iCo6.java new file mode 100644 index 000000000..d890bc6df --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/register/payment/methods/iCo6.java @@ -0,0 +1,142 @@ +package com.earth2me.essentials.register.payment.methods; + +import com.iCo6.iConomy; +import com.iCo6.system.Account; +import com.iCo6.system.Accounts; +import com.iCo6.system.Holdings; + +import com.earth2me.essentials.register.payment.Method; + +import org.bukkit.plugin.Plugin; + +/** + * iConomy 6 Implementation of Method + * + * @author Nijikokun (@nijikokun) + * @copyright (c) 2011 + * @license AOL license + */ +public class iCo6 implements Method { + private iConomy iConomy; + + public iConomy getPlugin() { + return this.iConomy; + } + + public String getName() { + return "iConomy"; + } + + public String getVersion() { + return "6"; + } + + public String format(double amount) { + return this.iConomy.format(amount); + } + + public boolean hasBanks() { + return false; + } + + public boolean hasBank(String bank) { + return false; + } + + public boolean hasAccount(String name) { + return (new Accounts()).exists(name); + } + + public boolean hasBankAccount(String bank, String name) { + return false; + } + + public MethodAccount getAccount(String name) { + return new iCoAccount((new Accounts()).get(name)); + } + + public MethodBankAccount getBankAccount(String bank, String name) { + return null; + } + + 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; + } + + public void setPlugin(Plugin plugin) { + iConomy = (iConomy)plugin; + } + + public class iCoAccount implements MethodAccount { + private Account account; + private Holdings holdings; + + public iCoAccount(Account account) { + this.account = account; + this.holdings = account.getHoldings(); + } + + public Account getiCoAccount() { + return account; + } + + public double balance() { + return this.holdings.getBalance(); + } + + public boolean set(double amount) { + if(this.holdings == null) return false; + this.holdings.setBalance(amount); + return true; + } + + public boolean add(double amount) { + if(this.holdings == null) return false; + this.holdings.add(amount); + return true; + } + + public boolean subtract(double amount) { + if(this.holdings == null) return false; + this.holdings.subtract(amount); + return true; + } + + public boolean multiply(double amount) { + if(this.holdings == null) return false; + this.holdings.multiply(amount); + return true; + } + + public boolean divide(double amount) { + if(this.holdings == null) return false; + this.holdings.divide(amount); + return true; + } + + public boolean hasEnough(double amount) { + return this.holdings.hasEnough(amount); + } + + public boolean hasOver(double amount) { + return this.holdings.hasOver(amount); + } + + public boolean hasUnder(double amount) { + return this.holdings.hasUnder(amount); + } + + public boolean isNegative() { + return this.holdings.isNegative(); + } + + public boolean remove() { + if(this.account == null) return false; + this.account.remove(); + return true; + } + } +}