-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
AbstractFlagTracker.java
175 lines (155 loc) · 7.12 KB
/
AbstractFlagTracker.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
package com.denizenscript.denizencore.flags;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.SlowWarning;
import com.denizenscript.denizencore.utilities.debugging.Warning;
import java.util.Collection;
public abstract class AbstractFlagTracker {
public abstract ObjectTag getFlagValue(String key);
public abstract TimeTag getFlagExpirationTime(String key);
public abstract Collection<String> listAllFlags();
public abstract void setFlag(String key, ObjectTag value, TimeTag expiration);
public boolean hasFlag(String key) {
return getFlagValue(key) != null;
}
public static <T extends FlaggableObject> void registerFlagHandlers(ObjectTagProcessor<T> processor) {
// <--[tag]
// @attribute <FlaggableObject.flag[<flag_name>]>
// @returns ObjectTag
// @description
// Returns the specified flag from the flaggable object.
// If the flag is expired, will return null.
// Consider also using <@link tag FlaggableObject.has_flag>.
// See <@link language flag system>.
// -->
processor.registerTag("flag", (attribute, object) -> {
AbstractFlagTracker tracker = object.getFlagTracker();
if (tracker == null) {
attribute.echoError("Cannot read flag tag for '" + object + "': " + object.getReasonNotFlaggable());
return null;
}
return tracker.doFlagTag(attribute);
});
// <--[tag]
// @attribute <FlaggableObject.has_flag[<flag_name>]>
// @returns ElementTag(Boolean)
// @description
// Returns true if the flaggable object has the specified flag, otherwise returns false.
// See <@link language flag system>.
// -->
processor.registerTag("has_flag", (attribute, object) -> {
AbstractFlagTracker tracker = object.getFlagTracker();
if (tracker == null) {
attribute.echoError("Cannot read has_flag tag for '" + object + "': " + object.getReasonNotFlaggable());
return null;
}
return tracker.doHasFlagTag(attribute);
});
// <--[tag]
// @attribute <FlaggableObject.flag_expiration[<flag_name>]>
// @returns TimeTag
// @description
// Returns a TimeTag indicating when the specified flag will expire.
// See <@link language flag system>.
// -->
processor.registerTag("flag_expiration", (attribute, object) -> {
AbstractFlagTracker tracker = object.getFlagTracker();
if (tracker == null) {
attribute.echoError("Cannot read flag_expiration tag for '" + object + "': " + object.getReasonNotFlaggable());
return null;
}
return tracker.doFlagExpirationTag(attribute);
});
// <--[tag]
// @attribute <FlaggableObject.list_flags>
// @returns ListTag
// @description
// Returns a list of the flaggable object's flags.
// Note that this is exclusively for debug/testing reasons, and should never be used in a real script.
// See <@link language flag system>.
// -->
processor.registerTag("list_flags", (attribute, object) -> {
AbstractFlagTracker tracker = object.getFlagTracker();
if (tracker == null) {
attribute.echoError("Cannot read list_flags tag for '" + object + "': " + object.getReasonNotFlaggable());
return null;
}
return tracker.doListFlagsTag(attribute);
});
// <--[tag]
// @attribute <FlaggableObject.flag_map>
// @returns MapTag
// @description
// Returns a raw map of the objects internal flag data.
// Note that this is exclusively for debug/testing reasons, and should never be used in a real script.
// See <@link language flag system>.
// -->
processor.registerTag("flag_map", (attribute, object) -> {
AbstractFlagTracker tracker = object.getFlagTracker();
if (tracker == null) {
attribute.echoError("Cannot read flag_map tag for '" + object + "': " + object.getReasonNotFlaggable());
return null;
}
return tracker.doFlagMapTag(attribute);
});
}
public ElementTag doHasFlagTag(Attribute attribute) {
if (!attribute.hasContext(1)) {
attribute.echoError("The has_flag[...] tag must have an input!");
return null;
}
return new ElementTag(hasFlag(attribute.getContext(1)));
}
public ObjectTag doFlagTag(Attribute attribute) {
if (!attribute.hasContext(1)) {
attribute.echoError("The flag[...] tag must have an input!");
return null;
}
if (attribute.getAttributeWithoutContext(2).equals("is_expired")) {
Deprecations.flagIsExpiredTag.warn(attribute.context);
boolean result = !hasFlag(attribute.getContext(1));
attribute.fulfill(1);
return new ElementTag(result);
}
else if (attribute.getAttributeWithoutContext(2).equals("expiration")) {
Deprecations.flagExpirationTag.warn(attribute.context);
TimeTag time = getFlagExpirationTime(attribute.getContext(1));
if (time == null) {
return null;
}
attribute.fulfill(1);
return new DurationTag((time.millis() - TimeTag.now().millis()) / 1000.0);
}
return getFlagValue(attribute.getContext(1));
}
public TimeTag doFlagExpirationTag(Attribute attribute) {
if (!attribute.hasContext(1)) {
attribute.echoError("The flag_expiration[...] tag must have an input!");
return null;
}
return getFlagExpirationTime(attribute.getContext(1));
}
public static Warning listFlagsTagWarning = new SlowWarning("The list_flags and flag_map tags are meant for testing/debugging only. Do not use it in scripts (ignore this warning if using for testing reasons).");
public ListTag doListFlagsTag(Attribute attribute) {
listFlagsTagWarning.warn(attribute.context);
ListTag list = new ListTag();
list.addAll(listAllFlags());
return list;
}
public MapTag doFlagMapTag(Attribute attribute) {
listFlagsTagWarning.warn(attribute.context);
if (this instanceof MapTagFlagTracker) {
return ((MapTagFlagTracker) this).map;
}
else {
MapTag result = new MapTag();
for (String key : listAllFlags()) {
result.putObject(key, getFlagValue(key));
}
return result;
}
}
}