ChestShop-3/com/nijikokun/register/payment/forChestShop/Methods.java

141 lines
4.1 KiB
Java
Raw Normal View History

package com.nijikokun.register.payment.forChestShop;
import com.nijikokun.register.payment.forChestShop.methods.BOSE6;
import com.nijikokun.register.payment.forChestShop.methods.BOSE7;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import java.util.HashSet;
import java.util.Set;
2011-05-16 17:35:12 +02:00
/**
* Methods.java
* Controls the getting / setting of methods & the method of payment used.
*
* @author: Nijikokun<nijikokun@gmail.com> (@nijikokun)
* @copyright: Copyright (C) 2011
* @license: GNUv3 Affero License <http://www.gnu.org/licenses/agpl-3.0.html>
*/
public class Methods {
2011-05-29 13:25:25 +02:00
private boolean self = false;
private Method Method = null;
2011-05-29 13:25:25 +02:00
private String preferred = "";
2011-05-16 17:35:12 +02:00
private Set<Method> Methods = new HashSet<Method>();
private Set<String> Dependencies = new HashSet<String>();
2011-05-29 13:25:25 +02:00
private Set<Method> Attachables = new HashSet<Method>();
2011-05-16 17:35:12 +02:00
public Methods() {
2011-05-29 13:25:25 +02:00
this._init();
}
/**
* Allows you to set which economy plugin is most preferred.
*
* @param preferred - preferred economy plugin
2011-05-29 13:25:25 +02:00
*/
public Methods(String preferred) {
this._init();
if(this.Dependencies.contains(preferred)) {
2011-05-29 13:25:25 +02:00
this.preferred = preferred;
}
}
private void _init() {
this.addMethod("iConomy", new com.nijikokun.register.payment.forChestShop.methods.iCo4());
this.addMethod("iConomy", new com.nijikokun.register.payment.forChestShop.methods.iCo5());
this.addMethod("BOSEconomy", new BOSE6());
this.addMethod("BOSEconomy", new BOSE7());
this.addMethod("Essentials", new com.nijikokun.register.payment.forChestShop.methods.EE17());
2011-05-16 17:35:12 +02:00
}
2011-05-16 17:35:12 +02:00
public Set<String> getDependencies() {
return Dependencies;
}
Method createMethod(Plugin plugin) {
for (Method method: Methods) {
2011-05-16 17:35:12 +02:00
if (method.isCompatible(plugin)) {
method.setPlugin(plugin);
return method;
}
}
2011-05-16 17:35:12 +02:00
return null;
}
2011-05-16 17:35:12 +02:00
private void addMethod(String name, Method method) {
Dependencies.add(name);
Methods.add(method);
}
public boolean hasMethod() {
2011-05-29 13:25:25 +02:00
return (Method != null);
2011-05-16 17:35:12 +02:00
}
public boolean setMethod(Plugin method) {
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;
Plugin plugin;
2011-05-29 13:25:25 +02:00
PluginManager manager = method.getServer().getPluginManager();
2011-05-16 17:35:12 +02:00
for(String name: this.Dependencies) {
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);
if(current == null) continue;
2011-05-29 13:25:25 +02:00
if(this.preferred.isEmpty())
2011-05-29 13:25:25 +02:00
this.Method = current;
else {
this.Attachables.add(current);
}
}
if(!this.preferred.isEmpty()) {
2011-05-29 13:25:25 +02:00
do {
if(hasMethod()) {
2011-05-29 13:25:25 +02:00
match = true;
} else {
for(Method attached: this.Attachables) {
if(attached == null) continue;
2011-05-29 13:25:25 +02:00
if(hasMethod()) {
2011-05-29 13:25:25 +02:00
match = true;
break;
}
if(this.preferred.isEmpty()) this.Method = attached;
2011-05-29 13:25:25 +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++;
}
} while(!match);
2011-05-16 17:35:12 +02:00
}
return hasMethod();
}
public Method getMethod() {
return Method;
}
2011-05-16 17:35:12 +02:00
public boolean checkDisabled(Plugin method) {
if(!hasMethod()) return true;
2011-05-16 17:35:12 +02:00
if (Method.isCompatible(method)) Method = null;
return (Method == null);
}
}