/
ModifyBlockCommand.java
523 lines (503 loc) · 23.7 KB
/
ModifyBlockCommand.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.command.TabCompleteHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
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.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ScriptUtilities;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class ModifyBlockCommand extends AbstractCommand implements Listener, Holdable {
public ModifyBlockCommand() {
setName("modifyblock");
setSyntax("modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally:<tool>) (delayed) (<script>) (<percent chance>|...) (source:<player>) (max_delay_ms:<#>)");
setRequiredArguments(2, 8);
Bukkit.getPluginManager().registerEvents(this, Denizen.getInstance());
// Keep the list empty automatically - we don't want to still block physics so much later that something else edited the block!
Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), () -> {
tick++;
if (physitick < tick - 1) {
block_physics.clear();
}
}, 2, 2);
isProcedural = false;
}
// <--[command]
// @Name ModifyBlock
// @Syntax modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally:<tool>) (delayed) (<script>) (<percent chance>|...) (source:<player>) (max_delay_ms:<#>)
// @Required 2
// @Maximum 8
// @Short Modifies blocks.
// @Synonyms SetBlock,ChangeBlock,PlaceBlock,BreakBlock
// @Group world
//
// @Description
// Changes blocks in the world based on the criteria given.
//
// Use 'no_physics' to place the blocks without physics taking over the modified blocks.
// This is useful for block types such as portals or water. This does NOT control physics for an extended period of time.
//
// Specify (<percent chance>|...) to give a chance of each material being placed (in any material at all).
//
// Use 'naturally:' when setting a block to air to break it naturally, meaning that it will drop items. Specify the tool item that should be used for calculating drops.
//
// Use 'delayed' to make the modifyblock slowly edit blocks at a time pace roughly equivalent to the server's limits.
// Optionally, specify 'max_delay_ms' to control how many milliseconds the 'delayed' set can run for in any given tick (defaults to 50).
//
// Note that specifying a list of locations will take more time in parsing than in the actual block modification.
//
// Optionally, specify a script to be ran after the delayed edits finish. (Doesn't fire if delayed is not set.)
//
// Optionally, specify a source player. When set, Bukkit events will fire that identify that player as the source of a change, and potentially cancel the change.
// The source argument might cause weird interoperation with other plugins, use with caution.
//
// The modifyblock command is ~waitable. Refer to <@link language ~waitable>.
//
// @Tags
// <LocationTag.material>
//
// @Usage
// Use to change the block a player is looking at to stone.
// - modifyblock <player.cursor_on> stone
//
// @Usage
// Use to modify an entire cuboid to half stone, half dirt.
// - modifyblock <player.location.to_cuboid[<player.cursor_on>]> stone|dirt
//
// @Usage
// Use to modify an entire cuboid to some stone, some dirt, and some left as it is.
// - modifyblock <player.location.to_cuboid[<player.cursor_on>]> stone|dirt 25|25
//
// @Usage
// Use to modify the ground beneath the player's feet.
// - modifyblock <player.location.add[2,-1,2].to_cuboid[<player.location.add[-2,-1,-2]>]> RED_WOOL
// -->
@Override
public void addCustomTabCompletions(TabCompletionsBuilder tab) {
TabCompleteHelper.tabCompleteBlockMaterials(tab);
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesArgumentType(CuboidTag.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")
&& (arg.startsWith("cu@") || !arg.getRawValue().contains("|"))) {
scriptEntry.addObject("locations", arg.asType(CuboidTag.class).getBlockLocationsUnfiltered(false));
}
else if (arg.matchesArgumentType(EllipsoidTag.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")
&& (arg.startsWith("ellipsoid@") || !arg.getRawValue().contains("|"))) {
scriptEntry.addObject("locations", arg.asType(EllipsoidTag.class).getBlockLocationsUnfiltered(false));
}
else if (arg.matchesArgumentList(LocationTag.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")) {
scriptEntry.addObject("location_list", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("materials")
&& arg.matchesArgumentList(MaterialTag.class)) {
scriptEntry.addObject("materials", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrefix("radius", "r")
&& arg.matchesInteger()) {
scriptEntry.addObject("radius", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesPrefix("height", "h")
&& arg.matchesInteger()) {
scriptEntry.addObject("height", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("depth")
&& arg.matchesPrefix("depth", "d")
&& arg.matchesInteger()) {
scriptEntry.addObject("depth", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("source")
&& arg.matchesPrefix("source")
&& arg.matchesArgumentType(PlayerTag.class)) {
scriptEntry.addObject("source", arg.asType(PlayerTag.class));
}
else if (!scriptEntry.hasObject("physics")
&& arg.matches("no_physics")) {
scriptEntry.addObject("physics", new ElementTag(false));
}
else if (!scriptEntry.hasObject("natural")
&& arg.matches("naturally")) {
scriptEntry.addObject("natural", new ItemTag(new ItemStack(Material.AIR)));
}
else if (!scriptEntry.hasObject("natural")
&& arg.matchesPrefix("naturally")
&& arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("natural", arg.asType(ItemTag.class));
}
else if (!scriptEntry.hasObject("max_delay_ms")
&& arg.matchesPrefix("max_delay_ms")
&& arg.matchesInteger()) {
scriptEntry.addObject("max_delay_ms", arg.asElement());
}
else if (!scriptEntry.hasObject("delayed")
&& arg.matches("delayed")) {
scriptEntry.addObject("delayed", new ElementTag(true));
}
else if (!scriptEntry.hasObject("script")
&& arg.limitToOnlyPrefix("script")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!scriptEntry.hasObject("percents")
&& arg.limitToOnlyPrefix("percents")) {
scriptEntry.addObject("percents", arg.asType(ListTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("materials")) {
throw new InvalidArgumentsException("Missing material argument!");
}
if (!scriptEntry.hasObject("locations") && !scriptEntry.hasObject("location_list")) {
throw new InvalidArgumentsException("Missing location argument!");
}
scriptEntry.defaultObject("radius", new ElementTag(0))
.defaultObject("max_delay_ms", new ElementTag(50))
.defaultObject("height", new ElementTag(0))
.defaultObject("depth", new ElementTag(0))
.defaultObject("physics", new ElementTag(true))
.defaultObject("delayed", new ElementTag(false));
}
public static LocationTag getLocAt(ListTag list, int index, ScriptEntry entry) {
ObjectTag obj = list.getObject(index);
if (obj instanceof LocationTag) {
return (LocationTag) obj;
}
else {
return LocationTag.valueOf(obj.toString(), entry.context);
}
}
public static boolean isLocationBad(ScriptEntry entry, LocationTag loc) {
if (loc == null) {
Debug.echoError(entry, "Input is not a valid LocationTag");
return true;
}
if (loc.getWorld() == null) {
Debug.echoError(entry, "Input '" + loc + "' is missing a world value");
return true;
}
return false;
}
@Override
public void execute(final ScriptEntry scriptEntry) {
final ListTag materials = scriptEntry.getObjectTag("materials");
final List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
final ListTag location_list = scriptEntry.getObjectTag("location_list");
final ElementTag physics = scriptEntry.getElement("physics");
final ItemTag natural = scriptEntry.getObjectTag("natural");
final ElementTag delayed = scriptEntry.getElement("delayed");
final ElementTag maxDelayMs = scriptEntry.getElement("max_delay_ms");
final ElementTag radiusElement = scriptEntry.getElement("radius");
final ElementTag heightElement = scriptEntry.getElement("height");
final ElementTag depthElement = scriptEntry.getElement("depth");
final ScriptTag script = scriptEntry.getObjectTag("script");
final PlayerTag source = scriptEntry.getObjectTag("source");
ListTag percents = scriptEntry.getObjectTag("percents");
if (percents != null && percents.size() != materials.size()) {
Debug.echoError(scriptEntry, "Percents length != materials length");
percents = null;
}
final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);
for (MaterialTag mat : materialList) {
if (!mat.getMaterial().isBlock()) {
Debug.echoError("Material '" + mat.getMaterial().name() + "' is not a block material");
return;
}
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), materials, physics, radiusElement, heightElement, depthElement, natural,
delayed, maxDelayMs, script, percents, source, (locations == null ? location_list : db("locations", locations)));
}
Player sourcePlayer = source == null ? null : source.getPlayerEntity();
final boolean doPhysics = physics.asBoolean();
final int radius = radiusElement.asInt();
final int height = heightElement.asInt();
final int depth = depthElement.asInt();
List<Float> percentages = null;
if (percents != null) {
percentages = new ArrayList<>();
for (String str : percents) {
percentages.add(new ElementTag(str).asFloat());
}
}
final List<Float> percs = percentages;
if (locations == null && location_list == null) {
Debug.echoError("Must specify a valid location!");
return;
}
if ((location_list != null && location_list.isEmpty()) || (locations != null && locations.isEmpty())) {
return;
}
if (materialList.isEmpty()) {
Debug.echoError("Must specify a valid material!");
return;
}
no_physics = !doPhysics;
if (delayed.asBoolean()) {
final long maxDelay = maxDelayMs.asLong();
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
try {
long start = CoreUtilities.monotonicMillis();
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
boolean was_static = preSetup(loc);
while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
LocationTag nLoc;
if (locations != null) {
nLoc = locations.get(index);
}
else {
nLoc = getLocAt(location_list, index, scriptEntry);
}
if (isLocationBad(scriptEntry, nLoc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
handleLocation(nLoc, index, materialList, doPhysics, natural, radius, height, depth, percs, sourcePlayer, scriptEntry);
index++;
if (CoreUtilities.monotonicMillis() - start > maxDelay) {
break;
}
}
postComplete(loc, was_static);
if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
if (script != null) {
ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, null, null, null, null, scriptEntry);
}
scriptEntry.setFinished(true);
cancel();
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
}
else {
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
return;
}
boolean was_static = preSetup(loc);
int index = 0;
if (locations != null) {
for (LocationTag obj : locations) {
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
else {
for (int i = 0; i < location_list.size(); i++) {
LocationTag obj = getLocAt(location_list, i, scriptEntry);
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
boolean preSetup(LocationTag loc0) {
// Freeze the first world in the list.
World world = loc0.getWorld();
boolean was_static = NMSHandler.worldHelper.isStatic(world);
if (no_physics) {
NMSHandler.worldHelper.setStatic(world, true);
}
return was_static;
}
void postComplete(Location loc, boolean was_static) {
// Unfreeze the first world in the list.
if (no_physics) {
NMSHandler.worldHelper.setStatic(loc.getWorld(), was_static);
}
no_physics = false;
}
void handleLocation(LocationTag location, int index, List<MaterialTag> materialList, boolean doPhysics,
ItemTag natural, int radius, int height, int depth, List<Float> percents, Player source, ScriptEntry entry) {
MaterialTag material;
if (percents == null) {
material = materialList.get(index % materialList.size());
}
else {
material = null;
for (int i = 0; i < materialList.size(); i++) {
float perc = percents.get(i) / 100f;
if (CoreUtilities.getRandom().nextDouble() <= perc) {
material = materialList.get(i);
break;
}
}
if (material == null) {
return;
}
}
location = location.getBlockLocation();
World world = location.getWorld();
if (source != null) {
Event event;
if (material.getMaterial() == Material.AIR) {
event = new BlockBreakEvent(location.getBlock(), source);
}
else {
Block block = location.getBlock();
BlockState state = NMSHandler.blockHelper.generateBlockState(block, material.getMaterial());
state.setBlockData(material.getModernData());
event = new BlockPlaceEvent(block, state, block, new ItemTag(material, 1).getItemStack(), source, true, EquipmentSlot.HAND);
}
Bukkit.getPluginManager().callEvent(event);
if (((Cancellable) event).isCancelled()) {
if (entry.dbCallShouldDebug()) {
Debug.echoDebug(entry, "Source event cancelled, not changing block.");
}
return;
}
}
setBlock(location, material, doPhysics, natural);
if (radius != 0) {
for (int x = 0; x < 2 * radius + 1; x++) {
for (int z = 0; z < 2 * radius + 1; z++) {
setBlock(new Location(world, location.getX() + x - radius, location.getY(), location.getZ() + z - radius), material, doPhysics, natural);
}
}
}
if (height != 0) {
for (int x = 0; x < 2 * radius + 1; x++) {
for (int z = 0; z < 2 * radius + 1; z++) {
for (int y = 1; y < height + 1; y++) {
setBlock(new Location(world, location.getX() + x - radius, location.getY() + y, location.getZ() + z - radius), material, doPhysics, natural);
}
}
}
}
if (depth != 0) {
for (int x = 0; x < 2 * radius + 1; x++) {
for (int z = 0; z < 2 * radius + 1; z++) {
for (int y = 1; y < depth + 1; y++) {
setBlock(new Location(world, location.getX() + x - radius, location.getY() - y, location.getZ() + z - radius), material, doPhysics, natural);
}
}
}
}
}
public static void setBlock(Location location, MaterialTag material, boolean physics, ItemTag natural) {
if (physics) {
block_physics.remove(location);
}
else {
block_physics.add(location);
physitick = tick;
}
if (!Utilities.isLocationYSafe(location)) {
Debug.echoError("Invalid modifyblock location: " + new LocationTag(location));
return;
}
if (natural != null && material.getMaterial() == Material.AIR) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
int xp = NMSHandler.blockHelper.getExpDrop(location.getBlock(), natural.getItemStack());
if (xp > 0) {
ExperienceOrb orb = (ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB);
orb.setExperience(xp);
}
}
location.getBlock().breakNaturally(natural.getItemStack());
}
else {
location.getBlock().setBlockData(material.getModernData(), physics);
}
}
public static boolean no_physics = false;
public static final HashSet<Location> block_physics = new HashSet<>();
public static long tick = 0;
public static long physitick = 0;
@EventHandler
public void blockPhysics(BlockPhysicsEvent event) {
if (no_physics) {
event.setCancelled(true);
}
if (block_physics.contains(event.getBlock().getLocation())) {
event.setCancelled(true);
}
}
@EventHandler
public void blockChanges(EntityChangeBlockEvent event) {
if (event.getEntity().getType() != EntityType.FALLING_BLOCK) {
return;
}
if (no_physics) {
event.setCancelled(true);
}
if (block_physics.contains(event.getBlock().getLocation())) {
event.setCancelled(true);
}
}
}