/
BukkitElementProperties.java
364 lines (327 loc) · 15.5 KB
/
BukkitElementProperties.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package net.aufdemrand.denizen.objects.properties.bukkit;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptHelper;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.scripts.commands.core.Comparable;
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.ChatColor;
public class BukkitElementProperties implements Property {
public static boolean describes(dObject element) {
return element instanceof Element;
}
public static BukkitElementProperties getFrom(dObject element) {
if (!describes(element)) return null;
else return new BukkitElementProperties((Element) element);
}
private BukkitElementProperties(Element element) {
this.element = element;
}
Element element;
@Override
public String getAttribute(Attribute attribute) {
// <--[tag]
// @attribute <el@element.is[<operator>].to[<element>]>
// @returns Element(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag el@element.is[<operator>].than[<element>]>
// -->
// <--[tag]
// @attribute <el@element.is[<operator>].than[<element>]>
// @returns Element(Boolean)
// @group comparison
// @description
// Takes an operator, and compares the value of the element to the supplied
// element. Returns the outcome of the comparable, either true or false. For
// information on operators, see <@link language operator>.
// Equivalent to <@link tag el@element.is[<operator>].to[<element>]>
// -->
if (attribute.startsWith("is") && attribute.hasContext(1)
&& (attribute.startsWith("to", 2) || attribute.startsWith("than", 2)) && attribute.hasContext(2)) {
// Use the Comparable object as implemented for the IF command. First, a new Comparable!
Comparable com = new Comparable();
// Check for negative logic
String operator;
if (attribute.getContext(1).startsWith("!")) {
operator = attribute.getContext(1).substring(1);
com.setNegativeLogic();
}
else operator = attribute.getContext(1);
// Operator is the value of the .is[] context. Valid are Comparable.Operators, same
// as used by the IF command.
Comparable.Operator comparableOperator = null;
try {
comparableOperator = Comparable.Operator.valueOf(operator.replace("==", "EQUALS")
.replace(">=", "OR_MORE").replace("<=", "OR_LESS").replace("<", "LESS")
.replace(">", "MORE").replace("=", "EQUALS").toUpperCase());
}
catch (IllegalArgumentException e) {
}
if (comparableOperator != null) {
com.setOperator(comparableOperator);
// Comparable is the value of this element
com.setComparable(element.asString());
// Compared_to is the value of the .to[] context.
com.setComparedto(attribute.getContext(2));
return new Element(com.determineOutcome()).getAttribute(attribute.fulfill(2));
}
else {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError("Unknown operator '" + operator + "'.");
}
}
// <--[tag]
// @attribute <el@element.as_chunk>
// @returns dChunk
// @group conversion
// @description
// Returns the element as a chunk. Note: the value must be a valid chunk.
// -->
if (attribute.startsWith("aschunk")
|| attribute.startsWith("as_chunk")) {
dObject object = Element.handleNull(element.asString(), dChunk.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dChunk", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_color>
// @returns dColor
// @group conversion
// @description
// Returns the element as a dColor. Note: the value must be a valid color.
// -->
if (attribute.startsWith("ascolor")
|| attribute.startsWith("as_color")) {
dObject object = Element.handleNull(element.asString(), dColor.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dColor", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_cuboid>
// @returns dCuboid
// @group conversion
// @description
// Returns the element as a cuboid. Note: the value must be a valid cuboid.
// -->
if (attribute.startsWith("ascuboid")
|| attribute.startsWith("as_cuboid")) {
dObject object = Element.handleNull(element.asString(), dCuboid.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dCuboid", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_entity>
// @returns dEntity
// @group conversion
// @description
// Returns the element as an entity. Note: the value must be a valid entity.
// -->
if (attribute.startsWith("asentity")
|| attribute.startsWith("as_entity")) {
dObject object = Element.handleNull(element.asString(), dEntity.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dEntity", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_inventory>
// @returns dInventory
// @group conversion
// @description
// Returns the element as an inventory. Note: the value must be a valid inventory.
// -->
if (attribute.startsWith("asinventory")
|| attribute.startsWith("as_inventory")) {
dObject object = Element.handleNull(element.asString(), dInventory.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dInventory", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_item>
// @returns dItem
// @group conversion
// @description
// Returns the element as an item. Additional attributes can be accessed by dItem.
// Note: the value must be a valid item.
// -->
if (attribute.startsWith("asitem")
|| attribute.startsWith("as_item")) {
dObject object = Element.handleNull(element.asString(), dItem.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dItem", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_location>
// @returns dLocation
// @group conversion
// @description
// Returns the element as a location. Note: the value must be a valid location.
// -->
if (attribute.startsWith("aslocation")
|| attribute.startsWith("as_location")) {
dObject object = Element.handleNull(element.asString(), dLocation.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dLocation", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_material>
// @returns dMaterial
// @group conversion
// @description
// Returns the element as a material. Note: the value must be a valid material.
// -->
if (attribute.startsWith("asmaterial")
|| attribute.startsWith("as_material")) {
dObject object = Element.handleNull(element.asString(), dMaterial.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dMaterial", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_npc>
// @returns dNPC
// @group conversion
// @description
// Returns the element as an NPC. Note: the value must be a valid NPC.
// -->
if (attribute.startsWith("asnpc")
|| attribute.startsWith("as_npc")) {
dObject object = Element.handleNull(element.asString(), dNPC.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dNPC", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_player>
// @returns dPlayer
// @group conversion
// @description
// Returns the element as a player. Note: the value must be a valid player. Can be online or offline.
// -->
if (attribute.startsWith("asplayer")
|| attribute.startsWith("as_player")) {
dObject object = Element.handleNull(element.asString(), dPlayer.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dPlayer", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_world>
// @returns dWorld
// @group conversion
// @description
// Returns the element as a world.
// -->
if (attribute.startsWith("asworld")
|| attribute.startsWith("as_world")) {
dObject object = Element.handleNull(element.asString(), dWorld.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dWorld", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.as_plugin>
// @returns dPlugin
// @group conversion
// @description
// Returns the element as a plugin. Note: the value must be a valid plugin.
// -->
if (attribute.startsWith("asplugin")
|| attribute.startsWith("as_plugin")) {
dObject object = Element.handleNull(element.asString(), dPlugin.valueOf(element.asString(),
new BukkitTagContext(attribute.getScriptEntry(), false)), "dPlugin", attribute.hasAlternative());
if (object != null)
return object.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <el@element.debug.no_color>
// @returns Element
// @group debug
// @description
// Returns a standard debug representation of the Element with colors stripped.
// -->
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(element.debug()))
.getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <el@element.last_color>
// @returns Element
// @group string checking
// @description
// Returns the ChatColors used at the end of a string.
// -->
if (attribute.startsWith("last_color"))
return new Element(ChatColor.getLastColors(element.asString())).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <el@element.format[<script>]>
// @returns Element
// @group string manipulation
// @description
// Returns the text re-formatted according to a format script.
// See <@link example using format scripts>.
// -->
if (attribute.startsWith("format")
&& attribute.hasContext(1)) {
FormatScriptContainer format = ScriptRegistry.getScriptContainer(attribute.getContext(1));
if (format == null) {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError("Could not find format script matching '" + attribute.getContext(1) + "'");
return null;
}
else {
return new Element(format.getFormattedText(element.asString(),
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null,
attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null))
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <el@element.strip_color>
// @returns Element
// @group string manipulation
// @description
// Returns the element with all color encoding stripped.
// -->
if (attribute.startsWith("strip_color"))
return new Element(ChatColor.stripColor(element.asString())).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <el@element.to_itemscript_hash>
// @returns Element(Number)
// @group conversion
// @description
// Shortens the element down to an itemscript hash ID, made of invisible color codes.
// -->
if (attribute.startsWith("to_itemscript_hash")) {
return new Element(ItemScriptHelper.createItemScriptID(element.asString()))
.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
return null;
}
@Override
public String getPropertyId() {
return "BukkitElementProperties";
}
@Override
public void adjust(Mechanism mechanism) {
// None
}
}