/
ModifyBlockCommand.java
412 lines (370 loc) · 17 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.nms.interfaces.WorldHelper;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dEllipsoid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.scripts.queues.core.InstantQueue;
import net.aufdemrand.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 {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Parse arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesArgumentType(dCuboid.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")) {
scriptEntry.addObject("locations", arg.asType(dCuboid.class).getBlockLocations());
}
else if (arg.matchesArgumentType(dEllipsoid.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")) {
scriptEntry.addObject("locations", arg.asType(dEllipsoid.class).getBlockLocations());
}
else if (arg.matchesArgumentList(dLocation.class)
&& !scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("location_list")) {
scriptEntry.addObject("location_list", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("materials")
&& arg.matchesArgumentList(dMaterial.class)) {
scriptEntry.addObject("materials", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrefix("radius", "r")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("radius", new Element(arg.getValue()));
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesPrefix("height", "h")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("height", new Element(arg.getValue()));
}
else if (!scriptEntry.hasObject("depth")
&& arg.matchesPrefix("depth", "d")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("depth", new Element(arg.getValue()));
}
else if (arg.matches("no_physics")) {
scriptEntry.addObject("physics", new Element(false));
}
else if (arg.matches("naturally")) {
scriptEntry.addObject("natural", new Element(true));
}
else if (arg.matches("delayed")) {
scriptEntry.addObject("delayed", new Element(true));
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(dScript.class)) {
scriptEntry.addObject("script", arg.asType(dScript.class));
}
else if (!scriptEntry.hasObject("percents")) {
scriptEntry.addObject("percents", arg.asType(dList.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 Element(0))
.defaultObject("height", new Element(0))
.defaultObject("depth", new Element(0))
.defaultObject("physics", new Element(true))
.defaultObject("natural", new Element(false))
.defaultObject("delayed", new Element(false));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
final dList materials = scriptEntry.getdObject("materials");
final List<dLocation> locations = (List<dLocation>) scriptEntry.getObject("locations");
final dList location_list = scriptEntry.getdObject("location_list");
final Element physics = scriptEntry.getElement("physics");
final Element natural = scriptEntry.getElement("natural");
final Element delayed = scriptEntry.getElement("delayed");
final Element radiusElement = scriptEntry.getElement("radius");
final Element heightElement = scriptEntry.getElement("height");
final Element depthElement = scriptEntry.getElement("depth");
final dScript script = scriptEntry.getdObject("script");
dList percents = scriptEntry.getdObject("percents");
if (percents != null && percents.size() != materials.size()) {
dB.echoError(scriptEntry.getResidingQueue(), "Percents length != materials length");
percents = null;
}
final List<dMaterial> materialList = materials.filter(dMaterial.class, scriptEntry);
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), (locations == null ? location_list.debug() : aH.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<Float>();
for (String str : percents) {
percentages.add(new Element(str).asFloat());
}
}
final List<Float> percs = percentages;
if ((locations == null || locations.size() == 0) && (location_list == null || location_list.size() == 0)) {
dB.echoError("Must specify a valid location!");
return;
}
if (materialList.size() == 0) {
dB.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();
dLocation loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = dLocation.valueOf(location_list.get(0));
}
boolean was_static = preSetup(loc);
while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
dLocation nLoc;
if (locations != null) {
nLoc = locations.get(index);
}
else {
nLoc = dLocation.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 = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName()))
.addEntries(entries);
queue.start();
}
scriptEntry.setFinished(true);
cancel();
}
}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
}
else {
dLocation loc;
if (locations != null) {
loc = locations.get(0);
}
else {
loc = dLocation.valueOf(location_list.get(0));
}
boolean was_static = preSetup(loc);
int index = 0;
if (locations != null) {
for (dObject obj : locations) {
handleLocation((dLocation) obj, index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
index++;
}
}
else {
for (String str : location_list) {
handleLocation(dLocation.valueOf(str), index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
index++;
}
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
boolean preSetup(dLocation loc0) {
// Freeze the first world in the list.
WorldHelper worldHelper = NMSHandler.getInstance().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.getInstance().getWorldHelper().setStatic(loc.getWorld(), was_static);
}
no_physics = false;
}
void handleLocation(dLocation location, int index, List<dMaterial> materialList, boolean doPhysics,
boolean isNatural, int radius, int height, int depth, List<Float> percents) {
dMaterial 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, dMaterial 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) {
dB.echoError("Invalid modifyblock location: " + new dLocation(location).toString());
return;
}
if (natural && material.getMaterial() == Material.AIR) {
location.getBlock().breakNaturally();
}
else {
// TODO: 1.13 - confirm this works
BlockData blockData = NMSHandler.getInstance().getBlockHelper().getBlockData(material.getMaterial(), material.getData());
blockData.setBlock(location.getBlock(), physics);
}
}
boolean no_physics = false;
public final List<Location> block_physics = new ArrayList<Location>();
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());
}
}