From d25c4a20170a13a0dda53746d075ad796caac797 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 15 Nov 2015 17:48:25 +0100 Subject: [PATCH] add optionalNbt, use it directly in protocol.json in packets and for defining slot --- package.json | 2 +- src/datatypes/minecraft.js | 82 ++++++++++---------------------------- test/packetTest.js | 24 +++++++++++ 3 files changed, 45 insertions(+), 63 deletions(-) diff --git a/package.json b/package.json index c09be9d..bd4514e 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "babel-runtime": "^5.4.4", "buffer-equal": "0.0.1", "lodash.reduce": "^3.1.2", - "minecraft-data": "^0.13.0", + "minecraft-data": "^0.16.1", "node-uuid": "~1.4.1", "prismarine-nbt": "0.1.0", "protodef": "0.2.0", diff --git a/src/datatypes/minecraft.js b/src/datatypes/minecraft.js index 30c3f1e..786b6f1 100644 --- a/src/datatypes/minecraft.js +++ b/src/datatypes/minecraft.js @@ -5,8 +5,8 @@ var uuid = require('node-uuid'); // TODO : remove type-specific, replace with generic containers and arrays. module.exports = { 'UUID': [readUUID, writeUUID, 16], - 'slot': [readSlot, writeSlot, sizeOfSlot], 'nbt': [readNbt, writeNbt, sizeOfNbt], + 'optionalNbt':[readOptionalNbt,writeOptionalNbt,sizeOfOptionalNbt], 'restBuffer': [readRestBuffer, writeRestBuffer, sizeOfRestBuffer], 'entityMetadataLoop': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata] }; @@ -23,67 +23,6 @@ function writeUUID(value, buffer, offset) { return offset + 16; } -function readSlot(buffer, offset) { - var value = {}; - var results = types.short[0](buffer, offset); - if(!results) return null; - value.blockId = results.value; - - if(value.blockId === -1) { - return { - value: value, - size: 2, - }; - } - - var cursorEnd = offset + 6; - if(cursorEnd > buffer.length) return null; - value.itemCount = buffer.readInt8(offset + 2); - value.itemDamage = buffer.readInt16BE(offset + 3); - var nbtData = buffer.readInt8(offset + 5); - if(nbtData == 0) { - return { - value: value, - size: 6 - } - } - var nbtData = readNbt(buffer, offset + 5); - value.nbtData = nbtData.value; - return { - value: value, - size: nbtData.size + 5 - }; -} - -function writeSlot(value, buffer, offset) { - buffer.writeInt16BE(value.blockId, offset); - if(value.blockId === -1) return offset + 2; - buffer.writeInt8(value.itemCount, offset + 2); - buffer.writeInt16BE(value.itemDamage, offset + 3); - var nbtDataLen; - if(value.nbtData) { - var newbuf = nbt.writeUncompressed(value.nbtData); - newbuf.copy(buffer, offset + 5); - nbtDataLen = newbuf.length; - } - else { - buffer.writeInt8(0, offset + 5); - nbtDataLen = 1; - } - return offset + 5 + nbtDataLen; -} - -function sizeOfSlot(value) { - if(value.blockId === -1) - return (2); - else if(!value.nbtData) { - return (6); - } else { - return (5 + sizeOfNbt(value.nbtData)); - } -} - - function readNbt(buffer, offset) { return nbt.proto.read(buffer,offset,"nbt"); } @@ -97,6 +36,25 @@ function sizeOfNbt(value) { } +function readOptionalNbt(buffer, offset) { + if(buffer.readInt8(offset) == 0) return {size:1}; + return nbt.proto.read(buffer,offset,"nbt"); +} + +function writeOptionalNbt(value, buffer, offset) { + if(value==undefined) { + buffer.writeInt8(0,offset); + return offset+1; + } + return nbt.proto.write(value,buffer,offset,"nbt"); +} + +function sizeOfOptionalNbt(value) { + if(value==undefined) + return 1; + return nbt.proto.sizeOf(value,"nbt"); +} + function readRestBuffer(buffer, offset) { return { value: buffer.slice(offset), diff --git a/test/packetTest.js b/test/packetTest.js index 9910059..08cf3b0 100644 --- a/test/packetTest.js +++ b/test/packetTest.js @@ -68,6 +68,30 @@ var values = { } } }, + 'nbt':{ + type:"compound", + name: "test", value: { + test1: {type: "int", value: 4}, + test2: {type: "long", value: [12, 42]}, + test3: {type: "byteArray", value: [32]}, + test4: {type: "string", value: "ohi"}, + test5: {type: "list", value: {type: "int", value: [4]}}, + test6: {type: "compound", value: {test: {type: "int", value: 4}}}, + test7: {type: "intArray", value: [12, 42]} + } + }, + 'optionalNbt':{ + type:"compound", + name: "test", value: { + test1: {type: "int", value: 4}, + test2: {type: "long", value: [12, 42]}, + test3: {type: "byteArray", value: [32]}, + test4: {type: "string", value: "ohi"}, + test5: {type: "list", value: {type: "int", value: [4]}}, + test6: {type: "compound", value: {test: {type: "int", value: 4}}}, + test7: {type: "intArray", value: [12, 42]} + } + }, 'long': [0, 1], 'entityMetadata': [ {key: 17, value: 0, type: 0}