/
SchematicCommand.java
502 lines (462 loc) · 21 KB
/
SchematicCommand.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.blocks.CuboidBlockSet;
import com.denizenscript.denizen.utilities.blocks.MCEditSchematicHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.interfaces.BlockData;
import com.denizenscript.denizen.objects.CuboidTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.tags.TagRunnable;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
public class SchematicCommand extends AbstractCommand implements Holdable, Listener {
// <--[command]
// @Name Schematic
// @Syntax schematic [create/load/unload/rotate/paste/save/flip_x/flip_y/flip_z] [name:<name>] (filename:<name>) (angle:<#>) (<location>) (<cuboid>) (delayed) (noair)
// @Group World
// @Required 2
// @Short Creates, loads, pastes, and saves schematics (Sets of blocks).
//
// @Description
// Creates, loads, pastes, and saves schematics. Schematics are files containing info about
// blocks and the order of those blocks.
//
// Denizen offers a number of tools to manipulate and work with schematics.
// Schematics can be rotated, flipped, pasted with no air, or pasted with a delay.
// The "noair" option skips air blocks in the pasted schematics- this means those air blocks will not replace
// any blocks in the target location.
// The "delayed" option delays how many blocks can be pasted at once. This is recommended for large schematics.
//
// @Tags
// <schematic[<name>].height>
// <schematic[<name>].length>
// <schematic[<name>].width>
// <schematic[<name>].block[<location>]>
// <schematic[<name>].origin>
// <schematic[<name>].blocks>
// <schematic[<name>].exists>
// <schematic[<name>].cuboid[<origin location>]>
// <schematic.list>
//
// @Usage
// Use to create a new schematic from a cuboid and an origin location
// - schematic create name:MySchematic cu@<player.location.sub[5,5,5]>|<player.location.add[5,5,5]> <player.location>
//
// @Usage
// Use to load a schematic
// - schematic load name:MySchematic
//
// @Usage
// Use to unload a schematic
// - schematic unload name:MySchematic
//
// @Usage
// Use to paste a loaded schematic with no air blocks
// - schematic paste name:MySchematic <player.location> noair
//
// @Usage
// Use to save a created schematic
// - schematic save name:MySchematic
// -->
@Override
public void onEnable() {
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
schematicTags(event);
}
}, "schematic", "schem");
schematics = new HashMap<>();
noPhys = false;
Bukkit.getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
public static boolean noPhys = false;
@EventHandler
public void onBlockPhysics(BlockPhysicsEvent event) {
if (noPhys) {
event.setCancelled(true);
}
}
private enum Type {CREATE, LOAD, UNLOAD, ROTATE, PASTE, SAVE, FLIP_X, FLIP_Y, FLIP_Z}
public static Map<String, CuboidBlockSet> schematics;
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", new ElementTag(arg.raw_value.toUpperCase()));
}
else if (!scriptEntry.hasObject("name")
&& arg.matchesPrefix("name")) {
scriptEntry.addObject("name", arg.asElement());
}
else if (!scriptEntry.hasObject("filename")
&& arg.matchesPrefix("filename")) {
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("angle")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("angle", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("cuboid")
&& arg.matchesArgumentType(CuboidTag.class)) {
scriptEntry.addObject("cuboid", arg.asType(CuboidTag.class));
}
else if (!scriptEntry.hasObject("delayed")
&& arg.matches("delayed")) {
scriptEntry.addObject("delayed", new ElementTag("true"));
}
else if (!scriptEntry.hasObject("noair")
&& arg.matches("noair")) {
scriptEntry.addObject("noair", new ElementTag("true"));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("type")) {
throw new InvalidArgumentsException("Missing type argument!");
}
if (!scriptEntry.hasObject("name")) {
throw new InvalidArgumentsException("Missing name argument!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
ElementTag angle = scriptEntry.getElement("angle");
ElementTag type = scriptEntry.getElement("type");
ElementTag name = scriptEntry.getElement("name");
ElementTag filename = scriptEntry.getElement("filename");
ElementTag noair = scriptEntry.getElement("noair");
ElementTag delayed = scriptEntry.getElement("delayed");
LocationTag location = scriptEntry.getObjectTag("location");
CuboidTag cuboid = scriptEntry.getObjectTag("cuboid");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), type.debug()
+ name.debug()
+ (location != null ? location.debug() : "")
+ (filename != null ? filename.debug() : "")
+ (cuboid != null ? cuboid.debug() : "")
+ (angle != null ? angle.debug() : "")
+ (noair != null ? noair.debug() : "")
+ (delayed != null ? delayed.debug() : ""));
}
CuboidBlockSet set;
Type ttype = Type.valueOf(type.asString());
if (scriptEntry.shouldWaitFor() && ttype != Type.PASTE) {
Debug.echoError("Tried to wait for a non-paste schematic command.");
scriptEntry.setFinished(true);
}
String fname = filename != null ? filename.asString() : name.asString();
switch (ttype) {
case CREATE:
if (schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
return;
}
if (cuboid == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Missing cuboid argument!");
return;
}
if (location == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Missing origin location argument!");
return;
}
try {
// TODO: Make this waitable!
set = new CuboidBlockSet(cuboid, location);
schematics.put(name.asString().toUpperCase(), set);
}
catch (Exception ex) {
Debug.echoError(scriptEntry.getResidingQueue(), "Error creating schematic object " + name.asString() + ".");
Debug.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case LOAD:
if (schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
return;
}
try {
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
if (!Utilities.canReadFile(f)) {
Debug.echoError("Server config denies reading files in that location.");
return;
}
if (!f.exists()) {
Debug.echoError("Schematic file " + fname + " does not exist. Are you sure it's in " + directory + "/plugins/Denizen/schematics/?");
return;
}
InputStream fs = new FileInputStream(f);
// TODO: Make this waitable!
set = MCEditSchematicHelper.fromMCEditStream(fs);
fs.close();
schematics.put(name.asString().toUpperCase(), set);
}
catch (Exception ex) {
Debug.echoError(scriptEntry.getResidingQueue(), "Error loading schematic file " + name.asString() + ".");
Debug.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case UNLOAD:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
schematics.remove(name.asString().toUpperCase());
break;
case ROTATE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
if (angle == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Missing angle argument!");
return;
}
// TODO: Make this waitable!
int ang = angle.asInt();
while (ang < 0) {
ang = 360 + ang;
}
while (ang > 360) {
ang -= 360;
}
while (ang > 0) {
ang -= 90;
schematics.get(name.asString().toUpperCase()).rotateOne();
}
break;
case FLIP_X:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
schematics.get(name.asString().toUpperCase()).flipX();
break;
case FLIP_Y:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
schematics.get(name.asString().toUpperCase()).flipY();
break;
case FLIP_Z:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
schematics.get(name.asString().toUpperCase()).flipZ();
break;
case PASTE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
if (location == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Missing location argument!");
return;
}
try {
if (delayed != null && delayed.asBoolean()) {
schematics.get(name.asString().toUpperCase()).setBlocksDelayed(location, new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
}
}, noair != null && noair.asBoolean());
}
else {
scriptEntry.setFinished(true);
schematics.get(name.asString().toUpperCase()).setBlocks(location, noair != null && noair.asBoolean());
}
}
catch (Exception ex) {
Debug.echoError(scriptEntry.getResidingQueue(), "Exception pasting schematic file " + name.asString() + ".");
Debug.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case SAVE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
try {
set = schematics.get(name.asString().toUpperCase());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
if (!Utilities.canWriteToFile(f)) {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot edit that file!");
return;
}
f.getParentFile().mkdirs();
// TODO: Make this waitable!
FileOutputStream fs = new FileOutputStream(f);
MCEditSchematicHelper.saveMCEditFormatToStream(set, fs);
fs.flush();
fs.close();
}
catch (Exception ex) {
Debug.echoError(scriptEntry.getResidingQueue(), "Error saving schematic file " + fname + ".");
Debug.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
}
}
public void schematicTags(ReplaceableTagEvent event) {
if (!event.matches("schematic", "schem")) {
return;
}
String id = event.hasNameContext() ? event.getNameContext().toUpperCase() : null;
Attribute attribute = event.getAttributes().fulfill(1);
// <--[tag]
// @attribute <schematic.list>
// @returns ListTag
// @description
// Returns a list of all loaded schematics.
// -->
if (attribute.startsWith("list")) {
event.setReplaced(new ListTag(schematics.keySet()).getAttribute(attribute.fulfill(1)));
}
if (id == null) {
return;
}
if (!schematics.containsKey(id)) {
// Meta below
if (attribute.startsWith("exists")) {
event.setReplaced(new ElementTag(false)
.getAttribute(attribute.fulfill(1)));
return;
}
Debug.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Schematic file " + id + " is not loaded.");
return;
}
CuboidBlockSet set = schematics.get(id);
//
// Check attributes
//
// <--[tag]
// @attribute <schematic[<name>].exists>
// @returns ElementTag(Boolean)
// @description
// Returns whether the schematic exists.
// -->
if (attribute.startsWith("exists")) {
event.setReplaced(new ElementTag(true)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].height>
// @returns ElementTag(Number)
// @description
// Returns the height (Y) of the schematic.
// -->
if (attribute.startsWith("height")) {
event.setReplaced(new ElementTag(set.y_length)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].length>
// @returns ElementTag(Number)
// @description
// Returns the length (Z) of the schematic.
// -->
if (attribute.startsWith("length")) {
event.setReplaced(new ElementTag(set.z_height)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].width>
// @returns ElementTag(Number)
// @description
// Returns the width (X) of the schematic.
// -->
if (attribute.startsWith("width")) {
event.setReplaced(new ElementTag(set.x_width)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].block[<location>]>
// @returns MaterialTag
// @description
// Returns the material for the block at the location in the schematic.
// -->
if (attribute.startsWith("block")) {
if (attribute.hasContext(1) && LocationTag.matches(attribute.getContext(1))) {
LocationTag location = LocationTag.valueOf(attribute.getContext(1));
BlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
event.setReplaced(new MaterialTag(block)
.getAttribute(attribute.fulfill(1)));
return;
}
}
// <--[tag]
// @attribute <schematic[<name>].origin>
// @returns LocationTag
// @description
// Returns the origin location of the schematic.
// -->
if (attribute.startsWith("origin")) {
event.setReplaced(new LocationTag(null, set.center_x, set.center_y, set.center_z)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].blocks>
// @returns ElementTag(Number)
// @description
// Returns the number of blocks in the schematic.
// -->
if (attribute.startsWith("blocks")) {
event.setReplaced(new ElementTag(set.blocks.size())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].cuboid[<origin location>]>
// @returns CuboidTag
// @description
// Returns a cuboid of where the schematic would be if it was pasted at an origin.
// -->
if (attribute.startsWith("cuboid") && attribute.hasContext(1)) {
LocationTag origin = LocationTag.valueOf(attribute.getContext(1));
event.setReplaced(set.getCuboid(origin)
.getAttribute(attribute.fulfill(1)));
return;
}
}
}