-
Notifications
You must be signed in to change notification settings - Fork 17
/
ModuleLoader.java
310 lines (262 loc) · 9.48 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
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.scripting;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCInfo;
import com.griefcraft.scripting.event.LWCAccessEvent;
import com.griefcraft.scripting.event.LWCBlockInteractEvent;
import com.griefcraft.scripting.event.LWCCommandEvent;
import com.griefcraft.scripting.event.LWCDropItemEvent;
import com.griefcraft.scripting.event.LWCEvent;
import com.griefcraft.scripting.event.LWCProtectionDestroyEvent;
import com.griefcraft.scripting.event.LWCProtectionInteractEvent;
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import com.griefcraft.scripting.event.LWCProtectionRegistrationPostEvent;
import com.griefcraft.scripting.event.LWCProtectionRemovePostEvent;
import com.griefcraft.scripting.event.LWCRedstoneEvent;
import com.griefcraft.scripting.event.LWCSendLocaleEvent;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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 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.onAccessRequest((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);
} else if (event instanceof LWCRedstoneEvent) {
module.onRedstone((LWCRedstoneEvent) event);
}
}
}
} catch (Throwable throwable) {
throw new ModuleException("LWC Module threw an uncaught exception! LWC version: " + LWCInfo.FULL_VERSION, throwable);
}
}
/**
* 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);
}
}