2013-01-04 19:04:06 -05:00
|
|
|
var assert = require('assert');
|
2013-04-05 23:02:36 -04:00
|
|
|
var util = require('util');
|
2014-12-30 03:55:04 -05:00
|
|
|
var zlib = require('zlib');
|
2015-03-06 13:50:57 -05:00
|
|
|
var nbt = require('prismarine-nbt');
|
2013-01-03 23:14:19 -05:00
|
|
|
|
|
|
|
var STRING_MAX_LENGTH = 240;
|
2013-08-05 07:14:12 -04:00
|
|
|
var SRV_STRING_MAX_LENGTH = 32767;
|
2013-01-03 23:14:19 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
// This is really just for the client.
|
|
|
|
var states = {
|
|
|
|
"HANDSHAKING": "handshaking",
|
|
|
|
"STATUS": "status",
|
|
|
|
"LOGIN": "login",
|
|
|
|
"PLAY": "play"
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
var packets = {
|
2014-01-14 00:56:56 -05:00
|
|
|
handshaking: {
|
|
|
|
toClient: {},
|
2014-12-30 13:23:58 -05:00
|
|
|
toServer: {
|
2014-03-16 13:06:57 -04:00
|
|
|
set_protocol: {id: 0x00, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "protocolVersion", type: "varint" },
|
|
|
|
{ name: "serverHost", type: "string" },
|
|
|
|
{ name: "serverPort", type: "ushort" },
|
|
|
|
{ name: "nextState", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
|
|
|
},
|
2013-08-05 07:14:12 -04:00
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
|
2014-03-16 13:06:57 -04:00
|
|
|
// TODO : protocollib names aren't the best around here
|
2014-01-14 00:56:56 -05:00
|
|
|
status: {
|
|
|
|
toClient: {
|
2014-03-16 13:06:57 -04:00
|
|
|
server_info: {id: 0x00, fields: [
|
2014-03-16 13:52:17 -04:00
|
|
|
{ name: "response", type: "ustring" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
ping: {id: 0x01, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "time", type: "long" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
toServer: {
|
2014-03-16 13:06:57 -04:00
|
|
|
ping_start: {id: 0x00, fields: []},
|
|
|
|
ping: {id: 0x01, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "time", type: "long" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
|
|
|
|
login: {
|
|
|
|
toClient: {
|
2014-03-16 13:06:57 -04:00
|
|
|
disconnect: {id: 0x00, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "reason", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
encryption_begin: {id: 0x01, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "serverId", type: "string" },
|
2015-01-01 17:20:47 -05:00
|
|
|
{ name: "publicKeyLength", type: "count", typeArgs: { type: "varint", countFor: "publicKey" } },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "publicKey", type: "buffer", typeArgs: { count: "publicKeyLength" } },
|
2015-01-01 17:20:47 -05:00
|
|
|
{ name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
success: {id: 0x02, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "uuid", type: "string" },
|
|
|
|
{ name: "username", type: "string" }
|
2014-12-30 03:08:21 -05:00
|
|
|
]},
|
|
|
|
compress: { id: 0x03, fields: [
|
|
|
|
{ name: "threshold", type: "varint"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
toServer: {
|
|
|
|
login_start: {id: 0x00, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "username", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
encryption_begin: {id: 0x01, fields: [
|
2015-01-01 17:20:47 -05:00
|
|
|
{ name: "sharedSecretLength", type: "count", typeArgs: { type: "varint", countFor: "sharedSecret" } },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "sharedSecret", type: "buffer", typeArgs: { count: "sharedSecretLength" } },
|
2015-01-01 17:20:47 -05:00
|
|
|
{ name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } },
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
|
|
|
|
play: {
|
|
|
|
toClient: {
|
|
|
|
keep_alive: {id: 0x00, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "keepAliveId", type: "varint" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
login: {id: 0x01, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "int" },
|
|
|
|
{ name: "gameMode", type: "ubyte" },
|
|
|
|
{ name: "dimension", type: "byte" },
|
|
|
|
{ name: "difficulty", type: "ubyte" },
|
|
|
|
{ name: "maxPlayers", type: "ubyte" },
|
|
|
|
{ name: "levelType", type: "string" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "reducedDebugInfo", type: "bool"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
chat: {id: 0x02, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "message", type: "ustring" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "position", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
update_time: {id: 0x03, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "age", type: "long" },
|
|
|
|
{ name: "time", type: "long" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_equipment: {id: 0x04, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "slot", type: "short" },
|
2014-04-13 12:05:54 -04:00
|
|
|
{ name: "item", type: "slot" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
spawn_position: {id: 0x05, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" } /* TODO: Implement position */
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
update_health: {id: 0x06, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "health", type: "float" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "food", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "foodSaturation", type: "float" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
respawn: {id: 0x07, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "dimension", type: "int" },
|
|
|
|
{ name: "difficulty", type: "ubyte" },
|
|
|
|
{ name: "gamemode", type: "ubyte" },
|
|
|
|
{ name: "levelType", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
position: {id: 0x08, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "double" },
|
|
|
|
{ name: "y", type: "double" },
|
|
|
|
{ name: "z", type: "double" },
|
|
|
|
{ name: "yaw", type: "float" },
|
|
|
|
{ name: "pitch", type: "float" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "flags", type: "byte" /* <Dinnerbone> It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is set, the x value is relative and not absolute. */}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
held_item_slot: {id: 0x09, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "slot", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
bed: {id: 0x0a, fields: [
|
2015-03-21 21:11:52 -04:00
|
|
|
{ name: "entityId", type: "varint" },
|
2015-01-01 17:20:47 -05:00
|
|
|
{ name: "location", type: "position" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
animation: {id: 0x0b, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "animation", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
named_entity_spawn: {id: 0x0c, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "playerUUID", type: "UUID"},
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "yaw", type: "byte" },
|
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "currentItem", type: "short" },
|
|
|
|
{ name: "metadata", type: "entityMetadata" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
collect: {id: 0x0d, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "collectedEntityId", type: "varint" },
|
|
|
|
{ name: "collectorEntityId", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
spawn_entity: {id: 0x0e, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "type", type: "byte" },
|
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "yaw", type: "byte" },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "objectData", type: "container", typeArgs: { fields: [
|
|
|
|
{ name: "intField", type: "int" },
|
|
|
|
{ name: "velocityX", type: "short", condition: function(field_values) {
|
|
|
|
return field_values['this']['intField'] != 0;
|
|
|
|
}},
|
|
|
|
{ name: "velocityY", type: "short", condition: function(field_values) {
|
|
|
|
return field_values['this']['intField'] != 0;
|
|
|
|
}},
|
|
|
|
{ name: "velocityZ", type: "short", condition: function(field_values) {
|
|
|
|
return field_values['this']['intField'] != 0;
|
|
|
|
}}
|
2014-12-30 13:23:58 -05:00
|
|
|
]}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
spawn_entity_living: {id: 0x0f, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "type", type: "ubyte" },
|
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "yaw", type: "byte" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "headPitch", type: "byte" },
|
|
|
|
{ name: "velocityX", type: "short" },
|
|
|
|
{ name: "velocityY", type: "short" },
|
|
|
|
{ name: "velocityZ", type: "short" },
|
|
|
|
{ name: "metadata", type: "entityMetadata" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
spawn_entity_painting: {id: 0x10, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "title", type: "string" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
|
|
|
{ name: "direction", type: "ubyte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
spawn_entity_experience_orb: {id: 0x11, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "count", type: "short" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_velocity: {id: 0x12, fields: [
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "velocityX", type: "short" },
|
|
|
|
{ name: "velocityY", type: "short" },
|
|
|
|
{ name: "velocityZ", type: "short" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
entity_destroy: {id: 0x13, fields: [
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "varint", countFor: "entityIds" } },
|
|
|
|
{ name: "entityIds", type: "array", typeArgs: { type: "varint", count: "count" } }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity: {id: 0x14, fields: [
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "entityId", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
rel_entity_move: {id: 0x15, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "dX", type: "byte" },
|
|
|
|
{ name: "dY", type: "byte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "dZ", type: "byte" },
|
|
|
|
{ name: "onGround", type: "bool"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_look: {id: 0x16, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "yaw", type: "byte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "onGround", type: "bool"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
entity_move_look: {id: 0x17, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "dX", type: "byte" },
|
|
|
|
{ name: "dY", type: "byte" },
|
|
|
|
{ name: "dZ", type: "byte" },
|
|
|
|
{ name: "yaw", type: "byte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "onGround", type: "bool"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_teleport: {id: 0x18, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "yaw", type: "byte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "pitch", type: "byte" },
|
|
|
|
{ name: "onGround", type: "bool"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
entity_head_rotation: {id: 0x19, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "headYaw", type: "byte" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_status: {id: 0x1a, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "int" },
|
|
|
|
{ name: "entityStatus", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
attach_entity: {id: 0x1b, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "int" },
|
|
|
|
{ name: "vehicleId", type: "int" },
|
|
|
|
{ name: "leash", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_metadata: {id: 0x1c, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "metadata", type: "entityMetadata" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
entity_effect: {id: 0x1d, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "effectId", type: "byte" },
|
|
|
|
{ name: "amplifier", type: "byte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "duration", type: "varint" },
|
|
|
|
{ name: "hideParticles", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
remove_entity_effect: {id: 0x1e, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "effectId", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
experience: {id: 0x1f, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "experienceBar", type: "float" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "level", type: "varint" },
|
|
|
|
{ name: "totalExperience", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
update_attributes: {id: 0x20, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "int", countFor: "properties" } },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "properties", type: "array", typeArgs: { count: "count",
|
2014-04-04 04:32:20 -04:00
|
|
|
type: "container", typeArgs: { fields: [
|
|
|
|
{ name: "key", type: "string" },
|
|
|
|
{ name: "value", type: "double" },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "listLength", type: "count", typeArgs: { type: "varint", countFor: "this.modifiers" } },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "modifiers", type: "array", typeArgs: { count: "this.listLength",
|
2014-04-04 04:32:20 -04:00
|
|
|
type: "container", typeArgs: { fields: [
|
|
|
|
{ name: "UUID", type: "UUID" },
|
|
|
|
{ name: "amount", type: "double" },
|
|
|
|
{ name: "operation", type: "byte" }
|
|
|
|
]}}}
|
|
|
|
]}
|
|
|
|
}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
map_chunk: {id: 0x21, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "groundUp", type: "bool" },
|
|
|
|
{ name: "bitMap", type: "ushort" },
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "chunkDataLength", type: "count", typeArgs: { type: "varint", countFor: "chunkData" } },
|
|
|
|
{ name: "chunkData", type: "buffer", typeArgs: { count: "chunkDataLength" } },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
multi_block_change: {id: 0x22, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "chunkX", type: "int" },
|
|
|
|
{ name: "chunkZ", type: "int" },
|
2015-03-02 21:00:34 -05:00
|
|
|
{ name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } },
|
|
|
|
{ name: "records", type: "array", typeArgs: { count: "recordCount", type: "container", typeArgs: { fields: [
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "horizontalPos", type: "ubyte" },
|
|
|
|
{ name: "y", type: "ubyte" },
|
|
|
|
{ name: "blockId", type: "varint" }
|
|
|
|
]}}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
block_change: {id: 0x23, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "type", type: "varint" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
block_action: {id: 0x24, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "byte1", type: "ubyte" },
|
|
|
|
{ name: "byte2", type: "ubyte" },
|
|
|
|
{ name: "blockId", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
block_break_animation: {id: 0x25, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "destroyStage", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-12-31 13:17:02 -05:00
|
|
|
map_chunk_bulk: {id: 0x26, fields: [
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "skyLightSent", type: "bool" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "chunkColumnCount", type: "count", typeArgs: { type: "varint", countFor: "meta" } },
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "meta", type: "array", typeArgs: { count: "chunkColumnCount", type: "container", typeArgs: { fields: [
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "bitMap", type: "ushort" },
|
2014-12-31 13:17:02 -05:00
|
|
|
]}}},
|
|
|
|
{ name: "data", type: "restBuffer" }
|
2014-12-31 13:24:13 -05:00
|
|
|
]},
|
|
|
|
explosion: {id: 0x27, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "float" },
|
|
|
|
{ name: "y", type: "float" },
|
|
|
|
{ name: "z", type: "float" },
|
|
|
|
{ name: "radius", type: "float" },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "int", countFor: "affectedBlockOffsets" } },
|
|
|
|
{ name: "affectedBlockOffsets", type: "array", typeArgs: { count: "count", type: "container", typeArgs: {
|
|
|
|
fields: [
|
|
|
|
{ name: "x", type: "byte" },
|
|
|
|
{ name: "y", type: "byte" },
|
|
|
|
{ name: "z", type: "byte" }
|
|
|
|
]
|
|
|
|
}}},
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "playerMotionX", type: "float" },
|
|
|
|
{ name: "playerMotionY", type: "float" },
|
|
|
|
{ name: "playerMotionZ", type: "float" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
world_event: {id: 0x28, fields: [ // TODO : kinda wtf naming there
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "effectId", type: "int" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "data", type: "int" },
|
|
|
|
{ name: "global", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
named_sound_effect: {id: 0x29, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "soundName", type: "string" },
|
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" },
|
|
|
|
{ name: "volume", type: "float" },
|
|
|
|
{ name: "pitch", type: "ubyte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
world_particles: {id: 0x2a, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "particleId", type: "int" },
|
|
|
|
{ name: "longDistance", type: "bool"},
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "float" },
|
|
|
|
{ name: "y", type: "float" },
|
|
|
|
{ name: "z", type: "float" },
|
|
|
|
{ name: "offsetX", type: "float" },
|
|
|
|
{ name: "offsetY", type: "float" },
|
|
|
|
{ name: "offsetZ", type: "float" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "particleData", type: "float" },
|
2015-02-27 09:35:22 -05:00
|
|
|
{ name: "particles", type: "count", typeArgs: { countFor: "data", type: "int" } },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "data", type: "array", typeArgs: { count: "particles", type: "varint" } }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
game_state_change: {id: 0x2b, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "reason", type: "ubyte" },
|
|
|
|
{ name: "gameMode", type: "float" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
spawn_entity_weather:{id: 0x2c, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
|
|
|
{ name: "type", type: "byte" },
|
|
|
|
{ name: "x", type: "int" },
|
|
|
|
{ name: "y", type: "int" },
|
|
|
|
{ name: "z", type: "int" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
open_window: {id: 0x2d, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "ubyte" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "inventoryType", type: "string" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowTitle", type: "string" },
|
|
|
|
{ name: "slotCount", type: "ubyte" },
|
|
|
|
{ name: "entityId", type: "int", condition: function(field_values) {
|
|
|
|
return field_values['inventoryType'] == 11;
|
|
|
|
} }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
close_window: {id: 0x2e, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "ubyte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
set_slot: {id: 0x2f, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "windowId", type: "byte" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "slot", type: "short" },
|
|
|
|
{ name: "item", type: "slot" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
window_items: {id: 0x30, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "ubyte" },
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } },
|
|
|
|
{ name: "items", type: "array", typeArgs: { type: "slot", count: "count" } }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-12-30 13:23:58 -05:00
|
|
|
craft_progress_bar: {id: 0x31, fields: [ /* TODO: Bad name for this packet imo */
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "ubyte" },
|
|
|
|
{ name: "property", type: "short" },
|
|
|
|
{ name: "value", type: "short" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
transaction:{id: 0x32, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "windowId", type: "byte" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "action", type: "short" },
|
|
|
|
{ name: "accepted", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
update_sign: {id: 0x33, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "text1", type: "string" },
|
|
|
|
{ name: "text2", type: "string" },
|
|
|
|
{ name: "text3", type: "string" },
|
|
|
|
{ name: "text4", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-12-31 13:17:02 -05:00
|
|
|
map: {id: 0x34, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "itemDamage", type: "varint" },
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "scale", type: "byte" },
|
|
|
|
{ name: "iconLength", type: "count", typeArgs: { type: "varint", countFor: "icons" } },
|
|
|
|
{ name: "icons", type: "array", typeArgs: { count: "iconLength", type: "container", typeArgs: { fields: [
|
|
|
|
{ name: "directionAndType", type: "byte" }, // Yeah... that will do
|
|
|
|
{ name: "x", type: "byte" },
|
|
|
|
{ name: "y", type: "byte" }
|
|
|
|
]}}},
|
|
|
|
{ name: "columns", type: "byte" },
|
|
|
|
{ name: "rows", type: "byte", condition: function(field_values) {
|
|
|
|
return field_values["columns"] !== 0;
|
|
|
|
}},
|
|
|
|
{ name: "x", type: "byte", condition: function(field_values) {
|
|
|
|
return field_values["columns"] !== 0;
|
|
|
|
}},
|
|
|
|
{ name: "y", type: "byte", condition: function(field_values) {
|
|
|
|
return field_values["columns"] !== 0;
|
|
|
|
}},
|
|
|
|
{ name: "dataLength", type: "count", typeArgs: { countFor: "data", type: "varint" }, condition: function(field_values) {
|
|
|
|
return field_values["columns"] !== 0;
|
|
|
|
}},
|
|
|
|
{ name: "data", type: "buffer", typeArgs: { count: "dataLength" }, condition: function(field_values) {
|
|
|
|
return field_values["columns"] !== 0;
|
|
|
|
}},
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
tile_entity_data:{id: 0x35, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "action", type: "ubyte" },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "nbtData", type: "restBuffer" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
open_sign_entity: {id: 0x36, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
statistics: {id: 0x37, fields: [
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "varint", countFor: "entries" } },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "entries", type: "array", typeArgs: { count: "count",
|
2014-04-04 04:32:20 -04:00
|
|
|
type: "container", typeArgs: { fields: [
|
|
|
|
{ name: "name", type: "string" },
|
|
|
|
{ name: "value", type: "varint" }
|
|
|
|
]}
|
|
|
|
}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-12-31 13:17:02 -05:00
|
|
|
player_info: {id: 0x38, fields: [
|
|
|
|
{ name: "action", type: "varint" },
|
|
|
|
{ name: "length", type: "count", typeArgs: { type: "varint", countFor: "data" }},
|
|
|
|
{ name: "data", type: "array", typeArgs: { count: "length", type: "container", typeArgs: { fields: [
|
2015-02-19 20:05:15 -05:00
|
|
|
{ name: "UUID", type: "UUID" },
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "name", type: "string", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0;
|
|
|
|
}},
|
|
|
|
{ name: "propertiesLength", type: "count", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0;
|
2015-02-22 15:49:48 -05:00
|
|
|
}, typeArgs: { countFor: "this.properties", type: "varint" }},
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "properties", type: "array", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0;
|
2015-02-22 15:49:48 -05:00
|
|
|
}, typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [
|
2014-12-31 13:24:13 -05:00
|
|
|
{ name: "name", type: "string" },
|
2015-02-22 15:49:48 -05:00
|
|
|
{ name: "value", type: "ustring" },
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "isSigned", type: "bool" },
|
2015-02-22 15:49:48 -05:00
|
|
|
{ name: "signature", type: "ustring", condition: function(field_values) {
|
2015-02-22 17:13:22 -05:00
|
|
|
return field_values["this"]["isSigned"];
|
2014-12-31 13:24:13 -05:00
|
|
|
}}
|
|
|
|
]}}},
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "gamemode", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0 || field_values["action"] === 1;
|
|
|
|
}},
|
|
|
|
{ name: "ping", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0 || field_values["action"] === 2;
|
|
|
|
}},
|
|
|
|
{ name: "hasDisplayName", type: "bool", condition: function(field_values) {
|
|
|
|
return field_values["action"] === 0 || field_values["action"] === 3;
|
|
|
|
}},
|
|
|
|
{ name: "displayName", type: "string", condition: function(field_values) {
|
|
|
|
return field_values["hasDisplayName"]; // Returns false if there is no value "hasDisplayName"
|
|
|
|
}}
|
|
|
|
]}}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
abilities: {id: 0x39, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "flags", type: "byte" },
|
|
|
|
{ name: "flyingSpeed", type: "float" },
|
|
|
|
{ name: "walkingSpeed", type: "float" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
tab_complete: {id: 0x3a, fields: [
|
2014-04-04 04:32:20 -04:00
|
|
|
{ name: "count", type: "count", typeArgs: { type: "varint", countFor: "matches" } },
|
|
|
|
{ name: "matches", type: "array", typeArgs: { type: "string", count: "count" } }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
scoreboard_objective: {id: 0x3b, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "name", type: "string" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "action", type: "byte" },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "displayText", type: "string", condition: function(field_values) {
|
|
|
|
return field_values["action"] == 0 || field_values["action"] == 2;
|
|
|
|
}},
|
|
|
|
{ name: "type", type: "string", condition: function(field_values) {
|
|
|
|
return field_values["action"] == 0 || field_values["action"] == 2;
|
|
|
|
}}
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-12-30 13:23:58 -05:00
|
|
|
scoreboard_score: {id: 0x3c, fields: [ /* TODO: itemName and scoreName may need to be switched */
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "itemName", type: "string" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "action", type: "byte" },
|
|
|
|
{ name: "scoreName", type: "string" },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "value", type: "varint", condition: function(field_values) {
|
2014-12-30 13:23:58 -05:00
|
|
|
return field_values['action'] != 1;
|
2013-12-30 10:05:22 -05:00
|
|
|
} }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
scoreboard_display_objective: {id: 0x3d, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "position", type: "byte" },
|
|
|
|
{ name: "name", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
scoreboard_team: {id: 0x3e, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "team", type: "string" },
|
|
|
|
{ name: "mode", type: "byte" },
|
|
|
|
{ name: "name", type: "string", condition: function(field_values) {
|
2014-04-04 04:32:20 -04:00
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
2013-12-30 10:05:22 -05:00
|
|
|
} },
|
|
|
|
{ name: "prefix", type: "string", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
|
|
|
} },
|
|
|
|
{ name: "suffix", type: "string", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
|
|
|
} },
|
|
|
|
{ name: "friendlyFire", type: "byte", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
|
|
|
} },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "nameTagVisibility", type: "string", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
|
|
|
} },
|
|
|
|
{ name: "color", type: "byte", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 2;
|
|
|
|
} },
|
2014-04-13 08:25:45 -04:00
|
|
|
{ name: "playerCount", type: "count", condition: function(field_values) {
|
2013-12-30 10:05:22 -05:00
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 3 || field_values['mode'] == 4;
|
2014-04-13 08:25:45 -04:00
|
|
|
}, typeArgs: { type: "short", countFor: "players" } },
|
|
|
|
{ name: "players", type: "array", condition: function(field_values) {
|
|
|
|
return field_values['mode'] == 0 || field_values['mode'] == 3 || field_values['mode'] == 4;
|
|
|
|
}, typeArgs: { type: "string", count: "playerCount" } }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
custom_payload: {id: 0x3f, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "channel", type: "string" },
|
2015-02-19 20:05:15 -05:00
|
|
|
{ name: "data", type: "restBuffer" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
kick_disconnect: {id: 0x40, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "reason", type: "string" }
|
2014-12-30 13:23:58 -05:00
|
|
|
]},
|
2014-12-30 13:59:57 -05:00
|
|
|
difficulty: { id: 0x41, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "difficulty", type: "ubyte" }
|
|
|
|
]},
|
|
|
|
combat_event: { id: 0x42, fields: [
|
|
|
|
{ name: "event", type: "varint"},
|
|
|
|
{ name: "duration", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values['event'] == 1;
|
|
|
|
} },
|
|
|
|
{ name: "playerId", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values['event'] == 2;
|
|
|
|
} },
|
|
|
|
{ name: "entityId", type: "int", condition: function(field_values) {
|
2015-02-22 17:55:41 -05:00
|
|
|
return field_values['event'] == 1 || field_values['event'] == 2;
|
2014-12-30 13:23:58 -05:00
|
|
|
} },
|
|
|
|
{ name: "message", type: "string", condition: function(field_values) {
|
|
|
|
return field_values['event'] == 2;
|
|
|
|
} }
|
|
|
|
]},
|
|
|
|
camera: { id: 0x43, fields: [
|
|
|
|
{ name: "cameraId", type: "varint" }
|
|
|
|
]},
|
|
|
|
world_border: { id: 0x44, fields: [
|
2014-12-30 13:40:22 -05:00
|
|
|
{ name: "action", type: "varint"},
|
|
|
|
{ name: "radius", type: "double", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 0;
|
|
|
|
} },
|
|
|
|
{ name: "x", type: "double", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 2 || field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "z", type: "double", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 2 || field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "old_radius", type: "double", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 1 || field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "new_radius", type: "double", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 1 || field_values['action'] == 3;
|
|
|
|
} },
|
2015-02-19 20:05:15 -05:00
|
|
|
{ name: "speed", type: "varint", condition: function(field_values) {
|
2014-12-30 13:40:22 -05:00
|
|
|
return field_values['action'] == 1 || field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "portalBoundary", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "warning_time", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 4 || field_values['action'] == 3;
|
|
|
|
} },
|
|
|
|
{ name: "warning_blocks", type: "varint", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 5 || field_values['action'] == 3;
|
|
|
|
} }
|
2014-12-30 13:23:58 -05:00
|
|
|
]},
|
|
|
|
title: { id: 0x45, fields: [
|
2014-12-30 13:55:38 -05:00
|
|
|
{ name: "action", type: "varint"},
|
|
|
|
{ name: "text", type: "string", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 0 || field_values['action'] == 1;
|
|
|
|
} },
|
|
|
|
{ name: "fadeIn", type: "int", condition: function(field_values) {
|
2014-12-31 13:17:02 -05:00
|
|
|
return field_values['action'] == 2;
|
2014-12-30 13:55:38 -05:00
|
|
|
} },
|
|
|
|
{ name: "stay", type: "int", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 2;
|
|
|
|
} },
|
|
|
|
{ name: "fadeOut", type: "int", condition: function(field_values) {
|
|
|
|
return field_values['action'] == 2;
|
|
|
|
} }
|
2014-12-30 13:23:58 -05:00
|
|
|
]},
|
|
|
|
set_compression: { id: 0x46, fields: [
|
|
|
|
{ name: "threshold", type: "varint"}
|
|
|
|
]},
|
|
|
|
playerlist_header: { id: 0x47, fields: [
|
|
|
|
{ name: "header", type: "string" },
|
2014-12-30 13:59:57 -05:00
|
|
|
{ name: "footer", type: "string" }
|
2014-12-30 13:23:58 -05:00
|
|
|
]},
|
|
|
|
resource_pack_send: { id: 0x48, fields: [
|
|
|
|
{ name: "url", type: "string" },
|
|
|
|
{ name: "hash", type: "string" }
|
|
|
|
]},
|
|
|
|
update_entity_nbt: { id: 0x49, fields: [
|
2014-12-30 13:59:57 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2014-12-31 13:17:02 -05:00
|
|
|
{ name: "tag", type: "restBuffer"}
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
},
|
2014-01-14 00:56:56 -05:00
|
|
|
toServer: {
|
|
|
|
keep_alive: {id: 0x00, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "keepAliveId", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
chat: {id: 0x01, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "message", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
use_entity: {id: 0x02, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "target", type: "varint" },
|
2015-02-22 17:55:41 -05:00
|
|
|
{ name: "mouse", type: "varint" },
|
|
|
|
{ name: "x", type: "float", condition: function(field_values) {
|
|
|
|
return field_values["mouse"] == 2;
|
|
|
|
}},
|
|
|
|
{ name: "y", type: "float", condition: function(field_values) {
|
|
|
|
return field_values["mouse"] == 2;
|
|
|
|
}},
|
|
|
|
{ name: "z", type: "float", condition: function(field_values) {
|
|
|
|
return field_values["mouse"] == 2;
|
|
|
|
}},
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
flying: {id: 0x03, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "onGround", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
position: {id: 0x04, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "double" },
|
|
|
|
{ name: "y", type: "double" },
|
|
|
|
{ name: "z", type: "double" },
|
|
|
|
{ name: "onGround", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
look: {id: 0x05, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "yaw", type: "float" },
|
|
|
|
{ name: "pitch", type: "float" },
|
|
|
|
{ name: "onGround", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
position_look: {id: 0x06, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "x", type: "double" },
|
|
|
|
{ name: "y", type: "double" },
|
|
|
|
{ name: "z", type: "double" },
|
|
|
|
{ name: "yaw", type: "float" },
|
|
|
|
{ name: "pitch", type: "float" },
|
|
|
|
{ name: "onGround", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
block_dig: {id: 0x07, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "status", type: "byte" },
|
2014-12-30 13:59:57 -05:00
|
|
|
{ name: "location", type: "position"},
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "face", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
block_place: {id: 0x08, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "direction", type: "byte" },
|
|
|
|
{ name: "heldItem", type: "slot" },
|
|
|
|
{ name: "cursorX", type: "byte" },
|
|
|
|
{ name: "cursorY", type: "byte" },
|
|
|
|
{ name: "cursorZ", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
held_item_slot: {id: 0x09, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "slotId", type: "short" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2015-02-22 17:55:41 -05:00
|
|
|
arm_animation: {id: 0x0a, fields: []},
|
2014-01-14 00:56:56 -05:00
|
|
|
entity_action: {id: 0x0b, fields: [
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "entityId", type: "varint" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "actionId", type: "varint" },
|
2014-12-30 14:06:48 -05:00
|
|
|
{ name: "jumpBoost", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
steer_vehicle: {id: 0x0c, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "sideways", type: "float" },
|
|
|
|
{ name: "forward", type: "float" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "jump", type: "ubyte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
close_window: {id: 0x0d, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
window_click: {id: 0x0e, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "byte" },
|
|
|
|
{ name: "slot", type: "short" },
|
|
|
|
{ name: "mouseButton", type: "byte" },
|
|
|
|
{ name: "action", type: "short" },
|
|
|
|
{ name: "mode", type: "byte" },
|
|
|
|
{ name: "item", type: "slot" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
transaction: {id: 0x0f, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "byte" },
|
|
|
|
{ name: "action", type: "short" },
|
|
|
|
{ name: "accepted", type: "bool" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
set_creative_slot: {id: 0x10, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "slot", type: "short" },
|
|
|
|
{ name: "item", type: "slot" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
enchant_item: {id: 0x11, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "windowId", type: "byte" },
|
|
|
|
{ name: "enchantment", type: "byte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
update_sign: {id: 0x12, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "location", type: "position" },
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "text1", type: "string" },
|
|
|
|
{ name: "text2", type: "string" },
|
|
|
|
{ name: "text3", type: "string" },
|
|
|
|
{ name: "text4", type: "string" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
abilities: {id: 0x13, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "flags", type: "byte" },
|
|
|
|
{ name: "flyingSpeed", type: "float" },
|
|
|
|
{ name: "walkingSpeed", type: "float" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
|
|
|
tab_complete: {id: 0x14, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "text", type: "string" },
|
2015-02-19 20:05:15 -05:00
|
|
|
{ name: "hasPosition", type: "bool" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "block", type: "position", condition: function(field_values) {
|
|
|
|
return field_values['hasPosition'];
|
|
|
|
} }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
settings: {id: 0x15, fields: [
|
2013-12-30 10:05:22 -05:00
|
|
|
{ name: "locale", type: "string" },
|
|
|
|
{ name: "viewDistance", type: "byte" },
|
|
|
|
{ name: "chatFlags", type: "byte" },
|
|
|
|
{ name: "chatColors", type: "bool" },
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "skinParts", type: "ubyte" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
client_command: {id: 0x16, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "payload", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]},
|
2014-03-16 13:06:57 -04:00
|
|
|
custom_payload: {id: 0x17, fields: [
|
2014-12-30 13:23:58 -05:00
|
|
|
{ name: "channel", type: "string" }, /* TODO: wiki.vg sats no dataLength is needed? */
|
2015-02-19 20:05:15 -05:00
|
|
|
{ name: "data", type: "restBuffer"}
|
2014-12-30 13:23:58 -05:00
|
|
|
]},
|
|
|
|
spectate: { id: 0x18, fields: [
|
|
|
|
{ name: "target", type: "UUID"}
|
|
|
|
]},
|
|
|
|
resource_pack_receive: { id: 0x19, fields: [
|
|
|
|
{ name: "hash", type: "string" },
|
|
|
|
{ name: "result", type: "varint" }
|
2014-01-14 00:56:56 -05:00
|
|
|
]}
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
|
2014-01-14 00:56:56 -05:00
|
|
|
var packetFields = {};
|
|
|
|
var packetNames = {};
|
|
|
|
var packetIds = {};
|
|
|
|
var packetStates = {toClient: {}, toServer: {}};
|
|
|
|
(function() {
|
|
|
|
for (var stateName in states) {
|
|
|
|
var state = states[stateName];
|
|
|
|
|
|
|
|
packetFields[state] = {toClient: [], toServer: []};
|
|
|
|
packetNames[state] = {toClient: [], toServer: []};
|
|
|
|
packetIds[state] = {toClient: [], toServer: []};
|
|
|
|
|
|
|
|
['toClient', 'toServer'].forEach(function(direction) {
|
|
|
|
for (var name in packets[state][direction]) {
|
|
|
|
var info = packets[state][direction][name];
|
|
|
|
var id = info.id;
|
|
|
|
var fields = info.fields;
|
|
|
|
|
|
|
|
assert(id !== undefined, 'missing id for packet '+name);
|
|
|
|
assert(fields !== undefined, 'missing fields for packet '+name);
|
2014-03-16 13:06:57 -04:00
|
|
|
assert(!packetNames[state][direction].hasOwnProperty(id), 'duplicate packet id '+id+' for '+name);
|
|
|
|
assert(!packetIds[state][direction].hasOwnProperty(name), 'duplicate packet name '+name+' for '+id);
|
|
|
|
assert(!packetFields[state][direction].hasOwnProperty(id), 'duplicate packet id '+id+' for '+name);
|
|
|
|
assert(!packetStates[direction].hasOwnProperty(name), 'duplicate packet name '+name+' for '+id+', must be unique across all states');
|
2014-01-14 00:56:56 -05:00
|
|
|
|
|
|
|
packetNames[state][direction][id] = name;
|
|
|
|
packetIds[state][direction][name] = id;
|
|
|
|
packetFields[state][direction][id] = fields;
|
|
|
|
packetStates[direction][name] = state;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-07 22:20:40 -05:00
|
|
|
var types = {
|
2013-03-18 16:02:57 -04:00
|
|
|
'byte': [readByte, writeByte, 1],
|
|
|
|
'ubyte': [readUByte, writeUByte, 1],
|
2014-04-01 09:43:57 -04:00
|
|
|
'short': [readShort, writeShort, 2],
|
|
|
|
'ushort': [readUShort, writeUShort, 2],
|
|
|
|
'int': [readInt, writeInt, 4],
|
2013-03-18 16:02:57 -04:00
|
|
|
'long': [readLong, writeLong, 8],
|
2013-12-30 10:05:22 -05:00
|
|
|
'varint': [readVarInt, writeVarInt, sizeOfVarInt],
|
2014-04-01 09:43:57 -04:00
|
|
|
'float': [readFloat, writeFloat, 4],
|
|
|
|
'double': [readDouble, writeDouble, 8],
|
|
|
|
'bool': [readBool, writeBool, 1],
|
|
|
|
'string': [readString, writeString, sizeOfString],
|
|
|
|
'ustring': [readString, writeString, sizeOfUString], // TODO : remove ustring
|
2014-04-04 04:32:20 -04:00
|
|
|
'UUID': [readUUID, writeUUID, 16],
|
|
|
|
'container': [readContainer, writeContainer, sizeOfContainer],
|
|
|
|
'array': [readArray, writeArray, sizeOfArray],
|
|
|
|
'buffer': [readBuffer, writeBuffer, sizeOfBuffer],
|
2015-02-19 20:05:15 -05:00
|
|
|
'restBuffer': [readRestBuffer, writeBuffer, sizeOfBuffer],
|
2014-04-04 04:32:20 -04:00
|
|
|
'count': [readCount, writeCount, sizeOfCount],
|
2014-04-01 09:43:57 -04:00
|
|
|
// TODO : remove type-specific, replace with generic containers and arrays.
|
2015-01-01 17:20:47 -05:00
|
|
|
'position': [readPosition, writePosition, 8],
|
2014-04-01 09:43:57 -04:00
|
|
|
'slot': [readSlot, writeSlot, sizeOfSlot],
|
2015-02-24 18:45:16 -05:00
|
|
|
'nbt': [readNbt, writeBuffer, sizeOfBuffer],
|
2013-03-18 16:02:57 -04:00
|
|
|
'entityMetadata': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata],
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
|
2013-04-05 23:02:36 -04:00
|
|
|
var debug;
|
|
|
|
if (process.env.NODE_DEBUG && /(minecraft-protocol|mc-proto)/.test(process.env.NODE_DEBUG)) {
|
|
|
|
var pid = process.pid;
|
|
|
|
debug = function(x) {
|
|
|
|
// if console is not set up yet, then skip this.
|
|
|
|
if (!console.error)
|
|
|
|
return;
|
|
|
|
console.error('MC-PROTO: %d', pid,
|
|
|
|
util.format.apply(util, arguments).slice(0, 500));
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debug = function() { };
|
|
|
|
}
|
|
|
|
|
2013-01-08 01:52:56 -05:00
|
|
|
var entityMetadataTypes = {
|
2014-04-04 04:32:20 -04:00
|
|
|
0: { type: 'byte' },
|
|
|
|
1: { type: 'short' },
|
|
|
|
2: { type: 'int' },
|
|
|
|
3: { type: 'float' },
|
|
|
|
4: { type: 'string' },
|
|
|
|
5: { type: 'slot' },
|
|
|
|
6: { type: 'container', typeArgs: { fields: [
|
|
|
|
{ name: 'x', type: 'int' },
|
|
|
|
{ name: 'y', type: 'int' },
|
|
|
|
{ name: 'z', type: 'int' }
|
2015-03-02 21:00:34 -05:00
|
|
|
]}},
|
|
|
|
7: { type: 'container', typeArgs: { fields: [
|
|
|
|
{ name: 'pitch', type: 'float' },
|
|
|
|
{ name: 'yaw', type: 'float' },
|
|
|
|
{ name: 'roll', type: 'float' }
|
2014-04-04 04:32:20 -04:00
|
|
|
]}}
|
2013-01-08 01:52:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// maps string type name to number
|
|
|
|
var entityMetadataTypeBytes = {};
|
|
|
|
for (var n in entityMetadataTypes) {
|
2013-11-19 18:06:57 -05:00
|
|
|
if (!entityMetadataTypes.hasOwnProperty(n)) continue;
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
entityMetadataTypeBytes[entityMetadataTypes[n].type] = n;
|
2013-01-08 01:52:56 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function sizeOfEntityMetadata(value) {
|
|
|
|
var size = 1 + value.length;
|
2013-11-19 18:06:57 -05:00
|
|
|
var item;
|
2013-01-08 02:44:17 -05:00
|
|
|
for (var i = 0; i < value.length; ++i) {
|
|
|
|
item = value[i];
|
2014-04-04 04:32:20 -04:00
|
|
|
size += sizeOf(item.value, entityMetadataTypes[entityMetadataTypeBytes[item.type]], {});
|
2013-01-08 02:44:17 -05:00
|
|
|
}
|
2013-03-18 16:02:57 -04:00
|
|
|
return size;
|
2013-01-08 00:50:43 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeEntityMetadata(value, buffer, offset) {
|
|
|
|
value.forEach(function(item) {
|
2013-03-18 17:13:03 -04:00
|
|
|
var type = entityMetadataTypeBytes[item.type];
|
|
|
|
var headerByte = (type << 5) | item.key;
|
2013-01-08 02:44:17 -05:00
|
|
|
buffer.writeUInt8(headerByte, offset);
|
2013-01-08 01:52:56 -05:00
|
|
|
offset += 1;
|
2014-04-04 04:32:20 -04:00
|
|
|
offset = write(item.value, buffer, offset, entityMetadataTypes[type], {});
|
2013-01-08 01:52:56 -05:00
|
|
|
});
|
2013-01-08 02:44:17 -05:00
|
|
|
buffer.writeUInt8(0x7f, offset);
|
2013-03-18 16:02:57 -04:00
|
|
|
return offset + 1;
|
2013-01-08 01:52:56 -05:00
|
|
|
}
|
2013-01-08 00:50:43 -05:00
|
|
|
|
2013-07-12 07:19:34 -04:00
|
|
|
function writeUUID(value, buffer, offset) {
|
2015-02-22 15:49:48 -05:00
|
|
|
buffer.writeUInt32BE(value[0], offset);
|
|
|
|
buffer.writeUInt32BE(value[1], offset + 4);
|
|
|
|
buffer.writeUInt32BE(value[2], offset + 8);
|
|
|
|
buffer.writeUInt32BE(value[3], offset + 12);
|
2013-07-12 07:19:34 -04:00
|
|
|
return offset + 16;
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
function readEntityMetadata(buffer, offset) {
|
|
|
|
var cursor = offset;
|
2013-01-08 02:44:17 -05:00
|
|
|
var metadata = [];
|
|
|
|
var item, key, type, results, reader, typeName, dataType;
|
2013-01-03 23:14:19 -05:00
|
|
|
while (true) {
|
|
|
|
if (cursor + 1 > buffer.length) return null;
|
|
|
|
item = buffer.readUInt8(cursor);
|
|
|
|
cursor += 1;
|
2013-01-08 02:44:17 -05:00
|
|
|
if (item === 0x7f) {
|
|
|
|
return {
|
|
|
|
value: metadata,
|
|
|
|
size: cursor - offset,
|
|
|
|
};
|
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
key = item & 0x1f;
|
|
|
|
type = item >> 5;
|
2014-04-04 04:32:20 -04:00
|
|
|
dataType = entityMetadataTypes[type];
|
2014-05-14 00:01:08 -04:00
|
|
|
typeName = dataType.type;
|
2015-03-02 21:00:34 -05:00
|
|
|
//debug("Reading entity metadata type " + dataType + " (" + ( typeName || "unknown" ) + ")");
|
2013-01-28 20:05:33 -05:00
|
|
|
if (!dataType) {
|
|
|
|
return {
|
2014-04-04 04:32:20 -04:00
|
|
|
error: new Error("unrecognized entity metadata type " + type)
|
2013-01-28 20:05:33 -05:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 04:32:20 -04:00
|
|
|
results = read(buffer, cursor, dataType, {});
|
2013-01-03 23:14:19 -05:00
|
|
|
if (! results) return null;
|
2013-01-08 02:44:17 -05:00
|
|
|
metadata.push({
|
|
|
|
key: key,
|
|
|
|
value: results.value,
|
|
|
|
type: typeName,
|
|
|
|
});
|
2013-01-03 23:14:19 -05:00
|
|
|
cursor += results.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:45:16 -05:00
|
|
|
function readNbt(buffer, offset) {
|
|
|
|
buffer = buffer.slice(offset);
|
|
|
|
return nbt.parseUncompressed(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeNbt(value, buffer, offset) {
|
|
|
|
var newbuf = nbt.writeUncompressed(value);
|
|
|
|
newbuf.copy(buffer, offset);
|
|
|
|
return offset + newbuf.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sizeOfNbt(value) {
|
|
|
|
return nbt.writeUncompressed(value).length;
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
function readString (buffer, offset) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var length = readVarInt(buffer, offset);
|
|
|
|
if (!!!length) return null;
|
|
|
|
var cursor = offset + length.size;
|
|
|
|
var stringLength = length.value;
|
|
|
|
var strEnd = cursor + stringLength;
|
2013-01-03 23:14:19 -05:00
|
|
|
if (strEnd > buffer.length) return null;
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
var value = buffer.toString('utf8', cursor, strEnd);
|
|
|
|
cursor = strEnd;
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
return {
|
2013-01-04 19:04:06 -05:00
|
|
|
value: value,
|
|
|
|
size: cursor - offset,
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-07-12 07:19:34 -04:00
|
|
|
function readUUID(buffer, offset) {
|
|
|
|
return {
|
|
|
|
value: [
|
2015-02-22 15:49:48 -05:00
|
|
|
buffer.readUInt32BE(offset),
|
|
|
|
buffer.readUInt32BE(offset + 4),
|
|
|
|
buffer.readUInt32BE(offset + 8),
|
|
|
|
buffer.readUInt32BE(offset + 12),
|
2013-07-12 07:19:34 -04:00
|
|
|
],
|
|
|
|
size: 16,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
function readShort(buffer, offset) {
|
|
|
|
if (offset + 2 > buffer.length) return null;
|
|
|
|
var value = buffer.readInt16BE(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readUShort(buffer, offset) {
|
|
|
|
if (offset + 2 > buffer.length) return null;
|
|
|
|
var value = buffer.readUInt16BE(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readInt(buffer, offset) {
|
|
|
|
if (offset + 4 > buffer.length) return null;
|
|
|
|
var value = buffer.readInt32BE(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 4,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readFloat(buffer, offset) {
|
|
|
|
if (offset + 4 > buffer.length) return null;
|
|
|
|
var value = buffer.readFloatBE(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 4,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readDouble(buffer, offset) {
|
|
|
|
if (offset + 8 > buffer.length) return null;
|
|
|
|
var value = buffer.readDoubleBE(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 8,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLong(buffer, offset) {
|
|
|
|
if (offset + 8 > buffer.length) return null;
|
|
|
|
return {
|
|
|
|
value: [buffer.readInt32BE(offset), buffer.readInt32BE(offset + 4)],
|
|
|
|
size: 8,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readByte(buffer, offset) {
|
|
|
|
if (offset + 1 > buffer.length) return null;
|
|
|
|
var value = buffer.readInt8(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readUByte(buffer, offset) {
|
|
|
|
if (offset + 1 > buffer.length) return null;
|
|
|
|
var value = buffer.readUInt8(offset);
|
|
|
|
return {
|
|
|
|
value: value,
|
|
|
|
size: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readBool(buffer, offset) {
|
|
|
|
if (offset + 1 > buffer.length) return null;
|
|
|
|
var value = buffer.readInt8(offset);
|
|
|
|
return {
|
|
|
|
value: !!value,
|
|
|
|
size: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-01 17:20:47 -05:00
|
|
|
function readPosition(buffer, offset) {
|
|
|
|
var longVal = readLong(buffer, offset).value; // I wish I could do destructuring...
|
|
|
|
var x = longVal[0] >> 6;
|
2015-03-03 07:36:26 -05:00
|
|
|
var y = ((longVal[0] & 0x3F) << 6) | ((longVal[1] >> 26) & 0x3f);
|
|
|
|
var z = longVal[1] & 0x3FFFFFF;
|
2015-01-01 17:20:47 -05:00
|
|
|
return {
|
|
|
|
value: { x: x, y: y, z: z },
|
|
|
|
size: 8
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:14:19 -05:00
|
|
|
function readSlot(buffer, offset) {
|
2015-02-24 18:45:16 -05:00
|
|
|
var value = {};
|
2013-01-03 23:14:19 -05:00
|
|
|
var results = readShort(buffer, offset);
|
|
|
|
if (! results) return null;
|
2015-02-24 18:45:16 -05:00
|
|
|
value.blockId = results.value;
|
2013-01-03 23:14:19 -05:00
|
|
|
|
2015-02-24 18:45:16 -05:00
|
|
|
if (value.blockId === -1) {
|
2013-01-03 23:14:19 -05:00
|
|
|
return {
|
2015-02-24 18:45:16 -05:00
|
|
|
value: value,
|
2015-03-02 21:00:34 -05:00
|
|
|
size: 2,
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:00:34 -05:00
|
|
|
var cursorEnd = offset + 6;
|
2013-01-03 23:14:19 -05:00
|
|
|
if (cursorEnd > buffer.length) return null;
|
2015-03-02 21:00:34 -05:00
|
|
|
value.itemCount = buffer.readInt8(offset + 2);
|
|
|
|
value.itemDamage = buffer.readInt16BE(offset + 3);
|
|
|
|
var nbtData = buffer.readInt8(offset + 5);
|
2015-02-24 18:45:16 -05:00
|
|
|
if (nbtData == 0) {
|
|
|
|
return {
|
|
|
|
value: value,
|
2015-03-02 21:00:34 -05:00
|
|
|
size: 6
|
2015-02-24 18:45:16 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 21:00:34 -05:00
|
|
|
var nbtData = readNbt(buffer, offset + 5);
|
|
|
|
value.nbtData = nbtData.value;
|
2013-01-03 23:14:19 -05:00
|
|
|
return {
|
2015-03-02 21:00:34 -05:00
|
|
|
value: value,
|
|
|
|
size: nbtData.size + 5
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function sizeOfSlot(value) {
|
2015-03-03 07:36:26 -05:00
|
|
|
if (value.blockId === -1)
|
2015-02-24 18:45:16 -05:00
|
|
|
return (2);
|
|
|
|
else if (!value.nbtData) {
|
|
|
|
return (6);
|
|
|
|
} else {
|
|
|
|
return (5 + sizeOfNbt(value.nbtData));
|
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 17:20:47 -05:00
|
|
|
function writePosition(value, buffer, offset) {
|
|
|
|
var longVal = [];
|
2015-03-03 07:36:26 -05:00
|
|
|
longVal[0] = ((value.x & 0x3FFFFFF) << 6) | ((value.y & 0xFFF) >> 6);
|
2015-01-01 17:20:47 -05:00
|
|
|
longVal[1] = ((value.y & 0x3F) << 26) | (value.z & 0x3FFFFFF);
|
|
|
|
return writeLong(longVal, buffer, offset);
|
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeSlot(value, buffer, offset) {
|
2015-03-03 07:36:26 -05:00
|
|
|
buffer.writeInt16BE(value.blockId, offset);
|
|
|
|
if (value.blockId === -1) return offset + 2;
|
2013-03-18 16:02:57 -04:00
|
|
|
buffer.writeInt8(value.itemCount, offset + 2);
|
|
|
|
buffer.writeInt16BE(value.itemDamage, offset + 3);
|
2015-02-24 18:45:16 -05:00
|
|
|
var nbtDataLen;
|
|
|
|
if (value.nbtData)
|
|
|
|
{
|
|
|
|
var newbuf = nbt.writeUncompressed(value.nbtData);
|
2015-02-27 09:35:22 -05:00
|
|
|
newbuf.copy(buffer, offset + 5);
|
2015-02-24 18:45:16 -05:00
|
|
|
nbtDataLen = newbuf.length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer.writeInt8(0, offset + 5);
|
|
|
|
nbtDataLen = 1;
|
|
|
|
}
|
|
|
|
return offset + 5 + nbtDataLen;
|
2013-11-19 18:06:57 -05:00
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function sizeOfString(value) {
|
2014-02-22 05:38:39 -05:00
|
|
|
var length = Buffer.byteLength(value, 'utf8');
|
|
|
|
assert.ok(length < STRING_MAX_LENGTH, "string greater than max length");
|
|
|
|
return sizeOfVarInt(length) + length;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 07:14:12 -04:00
|
|
|
function sizeOfUString(value) {
|
2014-02-22 05:38:39 -05:00
|
|
|
var length = Buffer.byteLength(value, 'utf8');
|
|
|
|
assert.ok(length < SRV_STRING_MAX_LENGTH, "string greater than max length");
|
|
|
|
return sizeOfVarInt(length) + length;
|
2013-08-05 07:14:12 -04:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeString(value, buffer, offset) {
|
2014-02-22 05:38:39 -05:00
|
|
|
var length = Buffer.byteLength(value, 'utf8');
|
|
|
|
offset = writeVarInt(length, buffer, offset);
|
|
|
|
buffer.write(value, offset, length, 'utf8');
|
|
|
|
return offset + length;
|
2013-11-19 18:06:57 -05:00
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeByte(value, buffer, offset) {
|
|
|
|
buffer.writeInt8(value, offset);
|
|
|
|
return offset + 1;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeBool(value, buffer, offset) {
|
|
|
|
buffer.writeInt8(+value, offset);
|
|
|
|
return offset + 1;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeUByte(value, buffer, offset) {
|
|
|
|
buffer.writeUInt8(value, offset);
|
|
|
|
return offset + 1;
|
2013-11-19 18:06:57 -05:00
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeFloat(value, buffer, offset) {
|
|
|
|
buffer.writeFloatBE(value, offset);
|
|
|
|
return offset + 4;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeDouble(value, buffer, offset) {
|
|
|
|
buffer.writeDoubleBE(value, offset);
|
|
|
|
return offset + 8;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeShort(value, buffer, offset) {
|
|
|
|
buffer.writeInt16BE(value, offset);
|
|
|
|
return offset + 2;
|
2013-01-07 22:14:14 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeUShort(value, buffer, offset) {
|
|
|
|
buffer.writeUInt16BE(value, offset);
|
|
|
|
return offset + 2;
|
2013-01-07 22:14:14 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeInt(value, buffer, offset) {
|
|
|
|
buffer.writeInt32BE(value, offset);
|
|
|
|
return offset + 4;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:02:57 -04:00
|
|
|
function writeLong(value, buffer, offset) {
|
|
|
|
buffer.writeInt32BE(value[0], offset);
|
|
|
|
buffer.writeInt32BE(value[1], offset + 4);
|
|
|
|
return offset + 8;
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
function readVarInt(buffer, offset) {
|
|
|
|
var result = 0;
|
|
|
|
var shift = 0;
|
|
|
|
var cursor = offset;
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
while (true) {
|
|
|
|
if (cursor + 1 > buffer.length) return null;
|
|
|
|
var b = buffer.readUInt8(cursor);
|
|
|
|
result |= ((b & 0x7f) << shift); // Add the bits to our number, except MSB
|
|
|
|
cursor++;
|
|
|
|
if (!(b & 0x80)) { // If the MSB is not set, we return the number
|
|
|
|
return {
|
|
|
|
value: result,
|
|
|
|
size: cursor - offset
|
|
|
|
};
|
|
|
|
}
|
|
|
|
shift += 7; // we only have 7 bits, MSB being the return-trigger
|
|
|
|
assert.ok(shift < 64, "varint is too big"); // Make sure our shift don't overflow.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sizeOfVarInt(value) {
|
|
|
|
var cursor = 0;
|
|
|
|
while (value & ~0x7F) {
|
|
|
|
value >>>= 7;
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
return cursor + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeVarInt(value, buffer, offset) {
|
|
|
|
var cursor = 0;
|
|
|
|
while (value & ~0x7F) {
|
|
|
|
buffer.writeUInt8((value & 0xFF) | 0x80, offset + cursor);
|
|
|
|
cursor++;
|
|
|
|
value >>>= 7;
|
|
|
|
}
|
|
|
|
buffer.writeUInt8(value, offset + cursor);
|
|
|
|
return offset + cursor + 1;
|
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function readContainer(buffer, offset, typeArgs, rootNode) {
|
|
|
|
var results = {
|
|
|
|
value: {},
|
|
|
|
size: 0
|
|
|
|
};
|
|
|
|
// BLEIGH. Huge hack because I have no way of knowing my current name.
|
|
|
|
// TODO : either pass fieldInfo instead of typeArgs as argument (bleigh), or send name as argument (verybleigh).
|
2015-02-24 18:45:16 -05:00
|
|
|
// TODO : what I do inside of roblabla/Protocols is have each "frame" create a new empty slate with just a "super" object pointing to the parent.
|
2014-04-04 04:32:20 -04:00
|
|
|
rootNode.this = results.value;
|
|
|
|
for (var index in typeArgs.fields) {
|
|
|
|
var readResults = read(buffer, offset, typeArgs.fields[index], rootNode);
|
2014-05-14 14:44:43 -04:00
|
|
|
if (readResults == null) { continue; }
|
2014-04-04 04:32:20 -04:00
|
|
|
results.size += readResults.size;
|
|
|
|
offset += readResults.size;
|
|
|
|
results.value[typeArgs.fields[index].name] = readResults.value;
|
|
|
|
}
|
|
|
|
delete rootNode.this;
|
|
|
|
return results;
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function writeContainer(value, buffer, offset, typeArgs, rootNode) {
|
2015-03-02 21:00:34 -05:00
|
|
|
var context = value.this ? value.this : value;
|
2014-04-04 04:32:20 -04:00
|
|
|
rootNode.this = value;
|
|
|
|
for (var index in typeArgs.fields) {
|
2015-03-02 21:00:34 -05:00
|
|
|
if (!context.hasOwnProperty(typeArgs.fields[index].name) && typeArgs.fields[index].type != "count" && !typeArgs.fields[index].condition)
|
|
|
|
{
|
2015-01-01 17:20:47 -05:00
|
|
|
debug(new Error("Missing Property " + typeArgs.fields[index].name).stack);
|
2015-03-02 21:00:34 -05:00
|
|
|
console.log(context);
|
|
|
|
}
|
|
|
|
offset = write(context[typeArgs.fields[index].name], buffer, offset, typeArgs.fields[index], rootNode);
|
2014-04-04 04:32:20 -04:00
|
|
|
}
|
|
|
|
delete rootNode.this;
|
|
|
|
return offset;
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function sizeOfContainer(value, typeArgs, rootNode) {
|
|
|
|
var size = 0;
|
2015-03-02 21:00:34 -05:00
|
|
|
var context = value.this ? value.this : value;
|
2014-04-04 04:32:20 -04:00
|
|
|
rootNode.this = value;
|
|
|
|
for (var index in typeArgs.fields) {
|
2015-03-02 21:00:34 -05:00
|
|
|
size += sizeOf(context[typeArgs.fields[index].name], typeArgs.fields[index], rootNode);
|
2014-04-04 04:32:20 -04:00
|
|
|
}
|
|
|
|
delete rootNode.this;
|
|
|
|
return size;
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
|
2014-12-30 13:23:58 -05:00
|
|
|
function readBuffer(buffer, offset, typeArgs, rootNode) {
|
2014-04-04 04:32:20 -04:00
|
|
|
var count = getField(typeArgs.count, rootNode);
|
2013-12-30 10:05:22 -05:00
|
|
|
return {
|
2014-04-04 04:32:20 -04:00
|
|
|
value: buffer.slice(offset, offset + count),
|
|
|
|
size: count
|
2013-12-30 10:05:22 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function writeBuffer(value, buffer, offset) {
|
|
|
|
value.copy(buffer, offset);
|
|
|
|
return offset + value.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sizeOfBuffer(value) {
|
|
|
|
return value.length;
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:02 -05:00
|
|
|
function readRestBuffer(buffer, offset, typeArgs, rootNode) {
|
|
|
|
return {
|
|
|
|
value: buffer.slice(offset),
|
|
|
|
size: buffer.length - offset
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function readArray(buffer, offset, typeArgs, rootNode) {
|
|
|
|
var results = {
|
|
|
|
value: [],
|
|
|
|
size: 0
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
2014-04-04 04:32:20 -04:00
|
|
|
var count = getField(typeArgs.count, rootNode);
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
var readResults = read(buffer, offset, { type: typeArgs.type, typeArgs: typeArgs.typeArgs }, rootNode);
|
|
|
|
results.size += readResults.size;
|
|
|
|
offset += readResults.size;
|
|
|
|
results.value.push(readResults.value);
|
|
|
|
}
|
|
|
|
return results;
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function writeArray(value, buffer, offset, typeArgs, rootNode) {
|
|
|
|
for (var index in value) {
|
|
|
|
offset = write(value[index], buffer, offset, { type: typeArgs.type, typeArgs: typeArgs.typeArgs }, rootNode);
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function sizeOfArray(value, typeArgs, rootNode) {
|
|
|
|
var size = 0;
|
|
|
|
for (var index in value) {
|
|
|
|
size += sizeOf(value[index], { type: typeArgs.type, typeArgs: typeArgs.typeArgs }, rootNode);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getField(countField, rootNode) {
|
|
|
|
var countFieldArr = countField.split(".");
|
|
|
|
var count = rootNode;
|
2014-10-17 12:28:38 -04:00
|
|
|
for (var index = 0; index < countFieldArr.length; index++) {
|
2014-04-04 04:32:20 -04:00
|
|
|
count = count[countFieldArr[index]];
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readCount(buffer, offset, typeArgs, rootNode) {
|
|
|
|
return read(buffer, offset, { type: typeArgs.type }, rootNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeCount(value, buffer, offset, typeArgs, rootNode) {
|
|
|
|
// Actually gets the required field, and writes its length. Value is unused.
|
|
|
|
// TODO : a bit hackityhack.
|
|
|
|
return write(getField(typeArgs.countFor, rootNode).length, buffer, offset, { type: typeArgs.type }, rootNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sizeOfCount(value, typeArgs, rootNode) {
|
|
|
|
// TODO : should I use value or getField().length ?
|
|
|
|
/*console.log(rootNode);
|
|
|
|
console.log(typeArgs);*/
|
|
|
|
return sizeOf(getField(typeArgs.countFor, rootNode).length, { type: typeArgs.type }, rootNode);
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:43:57 -04:00
|
|
|
function read(buffer, cursor, fieldInfo, rootNodes) {
|
|
|
|
if (fieldInfo.condition && !fieldInfo.condition(rootNodes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var type = types[fieldInfo.type];
|
|
|
|
if (!type) {
|
|
|
|
return {
|
|
|
|
error: new Error("missing data type: " + fieldInfo.type)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var readResults = type[0](buffer, cursor, fieldInfo.typeArgs, rootNodes);
|
2015-02-24 18:45:16 -05:00
|
|
|
if (readResults == null) {
|
|
|
|
throw new Error("Reader returned null : " + JSON.stringify(fieldInfo));
|
|
|
|
}
|
|
|
|
if (readResults && readResults.error) return { error: readResults.error };
|
2014-04-01 09:43:57 -04:00
|
|
|
return readResults;
|
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function write(value, buffer, offset, fieldInfo, rootNode) {
|
|
|
|
if (fieldInfo.condition && !fieldInfo.condition(rootNode)) {
|
2014-05-14 14:46:29 -04:00
|
|
|
return offset;
|
2014-04-01 09:43:57 -04:00
|
|
|
}
|
|
|
|
var type = types[fieldInfo.type];
|
|
|
|
if (!type) {
|
|
|
|
return {
|
|
|
|
error: new Error("missing data type: " + fieldInfo.type)
|
|
|
|
};
|
|
|
|
}
|
2014-04-04 04:32:20 -04:00
|
|
|
return type[1](value, buffer, offset, fieldInfo.typeArgs, rootNode);
|
2014-04-01 09:43:57 -04:00
|
|
|
}
|
|
|
|
|
2014-04-04 04:32:20 -04:00
|
|
|
function sizeOf(value, fieldInfo, rootNode) {
|
|
|
|
if (fieldInfo.condition && !fieldInfo.condition(rootNode)) {
|
2014-04-01 09:43:57 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var type = types[fieldInfo.type];
|
|
|
|
if (!type) {
|
|
|
|
throw new Error("missing data type: " + fieldInfo.type);
|
|
|
|
}
|
|
|
|
if (typeof type[2] === 'function') {
|
2014-04-04 04:32:20 -04:00
|
|
|
return type[2](value, fieldInfo.typeArgs, rootNode);
|
2014-04-01 09:43:57 -04:00
|
|
|
} else {
|
|
|
|
return type[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
function get(packetId, state, toServer) {
|
|
|
|
var direction = toServer ? "toServer" : "toClient";
|
2014-01-14 00:56:56 -05:00
|
|
|
var packetInfo = packetFields[state][direction][packetId];
|
2013-01-28 19:44:00 -05:00
|
|
|
if (!packetInfo) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
return packetInfo;
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 17:20:47 -05:00
|
|
|
// TODO : This does NOT contain the length prefix anymore.
|
2013-12-30 10:05:22 -05:00
|
|
|
function createPacketBuffer(packetId, state, params, isServer) {
|
|
|
|
var length = 0;
|
2014-01-14 00:56:56 -05:00
|
|
|
if (typeof packetId === 'string' && typeof state !== 'string' && !params) {
|
|
|
|
// simplified two-argument usage, createPacketBuffer(name, params)
|
|
|
|
params = state;
|
|
|
|
state = packetStates[!isServer ? 'toServer' : 'toClient'][packetId];
|
|
|
|
}
|
|
|
|
if (typeof packetId === 'string') packetId = packetIds[state][!isServer ? 'toServer' : 'toClient'][packetId];
|
|
|
|
assert.notEqual(packetId, undefined);
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
var packet = get(packetId, state, !isServer);
|
2013-01-28 19:44:00 -05:00
|
|
|
assert.notEqual(packet, null);
|
2013-01-03 23:14:19 -05:00
|
|
|
packet.forEach(function(fieldInfo) {
|
2015-03-02 21:00:34 -05:00
|
|
|
try {
|
2014-04-01 09:43:57 -04:00
|
|
|
length += sizeOf(params[fieldInfo.name], fieldInfo, params);
|
2015-03-02 21:00:34 -05:00
|
|
|
} catch (e) {
|
2015-03-03 12:11:36 -05:00
|
|
|
console.log("fieldInfo : " + JSON.stringify(fieldInfo));
|
|
|
|
console.log("params : " + JSON.stringify(params));
|
2015-03-02 21:00:34 -05:00
|
|
|
throw e;
|
|
|
|
}
|
2013-01-03 23:14:19 -05:00
|
|
|
});
|
2013-12-30 10:05:22 -05:00
|
|
|
length += sizeOfVarInt(packetId);
|
2015-01-01 17:20:47 -05:00
|
|
|
var size = length;// + sizeOfVarInt(length);
|
2013-01-03 23:14:19 -05:00
|
|
|
var buffer = new Buffer(size);
|
2015-01-01 17:20:47 -05:00
|
|
|
var offset = 0;//writeVarInt(length, buffer, 0);
|
2013-12-30 10:05:22 -05:00
|
|
|
offset = writeVarInt(packetId, buffer, offset);
|
2013-03-18 16:02:57 -04:00
|
|
|
packet.forEach(function(fieldInfo) {
|
|
|
|
var value = params[fieldInfo.name];
|
2015-01-01 17:20:47 -05:00
|
|
|
// TODO : A better check is probably needed
|
|
|
|
if(typeof value === "undefined" && fieldInfo.type != "count" && !fieldInfo.condition)
|
|
|
|
debug(new Error("Missing Property " + fieldInfo.name).stack);
|
2014-05-14 14:45:46 -04:00
|
|
|
offset = write(value, buffer, offset, fieldInfo, params);
|
2013-01-03 23:14:19 -05:00
|
|
|
});
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2014-12-30 03:55:04 -05:00
|
|
|
function compressPacketBuffer(buffer, callback) {
|
2015-01-01 17:20:47 -05:00
|
|
|
var dataLength = buffer.size;
|
2015-02-21 19:41:41 -05:00
|
|
|
zlib.deflate(buffer, function(err, buf) {
|
|
|
|
if (err)
|
|
|
|
callback(err);
|
|
|
|
else
|
|
|
|
newStylePacket(buffer, callback);
|
2015-01-01 17:20:47 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-21 19:41:41 -05:00
|
|
|
function oldStylePacket(buffer, callback) {
|
2015-01-01 17:20:47 -05:00
|
|
|
var packet = new Buffer(sizeOfVarInt(buffer.length) + buffer.length);
|
|
|
|
var cursor = writeVarInt(buffer.length, packet, 0);
|
|
|
|
writeBuffer(buffer, packet, cursor);
|
2015-02-21 19:41:41 -05:00
|
|
|
callback(null, packet);
|
2015-01-01 17:20:47 -05:00
|
|
|
}
|
|
|
|
|
2015-02-21 19:41:41 -05:00
|
|
|
function newStylePacket(buffer, callback) {
|
|
|
|
var sizeOfDataLength = sizeOfVarInt(0);
|
|
|
|
var sizeOfLength = sizeOfVarInt(buffer.length + sizeOfDataLength);
|
|
|
|
var size = sizeOfLength + sizeOfDataLength + buffer.length;
|
2015-01-01 17:20:47 -05:00
|
|
|
var packet = new Buffer(size);
|
2015-02-21 19:41:41 -05:00
|
|
|
var cursor = writeVarInt(size - sizeOfLength, packet, 0);
|
2015-01-01 17:20:47 -05:00
|
|
|
cursor = writeVarInt(0, packet, cursor);
|
|
|
|
writeBuffer(buffer, packet, cursor);
|
2015-02-21 19:41:41 -05:00
|
|
|
callback(null, packet);
|
2014-12-30 03:55:04 -05:00
|
|
|
}
|
|
|
|
|
2015-02-21 16:04:41 -05:00
|
|
|
function parsePacketData(buffer, state, isServer, packetsToParse) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var cursor = 0;
|
2014-04-10 19:48:06 -04:00
|
|
|
var packetIdField = readVarInt(buffer, cursor);
|
2013-12-30 10:05:22 -05:00
|
|
|
var packetId = packetIdField.value;
|
|
|
|
cursor += packetIdField.size;
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2015-02-22 15:49:48 -05:00
|
|
|
var results = { id: packetId, state: state };
|
2014-04-10 19:48:06 -04:00
|
|
|
// Only parse the packet if there is a need for it, AKA if there is a listener attached to it
|
|
|
|
var name = packetNames[state][isServer ? "toServer" : "toClient"][packetId];
|
2014-04-10 20:08:17 -04:00
|
|
|
var shouldParse = (!packetsToParse.hasOwnProperty(name) || packetsToParse[name] <= 0)
|
|
|
|
&& (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
|
|
|
|
if (shouldParse) {
|
2014-04-10 19:48:06 -04:00
|
|
|
return {
|
2015-02-22 15:49:48 -05:00
|
|
|
buffer: buffer,
|
|
|
|
results: results
|
2014-04-10 19:48:06 -04:00
|
|
|
};
|
|
|
|
}
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
var packetInfo = get(packetId, state, isServer);
|
|
|
|
if (packetInfo === null) {
|
2013-01-28 19:44:00 -05:00
|
|
|
return {
|
2013-12-30 10:05:22 -05:00
|
|
|
error: new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")"),
|
2014-04-10 19:48:06 -04:00
|
|
|
buffer: buffer,
|
2013-12-30 10:05:22 -05:00
|
|
|
results: results
|
2014-04-10 19:48:06 -04:00
|
|
|
};
|
2013-04-05 23:04:47 -04:00
|
|
|
} else {
|
2015-01-01 17:20:47 -05:00
|
|
|
var packetName = packetNames[state][isServer ? "toServer" : "toClient"][packetId];
|
|
|
|
debug("read packetId " + state + "." + packetName + " (0x" + packetId.toString(16) + ")");
|
2013-01-28 19:44:00 -05:00
|
|
|
}
|
2014-12-30 13:23:58 -05:00
|
|
|
|
2013-07-08 04:07:07 -04:00
|
|
|
var i, fieldInfo, readResults;
|
2013-01-03 23:14:19 -05:00
|
|
|
for (i = 0; i < packetInfo.length; ++i) {
|
|
|
|
fieldInfo = packetInfo[i];
|
2014-04-01 09:43:57 -04:00
|
|
|
readResults = read(buffer, cursor, fieldInfo, results);
|
|
|
|
/* A deserializer cannot return null anymore. Besides, read() returns
|
|
|
|
* null when the condition is not fulfilled.
|
|
|
|
if (!!!readResults) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var error = new Error("A deserializer returned null");
|
|
|
|
error.packetId = packetId;
|
|
|
|
error.fieldInfo = fieldInfo.name;
|
|
|
|
return {
|
|
|
|
size: length + lengthField.size,
|
|
|
|
error: error,
|
|
|
|
results: results
|
|
|
|
};
|
2014-04-01 09:43:57 -04:00
|
|
|
}*/
|
|
|
|
if (readResults === null) continue;
|
2013-12-30 10:05:22 -05:00
|
|
|
if (readResults.error) {
|
2013-07-08 04:07:07 -04:00
|
|
|
return readResults;
|
2013-01-28 19:44:00 -05:00
|
|
|
}
|
2013-02-10 21:05:42 -05:00
|
|
|
results[fieldInfo.name] = readResults.value;
|
2013-12-30 10:05:22 -05:00
|
|
|
cursor += readResults.size;
|
2013-01-03 23:14:19 -05:00
|
|
|
}
|
2015-03-02 21:00:34 -05:00
|
|
|
if (buffer.length > cursor)
|
|
|
|
console.log("DID NOT PARSE THE WHOLE THING!");
|
2013-04-15 00:40:09 -04:00
|
|
|
debug(results);
|
2013-01-03 23:14:19 -05:00
|
|
|
return {
|
|
|
|
results: results,
|
2014-04-10 19:48:06 -04:00
|
|
|
buffer: buffer
|
2013-01-03 23:14:19 -05:00
|
|
|
};
|
|
|
|
}
|
2013-01-07 23:36:14 -05:00
|
|
|
|
2015-02-21 16:04:41 -05:00
|
|
|
function parsePacket(buffer, state, isServer, packetsToParse) {
|
2015-02-05 13:02:34 -05:00
|
|
|
if (state == null) state = states.PLAY;
|
|
|
|
var cursor = 0;
|
|
|
|
var lengthField = readVarInt(buffer, 0);
|
|
|
|
if (!!!lengthField) return null;
|
|
|
|
var length = lengthField.value;
|
|
|
|
cursor += lengthField.size;
|
2015-02-21 16:04:41 -05:00
|
|
|
if (length + lengthField.size > buffer.length) return null; // fail early
|
|
|
|
var result = parsePacketData(buffer.slice(cursor, length + cursor), state, isServer, packetsToParse);
|
|
|
|
result.size = lengthField.size + length;
|
|
|
|
return result;
|
|
|
|
}
|
2015-02-05 13:02:34 -05:00
|
|
|
|
2015-02-21 16:04:41 -05:00
|
|
|
function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
|
2015-02-21 19:41:41 -05:00
|
|
|
var dataLengthField = readVarInt(buffer, 0);
|
|
|
|
var buf = buffer.slice(dataLengthField.size);
|
2015-02-05 13:02:34 -05:00
|
|
|
if(dataLengthField.value != 0) {
|
2015-02-21 19:41:41 -05:00
|
|
|
zlib.inflate(buf, function(err, newbuf) {
|
2015-02-19 20:05:15 -05:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2015-02-21 16:04:41 -05:00
|
|
|
cb(null, parsePacketData(newbuf, state, isServer, packetsToParse));
|
2015-02-19 20:05:15 -05:00
|
|
|
}
|
2015-01-02 12:50:54 -05:00
|
|
|
});
|
|
|
|
} else {
|
2015-02-21 19:41:41 -05:00
|
|
|
cb(null, parsePacketData(buf, state, isServer, packetsToParse));
|
2015-01-02 12:50:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
module.exports = {
|
2014-12-30 13:24:39 -05:00
|
|
|
version: 47,
|
|
|
|
minecraftVersion: '1.8.1',
|
2013-01-07 23:36:14 -05:00
|
|
|
sessionVersion: 13,
|
|
|
|
parsePacket: parsePacket,
|
2015-02-21 19:41:41 -05:00
|
|
|
parsePacketData: parsePacketData,
|
2015-01-02 12:50:54 -05:00
|
|
|
parseNewStylePacket: parseNewStylePacket,
|
2013-01-07 23:36:14 -05:00
|
|
|
createPacketBuffer: createPacketBuffer,
|
2014-12-30 03:55:04 -05:00
|
|
|
compressPacketBuffer: compressPacketBuffer,
|
2015-01-01 17:20:47 -05:00
|
|
|
oldStylePacket: oldStylePacket,
|
|
|
|
newStylePacket: newStylePacket,
|
2013-01-07 23:36:14 -05:00
|
|
|
STRING_MAX_LENGTH: STRING_MAX_LENGTH,
|
2014-01-14 00:56:56 -05:00
|
|
|
packetIds: packetIds,
|
|
|
|
packetNames: packetNames,
|
|
|
|
packetFields: packetFields,
|
2014-04-10 19:48:06 -04:00
|
|
|
packetStates: packetStates,
|
2015-02-21 19:41:41 -05:00
|
|
|
types: types,
|
2013-12-30 10:05:22 -05:00
|
|
|
states: states,
|
2013-01-07 23:36:14 -05:00
|
|
|
get: get,
|
2013-04-05 23:02:36 -04:00
|
|
|
debug: debug,
|
2013-01-07 23:36:14 -05:00
|
|
|
};
|