add optionalNbt, use it directly in protocol.json in packets and for defining slot

This commit is contained in:
Romain Beaumont 2015-11-15 17:48:25 +01:00
parent c9290e664b
commit d25c4a2017
3 changed files with 45 additions and 63 deletions

View file

@ -44,7 +44,7 @@
"babel-runtime": "^5.4.4", "babel-runtime": "^5.4.4",
"buffer-equal": "0.0.1", "buffer-equal": "0.0.1",
"lodash.reduce": "^3.1.2", "lodash.reduce": "^3.1.2",
"minecraft-data": "^0.13.0", "minecraft-data": "^0.16.1",
"node-uuid": "~1.4.1", "node-uuid": "~1.4.1",
"prismarine-nbt": "0.1.0", "prismarine-nbt": "0.1.0",
"protodef": "0.2.0", "protodef": "0.2.0",

View file

@ -5,8 +5,8 @@ var uuid = require('node-uuid');
// TODO : remove type-specific, replace with generic containers and arrays. // TODO : remove type-specific, replace with generic containers and arrays.
module.exports = { module.exports = {
'UUID': [readUUID, writeUUID, 16], 'UUID': [readUUID, writeUUID, 16],
'slot': [readSlot, writeSlot, sizeOfSlot],
'nbt': [readNbt, writeNbt, sizeOfNbt], 'nbt': [readNbt, writeNbt, sizeOfNbt],
'optionalNbt':[readOptionalNbt,writeOptionalNbt,sizeOfOptionalNbt],
'restBuffer': [readRestBuffer, writeRestBuffer, sizeOfRestBuffer], 'restBuffer': [readRestBuffer, writeRestBuffer, sizeOfRestBuffer],
'entityMetadataLoop': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata] 'entityMetadataLoop': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata]
}; };
@ -23,67 +23,6 @@ function writeUUID(value, buffer, offset) {
return offset + 16; 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) { function readNbt(buffer, offset) {
return nbt.proto.read(buffer,offset,"nbt"); 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) { function readRestBuffer(buffer, offset) {
return { return {
value: buffer.slice(offset), value: buffer.slice(offset),

View file

@ -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], 'long': [0, 1],
'entityMetadata': [ 'entityMetadata': [
{key: 17, value: 0, type: 0} {key: 17, value: 0, type: 0}