forked from 1b8/schematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.76 KB
/
index.js
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
var nbt = require('prismarine-nbt');
var vec3 = require('vec3');
module.exports = function (version) {
var Block = require('./lib/block')(version);
var BlockEntity = require('./lib/block-entity')(version);
var Entity = require('./lib/entity');
Schematic.parse = function (data, callback) {
if (!callback) callback = function (err) {
if (err) throw err;
};
nbt.parse(data, function (err, data) {
if (err) callback(err);
callback(err, new Schematic(nbt.simplify(data)));
});
};
function Schematic(raw) {
this._ = raw;
var entities = this._.Entities;
var blockEntities = this._.TileEntities;
this.raw = raw;
this.width = this._.Width;
this.height = this._.Height;
this.length = this._.Length;
this.entities = [];
this.blockEntities = [];
if(entities) {
for (var i = 0; i < entities.length; i++)
this.entities.push(new Entity(entities[i]));
}
if(blockEntities) {
for (i = 0; i < blockEntities.length; i++)
this.blockEntities.push(new BlockEntity(this, blockEntities[i]));
}
}
Schematic.prototype.getBlock = function (x, y, z) {
var pos = typeof x === 'number' ? vec3(x, y, z) : x;
// Check if it's a block entity
var blockEntity;
for (var i = 0; i < this.blockEntities.length; i++) {
blockEntity = this.blockEntities[i];
if (blockEntity.position.equals(pos)) return blockEntity;
}
var args = this._blockArgs(pos);
return new Block(args[0], args[1], args[2]);
};
// Returns the arguments to pass into new Block
Schematic.prototype._blockArgs = function (pos) {
var index = (pos.y * this.length + pos.z) * this.width + pos.x;
return [this._.Blocks[index]&0xff, this._.Data[index], pos];
};
return Schematic;
};