2011-06-14 15:07:44 +02:00
|
|
|
package com.nijikokun.register.payment.forChestShop;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-05-16 17:35:12 +02:00
|
|
|
/**
|
2011-08-13 12:08:34 +02:00
|
|
|
* The <code>Methods</code> initializes Methods that utilize the Method interface
|
|
|
|
* based on a "first come, first served" basis.
|
2011-05-16 17:35:12 +02:00
|
|
|
*
|
2011-08-13 12:08:34 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* <blockquote><pre>
|
|
|
|
* Methods methods = new Methods("iConomy");
|
|
|
|
* </pre></blockquote>
|
|
|
|
*
|
|
|
|
* @author: Nijikokun <nijikokun@shortmail.com> (@nijikokun)
|
2011-05-16 17:35:12 +02:00
|
|
|
* @copyright: Copyright (C) 2011
|
2011-08-13 12:08:34 +02:00
|
|
|
* @license: AOL license <http://aol.nexua.org>
|
2011-05-16 17:35:12 +02:00
|
|
|
*/
|
2011-05-15 18:16:25 +02:00
|
|
|
public class Methods {
|
2011-05-29 13:25:25 +02:00
|
|
|
private boolean self = false;
|
2011-05-15 18:16:25 +02:00
|
|
|
private Method Method = null;
|
2011-05-29 13:25:25 +02:00
|
|
|
private String preferred = "";
|
2011-08-13 12:08:34 +02:00
|
|
|
private final Set<Method> Methods = new HashSet<Method>();
|
|
|
|
private final Set<String> Dependencies = new HashSet<String>();
|
|
|
|
private final Set<Method> Attachables = new HashSet<Method>();
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* Initialize Method class
|
|
|
|
*/
|
2011-05-16 17:35:12 +02:00
|
|
|
public Methods() {
|
2011-05-29 13:25:25 +02:00
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-13 12:08:34 +02:00
|
|
|
* Initializes <code>Methods</code> class utilizing a "preferred" payment method check before
|
|
|
|
* returning the first method that was initialized.
|
2011-07-01 14:12:26 +02:00
|
|
|
*
|
2011-08-13 12:08:34 +02:00
|
|
|
* @param preferred Payment method that is most preferred for this setup.
|
2011-05-29 13:25:25 +02:00
|
|
|
*/
|
|
|
|
public Methods(String preferred) {
|
|
|
|
this._init();
|
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(this.Dependencies.contains(preferred)) {
|
2011-05-29 13:25:25 +02:00
|
|
|
this.preferred = preferred;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* Implement all methods along with their respective name & class.
|
|
|
|
*
|
|
|
|
* @see #Methods()
|
|
|
|
* @see #Methods(java.lang.String)
|
|
|
|
*/
|
2011-05-29 13:25:25 +02:00
|
|
|
private void _init() {
|
2011-08-13 12:08:34 +02:00
|
|
|
this.addMethod("iConomy", new com.nijikokun.register.payment.forChestShop.methods.iCo6());
|
2011-06-14 15:07:44 +02:00
|
|
|
this.addMethod("iConomy", new com.nijikokun.register.payment.forChestShop.methods.iCo5());
|
2011-08-13 12:08:34 +02:00
|
|
|
this.addMethod("iConomy", new com.nijikokun.register.payment.forChestShop.methods.iCo4());
|
|
|
|
this.addMethod("BOSEconomy", new com.nijikokun.register.payment.forChestShop.methods.BOSE6());
|
|
|
|
this.addMethod("BOSEconomy", new com.nijikokun.register.payment.forChestShop.methods.BOSE7());
|
2011-06-14 15:07:44 +02:00
|
|
|
this.addMethod("Essentials", new com.nijikokun.register.payment.forChestShop.methods.EE17());
|
2011-08-13 12:08:34 +02:00
|
|
|
this.addMethod("MultiCurrency", new com.nijikokun.register.payment.forChestShop.methods.MCUR());
|
2011-05-16 17:35:12 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* Returns an array of payment method names that have been loaded
|
|
|
|
* through the <code>_init</code> method.
|
|
|
|
*
|
|
|
|
* @return <code>Set<String></code> - Array of payment methods that are loaded.
|
|
|
|
* @see #setMethod(org.bukkit.plugin.Plugin)
|
|
|
|
*/
|
2011-05-16 17:35:12 +02:00
|
|
|
public Set<String> getDependencies() {
|
|
|
|
return Dependencies;
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* 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 <em>or</em> Null
|
|
|
|
*/
|
|
|
|
public Method createMethod(Plugin plugin) {
|
2011-07-01 14:12:26 +02:00
|
|
|
for (Method method: Methods) {
|
2011-05-16 17:35:12 +02:00
|
|
|
if (method.isCompatible(plugin)) {
|
|
|
|
method.setPlugin(plugin);
|
|
|
|
return method;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 17:35:12 +02:00
|
|
|
return null;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 17:35:12 +02:00
|
|
|
private void addMethod(String name, Method method) {
|
|
|
|
Dependencies.add(name);
|
|
|
|
Methods.add(method);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* Verifies if Register has set a payment method for usage yet.
|
|
|
|
*
|
|
|
|
* @return <code>boolean</code>
|
|
|
|
* @see #setMethod(org.bukkit.plugin.Plugin)
|
|
|
|
* @see #checkDisabled(org.bukkit.plugin.Plugin)
|
|
|
|
*/
|
2011-05-15 18:16:25 +02:00
|
|
|
public boolean hasMethod() {
|
2011-05-29 13:25:25 +02:00
|
|
|
return (Method != null);
|
2011-05-16 17:35:12 +02:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* 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 <code>boolean</code> True on success, False on failure.
|
|
|
|
*/
|
2011-05-16 17:35:12 +02:00
|
|
|
public boolean setMethod(Plugin method) {
|
2011-07-01 14:12:26 +02:00
|
|
|
if(hasMethod()) return true;
|
|
|
|
if(self) { self = false; return false; }
|
2011-05-16 17:35:12 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
int count = 0;
|
|
|
|
boolean match = false;
|
2011-07-15 21:45:26 +02:00
|
|
|
Plugin plugin;
|
2011-05-29 13:25:25 +02:00
|
|
|
PluginManager manager = method.getServer().getPluginManager();
|
2011-05-16 17:35:12 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
for(String name: this.Dependencies) {
|
2011-07-01 14:12:26 +02:00
|
|
|
if(hasMethod()) break;
|
|
|
|
if(method.getDescription().getName().equals(name)) plugin = method; else plugin = manager.getPlugin(name);
|
|
|
|
if(plugin == null) continue;
|
2011-05-16 17:35:12 +02:00
|
|
|
|
|
|
|
Method current = this.createMethod(plugin);
|
2011-07-01 14:12:26 +02:00
|
|
|
if(current == null) continue;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(this.preferred.isEmpty())
|
2011-05-29 13:25:25 +02:00
|
|
|
this.Method = current;
|
|
|
|
else {
|
|
|
|
this.Attachables.add(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(!this.preferred.isEmpty()) {
|
2011-05-29 13:25:25 +02:00
|
|
|
do {
|
2011-07-01 14:12:26 +02:00
|
|
|
if(hasMethod()) {
|
2011-05-29 13:25:25 +02:00
|
|
|
match = true;
|
|
|
|
} else {
|
2011-07-01 14:12:26 +02:00
|
|
|
for(Method attached: this.Attachables) {
|
|
|
|
if(attached == null) continue;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(hasMethod()) {
|
2011-05-29 13:25:25 +02:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(this.preferred.isEmpty()) this.Method = attached;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-01 14:12:26 +02:00
|
|
|
if(count == 0) {
|
|
|
|
if(this.preferred.equalsIgnoreCase(attached.getName()))
|
2011-05-29 13:25:25 +02:00
|
|
|
this.Method = attached;
|
|
|
|
} else {
|
|
|
|
this.Method = attached;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
2011-07-01 14:12:26 +02:00
|
|
|
} while(!match);
|
2011-05-16 17:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hasMethod();
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* Grab the existing and initialized (hopefully) Method Class.
|
|
|
|
*
|
|
|
|
* @return <code>Method</code> <em>or</em> <code>Null</code>
|
|
|
|
*/
|
2011-05-15 18:16:25 +02:00
|
|
|
public Method getMethod() {
|
|
|
|
return Method;
|
|
|
|
}
|
2011-05-16 17:35:12 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* 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 <code>boolean</code>
|
|
|
|
*/
|
2011-05-16 17:35:12 +02:00
|
|
|
public boolean checkDisabled(Plugin method) {
|
2011-07-01 14:12:26 +02:00
|
|
|
if(!hasMethod()) return true;
|
2011-05-16 17:35:12 +02:00
|
|
|
if (Method.isCompatible(method)) Method = null;
|
|
|
|
return (Method == null);
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|