-
Notifications
You must be signed in to change notification settings - Fork 17
/
ModuleLoader.java
386 lines (317 loc) · 11.8 KB
/
ModuleLoader.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.griefcraft.scripting;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCInfo;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.Module.Result;
import com.griefcraft.scripting.event.*;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import javax.naming.OperationNotSupportedException;
import java.util.*;
import java.util.logging.Logger;
public class ModuleLoader {
public enum Event {
/**
* Called when a module is loaded
*/
LOAD(0),
/**
* Called when a module is unloaded
*/
UNLOAD(0),
/**
* Called when a console or player issues a command
*/
COMMAND(3),
/**
* Called when redstone is passed to a protection
*/
REDSTONE(3),
/**
* Called when a protection is destroyed
*/
DESTROY_PROTECTION(5),
/**
* Called when a valid protection is left clicked
*/
INTERACT_PROTECTION(5),
/**
* Called when a block is left clicked
*/
INTERACT_BLOCK(3),
/**
* Called before a protection is registered
*/
REGISTER_PROTECTION(2),
/**
* Called when a protection needs to be checked if a player can access it
*/
ACCESS_PROTECTION(2),
/**
* Called when a protection needs to be checked if a player can admin it
*/
ADMIN_PROTECTION(2),
/**
* Called when a player drops an item
*/
DROP_ITEM(3),
/**
* Called after a protection is registered
*/
POST_REGISTRATION(1),
/**
* Called after a protection is confirmed to be set to be removed (cannot be cancelled here.)
*/
POST_REMOVAL(1),
/**
* Called when a localized message is sent to a player (e.g lwc.accessdenied)
*/
SEND_LOCALE(2),
// new / temp
ACCESS_REQUEST();
Event() { }
Event(int arguments) {
this.arguments = arguments;
}
public int getExpectedArguments() {
return arguments;
}
/**
* Expected amount of arguments (not counting the lwc object!)
*/
int arguments;
}
private static Logger logger = Logger.getLogger("Loader");
/**
* Path to the root of scripts
*/
public final static String ROOT_PATH = "plugins/LWC/";
/**
* Map of loaded modules
*/
// private Map<String, Map<String, MetaData>> packageModules = new HashMap<String, Map<String, MetaData>>();
private final Map<Plugin, List<MetaData>> pluginModules = new LinkedHashMap<Plugin, List<MetaData>>();
public ModuleLoader() {
// initialize the map with the LWC plugin
pluginModules.put(LWC.getInstance().getPlugin(), new ArrayList<MetaData>());
}
/**
* Dispatch an event
*
* @param event
*/
public void dispatchEvent(LWCEvent event) {
if(event == null) {
return;
}
try {
for(List<MetaData> modules : pluginModules.values()) {
for(MetaData metaData : modules) {
Module module = metaData.getModule();
if(event instanceof LWCAccessEvent) {
module.protectionAccessRequest((LWCAccessEvent) event);
} else if(event instanceof LWCBlockInteractEvent) {
module.onBlockInteract((LWCBlockInteractEvent) event);
} else if(event instanceof LWCCommandEvent) {
module.onCommand((LWCCommandEvent) event);
} else if(event instanceof LWCDropItemEvent) {
module.onDropItem((LWCDropItemEvent) event);
} else if(event instanceof LWCProtectionDestroyEvent) {
module.onDestroyProtection((LWCProtectionDestroyEvent) event);
} else if(event instanceof LWCProtectionInteractEvent) {
module.onProtectionInteract((LWCProtectionInteractEvent) event);
} else if(event instanceof LWCProtectionRegisterEvent) {
module.onRegisterProtection((LWCProtectionRegisterEvent) event);
} else if(event instanceof LWCProtectionRemovePostEvent) {
module.onPostRemoval((LWCProtectionRemovePostEvent) event);
} else if(event instanceof LWCProtectionRegistrationPostEvent) {
module.onPostRegistration((LWCProtectionRegistrationPostEvent) event);
} else if(event instanceof LWCSendLocaleEvent) {
module.onSendLocale((LWCSendLocaleEvent) event);
}
}
}
} catch (Throwable throwable) {
throw new ModuleException("LWC Module threw an uncaught exception! LWC version: " + LWCInfo.FULL_VERSION, throwable);
}
}
/**
* Dispatch an event
*
* @param event
* @param args
* @deprecated
*/
public Result dispatchEvent(Event event, Object... args) {
if (event.getExpectedArguments() > args.length) {
return Result.DEFAULT;
}
LWC lwc = LWC.getInstance();
Result result = Result.DEFAULT;
try {
for (List<MetaData> modules : pluginModules.values()) {
for (MetaData metaData : modules) {
Module module = metaData.getModule();
Result temp = Result.DEFAULT;
switch (event) {
case COMMAND:
temp = module.onCommand(lwc, (CommandSender) args[0], (String) args[1], (String[]) args[2]);
break;
case REDSTONE:
temp = module.onRedstone(lwc, (Protection) args[0], (Block) args[1], (Integer) args[2]);
break;
case DESTROY_PROTECTION:
temp = module.onDestroyProtection(lwc, (Player) args[0], (Protection) args[1], (Block) args[2], (Boolean) args[3], (Boolean) args[4]);
break;
case INTERACT_PROTECTION:
temp = module.onProtectionInteract(lwc, (Player) args[0], (Protection) args[1], (List<String>) args[2], (Boolean) args[3], (Boolean) args[4]);
break;
case INTERACT_BLOCK:
temp = module.onBlockInteract(lwc, (Player) args[0], (Block) args[1], (List<String>) args[2]);
break;
case REGISTER_PROTECTION:
temp = module.onRegisterProtection(lwc, (Player) args[0], (Block) args[1]);
break;
case ACCESS_PROTECTION:
temp = module.canAccessProtection(lwc, (Player) args[0], (Protection) args[1]);
break;
case ADMIN_PROTECTION:
temp = module.canAdminProtection(lwc, (Player) args[0], (Protection) args[1]);
break;
case DROP_ITEM:
temp = module.onDropItem(lwc, (Player) args[0], (Item) args[1], (ItemStack) args[2]);
break;
case POST_REGISTRATION:
module.onPostRegistration(lwc, (Protection) args[0]);
break;
case POST_REMOVAL:
module.onPostRemoval(lwc, (Protection) args[0]);
break;
case SEND_LOCALE:
temp = module.onSendLocale(lwc, (Player) args[0], (String) args[1]);
break;
case ACCESS_REQUEST:
throw new OperationNotSupportedException("ACCESS_REQUEST");
}
if (temp != Result.DEFAULT) {
result = temp;
}
if (result == Result.CANCEL) {
return result;
}
}
}
} catch (Throwable throwable) {
throw new ModuleException("LWC Module threw an uncaught exception! LWC version: " + LWCInfo.FULL_VERSION, throwable);
}
if (result == null) {
result = Result.DEFAULT;
}
return result;
}
/**
* Shutdown the plugin loader
*
* @todo broadcast UNLOAD
*/
public void shutdown() {
pluginModules.clear();
}
/**
* Load all of the modules not marked as loaded
*/
public void loadAll() {
LWC lwc = LWC.getInstance();
for (List<MetaData> modules : pluginModules.values()) {
for (MetaData metaData : modules) {
if (!metaData.isLoaded()) {
metaData.getModule().load(lwc);
metaData.trigger();
}
}
}
}
/**
* Get the first module represented by a class
*
* @param clazz
* @return
*/
public Module getModule(Class<? extends Module> clazz) {
for (List<MetaData> modules : pluginModules.values()) {
for (MetaData metaData : modules) {
Module module = metaData.getModule();
if (module.getClass() == clazz) {
return module;
}
}
}
return null;
}
/**
* Not intended to be used a lot -- use sparingly!
* The map returned is NOT modifiable.
*
* @return the registered modules
*/
public Map<Plugin, List<MetaData>> getRegisteredModules() {
return Collections.unmodifiableMap(pluginModules);
}
/**
* @return the number of registered modules
*/
public int getModuleCount() {
int count = 0;
for (List<MetaData> modules : pluginModules.values()) {
count += modules.size();
}
return count;
}
/**
* Register a module for a plugin
*
* @param plugin
* @param module
*/
public void registerModule(Plugin plugin, Module module) {
List<MetaData> modules = null;
if (plugin != null) {
modules = pluginModules.get(plugin);
}
if (modules == null) {
modules = new ArrayList<MetaData>();
}
MetaData metaData = new MetaData(module);
modules.add(metaData);
pluginModules.put(plugin, modules);
}
/**
* Remove the modules for a plugin
*
* @param plugin
*/
public void removeModules(Plugin plugin) {
pluginModules.remove(plugin);
}
}