/
EventManager.java
291 lines (207 loc) · 9.69 KB
/
EventManager.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
package net.aufdemrand.denizen.events;
import net.aufdemrand.denizen.events.bukkit.ScriptReloadEvent;
import net.aufdemrand.denizen.events.core.EntitySpawnSmartEvent;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizen.scripts.containers.core.WorldScriptContainer;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.SimplePluginManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class EventManager implements Listener {
public EventManager() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
///////////////////
// MAPS
////////////
// Map for keeping the WorldScriptContainers
public static Map<String, WorldScriptContainer> world_scripts =
new ConcurrentHashMap<String, WorldScriptContainer>(8, 0.9f, 1);
// Map for keeping the names of events
public static Map<String, List<WorldScriptContainer>> events =
new HashMap<String, List<WorldScriptContainer>>();
// Map for keeping track of registered smart_events
public static Set<SmartEvent> smart_events = new HashSet<SmartEvent>();
//////////////////
// PERFORMANCE
///////////
@EventHandler
public void scanWorldEvents(ScriptReloadEvent event) {
// Build a Map of scripts keyed by 'world events name'.
// Loop through each world script
for (WorldScriptContainer script : world_scripts.values()) {
if (script == null) continue;
// ...and through each event inside the script.
if (script.contains("EVENTS"))
for (String eventName : script.getConfigurationSection("EVENTS").getKeys(false)) {
List<WorldScriptContainer> list;
if (events.containsKey(eventName))
list = events.get(eventName);
else
list = new ArrayList<WorldScriptContainer>();
list.add(script);
events.put(eventName, list);
}
}
// dB.echoApproval("Built events map: " + events);
// Breakdown all SmartEvents (if still being used, they will reinitialize next)
for (SmartEvent smartEvent : smart_events)
smartEvent.breakDown();
// Pass these along to each SmartEvent so they can determine whether they can be enabled or not
for (SmartEvent smartEvent : smart_events) {
// If it should initialize, run _initialize!
if (smartEvent.shouldInitialize(events.keySet()))
smartEvent._initialize();
}
// Register any custom commands found with Bukkit
// TODO:
}
private Plugin plugin = DenizenAPI.getCurrentInstance();
public void registerCommand(String... aliases) {
PluginCommand command = getCommand(aliases[0], plugin);
command.setAliases(Arrays.asList(aliases));
getCommandMap().register(plugin.getDescription().getName(), command);
}
private static PluginCommand getCommand(String name, Plugin plugin) {
PluginCommand command = null;
try {
Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
c.setAccessible(true);
command = c.newInstance(name, plugin);
} catch (Exception e) {
e.printStackTrace();
}
return command;
}
private static CommandMap getCommandMap() {
CommandMap commandMap = null;
try {
if (Bukkit.getPluginManager() instanceof SimplePluginManager) {
Field f = SimplePluginManager.class.getDeclaredField("commandMap");
f.setAccessible(true);
commandMap = (CommandMap) f.get(Bukkit.getPluginManager());
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return commandMap;
}
public static List<String> trimEvents(List<String> event) {
List<String> parsed = new ArrayList<String>();
if (dB.showEventsTrimming) dB.echoApproval("Trimming world events '" + event.toString() + '\'');
for (String e : event)
if (events.containsKey("ON " + e.toUpperCase()))
parsed.add(e);
return parsed;
}
///////////////////
// COMPATIBILITY
//////////////
public static String doEvents(List<String> eventNames, dNPC npc, Player player, Map<String, dObject> context,
boolean usesIdentifiers) {
// If a list of events uses identifiers, also add those events to the list
// with their identifiers stripped
return doEvents(usesIdentifiers ? addAlternates(eventNames)
: eventNames,
npc, player, context);
}
public static List<String> addAlternates(List<String> events) {
// Strip object identifiers from world event names and add the results
// to the original list of world event names without any duplicates
Set<String> newEvents = new HashSet<String>();
for (String event : events) {
if (event.matches(".*?[a-z]{1,2}@[\\w ]+")) {
newEvents.add(event.replaceAll("[a-z]{1,2}@", ""));
}
}
List<String> finalEvents = new ArrayList<String>();
finalEvents.addAll(events);
finalEvents.addAll(newEvents);
return finalEvents;
}
///////////////////
// MECHANICS
///////////////
public static String doEvents(List<String> eventNames, dNPC npc, Player player, Map<String, dObject> context) {
String determination = "none";
// Trim again to catch events that don't trim internally.
eventNames = trimEvents(eventNames);
for (String eventName : eventNames) {
if (events.containsKey("ON " + eventName.toUpperCase()))
for (WorldScriptContainer script : events.get("ON " + eventName.toUpperCase())) {
if (script == null) continue;
// Fetch script from Event
//
// Note: a "new dPlayer(null)" will not be null itself,
// so keep a ternary operator here
List<ScriptEntry> entries = script.getEntries
(player != null ? new dPlayer(player) : null,
npc, "events.on " + eventName);
if (entries.isEmpty()) continue;
dB.report(script, "Event",
aH.debugObj("Type", "on " + eventName)
+ script.getAsScriptArg().debug()
+ (npc != null ? aH.debugObj("NPC", npc.toString()) : "")
+ (player != null ? aH.debugObj("Player", player.getName()) : "")
+ (context != null ? aH.debugObj("Context", context.toString()) : ""));
dB.echoDebug(script, dB.DebugElement.Header, "Building event 'ON " + eventName.toUpperCase()
+ "' for " + script.getName());
// Create new ID -- this is what we will look for when determining an outcome
long id = DetermineCommand.getNewId();
// Add the reqId to each of the entries for the determine command
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
// Add entries and context to the queue
ScriptQueue queue = InstantQueue.getQueue(null).addEntries(entries);
if (context != null) {
for (Map.Entry<String, dObject> entry : context.entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
}
// Start the queue!
queue.start();
// Check the determination
if (DetermineCommand.hasOutcome(id))
determination = DetermineCommand.getOutcome(id);
}
}
return determination;
}
////////////////////
// REGISTRATION
//////////////
public void registerCoreMembers() {
// Register all the 'Core' SmartEvents. This is called by Denizen's onEnable().
registerSmartEvent(new EntitySpawnSmartEvent());
}
public static void registerSmartEvent(SmartEvent event) {
// Seems simple enough
if (event != null)
smart_events.add(event);
}
}