-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
CuboidBlockSet.java
394 lines (367 loc) · 16.8 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
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
package net.aufdemrand.denizen.utilities.blocks;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.nms.util.jnbt.*;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.scripts.commands.world.SchematicCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class CuboidBlockSet implements BlockSet {
public CuboidBlockSet() {
}
public CuboidBlockSet(dCuboid cuboid, Location center) {
Location low = cuboid.pairs.get(0).low;
Location high = cuboid.pairs.get(0).high;
x_width = (high.getX() - low.getX()) + 1;
y_length = (high.getY() - low.getY()) + 1;
z_height = (high.getZ() - low.getZ()) + 1;
center_x = center.getX() - low.getX();
center_y = center.getY() - low.getY();
center_z = center.getZ() - low.getZ();
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.add(NMSHandler.getInstance().getBlockHelper().getBlockData(low.clone().add(x, y, z).getBlock()));
}
}
}
}
public List<BlockData> blocks = new ArrayList<BlockData>();
public double x_width;
public double y_length;
public double z_height;
public double center_x;
public double center_y;
public double center_z;
@Override
public List<BlockData> getBlocks() {
return blocks;
}
public dCuboid 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 dCuboid(low, high);
}
public class IntHolder {
public long theInt = 0;
}
@Override
public void setBlocksDelayed(final Location loc, final Runnable runme, final boolean noAir) {
final IntHolder index = new IntHolder();
final long goal = (long) (x_width * y_length * z_height);
new BukkitRunnable() {
@Override
public void run() {
SchematicCommand.noPhys = true;
long start = System.currentTimeMillis();
while (index.theInt < goal) {
long z = index.theInt % ((long) (z_height));
long y = ((index.theInt - z) % ((long) (y_length * z_height))) / ((long) z_height);
long x = (index.theInt - y - z) / ((long) (y_length * z_height));
if (!noAir || blocks.get((int)index.theInt).getMaterial() != Material.AIR) {
blocks.get((int) index.theInt).setBlock(loc.clone().add(x - center_x, y - center_y, z - center_z).getBlock());
}
index.theInt++;
if (System.currentTimeMillis() - start > 50) {
SchematicCommand.noPhys = false;
return;
}
}
SchematicCommand.noPhys = false;
if (runme != null) {
runme.run();
}
cancel();
}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
}
@Override
public void setBlocks(Location loc, boolean noAir) {
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++) {
if (!noAir || blocks.get(index).getMaterial() != Material.AIR) {
blocks.get(index).setBlock(loc.clone().add(x - center_x, y - center_y, z - center_z).getBlock());
}
index++;
}
}
}
SchematicCommand.noPhys = false;
}
public void rotateOne() {
List<BlockData> bd = new ArrayList<BlockData>();
double cx = center_x;
center_x = center_z;
center_z = cx;
for (int x = 0; x < z_height; x++) {
for (int y = 0; y < y_length; y++) {
for (int z = (int)x_width - 1; z >= 0; z--) {
bd.add(blockAt(z, y, x));
}
}
}
blocks = bd;
}
public void flipX() {
List<BlockData> bd = new ArrayList<BlockData>();
center_x = x_width - center_x;
for (int x = (int)x_width - 1; x >= 0; x--) {
for (int y = 0; y < y_length; y++) {
for (int z = 0; z < z_height; z++) {
bd.add(blockAt(x, y, z));
}
}
}
blocks = bd;
}
public void flipY() {
List<BlockData> bd = new ArrayList<BlockData>();
center_x = x_width - center_x;
for (int x = 0; x < x_width; x++) {
for (int y = (int)y_length - 1; y >= 0; y--) {
for (int z = 0; z < z_height; z++) {
bd.add(blockAt(x, y, z));
}
}
}
blocks = bd;
}
public void flipZ() {
List<BlockData> bd = new ArrayList<BlockData>();
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 = (int)z_height - 1; z >= 0; z--) {
bd.add(blockAt(x, y, z));
}
}
}
blocks = bd;
}
public BlockData blockAt(double X, double Y, double Z) {
return blocks.get((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;
*/
}
public static CuboidBlockSet fromMCEditStream(InputStream is) {
CuboidBlockSet cbs = new CuboidBlockSet();
try {
NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(is));
NamedTag rootTag = nbtStream.readNamedTag();
nbtStream.close();
if (!rootTag.getName().equals("Schematic")) {
throw new Exception("Tag 'Schematic' does not exist or is not first!");
}
CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
Map<String, Tag> schematic = schematicTag.getValue();
short width = (getChildTag(schematic, "Width", ShortTag.class).getValue());
short length = (getChildTag(schematic, "Length", ShortTag.class).getValue());
short height = (getChildTag(schematic, "Height", ShortTag.class).getValue());
int originX = 0;
int originY = 0;
int originZ = 0;
try {
originX = getChildTag(schematic, "DenizenOriginX", IntTag.class).getValue();
originY = getChildTag(schematic, "DenizenOriginY", IntTag.class).getValue();
originZ = getChildTag(schematic, "DenizenOriginZ", IntTag.class).getValue();
}
catch (Exception e) {
// Default origin, why not
}
cbs.x_width = width;
cbs.z_height = length;
cbs.y_length = height;
cbs.center_x = originX;
cbs.center_y = originY;
cbs.center_z = originZ;
// Disregard Offset
String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
if (!materials.equals("Alpha")) {
throw new Exception("Schematic file is not an Alpha schematic!");
}
byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
byte[] addId = new byte[0];
short[] blocks = new short[blockId.length];
if (schematic.containsKey("AddBlocks")) {
addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
}
for (int index = 0; index < blockId.length; index++) {
if ((index >> 1) >= addId.length) {
blocks[index] = (short) (blockId[index] & 0xFF);
}
else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
}
else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
}
}
}
List<Tag> tileEntities = getChildTag(schematic, "TileEntities", ListTag.class).getValue();
Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<BlockVector, Map<String, Tag>>();
for (Tag tag : tileEntities) {
if (!(tag instanceof CompoundTag)) {
continue;
}
CompoundTag t = (CompoundTag) tag;
int x = 0;
int y = 0;
int z = 0;
Map<String, Tag> values = new HashMap<String, Tag>();
for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
if (entry.getKey().equals("x")) {
if (entry.getValue() instanceof IntTag) {
x = ((IntTag) entry.getValue()).getValue();
}
}
else if (entry.getKey().equals("y")) {
if (entry.getValue() instanceof IntTag) {
y = ((IntTag) entry.getValue()).getValue();
}
}
else if (entry.getKey().equals("z")) {
if (entry.getValue() instanceof IntTag) {
z = ((IntTag) entry.getValue()).getValue();
}
}
values.put(entry.getKey(), entry.getValue());
}
BlockVector vec = new BlockVector(x, y, z);
tileEntitiesMap.put(vec, values);
}
org.bukkit.util.Vector vec = new Vector(width, height, length);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) {
int index = y * width * length + z * width + x;
BlockVector pt = new BlockVector(x, y, z);
BlockData block = NMSHandler.getInstance().getBlockHelper().getBlockData(blocks[index], blockData[index]);
if (tileEntitiesMap.containsKey(pt)) {
CompoundTag otag = NMSHandler.getInstance().createCompoundTag(tileEntitiesMap.get(pt));
block.setCompoundTag(otag);
}
cbs.blocks.add(block);
}
}
}
}
catch (Exception e) {
dB.echoError(e);
}
return cbs;
}
private static <T extends Tag> T getChildTag(Map<String, Tag> items, String key,
Class<T> expected) throws Exception {
if (!items.containsKey(key)) {
throw new Exception("Schematic file is missing a '" + key + "' tag");
}
Tag tag = items.get(key);
if (!expected.isInstance(tag)) {
throw new Exception(key + " tag is not of tag type " + expected.getName());
}
return expected.cast(tag);
}
// Thanks to WorldEdit for sample code
public void saveMCEditFormatToStream(OutputStream os) {
try {
HashMap<String, Tag> schematic = new HashMap<String, Tag>();
schematic.put("Width", new ShortTag((short) (x_width)));
schematic.put("Length", new ShortTag((short) (z_height)));
schematic.put("Height", new ShortTag((short) (y_length)));
schematic.put("Materials", new StringTag("Alpha"));
schematic.put("DenizenOriginX", new IntTag((int) center_x));
schematic.put("DenizenOriginY", new IntTag((int) center_y));
schematic.put("DenizenOriginZ", new IntTag((int) center_z));
schematic.put("WEOriginX", new IntTag((int) center_x));
schematic.put("WEOriginY", new IntTag((int) center_y));
schematic.put("WEOriginZ", new IntTag((int) center_z));
schematic.put("WEOffsetX", new IntTag(0));
schematic.put("WEOffsetY", new IntTag(0));
schematic.put("WEOffsetZ", new IntTag(0));
byte[] blocks = new byte[(int) ((x_width) * (y_length) * (z_height))];
byte[] addBlocks = null;
byte[] blockData = new byte[blocks.length];
ArrayList<Tag> tileEntities = new ArrayList<Tag>();
int indexer = 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++) {
int index = (int) (y * (x_width) * (z_height) + z * (x_width) + x);
BlockData bd = this.blocks.get(indexer);//blockAt(x, y, z);
indexer++;
if (bd.getMaterial().getId() > 255) {
if (addBlocks == null) {
addBlocks = new byte[(blocks.length >> 1) + 1];
}
addBlocks[index >> 1] = (byte) (((index & 1) == 0) ?
addBlocks[index >> 1] & 0xF0 | (bd.getMaterial().getId() >> 8) & 0xF
: addBlocks[index >> 1] & 0xF | ((bd.getMaterial().getId() >> 8) & 0xF) << 4);
}
blocks[index] = (byte) bd.getMaterial().getId();
blockData[index] = bd.getData();
CompoundTag rawTag = bd.getCompoundTag();
if (rawTag != null) {
HashMap<String, Tag> values = new HashMap<String, Tag>();
for (Map.Entry<String, Tag> entry : rawTag.getValue().entrySet()) {
values.put(entry.getKey(), entry.getValue());
}
// TODO: ??? -> values.put("id", new StringTag(null)); // block.getNbtId()
values.put("x", new IntTag(x));
values.put("y", new IntTag(y));
values.put("z", new IntTag(z));
CompoundTag tileEntityTag = NMSHandler.getInstance().createCompoundTag(values);
tileEntities.add(tileEntityTag);
}
}
}
}
schematic.put("Blocks", new ByteArrayTag(blocks));
schematic.put("Data", new ByteArrayTag(blockData));
schematic.put("Entities", new ListTag(CompoundTag.class, new ArrayList<Tag>()));
schematic.put("TileEntities", new ListTag(CompoundTag.class, tileEntities));
if (addBlocks != null) {
schematic.put("AddBlocks", new ByteArrayTag(addBlocks));
}
CompoundTag schematicTag = NMSHandler.getInstance().createCompoundTag(schematic);
NBTOutputStream stream = new NBTOutputStream(new GZIPOutputStream(os));
stream.writeNamedTag("Schematic", schematicTag);
os.flush();
stream.close();
}
catch (Exception e) {
dB.echoError(e);
}
}
}