/
ModifyBlockCommand.java
450 lines (410 loc) · 19.1 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
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.WorldHelper;
import com.denizenscript.denizen.objects.CuboidTag;
import com.denizenscript.denizen.objects.EllipsoidTag;
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.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.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
public class ModifyBlockCommand extends AbstractCommand implements Listener, Holdable {
// <--[command]
// @Name ModifyBlock
// @Syntax modifyblock [<location>|.../<ellipsoid>/<cuboid>] [<material>|...] (no_physics/naturally) (delayed) (<script>) (<percent chance>|...)
// @Required 2
// @Short Modifies blocks.
// @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. 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.
// Use 'delayed' to make the modifyblock slowly edit blocks at a time pace roughly equivalent to the server's limits.
// Note that specify 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.)
//
// 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 <cuboid[<player.location>|<player.cursor_on>]> stone|dirt
//
// @Usage
// Use to modify an entire cuboid to some stone, some dirt, and some left as it is.
// - modifyblock <cuboid[<player.location>|<player.cursor_on>]> stone|dirt 25|25
//
// @Usage
// Use to modify the ground beneath the player's feet.
// - modifyblock <cuboid[<player.location.add[2,-1,2]>|<player.location.add[-2,-1,-2]>]> RED_WOOL
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Parse arguments
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesArgumentType(CuboidTag.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")
&& arg.startsWith("cu@")) {
scriptEntry.addObject("locations", arg.asType(CuboidTag.class).getBlockLocationsUnfiltered());
}
else if (arg.matchesArgumentType(EllipsoidTag.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")
&& arg.startsWith("ellipsoid@")) {
scriptEntry.addObject("locations", arg.asType(EllipsoidTag.class).getBlockLocationsUnfiltered());
}
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.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("radius", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesPrefix("height", "h")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("height", new ElementTag(arg.getValue()));
}
else if (!scriptEntry.hasObject("depth")
&& arg.matchesPrefix("depth", "d")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("depth", new ElementTag(arg.getValue()));
}
else if (arg.matches("no_physics")) {
scriptEntry.addObject("physics", new ElementTag(false));
}
else if (arg.matches("naturally")) {
scriptEntry.addObject("natural", new ElementTag(true));
}
else if (arg.matches("delayed")) {
scriptEntry.addObject("delayed", new ElementTag(true));
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!scriptEntry.hasObject("percents")) {
scriptEntry.addObject("percents", arg.asType(ListTag.class));
}
else {
arg.reportUnhandled();
}
}
// Must have material
if (!scriptEntry.hasObject("materials")) {
throw new InvalidArgumentsException("Missing material argument!");
}
// ..and at least one location.
if (!scriptEntry.hasObject("locations") && !scriptEntry.hasObject("location_list")) {
throw new InvalidArgumentsException("Missing location argument!");
}
// Set some defaults
scriptEntry.defaultObject("radius", new ElementTag(0))
.defaultObject("height", new ElementTag(0))
.defaultObject("depth", new ElementTag(0))
.defaultObject("physics", new ElementTag(true))
.defaultObject("natural", new ElementTag(false))
.defaultObject("delayed", new ElementTag(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 ElementTag natural = scriptEntry.getElement("natural");
final ElementTag delayed = scriptEntry.getElement("delayed");
final ElementTag radiusElement = scriptEntry.getElement("radius");
final ElementTag heightElement = scriptEntry.getElement("height");
final ElementTag depthElement = scriptEntry.getElement("depth");
final ScriptTag script = scriptEntry.getObjectTag("script");
ListTag percents = scriptEntry.getObjectTag("percents");
if (percents != null && percents.size() != materials.size()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Percents length != materials length");
percents = null;
}
final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (locations == null ? location_list.debug() : ArgumentHelper.debugList("locations", locations))
+ materials.debug()
+ physics.debug()
+ radiusElement.debug()
+ heightElement.debug()
+ depthElement.debug()
+ natural.debug()
+ delayed.debug()
+ (script != null ? script.debug() : "")
+ (percents != null ? percents.debug() : ""));
}
final boolean doPhysics = physics.asBoolean();
final boolean isNatural = natural.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.size() == 0) || (locations != null && locations.size() == 0)) {
return;
}
if (materialList.size() == 0) {
Debug.echoError("Must specify a valid material!");
return;
}
no_physics = !doPhysics;
if (delayed.asBoolean()) {
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
long start = System.currentTimeMillis();
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = LocationTag.valueOf(location_list.get(0));
}
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 = LocationTag.valueOf(location_list.get(index));
}
handleLocation(nLoc, index, materialList, doPhysics, isNatural, radius, height, depth, percs);
index++;
if (System.currentTimeMillis() - start > 50) {
break;
}
}
postComplete(loc, was_static);
if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
if (script != null) {
List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
ScriptQueue queue = new InstantQueue(script.getContainer().getName())
.addEntries(entries);
queue.start();
}
scriptEntry.setFinished(true);
cancel();
}
}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
}
else {
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = LocationTag.valueOf(location_list.get(0));
}
boolean was_static = preSetup(loc);
int index = 0;
if (locations != null) {
for (ObjectTag obj : locations) {
handleLocation((LocationTag) obj, index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
index++;
}
}
else {
for (String str : location_list) {
handleLocation(LocationTag.valueOf(str), index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
index++;
}
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
boolean preSetup(LocationTag loc0) {
// Freeze the first world in the list.
WorldHelper worldHelper = NMSHandler.getWorldHelper();
World world = loc0.getWorld();
boolean was_static = worldHelper.isStatic(world);
if (no_physics) {
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.getWorldHelper().setStatic(loc.getWorld(), was_static);
}
no_physics = false;
}
void handleLocation(LocationTag location, int index, List<MaterialTag> materialList, boolean doPhysics,
boolean isNatural, int radius, int height, int depth, List<Float> percents) {
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;
}
}
World world = location.getWorld();
location.setX(location.getBlockX());
location.setY(location.getBlockY());
location.setZ(location.getBlockZ());
setBlock(location, material, doPhysics, isNatural);
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, isNatural);
}
}
}
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, isNatural);
}
}
}
}
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, isNatural);
}
}
}
}
}
void setBlock(Location location, MaterialTag material, boolean physics, boolean natural) {
if (physics) {
for (int i = 0; i < block_physics.size(); i++) {
if (compareloc(block_physics.get(i), location)) {
block_physics.remove(i--);
}
}
}
else {
block_physics.add(location);
physitick = tick;
}
if (location.getY() < 0 || location.getY() > 255) {
Debug.echoError("Invalid modifyblock location: " + new LocationTag(location).toString());
return;
}
if (natural && material.getMaterial() == Material.AIR) {
location.getBlock().breakNaturally();
}
else {
material.getNmsBlockData().setBlock(location.getBlock(), physics);
}
}
boolean no_physics = false;
public final List<Location> block_physics = new ArrayList<>();
long tick = 0;
long physitick = 0;
@Override
public void onEnable() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
// 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(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
tick++;
if (physitick < tick - 1) {
block_physics.clear();
}
}
}, 2, 2);
}
@EventHandler
public void blockPhysics(BlockPhysicsEvent event) {
if (no_physics) {
event.setCancelled(true);
}
for (Location loc : block_physics) {
if (compareloc(event.getBlock().getLocation(), loc)) {
event.setCancelled(true);
}
}
}
@EventHandler
public void blockChanges(EntityChangeBlockEvent event) {
if (event.getEntity().getType() != EntityType.FALLING_BLOCK) {
return;
}
if (no_physics) {
event.setCancelled(true);
}
for (Location loc : block_physics) {
if (compareloc(event.getBlock().getLocation(), loc)) {
event.setCancelled(true);
}
}
}
boolean compareloc(Location lone, Location ltwo) {
return lone.getBlockX() == ltwo.getBlockX() && lone.getBlockY() == ltwo.getBlockY() &&
lone.getBlockZ() == ltwo.getBlockZ() && lone.getWorld().getName().equalsIgnoreCase(ltwo.getWorld().getName());
}
}