diff --git a/lib/plugins/blocks.js b/lib/plugins/blocks.js index b5ab977f4..cd3500778 100644 --- a/lib/plugins/blocks.js +++ b/lib/plugins/blocks.js @@ -6,6 +6,7 @@ var Block = require("prismarine-block")(version); var Painting = require('../painting'); var Location = require('../location'); var ChatMessage = require('../chat_message'); +var Chunk = require('prismarine-chunk')(version); module.exports = inject; @@ -42,47 +43,10 @@ function inject(bot) { 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; + if(!column) columns[key] = column = new Chunk(); - // 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; - } + column.load(args.data,args.bitMap); - assert.strictEqual(offset, args.data.length); bot.emit("chunkColumnLoad", columnCorner); } @@ -115,36 +79,25 @@ function inject(bot) { } } + function posInChunk(pos) + { + return pos.floored().modulus(new Vec3(16,256,16)); + } + 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=column.getBlock(posInChunk(absolutePoint)); 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) { @@ -167,14 +120,8 @@ function inject(bot) { // 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); + column.setBlockType(posInChunk(point),type); + column.setBlockData(posInChunk(point),metadata); delete signs[loc.floored]; diff --git a/package.json b/package.json index f0a3b1ebe..cd9c21391 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "mojangson": "^0.2.1", "prismarine-biome": "^1.0.1", "prismarine-block": "^1.0.1", + "prismarine-chunk": "^1.3.0", "prismarine-entity": "^0.2.0", "prismarine-item": "^1.0.1", "prismarine-recipe": "^1.0.1",