/
PluginInfo.java
316 lines (280 loc) · 9.08 KB
/
PluginInfo.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package net.ME1312.Galaxi.Plugin;
import net.ME1312.Galaxi.Galaxi;
import net.ME1312.Galaxi.Library.Config.YAMLSection;
import net.ME1312.Galaxi.Library.Config.YAMLValue;
import net.ME1312.Galaxi.Library.Exception.IllegalPluginException;
import net.ME1312.Galaxi.Library.ExtraDataHandler;
import net.ME1312.Galaxi.Library.Log.Logger;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.*;
/**
* Plugin Info Class
*
* @see Plugin
*/
public class PluginInfo implements ExtraDataHandler {
private static ArrayList<String> usedNames = new ArrayList<String>();
private static HashMap<Class<?>, PluginInfo> pluginMap = new HashMap<Class<?>, PluginInfo>();
private Object plugin;
private String name;
private String display;
private Version version;
private Version signature;
private List<String> authors;
private String desc;
private URL website;
private List<String> loadBefore;
private List<String> depend;
private List<String> softDepend;
private Logger logger = null;
private Runnable updateChecker = null;
private boolean enabled = false;
private YAMLSection extra = new YAMLSection();
public static PluginInfo getPluginInfo(Object main) throws InvocationTargetException {
Class<?> mainClass = main.getClass();
if (!pluginMap.keySet().contains(mainClass)) {
try {
String name = mainClass.getAnnotation(Plugin.class).name().replaceAll("<|>|:|\\*|\\||\\?|\"|/|\\\\|\\n", "-");
String display = mainClass.getAnnotation(Plugin.class).name();
Version version = Version.fromString(mainClass.getAnnotation(Plugin.class).version());
Version signature = (mainClass.getAnnotation(Plugin.class).signature().length() > 0)?Version.fromString(mainClass.getAnnotation(Plugin.class).signature()):null;
List<String> authors = Arrays.asList(mainClass.getAnnotation(Plugin.class).authors());
String description = (mainClass.getAnnotation(Plugin.class).description().length() > 0)?mainClass.getAnnotation(Plugin.class).description():null;
URL website = (mainClass.getAnnotation(Plugin.class).website().length() > 0)?new URL(mainClass.getAnnotation(Plugin.class).website()):null;
List<String> loadBefore = Arrays.asList(mainClass.getAnnotation(Plugin.class).loadBefore());
List<String> dependancies = Arrays.asList(mainClass.getAnnotation(Plugin.class).dependencies());
List<String> softDependancies = Arrays.asList(mainClass.getAnnotation(Plugin.class).softDependencies());
PluginInfo plugin = new PluginInfo(main, name, version, authors, description, website, loadBefore, dependancies, softDependancies);
plugin.setDisplayName(display);
plugin.setSignature(signature);
pluginMap.put(mainClass, plugin);
usedNames.add(name.toLowerCase());
} catch (Throwable e) {
throw new IllegalPluginException(e, "Could not load plugin data from main class: " + mainClass.getCanonicalName());
}
}
return pluginMap.get(mainClass);
}
private PluginInfo(Object plugin, String name, Version version, List<String> authors, String description, URL website, List<String> loadBefore, List<String> dependencies, List<String> softDependencies) {
if (Util.isNull(plugin, name, version, authors)) throw new NullPointerException();
if (name.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty name");
if (version.toString().length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty version");
if (authors.size() == 0) throw new ArrayIndexOutOfBoundsException("Cannot use an empty authors list");
if (description != null && description.length() == 0) description = null;
this.plugin = plugin;
this.name = name;
this.version = version;
this.authors = authors;
this.desc = description;
this.website = website;
this.loadBefore = (loadBefore == null)?Collections.emptyList():loadBefore;
this.depend = (dependencies == null)?Collections.emptyList():dependencies;
this.softDepend = (softDependencies == null)?Collections.emptyList():softDependencies;
}
/**
* Get the Plugin's ClassLoader
*
* @return Plugin ClassLoader
*/
public ClassLoader getLoader() {
return plugin.getClass().getClassLoader();
}
/**
* Get Plugin Object
*
* @return Plugin Object
*/
public Object get() {
return plugin;
}
/**
* Get Plugin's Name
*
* @return Plugin Name
*/
public String getName() {
return this.name;
}
/**
* Get Plugin's Display Name
*
* @return Display Name
*/
public String getDisplayName() {
return (display == null)?getName():display;
}
/**
* Set Plugin's Display Name
*
* @param value Value (or null to reset)
*/
public void setDisplayName(String value) {
if (value == null || value.length() == 0 || getName().equals(value)) {
this.display = null;
} else {
this.display = value;
}
}
/**
* Get Plugin's Version
*
* @return Plugin Version
*/
public Version getVersion() {
return this.version;
}
/**
* Get Plugin's Build Signature
*
* @return Plugin Build Signature
*/
public Version getSignature() {
return this.signature;
}
/**
* Set Plugin's Build Signature (may only be done once)
*
* @param value Plugin Build Signature
*/
public void setSignature(Version value) {
if (signature == null) {
signature = value;
}
}
/**
* Get Authors List
*
* @return Authors List
*/
public List<String> getAuthors() {
return this.authors;
}
/**
* Get Plugin Description
*
* @return Plugin Description
*/
public String getDescription() {
return this.desc;
}
/**
* Get Authors' Website
*
* @return Authors' Website
*/
public URL getWebsite() {
return this.website;
}
/**
* Gets the Load Before Plugins List
*
* @return Load Before Plugins List
*/
public List<String> getLoadBefore() {
return this.loadBefore;
}
/**
* Gets the Dependencies List
*
* @return Dependencies List
*/
public List<String> getDependancies() {
return this.depend;
}
/**
* Gets the Soft Dependencies List
*
* @return Soft Dependencies List
*/
public List<String> getSoftDependancies() {
return this.softDepend;
}
/**
* Sets the Plugin's Enabled Status
*
* @return Enabled Status
*/
public boolean isEnabled() {
return enabled;
}
/**
* Sets if the Plugin is Enabled
*
* @param value Value
*/
public void setEnabled(boolean value) {
enabled = value;
}
/**
* Replace this Plugin's Logger with a custom one
*
* @param value Value
*/
public void setLogger(Logger value) {
if (Util.isNull(value)) throw new NullPointerException();
logger = value;
}
/**
* Gets the default Logger for this Plugin
*
* @return Logger
*/
public Logger getLogger() {
if (logger == null) logger = new Logger(name);
return logger;
}
/**
* Get the Update Checker for this Plugin
*
* @return Update Checker
*/
public Runnable getUpdateChecker() {
return updateChecker;
}
/**
* Set the Update Checker for this Plugin
*
* @param checker Value
*/
public void setUpdateChecker(Runnable checker) {
this.updateChecker = checker;
}
/**
* Gets this Plugin's data folder
*
* @return Data Folder
*/
public File getDataFolder() {
File dir = new File(Galaxi.getInstance().getRuntimeDirectory(), "Plugins" + File.separator + name);
if (!dir.exists()) dir.mkdirs();
return dir;
}
@Override
public void addExtra(String handle, Object value) {
if (Util.isNull(handle, value)) throw new NullPointerException();
extra.set(handle, value);
}
@Override
public boolean hasExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
return extra.getKeys().contains(handle);
}
@Override
public YAMLValue getExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
return extra.get(handle);
}
@Override
public YAMLSection getExtra() {
return extra.clone();
}
@Override
public void removeExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
extra.remove(handle);
}
}