-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
342 lines (306 loc) · 9.57 KB
/
main.js
File metadata and controls
342 lines (306 loc) · 9.57 KB
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
const NBT = require("parsenbt-js");
function clamp(x, a, b) {
return x < a ? a : x > b ? b : x
}
class MCStructure {
/**
* Deserialize mcstructure from blob.
* @param {ArrayBuffer} buf - Input buffer
* @returns {MCStructure}
*/
static deserialize(buf) {
var snbt = NBT.Reader(buf, { littleEndian: true, asProxy: true })["comp>"]
, stru = snbt["comp>structure"]
, result = new MCStructure(
snbt["list>size"][1],
snbt["list>size"][2],
snbt["list>size"][3]
);
result.origin = {
x: snbt["list>structure_world_origin"][1],
y: snbt["list>structure_world_origin"][2],
z: snbt["list>structure_world_origin"][3]
};
result.version = snbt["i32>format_version"];
result.blockIndices.extron = Int32Array.from(stru["list>block_indices"][1].slice(1));
result.blockIndices.intron = Int32Array.from(stru["list>block_indices"][2].slice(1));
result.entities = stru["list>entities"].slice(1);
result.palette.block = stru["comp>palette"]["comp>default"]["list>block_palette"].slice(1);
result.palette.blockEntity = stru["comp>palette"]["comp>default"]["comp>block_position_data"];
return result
}
/**
* Remove unused blocks in structure.
*
* Changes original object.
* @param {MCStructure} a
* @returns {MCStructure}
*/
static makePruned(a) {
var bitset = new Uint8Array(Math.ceil(a.palette.block.length / 8))
, map = new Int32Array(a.palette.block.length);
for (var d of a.blockIndices.intron)
if (d >= 0)
bitset[d >> 8] |= 1 << d & 7;
for (var d of a.blockIndices.extron)
if (d >= 0)
bitset[d >> 8] |= 1 << d & 7;
for (var i = 0, j = 0; i < a.palette.block.length; i++)
if (bitset[i >> 8] & (1 << i & 7)) {
map[i] = j;
j++;
} else
a.palette.block.splice(i);
for (var i = 0, j = a.getVolume(); i < j; i++) {
if (a.blockIndices.extron[i] >= 0)
a.blockIndices.extron[i] = map[a.blockIndices.extron[i]];
if (a.blockIndices.intron[i] >= 0)
a.blockIndices.intron[i] = map[a.blockIndices.intron[i]];
}
return a
}
/**
* Create a mcstructure with given size.
* @param {number} xL - X side length.
* @param {number} yL - Y side length.
* @param {number} zL - Z side length.
*/
constructor(xL, yL, zL) {
if (xL < 0 || yL < 0 || zL < 0 || xL * yL * zL > 1073741824)
throw new Error("Invalid structure size");
this.version = 1;
this.size = {
xL: xL,
yL: yL,
zL: zL
};
this.origin = {
x: 0,
y: 0,
z: 0
};
this.blockIndices = {
extron: new Int32Array(xL * yL * zL).fill(-1),
intron: new Int32Array(xL * yL * zL).fill(-1)
};
this.palette = {
block: [],
blockEntity: {}
}
this.entities = [];
// Entity ID
this.uniqueID = 0n;
// Specify the layer where the operation is performed.
this.operationOnLayer = "extron";
}
/**
* Get volume of of the structure.
* @returns {number}
*/
getVolume() {
return this.size.xL * this.size.yL * this.size.zL
}
/**
* Get the size of of the structure.
* @returns {{x:number, y:number, z:number}}
*/
getSize() {
return {
x: this.size.xL,
y: this.size.yL,
z: this.size.zL
};
}
/**
* Get a block in the structure.
* @param {object} pos - Position in the structure.
* @param {number} pos.x - Pos X.
* @param {number} pos.y - Pos Y.
* @param {number} pos.z - Pos Z.
* @returns {object}
*/
getBlock(pos) {
var x = clamp(pos.x, 0, this.size.xL)
, y = clamp(pos.y, 0, this.size.yL)
, z = clamp(pos.z, 0, this.size.zL)
, index = z + this.size.zL * (y + this.size.yL * x)
, block = this.blockIndices[this.operationOnLayer][index];
return this.palette.block[block];
}
/**
* Get block entity.
* @param {object} pos - Position in the structure.
* @param {number} pos.x - Pos X.
* @param {number} pos.y - Pos Y.
* @param {number} pos.z - Pos Z.
* @returns {object}
*/
getBlockData(pos) {
var x = clamp(pos.x, 0, this.size.xL)
, y = clamp(pos.y, 0, this.size.yL)
, z = clamp(pos.z, 0, this.size.zL)
, index = z + this.size.zL * (y + this.size.yL * x)
return this.palette.blockEntity["comp>" + index]["comp>block_entity_data"];
}
/**
* Get specified entity.
* @param {BigInt|number} uniqueID - Entity ID.
* @returns {object|null} The specified entity or null.
*/
getEntity(uniqueID) {
for (var e of this.entities)
if (e["i64>UniqueID"] == BigInt(uniqueID))
return e;
return null
}
/**
* Place a block, and put given NBT data into block indices.
* @param {object} pos - Position in the structure.
* @param {number} pos.x - Pos X.
* @param {number} pos.y - Pos Y.
* @param {number} pos.z - Pos Z.
* @param {object} block - Data of the block.
* @returns {boolean}
*/
setBlock(pos, block) {
var x = clamp(pos.x, 0, this.size.xL)
, y = clamp(pos.y, 0, this.size.yL)
, z = clamp(pos.z, 0, this.size.zL)
, index = z + this.size.zL * (y + this.size.yL * x);
for (var i = 0; i < this.palette.block.length; i++)
if (NBT.equal(this.palette.block[i], block)) {
this.blockIndices[this.operationOnLayer][index] = i;
return true
}
this.palette.block.push(block);
this.blockIndices[this.operationOnLayer][index] = this.palette.block.length - 1;
return true
}
/**
* Set the block entity of a block.
* @param {object} pos - Position in the structure.
* @param {number} pos.x - Pos X.
* @param {number} pos.y - Pos Y.
* @param {number} pos.z - Pos Z.
* @param {object} nbt - Block entity data.
* @returns {boolean}
*/
setBlockData(pos, nbt) {
var x = clamp(pos.x, 0, this.size.xL)
, y = clamp(pos.y, 0, this.size.yL)
, z = clamp(pos.z, 0, this.size.zL)
, index = z + this.size.zL * (y + this.size.yL * x)
, be = NBT.create(true)
, bd;
bd = be["comp>block_entity_data"] = NBT.assign(NBT.create(true), nbt);
bd["i32>x"] = x - this.origin.x;
bd["i32>y"] = y - this.origin.y;
bd["i32>z"] = z - this.origin.z;
this.palette.blockEntity["comp>" + index] = be;
return true
}
/**
* Fill specified area with given block.
* @param {object} from - Position in the structure.
* @param {number} from.x
* @param {number} from.y
* @param {number} from.z
* @param {object} to - Position in the structure.
* @param {number} to.x
* @param {number} to.y
* @param {number} to.z
* @param {object} block - Data of the block.
* @returns {boolean}
*/
fill(from, to, block) {
var pb = this.palette.block
, pos = [
clamp(from.x, 0, this.size.xL),
clamp(from.y, 0, this.size.yL),
clamp(from.z, 0, this.size.zL),
clamp(to.x, 0, this.size.xL),
clamp(to.y, 0, this.size.yL),
clamp(to.z, 0, this.size.zL)
]
, bi, index;
for (var i = 0; i < 3; i++) {
pos[i] = Math.floor(pos[i]);
pos[i + 3] = Math.floor(pos[i + 3]);
if (pos[i] > pos[i + 3])
[pos[i], pos[i + 3]] = [pos[i + 3], pos[i]];
}
for (var i = 0; i < pb.length; i++)
if (NBT.equal(pb[i], block)) {
bi = i;
break
} else if (i == pb.length - 1) {
bi = pb.length;
pb.push(block);
}
for (var xC = pos[0]; xC <= pos[3]; xC++)
for (var yC = pos[1]; yC <= pos[4]; yC++)
for (var zC = pos[2]; zC <= pos[5]; zC++) {
index = zC + this.size.zL * (yC + this.size.yL * xC);
this.blockIndices[this.operationOnLayer][index] = bi;
}
return true
}
/**
* Place an entity.
* @param {object} entity - Entity data.
* @param {object} pos - Position in the structure.
* @param {number} pos.x - Pos X.
* @param {number} pos.y - Pos Y.
* @param {number} pos.z - Pos Z.
* @returns {BigInt} Entity ID.
*/
summon(entity, pos) {
if (pos) {
var x = clamp(pos.x, 0, this.size.xL)
, y = clamp(pos.y, 0, this.size.yL)
, z = clamp(pos.z, 0, this.size.zL);
entity["list>Pos"] = ["f32", x, y, z];
}
entity["i64>UniqueID"] = this.uniqueID;
this.entities.push(entity);
return this.uniqueID++
}
/**
* Remove specified entity.
* @param {BigInt|number} uniqueID - Entity ID.
* @returns {object|null} The entity removed or null.
*/
kill(uniqueID) {
for (var i = 0; i < this.entities.length; i++)
if (this.entities[i]["i64>UniqueID"] == BigInt(uniqueID))
return this.entities.splice(i, 1);
return null
}
/**
* Serialize MCStructure object to NBT.
* @returns {ArrayBuffer}
*/
serialize() {
var result = NBT.create()
, stru = NBT.create();
result["list>size"] = ["i32", this.size.xL, this.size.yL, this.size.zL];
result["list>structure_world_origin"] = ["i32", this.origin.x, this.origin.y, this.origin.z];
result["i32>format_version"] = this.version;
result["comp>structure"] = stru;
stru["list>block_indices"] = [
"list",
this.blockIndices.extron,
this.blockIndices.intron
];
stru["list>entities"] = ["comp", ...this.entities];
stru["comp>palette"] = NBT.create();
stru["comp>palette"]["comp>default"] = NBT.create();
stru["comp>palette"]["comp>default"]["list>block_palette"] = [
"comp",
...this.palette.block
];
stru["comp>palette"]["comp>default"]["comp>block_position_data"] = this.palette.blockEntity;
return NBT.Writer(result, { littleEndian: true, allowBigInt: true, allowTypedArray: true })
}
}
module.exports = MCStructure;