/
CuboidBlockSet.java
366 lines (335 loc) · 14.2 KB
/
CuboidBlockSet.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
package com.denizenscript.denizen.utilities.blocks;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.scripts.commands.world.SchematicCommand;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class CuboidBlockSet implements BlockSet {
public CuboidBlockSet() {
}
public CuboidBlockSet(CuboidTag cuboid, Location center, boolean copyFlags) {
hasFlags = copyFlags;
Location low = cuboid.pairs.get(0).low;
Location high = cuboid.pairs.get(0).high;
x_width = (int) ((high.getX() - low.getX()) + 1);
y_length = (int) ((high.getY() - low.getY()) + 1);
z_height = (int) ((high.getZ() - low.getZ()) + 1);
center_x = (int) (center.getX() - low.getX());
center_y = (int) (center.getY() - low.getY());
center_z = (int) (center.getZ() - low.getZ());
blocks = new FullBlockData[(x_width * y_length * z_height)];
int index = 0;
for (int x = 0; x < x_width; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = 0; z < z_height; z++) {
blocks[index++] = new FullBlockData(low.clone().add(x, y, z).getBlock(), copyFlags);
}
}
}
}
public void buildDelayed(CuboidTag cuboid, Location center, Runnable runme, long maxDelayMs, boolean copyFlags) {
hasFlags = copyFlags;
Location low = cuboid.pairs.get(0).low;
Location high = cuboid.pairs.get(0).high;
x_width = (int) ((high.getX() - low.getX()) + 1);
y_length = (int) ((high.getY() - low.getY()) + 1);
z_height = (int) ((high.getZ() - low.getZ()) + 1);
center_x = (int) (center.getX() - low.getX());
center_y = (int) (center.getY() - low.getY());
center_z = (int) (center.getZ() - low.getZ());
final long goal = (long) (x_width * y_length * z_height);
blocks = new FullBlockData[(x_width * y_length * z_height)];
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
long start = System.currentTimeMillis();
while (index < goal) {
long z = index % ((long) (z_height));
long y = ((index - z) % ((long) (y_length * z_height))) / ((long) z_height);
long x = (index - y - z) / ((long) (y_length * z_height));
blocks[index] = new FullBlockData(low.clone().add(x, y, z).getBlock(), copyFlags);
index++;
if (System.currentTimeMillis() - start > maxDelayMs) {
SchematicCommand.noPhys = false;
return;
}
}
if (runme != null) {
runme.run();
}
cancel();
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
}
public FullBlockData[] blocks = null;
public boolean hasFlags = false;
public int x_width;
public int y_length;
public int z_height;
public int center_x;
public int center_y;
public int center_z;
public ListTag entities = null;
@Override
public FullBlockData[] getBlocks() {
return blocks;
}
public CuboidTag getCuboid(Location loc) {
Location low = loc.clone().subtract(center_x, center_y, center_z);
Location high = low.clone().add(x_width, y_length, z_height);
return new CuboidTag(low, high);
}
public static BlockFace rotateFaceOne(BlockFace face) {
switch (face) {
case NORTH:
return BlockFace.WEST;
case EAST:
return BlockFace.NORTH;
case SOUTH:
return BlockFace.EAST;
case WEST:
return BlockFace.SOUTH;
default:
return BlockFace.SELF;
}
}
public static HashSet<EntityType> copyTypes = new HashSet<>(Arrays.asList(EntityType.PAINTING, EntityType.ITEM_FRAME, EntityType.ARMOR_STAND));
public void buildEntities(CuboidTag cuboid, Location center) {
entities = new ListTag();
for (Entity ent : cuboid.getWorld().getEntities()) {
if (cuboid.isInsideCuboid(ent.getLocation())) {
if (copyTypes.contains(ent.getType())) {
MapTag data = new MapTag();
data.putObject("entity", new EntityTag(ent).describe());
data.putObject("rotation", new ElementTag(0));
Vector offset = ent.getLocation().toVector().subtract(center.toVector());
data.putObject("offset", new LocationTag((String) null, offset.getX(), offset.getY(), offset.getZ(), ent.getLocation().getYaw(), ent.getLocation().getPitch()));
entities.addObject(data);
}
}
}
}
public void pasteEntities(LocationTag relative) {
if (entities == null) {
return;
}
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
try {
LocationTag offset = data.getObject("offset").asType(LocationTag.class, CoreUtilities.noDebugContext);
int rotation = data.getObject("rotation").asType(ElementTag.class, CoreUtilities.noDebugContext).asInt();
EntityTag entity = data.getObject("entity").asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity == null || offset == null) {
continue;
}
entity = entity.duplicate();
offset = offset.clone();
if (rotation != 0) {
ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
for (Mechanism mech : entity.getWaitingMechanisms()) {
if (mech.getName().equals("rotation")) {
String rotationName = mech.getValue().asString();
BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
for (int i = 0; i < rotation; i += 90) {
face = rotateFaceOne(face);
}
offset.add(face.getDirection().multiply(0.1)); // Compensate for hanging locations being very stupid
mechs.add(new Mechanism(new ElementTag("rotation"), new ElementTag(face.name()), CoreUtilities.noDebugContext));
}
else {
mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
}
}
entity.mechanisms = mechs;
}
else {
for (Mechanism mechanism : entity.mechanisms) {
mechanism.context = CoreUtilities.noDebugContext;
}
}
Location spawnLoc = relative.clone().add(offset);
spawnLoc.setYaw(offset.getYaw() + rotation);
spawnLoc.setPitch(offset.getPitch());
entity.spawnAt(spawnLoc);
}
catch (Exception ex) {
Debug.echoError(ex);
}
}
}
public void setBlockSingle(FullBlockData block, int x, int y, int z, InputParams input) {
if (input.noAir && block.data.getMaterial() == Material.AIR) {
return;
}
if (block.data.getMaterial() == Material.STRUCTURE_VOID) {
return;
}
int finalY = input.centerLocation.getBlockY() + y - center_y;
if (finalY < 0 || finalY > 255) {
return;
}
Block destBlock = input.centerLocation.clone().add(x - center_x, y - center_y, z - center_z).getBlock();
if (input.mask != null && !input.mask.contains(destBlock.getType())) {
return;
}
if (input.fakeTo == null) {
block.set(destBlock, false);
}
else {
FakeBlock.showFakeBlockTo(input.fakeTo, new LocationTag(destBlock.getLocation()), new MaterialTag(block.data), input.fakeDuration);
}
}
@Override
public void setBlocksDelayed(final Runnable runme, final InputParams input, long maxDelayMs) {
final long goal = (long) (x_width * y_length * z_height);
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
SchematicCommand.noPhys = true;
long start = System.currentTimeMillis();
while (index < goal) {
int z = index % (z_height);
int y = ((index - z) % (y_length * z_height)) / z_height;
int x = (index - y - z) / (y_length * z_height);
setBlockSingle(blocks[index], x, y, z, input);
index++;
if (System.currentTimeMillis() - start > maxDelayMs) {
SchematicCommand.noPhys = false;
return;
}
}
SchematicCommand.noPhys = false;
cancel();
if (runme != null) {
runme.run();
}
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
}
@Override
public void setBlocks(InputParams input) {
SchematicCommand.noPhys = true;
int index = 0;
for (int x = 0; x < x_width; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = 0; z < z_height; z++) {
setBlockSingle(blocks[index], x, y, z, input);
index++;
}
}
}
SchematicCommand.noPhys = false;
}
public void rotateEntitiesOne() {
if (entities == null) {
return;
}
ListTag outEntities = new ListTag();
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
LocationTag offset = data.getObject("offset").asType(LocationTag.class, CoreUtilities.noDebugContext);
int rotation = data.getObject("rotation").asType(ElementTag.class, CoreUtilities.noDebugContext).asInt();
offset = new LocationTag((String) null, offset.getZ(), offset.getY(), -offset.getX() + 1, offset.getYaw(), offset.getPitch());
rotation += 90;
while (rotation >= 360) {
rotation -= 360;
}
data = data.duplicate();
data.putObject("offset", offset);
data.putObject("rotation", new ElementTag(rotation));
outEntities.addObject(data);
}
entities = outEntities;
}
public void rotateOne() {
rotateEntitiesOne();
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
int cx = center_x;
center_x = center_z;
center_z = x_width - 1 - cx;
for (int x = 0; x < z_height; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = x_width - 1; z >= 0; z--) {
bd[index++] = blockAt(z, y, x).rotateOne();
}
}
}
int xw = x_width;
x_width = z_height;
z_height = xw;
blocks = bd;
}
public void flipX() {
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
center_x = x_width - center_x;
for (int x = x_width - 1; x >= 0; x--) {
for (int y = 0; y < y_length; y++) {
for (int z = 0; z < z_height; z++) {
bd[index++] = blockAt(x, y, z);
}
}
}
blocks = bd;
}
public void flipY() {
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
center_x = x_width - center_x;
for (int x = 0; x < x_width; x++) {
for (int y = y_length - 1; y >= 0; y--) {
for (int z = 0; z < z_height; z++) {
bd[index++] = blockAt(x, y, z);
}
}
}
blocks = bd;
}
public void flipZ() {
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
center_x = x_width - center_x;
for (int x = 0; x < x_width; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = z_height - 1; z >= 0; z--) {
bd[index++] = blockAt(x, y, z);
}
}
}
blocks = bd;
}
public FullBlockData blockAt(double X, double Y, double Z) {
return blocks[(int) (Z + Y * z_height + X * z_height * y_length)];
// This calculation should produce the same result as the below nonsense:
/*
int index = 0;
for (int x = 0; x < x_width; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = 0; z < z_height; z++) {
if (x == X && y == Y && z == Z) {
return blocks.get(index);
}
index++;
}
}
}
return null;
*/
}
}