/
CuboidBlockSet.java
246 lines (222 loc) · 8.64 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
package com.denizenscript.denizen.utilities.blocks;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.CuboidTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.scripts.commands.world.SchematicCommand;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitRunnable;
public class CuboidBlockSet implements BlockSet {
public CuboidBlockSet() {
}
public CuboidBlockSet(CuboidTag cuboid, Location center) {
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());
}
}
}
}
public void buildDelayed(CuboidTag cuboid, Location center, Runnable runme, long maxDelayMs) {
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());
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 int x_width;
public int y_length;
public int z_height;
public int center_x;
public int center_y;
public int center_z;
@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 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;
if (runme != null) {
runme.run();
}
cancel();
}
}.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 rotateOne() {
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
int cx = center_x;
center_x = center_z;
center_z = x_width - 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;
*/
}
}