summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/economy/register/Methods.java
blob: eb3138473d58d42cfb157bab8ea655117b5687d3 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.earth2me.essentials.economy.register;

import java.util.HashSet;
import java.util.Set;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;


/**
 * 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.
 *
 * 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>
 *  economy:
 *      preferred: "iConomy"
 * </pre></blockquote>
 *
 * @author: Nijikokun <nijikokun@shortmail.com> (@nijikokun) @copyright: Copyright (C) 2011 @license: AOL license
 * <http://aol.nexua.org>
 */
public class Methods
{
	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>();

	static
	{
		_init();
	}

	/**
	 * Implement all methods along with their respective name & class.
	 */
	private static void _init()
	{
		addMethod("iConomy", new com.earth2me.essentials.economy.register.methods.iCo6());
		addMethod("iConomy", new com.earth2me.essentials.economy.register.methods.iCo5());
		addMethod("iConomy", new com.earth2me.essentials.economy.register.methods.iCo4());
		addMethod("BOSEconomy", new com.earth2me.essentials.economy.register.methods.BOSE6());
		addMethod("BOSEconomy", new com.earth2me.essentials.economy.register.methods.BOSE7());
		addMethod("Currency", new com.earth2me.essentials.economy.register.methods.MCUR());
		Dependencies.add("MultiCurrency");
		addMethod("Vault", new com.earth2me.essentials.economy.register.methods.VaultEco());
	}

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

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

		return null;
	}

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

	/**
	 * Checks Plugin Class against a multitude of checks to verify it's usability as a payment method.
	 *
	 * @param <code>PluginManager</code> the plugin manager for the server
	 * @return
	 * <code>boolean</code> True on success, False on failure.
	 */
	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;

		for (String name : getDependencies())
		{
			if (hasMethod())
			{
				break;
			}

			plugin = manager.getPlugin(name);
			if (plugin == null || !plugin.isEnabled())
			{
				continue;
			}

			Method current = createMethod(plugin);
			if (current == null)
			{
				continue;
			}

			if (preferred.isEmpty())
			{
				Method = current;
			}
			else
			{
				Attachables.add(current);
			}
		}

		if (!preferred.isEmpty())
		{
			do
			{
				if (hasMethod())
				{
					match = true;
				}
				else
				{
					for (Method attached : Attachables)
					{
						if (attached == null)
						{
							continue;
						}

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

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

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

					count++;
				}
			}
			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 static 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 static boolean checkDisabled(Plugin method)
	{
		if (!hasMethod())
		{
			return true;
		}

		if (Method.isCompatible(method))
		{
			Method = null;
		}

		return (Method == null);
	}
}