summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/register/payment/Methods.java
blob: 3dc7a2c638684d06dd37a00a60a880c773834eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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;


/**
 * The <code>Methods</code> initializes Methods that utilize the Method interface
 * based on a "first come, first served" basis.
 *
 * 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)
 * @copyright: Copyright (C) 2011
 * @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>();

	/**
	 * 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;
		}
	}

	/**
	 * 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());
	}

	/**
	 * 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)
	 */
	public Set<String> getDependencies()
	{
		return Dependencies;
	}

	/**
	 * 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)
	{
		for (Method method : Methods)
		{
			if (method.isCompatible(plugin))
			{
				method.setPlugin(plugin);
				return method;
			}
		}

		return null;
	}

	private void addMethod(String name, Method method)
	{
		Dependencies.add(name);
		Methods.add(method);
	}

	/**
	 * 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)
	 */
	public boolean hasMethod()
	{
		return (Method != null);
	}

	/**
	 * 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.
	 */
	public boolean setMethod(Plugin method)
	{
		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;
			}

			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())
		{
			do
			{
				if (hasMethod())
				{
					match = true;
				}
				else
				{
					for (Method attached : this.Attachables)
					{
						if (attached == null)
						{
							continue;
						}

						if (hasMethod())
						{
							match = true;
							break;
						}

						if (this.preferred.isEmpty())
						{
							this.Method = attached;
						}

						if (count == 0)
						{
							if (this.preferred.equalsIgnoreCase(attached.getName()))
							{
								this.Method = attached;
							}
						}
						else
						{
							this.Method = attached;
						}
					}

					count++;
				}
			}
			while (!match);
		}

		return hasMethod();
	}

	/**
	 * Grab the existing and initialized (hopefully) Method Class.
	 *
	 * @return <code>Method</code> <em>or</em> <code>Null</code>
	 */
	public Method getMethod()
	{
		return Method;
	}

	/**
	 * 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>
	 */
	public boolean checkDisabled(Plugin method)
	{
		if (!hasMethod())
		{
			return true;
		}
		if (Method.isCompatible(method))
		{
			Method = null;
		}
		return (Method == null);
	}
}