-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
OldEventManager.java
210 lines (184 loc) · 8.4 KB
/
OldEventManager.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
package com.denizenscript.denizencore.events;
import com.denizenscript.denizencore.scripts.queues.ContextSource;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.core.WorldScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.*;
@Deprecated
public class OldEventManager {
///////////////////
// MAPS
////////////
// Map for keeping the WorldScriptContainers
public static Map<String, WorldScriptContainer> world_scripts = new HashMap<>();
// Map for keeping the names of events
public static Map<String, List<WorldScriptContainer>> events = new HashMap<>();
//////////////////
// PERFORMANCE
///////////
public static void scanWorldEvents() {
try {
// Build a Map of scripts keyed by 'world events name'.
// Loop through each world script
if (CoreConfiguration.debugLoadingInfo) {
Debug.log("Scanning " + world_scripts.size() + " world scripts...");
}
for (WorldScriptContainer script : world_scripts.values()) {
if (script == null) {
Debug.echoError("Null world script?!");
continue;
}
// ...and through each event inside the script.
if (script.contains("EVENTS", Map.class)) {
YamlConfiguration configSection = script.getConfigurationSection("EVENTS");
if (configSection == null) {
Debug.echoError("Script '" + script.getName() + "' has an invalid events block!");
break;
}
Set<StringHolder> keys = configSection.getKeys(false);
if (keys == null) {
Debug.echoError("Script '" + script.getName() + "' has an empty events block!");
break;
}
for (StringHolder eventName1 : keys) {
String eventName = eventName1.str.toUpperCase();
List<WorldScriptContainer> list;
if (events.containsKey(eventName)) {
list = events.get(eventName);
}
else {
list = new ArrayList<>();
}
list.add(script);
events.put(eventName, list);
}
}
else {
Debug.echoError("Script '" + script.getName() + "' does not have an events block!");
}
}
}
catch (Exception e) {
Debug.echoError(e);
}
}
public static List<String> trimEvents(List<String> original) {
List<String> event = new ArrayList<>(original);
List<String> parsed = new ArrayList<>();
if (Debug.showEventsTrimming) {
Debug.echoApproval("Trimming world events '" + event + '\'');
}
// Remove any duplicate event names
for (int i = 0; i < event.size(); i++) {
for (int x = 0; x < event.size(); x++) {
if (i != x && event.get(i).equalsIgnoreCase(event.get(x))) {
event.remove(i);
i--;
break;
}
}
}
// Create a new list, only containing events that have existing scripts
for (String e : event) {
if (events.containsKey("ON " + e.toUpperCase())) {
parsed.add(e);
}
}
return parsed;
}
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<>();
for (String event : events) {
// NOTE: The below code deletes [a-z]{1,2}\@
if (event.indexOf('@') != -1) {
StringBuilder sb = new StringBuilder();
int len = event.length();
char[] data = event.toCharArray();
for (int i = 0; i < len; i++) {
if (data[i] >= 'a' && data[i] <= 'z' && i + 1 < len) {
if (i + 2 < len && data[i + 2] == '@') {
i += 2;
continue;
}
if (data[i + 1] == '@') {
i++;
continue;
}
}
sb.append(data[i]);
}
newEvents.add(sb.toString());
}
}
List<String> finalEvents = new ArrayList<>();
finalEvents.addAll(events);
finalEvents.addAll(newEvents);
return finalEvents;
}
///////////////////
// MECHANICS
///////////////
public static List<String> doEvents(List<String> eventNames, ScriptEntryData data, Map<String, ObjectTag> context, boolean strip_ids) {
return doEvents(addAlternates(eventNames), data, context);
}
public static List<String> doEvents(List<String> eventNames, ScriptEntryData data, Map<String, ObjectTag> context) {
try {
List<String> determinations = new ArrayList<>();
// 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
List<ScriptEntry> entries = script.getEntries(data, "events.on " + eventName);
if (entries.isEmpty()) {
continue;
}
Debug.report(script, "Event", ArgumentHelper.debugObj("Type", "on " + eventName), script.getAsScriptArg(), data.toString(), (context != null ? ArgumentHelper.debugObj("Context", context.toString()) : ""));
Debug.echoDebug(script, Debug.DebugElement.Header, "Building event 'ON " + eventName.toUpperCase() + "' for " + script.getName());
// Add entries and context to the queue
ScriptQueue queue = new InstantQueue(script.getName());
queue.addEntries(entries);
if (context != null) {
OldEventContextSource oecs = new OldEventContextSource();
oecs.contexts = context;
queue.setContextSource(oecs);
}
// Start the queue!
queue.start();
// Check the determination
if (queue.determinations != null) {
determinations = queue.determinations;
}
}
}
}
return determinations;
}
catch (Exception e) {
Debug.echoError(e);
return new ArrayList<>();
}
}
@Deprecated
public static class OldEventContextSource implements ContextSource {
public Map<String, ObjectTag> contexts;
@Override
public ObjectTag getContext(String name) {
return contexts.get(name);
}
}
}