-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CustomScriptEvent.java
144 lines (125 loc) · 4.67 KB
/
CustomScriptEvent.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
package com.denizenscript.denizencore.events.core;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.util.List;
public class CustomScriptEvent extends ScriptEvent {
// <--[event]
// @Events
// custom event
//
// @Switch id:<id> to only run the event if the given ID is used. This should almost always be specified.
// @Switch data:<key>:<value> to only run the event if the given data key matches the given value, using advanced matchers for the given object type (note: the 'customevent' command call has to be careful about object type for this).
//
// @Regex ^on custom event$
//
// @Group Core
//
// @Triggers when called by a script using <@link command customevent>.
//
// @Context
// <context.id> returns the ID that was used.
// <context.data> returns the MapTag of input data (if any! some events don't have context data).
// <context.(key)> returns the value of the input data key, if available.
//
// @Determine
// "OUTPUT:" + Anything, to add that value to the output list (note this is an ADD, not a SET).
//
// -->
public static CustomScriptEvent instance;
public ScriptEntryData entryData = null;
public String id;
public MapTag contextData;
public boolean anyMatched = false;
public ListTag determinations;
public CustomScriptEvent() {
instance = this;
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("custom event");
}
@Override
public boolean matches(ScriptPath path) {
if (!runGenericSwitchCheck(path, "id", id)) {
return false;
}
for (String switchText : path.rawSwitches) {
if (switchText.startsWith("data:")) {
String compare = switchText.substring("data:".length());
List<String> parts = CoreUtilities.split(compare, ':', 2);
if (parts.size() != 2) {
Debug.echoError("Invalid custom event switch (missing data key or value): " + switchText);
return false;
}
if (contextData == null) {
return false;
}
ObjectTag val = contextData.getDeepObject(parts.get(0));
if (val == null) {
return false;
}
if (!val.advancedMatches(parts.get(1))) {
return false;
}
}
}
anyMatched = true;
return true;
}
@Override
public ScriptEntryData getScriptEntryData() {
return entryData;
}
@Override
public String getName() {
return "CustomEvent";
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "id": return new ElementTag(id, true);
case "data": return contextData;
}
if (contextData != null) {
ObjectTag val = contextData.getObject(name);
if (val != null) {
return val;
}
}
return super.getContext(name);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determination) {
if (determination instanceof ElementTag && CoreUtilities.toLowerCase(determination.toString()).startsWith("output:")) {
determinations.add(determination.toString().substring("output:".length()));
return true;
}
return super.applyDetermination(path, determination);
}
boolean enabled = false;
@Override
public void init() {
enabled = true;
}
@Override
public void destroy() {
enabled = false;
}
public static CustomScriptEvent runCustomEvent(ScriptEntryData data, String id, MapTag contextData) {
if (!instance.enabled) {
return null;
}
instance.cancelled = false;
instance.determinations = new ListTag();
instance.entryData = data;
instance.id = id;
instance.contextData = contextData;
return (CustomScriptEvent) instance.fire();
}
}