-
-
Notifications
You must be signed in to change notification settings - Fork 961
/
Copy pathblocks.js
355 lines (310 loc) · 10.3 KB
/
blocks.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
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
var vec3 = require('vec3');
var Vec3 = vec3.Vec3;
var assert = require('assert');
var version=require("../version");
var Block = require("prismarine-block")(version);
var Painting = require('../painting');
var Location = require('../location');
var ChatMessage = require('../chat_message');
module.exports = inject;
var paintingFaceToVec = [
new Vec3(0, 0, -1),
new Vec3(-1, 0, 0),
new Vec3(0, 0, 1),
new Vec3(1, 0, 0),
];
function inject(bot) {
var columns = {};
var signs = {};
var paintingsByPos = {};
var paintingsById = {};
function addPainting(painting) {
paintingsById[painting.id] = painting;
paintingsByPos[painting.position] = painting;
}
function deletePainting(painting) {
delete paintingsById[painting.id];
delete paintingsByPos[painting.position];
}
function addColumn(args) {
var columnCorner = new Vec3(args.x * 16, 0, args.z * 16);
var key = columnKeyXZ(columnCorner.x, columnCorner.z);
if(!args.bitMap) {
// stop storing the chunk column
delete columns[key];
bot.emit("chunkColumnUnload", columnCorner);
return;
}
var column = columns[key];
if(!column) columns[key] = column = new Column();
var chunkIncluded = new Array(16);
var y;
for(y = 0; y < 16; ++y) {
chunkIncluded[y] = args.bitMap & (1 << y);
}
var offset = 0;
// block types
var size = 16 * 16 * 16 * 2;
for(y = 0; y < 16; ++y) {
var blockId = args.data.slice(offset, offset + size);
// Column.metadata doesn't exist anymore.
column.blockType[y] = chunkIncluded[y] ? blockId : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
size = 16 * 16 * 16 / 2;
// light
for(y = 0; y < 16; ++y) {
column.light[y] = chunkIncluded[y] ?
args.data.slice(offset, offset + size) : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
// sky light
if(args.skyLightSent) {
for(y = 0; y < 16; ++y) {
column.skyLight[y] = chunkIncluded[y] ?
args.data.slice(offset, offset + size) : null;
offset = chunkIncluded[y] ? offset + size : offset;
}
}
// biome
if(args.groundUp) {
size = 256;
column.biome = args.data.slice(offset, offset + size);
offset += size;
}
assert.strictEqual(offset, args.data.length);
bot.emit("chunkColumnLoad", columnCorner);
}
function findBlock(options) {
var check;
if(typeof(options.matching) !== 'function') {
if(!Array.isArray(options.matching)) {
options.matching = [options.matching];
}
check=isMatchingType;
}
else check=options.matching;
options.point = options.point || bot.entity.position;
options.maxDistance = options.maxDistance || 16;
var cursor = vec3();
var point = options.point;
var max = options.maxDistance;
var found;
for(cursor.x = point.x - max; cursor.x < point.x + max; cursor.x++) {
for(cursor.y = point.y - max; cursor.y < point.y + max; cursor.y++) {
for(cursor.z = point.z - max; cursor.z < point.z + max; cursor.z++) {
found = bot.blockAt(cursor);
if (check(found)) return found;
}
}
}
function isMatchingType(block) {
return options.matching.indexOf(block.type) >= 0;
}
}
function blockAt(absolutePoint) {
var loc = new Location(absolutePoint);
var key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z);
var column = columns[key];
// null column means chunk not loaded
if(!column) return null;
var blockType = double_bite(column.blockType);
var nibbleIndex = loc.blockIndex >> 1;
var lowNibble = loc.blockIndex % 2 === 1;
var biomeId = column.biome.readUInt8(loc.biomeBlockIndex);
var block = new Block(blockType >> 4, biomeId, blockType & 0x0f);
block.light = nib(column.light);
block.skyLight = nib(column.skyLight);
block.position = loc.floored;
block.signText = signs[loc.floored];
block.painting = paintingsByPos[loc.floored];
return block;
function double_bite(array) {
var buf = array[loc.chunkYIndex];
return buf ? buf.readUInt16LE(loc.blockIndex * 2) : 0;
}
function nib(array) {
var buf = array[loc.chunkYIndex];
return buf ? nibble(buf.readUInt8(nibbleIndex), lowNibble) : 0;
}
}
function chunkColumn(x, z) {
var key = columnKeyXZ(x, z);
return columns[key];
}
function emitBlockUpdate(oldBlock, newBlock) {
bot.emit("blockUpdate", oldBlock, newBlock);
var position = oldBlock ? oldBlock.position :
(newBlock ? newBlock.position : null);
if(position) bot.emit("blockUpdate:" + newBlock.position, oldBlock, newBlock);
}
function updateBlock(point, type, metadata) {
var oldBlock = blockAt(point);
var loc = new Location(point);
var key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z);
var column = columns[key];
// sometimes minecraft server sends us block updates before it sends
// us the column that the block is in. ignore this.
if(!column) return;
var blockTypeBuffer = column.blockType[loc.chunkYIndex];
// if it's null, it was all air, but now we're inserting a block.
if(!blockTypeBuffer) {
blockTypeBuffer = new Buffer(16 * 16 * 16 * 2);
blockTypeBuffer.fill(0);
column.blockType[loc.chunkYIndex] = blockTypeBuffer;
}
blockTypeBuffer.writeUInt16LE((type << 4) | metadata, loc.blockIndex * 2);
delete signs[loc.floored];
var painting = paintingsByPos[loc.floored];
if(painting) deletePainting(painting);
emitBlockUpdate(oldBlock, blockAt(point));
}
bot._client.on('map_chunk', function(packet) {
addColumn({
x: packet.x,
z: packet.z,
bitMap: packet.bitMap,
skyLightSent: bot.game.dimension!=="nether",
groundUp: packet.groundUp,
data: packet.chunkData,
});
});
bot._client.on('map_chunk_bulk', function(packet) {
var offset = 0;
var meta, i, size;
for(i = 0; i < packet.meta.length; ++i) {
meta = packet.meta[i];
size = (8192 + (packet.skyLightSent ? 2048 : 0)) *
onesInShort(meta.bitMap) + // block ids
2048 * onesInShort(meta.bitMap) + // (two bytes per block id)
256; // biomes
addColumn({
x: meta.x,
z: meta.z,
bitMap: meta.bitMap,
skyLightSent: packet.skyLightSent,
groundUp: true,
data: packet.data.slice(offset, offset + size),
});
offset += size;
}
assert.strictEqual(offset, packet.data.length);
});
bot._client.on('multi_block_change', function(packet) {
// multi block change
var i, record, metadata, type, blockX, blockZ, y, pt;
for(i = 0; i < packet.records.length; ++i) {
record = packet.records[i];
metadata = (record.blockId & 0x0f);
type = (record.blockId) >> 4;
y = record.y;
blockZ = (record.horizontalPos & 0x0f);
blockX = (record.horizontalPos >> 4) & 0x0f;
pt = new Vec3(packet.chunkX * 16 + blockX, y, packet.chunkZ * 16 + blockZ);
updateBlock(pt, type, metadata);
}
});
bot._client.on('block_change', function(packet) {
// block change
var pt = new Vec3(packet.location.x, packet.location.y, packet.location.z);
updateBlock(pt, packet.type >> 4, packet.type & 0x0f);
});
bot._client.on('explosion', function(packet) {
// explosion
packet.affectedBlockOffsets.forEach(function(offset) {
var pt = vec3(offset).offset(packet.x, packet.y, packet.z);
updateBlock(pt.floor(), 0, 0);
});
});
bot._client.on('spawn_entity_painting', function(packet) {
var pos = new Vec3(packet.location.x, packet.location.y, packet.location.z);
var painting = new Painting(packet.entityId,
pos, packet.title, paintingFaceToVec[packet.direction]);
addPainting(painting);
});
bot._client.on('entity_destroy', function(packet) {
// destroy entity
packet.entityIds.forEach(function(id) {
var painting = paintingsById[id];
if(painting) deletePainting(painting);
});
});
bot._client.on('update_sign', function(packet) {
// update sign
var pos = new Vec3(packet.location.x, packet.location.y, packet.location.z);
var oldBlock = blockAt(pos);
// Some servers send blank lines in signs wrong, causing the chat handler to crash.
if(packet.text1 == 'null' || packet.text1 == '') {
packet.text1 = '""'
}
if(packet.text2 == 'null' || packet.text2 == '') {
packet.text2 = '""'
}
if(packet.text3 == 'null' || packet.text3 == '') {
packet.text3 = '""'
}
if(packet.text4 == 'null' || packet.text4 == '') {
packet.text4 = '""'
}
signs[pos] = new ChatMessage(JSON.parse(packet.text1)) + "\n" + new ChatMessage(JSON.parse(packet.text2)) +
"\n" + new ChatMessage(JSON.parse(packet.text3)) + "\n" + new ChatMessage(JSON.parse(packet.text4));
emitBlockUpdate(oldBlock, blockAt(pos));
});
bot.updateSign = function(block, text) {
var lines = text.split("\n");
if(lines.length > 4) {
bot.emit("error", new Error("too many lines for sign text"));
return;
}
for(var i = 0; i < lines.length; ++i) {
if(lines[i].length > 15) {
bot.emit("error", new Error("signs have max line length 15"));
return;
}
}
bot._client.write('update_sign', {
location: block.position,
text1: JSON.stringify(lines[0]),
text2: JSON.stringify(lines[1]),
text3: JSON.stringify(lines[2]),
text4: JSON.stringify(lines[3])
});
};
// if we get a respawn packet and the dimension is changed,
// unload all chunks from memory.
var dimension;
bot._client.on('login', function(packet) {
dimension = packet.dimension
});
bot._client.on('respawn', function(packet) {
if(dimension === packet.dimension) return;
dimension = packet.dimension;
columns = {};
});
bot.findBlock = findBlock;
bot.blockAt = blockAt;
bot._chunkColumn = chunkColumn;
bot._updateBlock = updateBlock;
}
function columnKeyXZ(x, z) {
return x + ',' + z;
}
function onesInShort(n) {
n = n & 0xffff;
var count = 0;
for(var i = 0; i < 16; ++i) {
count = ((1 << i) & n) ? count + 1 : count;
}
return count;
}
function nibble(wholeByte, low) {
return low ?
wholeByte & 0x0f :
wholeByte >> 4;
}
function Column() {
this.blockType = new Array(16);
this.light = new Array(16);
this.skyLight = new Array(16);
this.biome = null;
}