/
ModifyBlockCommand.java
333 lines (285 loc) · 13.8 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
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.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* Modifies blocks based based of single block location.
* Possibility to do faux animations with blocks.
*
* @author Mason Adkins, aufdemrand, mcmonkey
*/
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.matchesArgumentList(dLocation.class)){
scriptEntry.addObject("locations", arg.asType(dList.class).filter(dLocation.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
arg.reportUnhandled();
}
// Must have material
if (!scriptEntry.hasObject("materials"))
throw new InvalidArgumentsException("Missing material argument!");
// ..and at least one location.
if (!scriptEntry.hasObject("locations"))
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) throws CommandExecutionException {
final dList materials = (dList) scriptEntry.getObject("materials");
final List<dObject> locations = (List<dObject>) scriptEntry.getObject("locations");
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 List<dMaterial> materialList = materials.filter(dMaterial.class);
final dScript script = scriptEntry.getdObject("script");
dB.report(scriptEntry, getName(), aH.debugList("locations", locations)
+ materials.debug()
+ physics.debug()
+ radiusElement.debug()
+ heightElement.debug()
+ depthElement.debug()
+ natural.debug()
+ delayed.debug()
+ (script != null ? script.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();
if (locations.size() == 0)
dB.echoError("Must have a valid location!");
if (materials.size() == 0)
dB.echoError("Must have a valid material!");
no_physics = !doPhysics;
if (delayed.asBoolean()) {
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
boolean was_static = preSetup(locations);
long start = System.currentTimeMillis();
Location loc = (dLocation)locations.get(0);
while (locations.size() > 0) {
handleLocation((dLocation) locations.get(0), index, materialList, doPhysics, isNatural, radius, height, depth);
locations.remove(0);
index++;
if (System.currentTimeMillis() - start > 50) {
break;
}
}
postComplete(loc, was_static);
if (locations.size() == 0) {
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 {
boolean was_static = preSetup(locations);
Location loc = (dLocation)locations.get(0);
int index = 0;
for (dObject obj : locations) {
handleLocation((dLocation) obj, index, materialList, doPhysics, isNatural, radius, height, depth);
index++;
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
boolean preSetup(List<dObject> locations) {
if (locations.size() == 0)
return false;
// Freeze the first world in the list.
// TODO: make this do all worlds from the locations in the list
CraftWorld craftWorld = (CraftWorld)((dLocation)locations.get(0)).getWorld();
boolean was_static = craftWorld.getHandle().isStatic;
if (no_physics)
setWorldIsStatic(((dLocation)locations.get(0)).getWorld(), true);
return was_static;
}
void postComplete(Location loc, boolean was_static) {
// Unfreeze the first world in the list.
// TODO: make this do all worlds from the locations in the list
if (no_physics)
setWorldIsStatic(loc.getWorld(), was_static);
no_physics = false;
}
private static Field isStaticField = null;
void setWorldIsStatic(World world, boolean isStatic) {
try {
CraftWorld craftWorld = (CraftWorld) world;
if (isStaticField == null) {
isStaticField = craftWorld.getHandle().getClass().getField("isStatic");
isStaticField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(isStaticField, isStaticField.getModifiers() & ~Modifier.FINAL);
}
isStaticField.set(craftWorld.getHandle(), isStatic);
} catch (Exception e) {
dB.echoError(e);
}
}
void handleLocation(dLocation location, int index, List<dMaterial> materialList, boolean doPhysics,
boolean isNatural, int radius, int height, int depth) {
dMaterial material = materialList.get(index % materialList.size());
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 (natural && material.getMaterial() == Material.AIR)
location.getBlock().breakNaturally();
else
location.getBlock().setTypeIdAndData(material.getMaterial().getId(), material.getMaterialData().getData(), 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());
}
}