Skip to content

Commit a4c3259

Browse files
authored
Merge pull request #465 from PrismarineJS/pchunk
use prismarine-chunk, work on #334
2 parents 66dfdff + 0da3b21 commit a4c3259

File tree

2 files changed

+13
-65
lines changed

2 files changed

+13
-65
lines changed

lib/plugins/blocks.js

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Block = require("prismarine-block")(version);
66
var Painting = require('../painting');
77
var Location = require('../location');
88
var ChatMessage = require('../chat_message');
9+
var Chunk = require('prismarine-chunk')(version);
910

1011
module.exports = inject;
1112

@@ -42,47 +43,10 @@ function inject(bot) {
4243
return;
4344
}
4445
var column = columns[key];
45-
if(!column) columns[key] = column = new Column();
46-
var chunkIncluded = new Array(16);
47-
var y;
48-
for(y = 0; y < 16; ++y) {
49-
chunkIncluded[y] = args.bitMap & (1 << y);
50-
}
51-
52-
var offset = 0;
46+
if(!column) columns[key] = column = new Chunk();
5347

54-
// block types
55-
var size = 16 * 16 * 16 * 2;
56-
for(y = 0; y < 16; ++y) {
57-
var blockId = args.data.slice(offset, offset + size);
58-
59-
// Column.metadata doesn't exist anymore.
60-
column.blockType[y] = chunkIncluded[y] ? blockId : null;
61-
offset = chunkIncluded[y] ? offset + size : offset;
62-
}
63-
size = 16 * 16 * 16 / 2;
64-
// light
65-
for(y = 0; y < 16; ++y) {
66-
column.light[y] = chunkIncluded[y] ?
67-
args.data.slice(offset, offset + size) : null;
68-
offset = chunkIncluded[y] ? offset + size : offset;
69-
}
70-
// sky light
71-
if(args.skyLightSent) {
72-
for(y = 0; y < 16; ++y) {
73-
column.skyLight[y] = chunkIncluded[y] ?
74-
args.data.slice(offset, offset + size) : null;
75-
offset = chunkIncluded[y] ? offset + size : offset;
76-
}
77-
}
78-
// biome
79-
if(args.groundUp) {
80-
size = 256;
81-
column.biome = args.data.slice(offset, offset + size);
82-
offset += size;
83-
}
48+
column.load(args.data,args.bitMap);
8449

85-
assert.strictEqual(offset, args.data.length);
8650
bot.emit("chunkColumnLoad", columnCorner);
8751
}
8852

@@ -115,36 +79,25 @@ function inject(bot) {
11579
}
11680
}
11781

82+
function posInChunk(pos)
83+
{
84+
return pos.floored().modulus(new Vec3(16,256,16));
85+
}
86+
11887
function blockAt(absolutePoint) {
11988
var loc = new Location(absolutePoint);
12089
var key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z);
90+
12191
var column = columns[key];
12292
// null column means chunk not loaded
12393
if(!column) return null;
124-
var blockType = double_bite(column.blockType);
125-
var nibbleIndex = loc.blockIndex >> 1;
126-
var lowNibble = loc.blockIndex % 2 === 1;
12794

128-
var biomeId = column.biome.readUInt8(loc.biomeBlockIndex);
129-
130-
var block = new Block(blockType >> 4, biomeId, blockType & 0x0f);
131-
block.light = nib(column.light);
132-
block.skyLight = nib(column.skyLight);
95+
block=column.getBlock(posInChunk(absolutePoint));
13396
block.position = loc.floored;
13497
block.signText = signs[loc.floored];
13598
block.painting = paintingsByPos[loc.floored];
13699

137100
return block;
138-
139-
function double_bite(array) {
140-
var buf = array[loc.chunkYIndex];
141-
return buf ? buf.readUInt16LE(loc.blockIndex * 2) : 0;
142-
}
143-
144-
function nib(array) {
145-
var buf = array[loc.chunkYIndex];
146-
return buf ? nibble(buf.readUInt8(nibbleIndex), lowNibble) : 0;
147-
}
148101
}
149102

150103
function chunkColumn(x, z) {
@@ -167,14 +120,8 @@ function inject(bot) {
167120
// sometimes minecraft server sends us block updates before it sends
168121
// us the column that the block is in. ignore this.
169122
if(!column) return;
170-
var blockTypeBuffer = column.blockType[loc.chunkYIndex];
171-
// if it's null, it was all air, but now we're inserting a block.
172-
if(!blockTypeBuffer) {
173-
blockTypeBuffer = new Buffer(16 * 16 * 16 * 2);
174-
blockTypeBuffer.fill(0);
175-
column.blockType[loc.chunkYIndex] = blockTypeBuffer;
176-
}
177-
blockTypeBuffer.writeUInt16LE((type << 4) | metadata, loc.blockIndex * 2);
123+
column.setBlockType(posInChunk(point),type);
124+
column.setBlockData(posInChunk(point),metadata);
178125

179126
delete signs[loc.floored];
180127

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"mojangson": "^0.2.1",
2222
"prismarine-biome": "^1.0.1",
2323
"prismarine-block": "^1.0.1",
24+
"prismarine-chunk": "^1.3.0",
2425
"prismarine-entity": "^0.2.0",
2526
"prismarine-item": "^1.0.1",
2627
"prismarine-recipe": "^1.0.1",

0 commit comments

Comments
 (0)