mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
Add option type
This commit is contained in:
parent
9d7e4b5943
commit
ed372e6f39
2 changed files with 31 additions and 0 deletions
|
@ -2,6 +2,7 @@ var { getField, getFieldInfo } = require('../utils');
|
|||
|
||||
module.exports = {
|
||||
'switch': [readSwitch, writeSwitch, sizeOfSwitch],
|
||||
'option': [readOption, writeOption, sizeOfOption],
|
||||
};
|
||||
|
||||
function readSwitch(buffer, offset, typeArgs, rootNode) {
|
||||
|
@ -39,3 +40,30 @@ function sizeOfSwitch(value, typeArgs, rootNode) {
|
|||
fieldInfo = getFieldInfo(typeArgs.fields[compareTo]);
|
||||
return this.sizeOf(value, fieldInfo, rootNode);
|
||||
}
|
||||
|
||||
function readOption(buffer, offset, typeArgs, context) {
|
||||
var val = buffer.readUInt8(offset++);
|
||||
if (val !== 0) {
|
||||
var retVal = this.read(buffer, offset, typeArgs, context);
|
||||
retval.size++;
|
||||
return retval;
|
||||
} else {
|
||||
return {
|
||||
size: 1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function writeOption(value, buffer, offset, typeArgs, context) {
|
||||
if (value != null) {
|
||||
buffer.writeUInt8(1, offset++);
|
||||
this.write(value, buffer, offset, typeArgs, context);
|
||||
} else {
|
||||
buffer.writeUInt8(0, offset++);
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
function sizeOfOption(value, typeArgs, context) {
|
||||
return value == null ? 1 : this.sizeOf(value, typeArgs, context) + 1;
|
||||
}
|
||||
|
|
|
@ -120,6 +120,9 @@ var values = {
|
|||
else
|
||||
return getValue(i, packet);
|
||||
},
|
||||
'option': function(typeArgs, packet) {
|
||||
return getValue(typeArgs, packet);
|
||||
}
|
||||
};
|
||||
|
||||
function getValue(_type, packet) {
|
||||
|
|
Loading…
Reference in a new issue