From d37d801e01a5bdb037893cabf5ffe051cabce16b Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 02:13:07 +0200 Subject: [PATCH 01/12] move protocol definition in separate file --- src/protocol.js | 788 +------------------------------------------- src/protocol_def.js | 787 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 788 insertions(+), 787 deletions(-) create mode 100644 src/protocol_def.js diff --git a/src/protocol.js b/src/protocol.js index 1172a14..7149529 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -14,793 +14,7 @@ var states = { "PLAY": "play" } -var packets = { - handshaking: { - toClient: {}, - toServer: { - set_protocol: {id: 0x00, fields: [ - { name: "protocolVersion", type: "varint" }, - { name: "serverHost", type: "string" }, - { name: "serverPort", type: "ushort" }, - { name: "nextState", type: "varint" } - ]} - }, - }, - -// TODO : protocollib names aren't the best around here - status: { - toClient: { - server_info: {id: 0x00, fields: [ - { name: "response", type: "ustring" } - ]}, - ping: {id: 0x01, fields: [ - { name: "time", type: "long" } - ]} - }, - toServer: { - ping_start: {id: 0x00, fields: []}, - ping: {id: 0x01, fields: [ - { name: "time", type: "long" } - ]} - } - }, - - login: { - toClient: { - disconnect: {id: 0x00, fields: [ - { name: "reason", type: "string" } - ]}, - encryption_begin: {id: 0x01, fields: [ - { name: "serverId", type: "string" }, - { name: "publicKeyLength", type: "count", typeArgs: { type: "varint", countFor: "publicKey" } }, - { name: "publicKey", type: "buffer", typeArgs: { count: "publicKeyLength" } }, - { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, - { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, - ]}, - success: {id: 0x02, fields: [ - { name: "uuid", type: "string" }, - { name: "username", type: "string" } - ]}, - compress: { id: 0x03, fields: [ - { name: "threshold", type: "varint"} - ]} - }, - toServer: { - login_start: {id: 0x00, fields: [ - { name: "username", type: "string" } - ]}, - encryption_begin: {id: 0x01, fields: [ - { name: "sharedSecretLength", type: "count", typeArgs: { type: "varint", countFor: "sharedSecret" } }, - { name: "sharedSecret", type: "buffer", typeArgs: { count: "sharedSecretLength" } }, - { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, - { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, - ]} - } - }, - - play: { - toClient: { - keep_alive: {id: 0x00, fields: [ - { name: "keepAliveId", type: "varint" }, - ]}, - login: {id: 0x01, fields: [ - { name: "entityId", type: "int" }, - { name: "gameMode", type: "ubyte" }, - { name: "dimension", type: "byte" }, - { name: "difficulty", type: "ubyte" }, - { name: "maxPlayers", type: "ubyte" }, - { name: "levelType", type: "string" }, - { name: "reducedDebugInfo", type: "bool"} - ]}, - chat: {id: 0x02, fields: [ - { name: "message", type: "ustring" }, - { name: "position", type: "byte" } - ]}, - update_time: {id: 0x03, fields: [ - { name: "age", type: "long" }, - { name: "time", type: "long" }, - ]}, - entity_equipment: {id: 0x04, fields: [ - { name: "entityId", type: "varint" }, - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - spawn_position: {id: 0x05, fields: [ - { name: "location", type: "position" } /* TODO: Implement position */ - ]}, - update_health: {id: 0x06, fields: [ - { name: "health", type: "float" }, - { name: "food", type: "varint" }, - { name: "foodSaturation", type: "float" } - ]}, - respawn: {id: 0x07, fields: [ - { name: "dimension", type: "int" }, - { name: "difficulty", type: "ubyte" }, - { name: "gamemode", type: "ubyte" }, - { name: "levelType", type: "string" } - ]}, - position: {id: 0x08, fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "flags", type: "byte" /* It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is set, the x value is relative and not absolute. */} - ]}, - held_item_slot: {id: 0x09, fields: [ - { name: "slot", type: "byte" } - ]}, - bed: {id: 0x0a, fields: [ - { name: "entityId", type: "varint" }, - { name: "location", type: "position" } - ]}, - animation: {id: 0x0b, fields: [ - { name: "entityId", type: "varint" }, - { name: "animation", type: "ubyte" } - ]}, - named_entity_spawn: {id: 0x0c, fields: [ - { name: "entityId", type: "varint" }, - { name: "playerUUID", type: "UUID"}, - { 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" } - ]}, - collect: {id: 0x0d, fields: [ - { name: "collectedEntityId", type: "varint" }, - { name: "collectorEntityId", type: "varint" } - ]}, - spawn_entity: {id: 0x0e, fields: [ - { 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" }, - { 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; - }} - ]}} - ]}, - spawn_entity_living: {id: 0x0f, fields: [ - { name: "entityId", type: "varint" }, - { name: "type", type: "ubyte" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "headPitch", type: "byte" }, - { name: "velocityX", type: "short" }, - { name: "velocityY", type: "short" }, - { name: "velocityZ", type: "short" }, - { name: "metadata", type: "entityMetadata" }, - ]}, - spawn_entity_painting: {id: 0x10, fields: [ - { name: "entityId", type: "varint" }, - { name: "title", type: "string" }, - { name: "location", type: "position" }, - { name: "direction", type: "ubyte" } - ]}, - spawn_entity_experience_orb: {id: 0x11, fields: [ - { name: "entityId", type: "varint" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "count", type: "short" } - ]}, - entity_velocity: {id: 0x12, fields: [ - { name: "entityId", type: "varint" }, - { name: "velocityX", type: "short" }, - { name: "velocityY", type: "short" }, - { name: "velocityZ", type: "short" } - ]}, - entity_destroy: {id: 0x13, fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entityIds" } }, - { name: "entityIds", type: "array", typeArgs: { type: "varint", count: "count" } } - ]}, - entity: {id: 0x14, fields: [ - { name: "entityId", type: "varint" } - ]}, - rel_entity_move: {id: 0x15, fields: [ - { name: "entityId", type: "varint" }, - { name: "dX", type: "byte" }, - { name: "dY", type: "byte" }, - { name: "dZ", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_look: {id: 0x16, fields: [ - { name: "entityId", type: "varint" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_move_look: {id: 0x17, fields: [ - { name: "entityId", type: "varint" }, - { name: "dX", type: "byte" }, - { name: "dY", type: "byte" }, - { name: "dZ", type: "byte" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_teleport: {id: 0x18, fields: [ - { name: "entityId", type: "varint" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_head_rotation: {id: 0x19, fields: [ - { name: "entityId", type: "varint" }, - { name: "headYaw", type: "byte" }, - ]}, - entity_status: {id: 0x1a, fields: [ - { name: "entityId", type: "int" }, - { name: "entityStatus", type: "byte" } - ]}, - attach_entity: {id: 0x1b, fields: [ - { name: "entityId", type: "int" }, - { name: "vehicleId", type: "int" }, - { name: "leash", type: "bool" } - ]}, - entity_metadata: {id: 0x1c, fields: [ - { name: "entityId", type: "varint" }, - { name: "metadata", type: "entityMetadata" } - ]}, - entity_effect: {id: 0x1d, fields: [ - { name: "entityId", type: "varint" }, - { name: "effectId", type: "byte" }, - { name: "amplifier", type: "byte" }, - { name: "duration", type: "varint" }, - { name: "hideParticles", type: "bool" } - ]}, - remove_entity_effect: {id: 0x1e, fields: [ - { name: "entityId", type: "varint" }, - { name: "effectId", type: "byte" } - ]}, - experience: {id: 0x1f, fields: [ - { name: "experienceBar", type: "float" }, - { name: "level", type: "varint" }, - { name: "totalExperience", type: "varint" } - ]}, - update_attributes: {id: 0x20, fields: [ - { name: "entityId", type: "varint" }, - { name: "count", type: "count", typeArgs: { type: "int", countFor: "properties" } }, - { name: "properties", type: "array", typeArgs: { count: "count", - type: "container", typeArgs: { fields: [ - { name: "key", type: "string" }, - { name: "value", type: "double" }, - { name: "listLength", type: "count", typeArgs: { type: "varint", countFor: "this.modifiers" } }, - { name: "modifiers", type: "array", typeArgs: { count: "this.listLength", - type: "container", typeArgs: { fields: [ - { name: "UUID", type: "UUID" }, - { name: "amount", type: "double" }, - { name: "operation", type: "byte" } - ]}}} - ]} - }} - ]}, - map_chunk: {id: 0x21, fields: [ - { name: "x", type: "int" }, - { name: "z", type: "int" }, - { name: "groundUp", type: "bool" }, - { name: "bitMap", type: "ushort" }, - { name: "chunkDataLength", type: "count", typeArgs: { type: "varint", countFor: "chunkData" } }, - { name: "chunkData", type: "buffer", typeArgs: { count: "chunkDataLength" } }, - ]}, - multi_block_change: {id: 0x22, fields: [ - { name: "chunkX", type: "int" }, - { name: "chunkZ", type: "int" }, - { name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } }, - { name: "records", type: "array", typeArgs: { count: "recordCount", type: "container", typeArgs: { fields: [ - { name: "horizontalPos", type: "ubyte" }, - { name: "y", type: "ubyte" }, - { name: "blockId", type: "varint" } - ]}}} - ]}, - block_change: {id: 0x23, fields: [ - { name: "location", type: "position" }, - { name: "type", type: "varint" }, - ]}, - block_action: {id: 0x24, fields: [ - { name: "location", type: "position" }, - { name: "byte1", type: "ubyte" }, - { name: "byte2", type: "ubyte" }, - { name: "blockId", type: "varint" } - ]}, - block_break_animation: {id: 0x25, fields: [ - { name: "entityId", type: "varint" }, - { name: "location", type: "position" }, - { name: "destroyStage", type: "byte" } - ]}, - map_chunk_bulk: {id: 0x26, fields: [ - { name: "skyLightSent", type: "bool" }, - { name: "chunkColumnCount", type: "count", typeArgs: { type: "varint", countFor: "meta" } }, - { name: "meta", type: "array", typeArgs: { count: "chunkColumnCount", type: "container", typeArgs: { fields: [ - { name: "x", type: "int" }, - { name: "z", type: "int" }, - { name: "bitMap", type: "ushort" }, - ]}}}, - { name: "data", type: "restBuffer" } - ]}, - explosion: {id: 0x27, fields: [ - { name: "x", type: "float" }, - { name: "y", type: "float" }, - { name: "z", type: "float" }, - { name: "radius", type: "float" }, - { 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" } - ] - }}}, - { name: "playerMotionX", type: "float" }, - { name: "playerMotionY", type: "float" }, - { name: "playerMotionZ", type: "float" } - ]}, - world_event: {id: 0x28, fields: [ // TODO : kinda wtf naming there - { name: "effectId", type: "int" }, - { name: "location", type: "position" }, - { name: "data", type: "int" }, - { name: "global", type: "bool" } - ]}, - named_sound_effect: {id: 0x29, fields: [ - { name: "soundName", type: "string" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "volume", type: "float" }, - { name: "pitch", type: "ubyte" } - ]}, - world_particles: {id: 0x2a, fields: [ - { name: "particleId", type: "int" }, - { name: "longDistance", type: "bool"}, - { name: "x", type: "float" }, - { name: "y", type: "float" }, - { name: "z", type: "float" }, - { name: "offsetX", type: "float" }, - { name: "offsetY", type: "float" }, - { name: "offsetZ", type: "float" }, - { name: "particleData", type: "float" }, - { name: "particles", type: "int" }, - { name: "data", type: "array", typeArgs: { count: function(fields) { - if (fields.particleId === 36) - return 2; - else if (fields.particleId === 37 || fields.particleId === 38) - return 1; - else - return 0; - }, type: "varint" } } - ]}, - game_state_change: {id: 0x2b, fields: [ - { name: "reason", type: "ubyte" }, - { name: "gameMode", type: "float" } - ]}, - spawn_entity_weather:{id: 0x2c, fields: [ - { name: "entityId", type: "varint" }, - { name: "type", type: "byte" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" } - ]}, - open_window: {id: 0x2d, fields: [ - { name: "windowId", type: "ubyte" }, - { name: "inventoryType", type: "string" }, - { name: "windowTitle", type: "string" }, - { name: "slotCount", type: "ubyte" }, - { name: "entityId", type: "int", condition: function(field_values) { - return field_values['inventoryType'] == 11; - } } - ]}, - close_window: {id: 0x2e, fields: [ - { name: "windowId", type: "ubyte" } - ]}, - set_slot: {id: 0x2f, fields: [ - { name: "windowId", type: "byte" }, - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - window_items: {id: 0x30, fields: [ - { name: "windowId", type: "ubyte" }, - { name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } }, - { name: "items", type: "array", typeArgs: { type: "slot", count: "count" } } - ]}, - craft_progress_bar: {id: 0x31, fields: [ /* TODO: Bad name for this packet imo */ - { name: "windowId", type: "ubyte" }, - { name: "property", type: "short" }, - { name: "value", type: "short" } - ]}, - transaction:{id: 0x32, fields: [ - { name: "windowId", type: "byte" }, - { name: "action", type: "short" }, - { name: "accepted", type: "bool" } - ]}, - update_sign: {id: 0x33, fields: [ - { name: "location", type: "position" }, - { name: "text1", type: "string" }, - { name: "text2", type: "string" }, - { name: "text3", type: "string" }, - { name: "text4", type: "string" } - ]}, - map: {id: 0x34, fields: [ - { name: "itemDamage", type: "varint" }, - { 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; - }}, - ]}, - tile_entity_data:{id: 0x35, fields: [ - { name: "location", type: "position" }, - { name: "action", type: "ubyte" }, - { name: "nbtData", type: "restBuffer" } - ]}, - open_sign_entity: {id: 0x36, fields: [ - { name: "location", type: "position" }, - ]}, - statistics: {id: 0x37, fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entries" } }, - { name: "entries", type: "array", typeArgs: { count: "count", - type: "container", typeArgs: { fields: [ - { name: "name", type: "string" }, - { name: "value", type: "varint" } - ]} - }} - ]}, - 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: [ - { name: "UUID", type: "UUID" }, - { 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; - }, typeArgs: { countFor: "this.properties", type: "varint" }}, - { name: "properties", type: "array", condition: function(field_values) { - return field_values["action"] === 0; - }, typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [ - { name: "name", type: "string" }, - { name: "value", type: "ustring" }, - { name: "isSigned", type: "bool" }, - { name: "signature", type: "ustring", condition: function(field_values) { - return field_values["this"]["isSigned"]; - }} - ]}}}, - { 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" - }} - ]}}} - ]}, - abilities: {id: 0x39, fields: [ - { name: "flags", type: "byte" }, - { name: "flyingSpeed", type: "float" }, - { name: "walkingSpeed", type: "float" } - ]}, - tab_complete: {id: 0x3a, fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "matches" } }, - { name: "matches", type: "array", typeArgs: { type: "string", count: "count" } } - ]}, - scoreboard_objective: {id: 0x3b, fields: [ - { name: "name", type: "string" }, - { name: "action", type: "byte" }, - { 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; - }} - ]}, - scoreboard_score: {id: 0x3c, fields: [ /* TODO: itemName and scoreName may need to be switched */ - { name: "itemName", type: "string" }, - { name: "action", type: "byte" }, - { name: "scoreName", type: "string" }, - { name: "value", type: "varint", condition: function(field_values) { - return field_values['action'] != 1; - } } - ]}, - scoreboard_display_objective: {id: 0x3d, fields: [ - { name: "position", type: "byte" }, - { name: "name", type: "string" } - ]}, - scoreboard_team: {id: 0x3e, fields: [ - { name: "team", type: "string" }, - { name: "mode", type: "byte" }, - { name: "name", type: "string", condition: function(field_values) { - return field_values['mode'] == 0 || field_values['mode'] == 2; - } }, - { 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; - } }, - { 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; - } }, - { name: "playerCount", type: "count", condition: function(field_values) { - return field_values['mode'] == 0 || field_values['mode'] == 3 || field_values['mode'] == 4; - }, 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" } } - ]}, - custom_payload: {id: 0x3f, fields: [ - { name: "channel", type: "string" }, - { name: "data", type: "restBuffer" } - ]}, - kick_disconnect: {id: 0x40, fields: [ - { name: "reason", type: "string" } - ]}, - difficulty: { id: 0x41, fields: [ - { 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) { - return field_values['event'] == 1 || field_values['event'] == 2; - } }, - { 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: [ - { 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; - } }, - { name: "speed", type: "varint", condition: function(field_values) { - 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; - } } - ]}, - title: { id: 0x45, fields: [ - { 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) { - return field_values['action'] == 2; - } }, - { 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; - } } - ]}, - set_compression: { id: 0x46, fields: [ - { name: "threshold", type: "varint"} - ]}, - playerlist_header: { id: 0x47, fields: [ - { name: "header", type: "string" }, - { name: "footer", type: "string" } - ]}, - resource_pack_send: { id: 0x48, fields: [ - { name: "url", type: "string" }, - { name: "hash", type: "string" } - ]}, - update_entity_nbt: { id: 0x49, fields: [ - { name: "entityId", type: "varint" }, - { name: "tag", type: "restBuffer"} - ]} - }, - toServer: { - keep_alive: {id: 0x00, fields: [ - { name: "keepAliveId", type: "varint" } - ]}, - chat: {id: 0x01, fields: [ - { name: "message", type: "string" } - ]}, - use_entity: {id: 0x02, fields: [ - { name: "target", type: "varint" }, - { 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; - }}, - ]}, - flying: {id: 0x03, fields: [ - { name: "onGround", type: "bool" } - ]}, - position: {id: 0x04, fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "onGround", type: "bool" } - ]}, - look: {id: 0x05, fields: [ - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "onGround", type: "bool" } - ]}, - position_look: {id: 0x06, fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "onGround", type: "bool" } - ]}, - block_dig: {id: 0x07, fields: [ - { name: "status", type: "byte" }, - { name: "location", type: "position"}, - { name: "face", type: "byte" } - ]}, - block_place: {id: 0x08, fields: [ - { name: "location", type: "position" }, - { name: "direction", type: "byte" }, - { name: "heldItem", type: "slot" }, - { name: "cursorX", type: "byte" }, - { name: "cursorY", type: "byte" }, - { name: "cursorZ", type: "byte" } - ]}, - held_item_slot: {id: 0x09, fields: [ - { name: "slotId", type: "short" } - ]}, - arm_animation: {id: 0x0a, fields: []}, - entity_action: {id: 0x0b, fields: [ - { name: "entityId", type: "varint" }, - { name: "actionId", type: "varint" }, - { name: "jumpBoost", type: "varint" } - ]}, - steer_vehicle: {id: 0x0c, fields: [ - { name: "sideways", type: "float" }, - { name: "forward", type: "float" }, - { name: "jump", type: "ubyte" } - ]}, - close_window: {id: 0x0d, fields: [ - { name: "windowId", type: "byte" } - ]}, - window_click: {id: 0x0e, fields: [ - { name: "windowId", type: "byte" }, - { name: "slot", type: "short" }, - { name: "mouseButton", type: "byte" }, - { name: "action", type: "short" }, - { name: "mode", type: "byte" }, - { name: "item", type: "slot" } - ]}, - transaction: {id: 0x0f, fields: [ - { name: "windowId", type: "byte" }, - { name: "action", type: "short" }, - { name: "accepted", type: "bool" } - ]}, - set_creative_slot: {id: 0x10, fields: [ - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - enchant_item: {id: 0x11, fields: [ - { name: "windowId", type: "byte" }, - { name: "enchantment", type: "byte" } - ]}, - update_sign: {id: 0x12, fields: [ - { name: "location", type: "position" }, - { name: "text1", type: "string" }, - { name: "text2", type: "string" }, - { name: "text3", type: "string" }, - { name: "text4", type: "string" } - ]}, - abilities: {id: 0x13, fields: [ - { name: "flags", type: "byte" }, - { name: "flyingSpeed", type: "float" }, - { name: "walkingSpeed", type: "float" } - ]}, - tab_complete: {id: 0x14, fields: [ - { name: "text", type: "string" }, - { name: "hasPosition", type: "bool" }, - { name: "block", type: "position", condition: function(field_values) { - return field_values['hasPosition']; - } } - ]}, - settings: {id: 0x15, fields: [ - { name: "locale", type: "string" }, - { name: "viewDistance", type: "byte" }, - { name: "chatFlags", type: "byte" }, - { name: "chatColors", type: "bool" }, - { name: "skinParts", type: "ubyte" } - ]}, - client_command: {id: 0x16, fields: [ - { name: "payload", type: "varint" } - ]}, - custom_payload: {id: 0x17, fields: [ - { name: "channel", type: "string" }, /* TODO: wiki.vg sats no dataLength is needed? */ - { name: "data", type: "restBuffer"} - ]}, - spectate: { id: 0x18, fields: [ - { name: "target", type: "UUID"} - ]}, - resource_pack_receive: { id: 0x19, fields: [ - { name: "hash", type: "string" }, - { name: "result", type: "varint" } - ]} - } - } -}; +var packets=require("./protocol_def.js"); var packetFields = {}; var packetNames = {}; diff --git a/src/protocol_def.js b/src/protocol_def.js new file mode 100644 index 0000000..77fc139 --- /dev/null +++ b/src/protocol_def.js @@ -0,0 +1,787 @@ +module.exports= { + handshaking: { + toClient: {}, + toServer: { + set_protocol: {id: 0x00, fields: [ + { name: "protocolVersion", type: "varint" }, + { name: "serverHost", type: "string" }, + { name: "serverPort", type: "ushort" }, + { name: "nextState", type: "varint" } + ]} + }, + }, + +// TODO : protocollib names aren't the best around here + status: { + toClient: { + server_info: {id: 0x00, fields: [ + { name: "response", type: "ustring" } + ]}, + ping: {id: 0x01, fields: [ + { name: "time", type: "long" } + ]} + }, + toServer: { + ping_start: {id: 0x00, fields: []}, + ping: {id: 0x01, fields: [ + { name: "time", type: "long" } + ]} + } + }, + + login: { + toClient: { + disconnect: {id: 0x00, fields: [ + { name: "reason", type: "string" } + ]}, + encryption_begin: {id: 0x01, fields: [ + { name: "serverId", type: "string" }, + { name: "publicKeyLength", type: "count", typeArgs: { type: "varint", countFor: "publicKey" } }, + { name: "publicKey", type: "buffer", typeArgs: { count: "publicKeyLength" } }, + { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, + { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, + ]}, + success: {id: 0x02, fields: [ + { name: "uuid", type: "string" }, + { name: "username", type: "string" } + ]}, + compress: { id: 0x03, fields: [ + { name: "threshold", type: "varint"} + ]} + }, + toServer: { + login_start: {id: 0x00, fields: [ + { name: "username", type: "string" } + ]}, + encryption_begin: {id: 0x01, fields: [ + { name: "sharedSecretLength", type: "count", typeArgs: { type: "varint", countFor: "sharedSecret" } }, + { name: "sharedSecret", type: "buffer", typeArgs: { count: "sharedSecretLength" } }, + { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, + { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, + ]} + } + }, + + play: { + toClient: { + keep_alive: {id: 0x00, fields: [ + { name: "keepAliveId", type: "varint" }, + ]}, + login: {id: 0x01, fields: [ + { name: "entityId", type: "int" }, + { name: "gameMode", type: "ubyte" }, + { name: "dimension", type: "byte" }, + { name: "difficulty", type: "ubyte" }, + { name: "maxPlayers", type: "ubyte" }, + { name: "levelType", type: "string" }, + { name: "reducedDebugInfo", type: "bool"} + ]}, + chat: {id: 0x02, fields: [ + { name: "message", type: "ustring" }, + { name: "position", type: "byte" } + ]}, + update_time: {id: 0x03, fields: [ + { name: "age", type: "long" }, + { name: "time", type: "long" }, + ]}, + entity_equipment: {id: 0x04, fields: [ + { name: "entityId", type: "varint" }, + { name: "slot", type: "short" }, + { name: "item", type: "slot" } + ]}, + spawn_position: {id: 0x05, fields: [ + { name: "location", type: "position" } /* TODO: Implement position */ + ]}, + update_health: {id: 0x06, fields: [ + { name: "health", type: "float" }, + { name: "food", type: "varint" }, + { name: "foodSaturation", type: "float" } + ]}, + respawn: {id: 0x07, fields: [ + { name: "dimension", type: "int" }, + { name: "difficulty", type: "ubyte" }, + { name: "gamemode", type: "ubyte" }, + { name: "levelType", type: "string" } + ]}, + position: {id: 0x08, fields: [ + { name: "x", type: "double" }, + { name: "y", type: "double" }, + { name: "z", type: "double" }, + { name: "yaw", type: "float" }, + { name: "pitch", type: "float" }, + { name: "flags", type: "byte" /* It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is set, the x value is relative and not absolute. */} + ]}, + held_item_slot: {id: 0x09, fields: [ + { name: "slot", type: "byte" } + ]}, + bed: {id: 0x0a, fields: [ + { name: "entityId", type: "varint" }, + { name: "location", type: "position" } + ]}, + animation: {id: 0x0b, fields: [ + { name: "entityId", type: "varint" }, + { name: "animation", type: "ubyte" } + ]}, + named_entity_spawn: {id: 0x0c, fields: [ + { name: "entityId", type: "varint" }, + { name: "playerUUID", type: "UUID"}, + { 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" } + ]}, + collect: {id: 0x0d, fields: [ + { name: "collectedEntityId", type: "varint" }, + { name: "collectorEntityId", type: "varint" } + ]}, + spawn_entity: {id: 0x0e, fields: [ + { 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" }, + { 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; + }} + ]}} + ]}, + spawn_entity_living: {id: 0x0f, fields: [ + { name: "entityId", type: "varint" }, + { name: "type", type: "ubyte" }, + { name: "x", type: "int" }, + { name: "y", type: "int" }, + { name: "z", type: "int" }, + { name: "yaw", type: "byte" }, + { name: "pitch", type: "byte" }, + { name: "headPitch", type: "byte" }, + { name: "velocityX", type: "short" }, + { name: "velocityY", type: "short" }, + { name: "velocityZ", type: "short" }, + { name: "metadata", type: "entityMetadata" }, + ]}, + spawn_entity_painting: {id: 0x10, fields: [ + { name: "entityId", type: "varint" }, + { name: "title", type: "string" }, + { name: "location", type: "position" }, + { name: "direction", type: "ubyte" } + ]}, + spawn_entity_experience_orb: {id: 0x11, fields: [ + { name: "entityId", type: "varint" }, + { name: "x", type: "int" }, + { name: "y", type: "int" }, + { name: "z", type: "int" }, + { name: "count", type: "short" } + ]}, + entity_velocity: {id: 0x12, fields: [ + { name: "entityId", type: "varint" }, + { name: "velocityX", type: "short" }, + { name: "velocityY", type: "short" }, + { name: "velocityZ", type: "short" } + ]}, + entity_destroy: {id: 0x13, fields: [ + { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entityIds" } }, + { name: "entityIds", type: "array", typeArgs: { type: "varint", count: "count" } } + ]}, + entity: {id: 0x14, fields: [ + { name: "entityId", type: "varint" } + ]}, + rel_entity_move: {id: 0x15, fields: [ + { name: "entityId", type: "varint" }, + { name: "dX", type: "byte" }, + { name: "dY", type: "byte" }, + { name: "dZ", type: "byte" }, + { name: "onGround", type: "bool"} + ]}, + entity_look: {id: 0x16, fields: [ + { name: "entityId", type: "varint" }, + { name: "yaw", type: "byte" }, + { name: "pitch", type: "byte" }, + { name: "onGround", type: "bool"} + ]}, + entity_move_look: {id: 0x17, fields: [ + { name: "entityId", type: "varint" }, + { name: "dX", type: "byte" }, + { name: "dY", type: "byte" }, + { name: "dZ", type: "byte" }, + { name: "yaw", type: "byte" }, + { name: "pitch", type: "byte" }, + { name: "onGround", type: "bool"} + ]}, + entity_teleport: {id: 0x18, fields: [ + { name: "entityId", type: "varint" }, + { name: "x", type: "int" }, + { name: "y", type: "int" }, + { name: "z", type: "int" }, + { name: "yaw", type: "byte" }, + { name: "pitch", type: "byte" }, + { name: "onGround", type: "bool"} + ]}, + entity_head_rotation: {id: 0x19, fields: [ + { name: "entityId", type: "varint" }, + { name: "headYaw", type: "byte" }, + ]}, + entity_status: {id: 0x1a, fields: [ + { name: "entityId", type: "int" }, + { name: "entityStatus", type: "byte" } + ]}, + attach_entity: {id: 0x1b, fields: [ + { name: "entityId", type: "int" }, + { name: "vehicleId", type: "int" }, + { name: "leash", type: "bool" } + ]}, + entity_metadata: {id: 0x1c, fields: [ + { name: "entityId", type: "varint" }, + { name: "metadata", type: "entityMetadata" } + ]}, + entity_effect: {id: 0x1d, fields: [ + { name: "entityId", type: "varint" }, + { name: "effectId", type: "byte" }, + { name: "amplifier", type: "byte" }, + { name: "duration", type: "varint" }, + { name: "hideParticles", type: "bool" } + ]}, + remove_entity_effect: {id: 0x1e, fields: [ + { name: "entityId", type: "varint" }, + { name: "effectId", type: "byte" } + ]}, + experience: {id: 0x1f, fields: [ + { name: "experienceBar", type: "float" }, + { name: "level", type: "varint" }, + { name: "totalExperience", type: "varint" } + ]}, + update_attributes: {id: 0x20, fields: [ + { name: "entityId", type: "varint" }, + { name: "count", type: "count", typeArgs: { type: "int", countFor: "properties" } }, + { name: "properties", type: "array", typeArgs: { count: "count", + type: "container", typeArgs: { fields: [ + { name: "key", type: "string" }, + { name: "value", type: "double" }, + { name: "listLength", type: "count", typeArgs: { type: "varint", countFor: "this.modifiers" } }, + { name: "modifiers", type: "array", typeArgs: { count: "this.listLength", + type: "container", typeArgs: { fields: [ + { name: "UUID", type: "UUID" }, + { name: "amount", type: "double" }, + { name: "operation", type: "byte" } + ]}}} + ]} + }} + ]}, + map_chunk: {id: 0x21, fields: [ + { name: "x", type: "int" }, + { name: "z", type: "int" }, + { name: "groundUp", type: "bool" }, + { name: "bitMap", type: "ushort" }, + { name: "chunkDataLength", type: "count", typeArgs: { type: "varint", countFor: "chunkData" } }, + { name: "chunkData", type: "buffer", typeArgs: { count: "chunkDataLength" } }, + ]}, + multi_block_change: {id: 0x22, fields: [ + { name: "chunkX", type: "int" }, + { name: "chunkZ", type: "int" }, + { name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } }, + { name: "records", type: "array", typeArgs: { count: "recordCount", type: "container", typeArgs: { fields: [ + { name: "horizontalPos", type: "ubyte" }, + { name: "y", type: "ubyte" }, + { name: "blockId", type: "varint" } + ]}}} + ]}, + block_change: {id: 0x23, fields: [ + { name: "location", type: "position" }, + { name: "type", type: "varint" }, + ]}, + block_action: {id: 0x24, fields: [ + { name: "location", type: "position" }, + { name: "byte1", type: "ubyte" }, + { name: "byte2", type: "ubyte" }, + { name: "blockId", type: "varint" } + ]}, + block_break_animation: {id: 0x25, fields: [ + { name: "entityId", type: "varint" }, + { name: "location", type: "position" }, + { name: "destroyStage", type: "byte" } + ]}, + map_chunk_bulk: {id: 0x26, fields: [ + { name: "skyLightSent", type: "bool" }, + { name: "chunkColumnCount", type: "count", typeArgs: { type: "varint", countFor: "meta" } }, + { name: "meta", type: "array", typeArgs: { count: "chunkColumnCount", type: "container", typeArgs: { fields: [ + { name: "x", type: "int" }, + { name: "z", type: "int" }, + { name: "bitMap", type: "ushort" }, + ]}}}, + { name: "data", type: "restBuffer" } + ]}, + explosion: {id: 0x27, fields: [ + { name: "x", type: "float" }, + { name: "y", type: "float" }, + { name: "z", type: "float" }, + { name: "radius", type: "float" }, + { 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" } + ] + }}}, + { name: "playerMotionX", type: "float" }, + { name: "playerMotionY", type: "float" }, + { name: "playerMotionZ", type: "float" } + ]}, + world_event: {id: 0x28, fields: [ // TODO : kinda wtf naming there + { name: "effectId", type: "int" }, + { name: "location", type: "position" }, + { name: "data", type: "int" }, + { name: "global", type: "bool" } + ]}, + named_sound_effect: {id: 0x29, fields: [ + { name: "soundName", type: "string" }, + { name: "x", type: "int" }, + { name: "y", type: "int" }, + { name: "z", type: "int" }, + { name: "volume", type: "float" }, + { name: "pitch", type: "ubyte" } + ]}, + world_particles: {id: 0x2a, fields: [ + { name: "particleId", type: "int" }, + { name: "longDistance", type: "bool"}, + { name: "x", type: "float" }, + { name: "y", type: "float" }, + { name: "z", type: "float" }, + { name: "offsetX", type: "float" }, + { name: "offsetY", type: "float" }, + { name: "offsetZ", type: "float" }, + { name: "particleData", type: "float" }, + { name: "particles", type: "int" }, + { name: "data", type: "array", typeArgs: { count: function(fields) { + if (fields.particleId === 36) + return 2; + else if (fields.particleId === 37 || fields.particleId === 38) + return 1; + else + return 0; + }, type: "varint" } } + ]}, + game_state_change: {id: 0x2b, fields: [ + { name: "reason", type: "ubyte" }, + { name: "gameMode", type: "float" } + ]}, + spawn_entity_weather:{id: 0x2c, fields: [ + { name: "entityId", type: "varint" }, + { name: "type", type: "byte" }, + { name: "x", type: "int" }, + { name: "y", type: "int" }, + { name: "z", type: "int" } + ]}, + open_window: {id: 0x2d, fields: [ + { name: "windowId", type: "ubyte" }, + { name: "inventoryType", type: "string" }, + { name: "windowTitle", type: "string" }, + { name: "slotCount", type: "ubyte" }, + { name: "entityId", type: "int", condition: function(field_values) { + return field_values['inventoryType'] == 11; + } } + ]}, + close_window: {id: 0x2e, fields: [ + { name: "windowId", type: "ubyte" } + ]}, + set_slot: {id: 0x2f, fields: [ + { name: "windowId", type: "byte" }, + { name: "slot", type: "short" }, + { name: "item", type: "slot" } + ]}, + window_items: {id: 0x30, fields: [ + { name: "windowId", type: "ubyte" }, + { name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } }, + { name: "items", type: "array", typeArgs: { type: "slot", count: "count" } } + ]}, + craft_progress_bar: {id: 0x31, fields: [ /* TODO: Bad name for this packet imo */ + { name: "windowId", type: "ubyte" }, + { name: "property", type: "short" }, + { name: "value", type: "short" } + ]}, + transaction:{id: 0x32, fields: [ + { name: "windowId", type: "byte" }, + { name: "action", type: "short" }, + { name: "accepted", type: "bool" } + ]}, + update_sign: {id: 0x33, fields: [ + { name: "location", type: "position" }, + { name: "text1", type: "string" }, + { name: "text2", type: "string" }, + { name: "text3", type: "string" }, + { name: "text4", type: "string" } + ]}, + map: {id: 0x34, fields: [ + { name: "itemDamage", type: "varint" }, + { 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; + }}, + ]}, + tile_entity_data:{id: 0x35, fields: [ + { name: "location", type: "position" }, + { name: "action", type: "ubyte" }, + { name: "nbtData", type: "restBuffer" } + ]}, + open_sign_entity: {id: 0x36, fields: [ + { name: "location", type: "position" }, + ]}, + statistics: {id: 0x37, fields: [ + { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entries" } }, + { name: "entries", type: "array", typeArgs: { count: "count", + type: "container", typeArgs: { fields: [ + { name: "name", type: "string" }, + { name: "value", type: "varint" } + ]} + }} + ]}, + 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: [ + { name: "UUID", type: "UUID" }, + { 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; + }, typeArgs: { countFor: "this.properties", type: "varint" }}, + { name: "properties", type: "array", condition: function(field_values) { + return field_values["action"] === 0; + }, typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [ + { name: "name", type: "string" }, + { name: "value", type: "ustring" }, + { name: "isSigned", type: "bool" }, + { name: "signature", type: "ustring", condition: function(field_values) { + return field_values["this"]["isSigned"]; + }} + ]}}}, + { 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" + }} + ]}}} + ]}, + abilities: {id: 0x39, fields: [ + { name: "flags", type: "byte" }, + { name: "flyingSpeed", type: "float" }, + { name: "walkingSpeed", type: "float" } + ]}, + tab_complete: {id: 0x3a, fields: [ + { name: "count", type: "count", typeArgs: { type: "varint", countFor: "matches" } }, + { name: "matches", type: "array", typeArgs: { type: "string", count: "count" } } + ]}, + scoreboard_objective: {id: 0x3b, fields: [ + { name: "name", type: "string" }, + { name: "action", type: "byte" }, + { 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; + }} + ]}, + scoreboard_score: {id: 0x3c, fields: [ /* TODO: itemName and scoreName may need to be switched */ + { name: "itemName", type: "string" }, + { name: "action", type: "byte" }, + { name: "scoreName", type: "string" }, + { name: "value", type: "varint", condition: function(field_values) { + return field_values['action'] != 1; + } } + ]}, + scoreboard_display_objective: {id: 0x3d, fields: [ + { name: "position", type: "byte" }, + { name: "name", type: "string" } + ]}, + scoreboard_team: {id: 0x3e, fields: [ + { name: "team", type: "string" }, + { name: "mode", type: "byte" }, + { name: "name", type: "string", condition: function(field_values) { + return field_values['mode'] == 0 || field_values['mode'] == 2; + } }, + { 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; + } }, + { 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; + } }, + { name: "playerCount", type: "count", condition: function(field_values) { + return field_values['mode'] == 0 || field_values['mode'] == 3 || field_values['mode'] == 4; + }, 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" } } + ]}, + custom_payload: {id: 0x3f, fields: [ + { name: "channel", type: "string" }, + { name: "data", type: "restBuffer" } + ]}, + kick_disconnect: {id: 0x40, fields: [ + { name: "reason", type: "string" } + ]}, + difficulty: { id: 0x41, fields: [ + { 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) { + return field_values['event'] == 1 || field_values['event'] == 2; + } }, + { 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: [ + { 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; + } }, + { name: "speed", type: "varint", condition: function(field_values) { + 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; + } } + ]}, + title: { id: 0x45, fields: [ + { 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) { + return field_values['action'] == 2; + } }, + { 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; + } } + ]}, + set_compression: { id: 0x46, fields: [ + { name: "threshold", type: "varint"} + ]}, + playerlist_header: { id: 0x47, fields: [ + { name: "header", type: "string" }, + { name: "footer", type: "string" } + ]}, + resource_pack_send: { id: 0x48, fields: [ + { name: "url", type: "string" }, + { name: "hash", type: "string" } + ]}, + update_entity_nbt: { id: 0x49, fields: [ + { name: "entityId", type: "varint" }, + { name: "tag", type: "restBuffer"} + ]} + }, + toServer: { + keep_alive: {id: 0x00, fields: [ + { name: "keepAliveId", type: "varint" } + ]}, + chat: {id: 0x01, fields: [ + { name: "message", type: "string" } + ]}, + use_entity: {id: 0x02, fields: [ + { name: "target", type: "varint" }, + { 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; + }}, + ]}, + flying: {id: 0x03, fields: [ + { name: "onGround", type: "bool" } + ]}, + position: {id: 0x04, fields: [ + { name: "x", type: "double" }, + { name: "y", type: "double" }, + { name: "z", type: "double" }, + { name: "onGround", type: "bool" } + ]}, + look: {id: 0x05, fields: [ + { name: "yaw", type: "float" }, + { name: "pitch", type: "float" }, + { name: "onGround", type: "bool" } + ]}, + position_look: {id: 0x06, fields: [ + { name: "x", type: "double" }, + { name: "y", type: "double" }, + { name: "z", type: "double" }, + { name: "yaw", type: "float" }, + { name: "pitch", type: "float" }, + { name: "onGround", type: "bool" } + ]}, + block_dig: {id: 0x07, fields: [ + { name: "status", type: "byte" }, + { name: "location", type: "position"}, + { name: "face", type: "byte" } + ]}, + block_place: {id: 0x08, fields: [ + { name: "location", type: "position" }, + { name: "direction", type: "byte" }, + { name: "heldItem", type: "slot" }, + { name: "cursorX", type: "byte" }, + { name: "cursorY", type: "byte" }, + { name: "cursorZ", type: "byte" } + ]}, + held_item_slot: {id: 0x09, fields: [ + { name: "slotId", type: "short" } + ]}, + arm_animation: {id: 0x0a, fields: []}, + entity_action: {id: 0x0b, fields: [ + { name: "entityId", type: "varint" }, + { name: "actionId", type: "varint" }, + { name: "jumpBoost", type: "varint" } + ]}, + steer_vehicle: {id: 0x0c, fields: [ + { name: "sideways", type: "float" }, + { name: "forward", type: "float" }, + { name: "jump", type: "ubyte" } + ]}, + close_window: {id: 0x0d, fields: [ + { name: "windowId", type: "byte" } + ]}, + window_click: {id: 0x0e, fields: [ + { name: "windowId", type: "byte" }, + { name: "slot", type: "short" }, + { name: "mouseButton", type: "byte" }, + { name: "action", type: "short" }, + { name: "mode", type: "byte" }, + { name: "item", type: "slot" } + ]}, + transaction: {id: 0x0f, fields: [ + { name: "windowId", type: "byte" }, + { name: "action", type: "short" }, + { name: "accepted", type: "bool" } + ]}, + set_creative_slot: {id: 0x10, fields: [ + { name: "slot", type: "short" }, + { name: "item", type: "slot" } + ]}, + enchant_item: {id: 0x11, fields: [ + { name: "windowId", type: "byte" }, + { name: "enchantment", type: "byte" } + ]}, + update_sign: {id: 0x12, fields: [ + { name: "location", type: "position" }, + { name: "text1", type: "string" }, + { name: "text2", type: "string" }, + { name: "text3", type: "string" }, + { name: "text4", type: "string" } + ]}, + abilities: {id: 0x13, fields: [ + { name: "flags", type: "byte" }, + { name: "flyingSpeed", type: "float" }, + { name: "walkingSpeed", type: "float" } + ]}, + tab_complete: {id: 0x14, fields: [ + { name: "text", type: "string" }, + { name: "hasPosition", type: "bool" }, + { name: "block", type: "position", condition: function(field_values) { + return field_values['hasPosition']; + } } + ]}, + settings: {id: 0x15, fields: [ + { name: "locale", type: "string" }, + { name: "viewDistance", type: "byte" }, + { name: "chatFlags", type: "byte" }, + { name: "chatColors", type: "bool" }, + { name: "skinParts", type: "ubyte" } + ]}, + client_command: {id: 0x16, fields: [ + { name: "payload", type: "varint" } + ]}, + custom_payload: {id: 0x17, fields: [ + { name: "channel", type: "string" }, /* TODO: wiki.vg sats no dataLength is needed? */ + { name: "data", type: "restBuffer"} + ]}, + spectate: { id: 0x18, fields: [ + { name: "target", type: "UUID"} + ]}, + resource_pack_receive: { id: 0x19, fields: [ + { name: "hash", type: "string" }, + { name: "result", type: "varint" } + ]} + } + } +}; From e47feb1cf6e45f6bcb3afa20d5ead6629634ec92 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 02:26:45 +0200 Subject: [PATCH 02/12] encapsulate condition evaluation --- src/protocol.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index 7149529..c0796a8 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -119,6 +119,11 @@ for (var n in entityMetadataTypes) { entityMetadataTypeBytes[entityMetadataTypes[n].type] = n; } +function evalCondition(condition,arg) +{ + return condition(arg); +} + function sizeOfEntityMetadata(value) { var size = 1 + value.length; var item; @@ -633,7 +638,7 @@ function sizeOfCount(value, typeArgs, rootNode) { } function read(buffer, cursor, fieldInfo, rootNodes) { - if (fieldInfo.condition && !fieldInfo.condition(rootNodes)) { + if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNodes)) { return null; } var type = types[fieldInfo.type]; @@ -651,7 +656,7 @@ function read(buffer, cursor, fieldInfo, rootNodes) { } function write(value, buffer, offset, fieldInfo, rootNode) { - if (fieldInfo.condition && !fieldInfo.condition(rootNode)) { + if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNode)) { return offset; } var type = types[fieldInfo.type]; @@ -664,7 +669,7 @@ function write(value, buffer, offset, fieldInfo, rootNode) { } function sizeOf(value, fieldInfo, rootNode) { - if (fieldInfo.condition && !fieldInfo.condition(rootNode)) { + if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNode)) { return 0; } var type = types[fieldInfo.type]; From bf8a7899aeef484826e68261337d1cf56b6d1ae0 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:08:30 +0200 Subject: [PATCH 03/12] transform condition functions into objects --- src/protocol.js | 19 ++++- src/protocol_def.js | 198 ++++++++++++-------------------------------- test/test.js | 2 +- 3 files changed, 69 insertions(+), 150 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index c0796a8..f3b9632 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -119,9 +119,23 @@ for (var n in entityMetadataTypes) { entityMetadataTypeBytes[entityMetadataTypes[n].type] = n; } -function evalCondition(condition,arg) +function isFunction(functionToCheck) { + var getType = {}; + return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; +} + +function evalCondition(condition,field_values) { - return condition(arg); + if(!isFunction(condition)) + { + var field_value_to_test="this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field]; + var b=condition.values.some(function(value) {return field_value_to_test===value;}); + if("different" in condition && condition["different"]) + return !b; + else + return b; + } + return condition(field_values); } function sizeOfEntityMetadata(value) { @@ -869,4 +883,5 @@ module.exports = { states: states, get: get, debug: debug, + evalCondition:evalCondition }; diff --git a/src/protocol_def.js b/src/protocol_def.js index 77fc139..528d6db 100644 --- a/src/protocol_def.js +++ b/src/protocol_def.js @@ -147,15 +147,9 @@ module.exports= { { name: "yaw", type: "byte" }, { 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; - }} + { name: "velocityX", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}}, + { name: "velocityY", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}}, + { name: "velocityZ", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}} ]}} ]}, spawn_entity_living: {id: 0x0f, fields: [ @@ -389,9 +383,7 @@ module.exports= { { name: "inventoryType", type: "string" }, { name: "windowTitle", type: "string" }, { name: "slotCount", type: "ubyte" }, - { name: "entityId", type: "int", condition: function(field_values) { - return field_values['inventoryType'] == 11; - } } + { name: "entityId", type: "int", condition: {"field":"inventoryType","values":[11]}} ]}, close_window: {id: 0x2e, fields: [ { name: "windowId", type: "ubyte" } @@ -433,21 +425,11 @@ module.exports= { { 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; - }}, + { name: "rows", type: "byte", condition: {"field":"columns","values":[0],"different":true}}, + { name: "x", type: "byte", condition: {"field":"columns","values":[0],"different":true}}, + { name: "y", type: "byte", condition: {"field":"columns","values":[0],"different":true}}, + { name: "dataLength", type: "count", typeArgs: { countFor: "data", type: "varint" }, condition: {"field":"columns","values":[0],"different":true}}, + { name: "data", type: "buffer", typeArgs: { count: "dataLength" }, condition: {"field":"columns","values":[0],"different":true}}, ]}, tile_entity_data:{id: 0x35, fields: [ { name: "location", type: "position" }, @@ -471,34 +453,20 @@ module.exports= { { name: "length", type: "count", typeArgs: { type: "varint", countFor: "data" }}, { name: "data", type: "array", typeArgs: { count: "length", type: "container", typeArgs: { fields: [ { name: "UUID", type: "UUID" }, - { 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; - }, typeArgs: { countFor: "this.properties", type: "varint" }}, - { name: "properties", type: "array", condition: function(field_values) { - return field_values["action"] === 0; - }, typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [ + { name: "name", type: "string", condition: {"field":"action","values":[0]}}, + { name: "propertiesLength", type: "count", condition: {"field":"action","values":[0]} + , typeArgs: { countFor: "this.properties", type: "varint" }}, + { name: "properties", type: "array", condition: {"field":"action","values":[0]} + , typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [ { name: "name", type: "string" }, { name: "value", type: "ustring" }, { name: "isSigned", type: "bool" }, - { name: "signature", type: "ustring", condition: function(field_values) { - return field_values["this"]["isSigned"]; - }} + { name: "signature", type: "ustring", condition: {"field":"isSigned","values":[true],"this":true}} ]}}}, - { 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" - }} + { name: "gamemode", type: "varint", condition: {"field":"action","values":[0,1]}}, + { name: "ping", type: "varint", condition: {"field":"action","values":[0,2]}}, + { name: "hasDisplayName", type: "bool", condition: {"field":"action","values":[0,3]}}, + { name: "displayName", type: "string", condition: {"field":"hasDisplayName","values":[true]}} ]}}} ]}, abilities: {id: 0x39, fields: [ @@ -513,20 +481,14 @@ module.exports= { scoreboard_objective: {id: 0x3b, fields: [ { name: "name", type: "string" }, { name: "action", type: "byte" }, - { 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; - }} + { name: "displayText", type: "string", condition: {"field":"action","values":[0,2]}}, + { name: "type", type: "string", condition: {"field":"action","values":[0,2]}} ]}, scoreboard_score: {id: 0x3c, fields: [ /* TODO: itemName and scoreName may need to be switched */ { name: "itemName", type: "string" }, { name: "action", type: "byte" }, { name: "scoreName", type: "string" }, - { name: "value", type: "varint", condition: function(field_values) { - return field_values['action'] != 1; - } } + { name: "value", type: "varint", condition: {"field":"action","values":[1],"different":true}} ]}, scoreboard_display_objective: {id: 0x3d, fields: [ { name: "position", type: "byte" }, @@ -535,30 +497,14 @@ module.exports= { scoreboard_team: {id: 0x3e, fields: [ { name: "team", type: "string" }, { name: "mode", type: "byte" }, - { name: "name", type: "string", condition: function(field_values) { - return field_values['mode'] == 0 || field_values['mode'] == 2; - } }, - { 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; - } }, - { 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; - } }, - { name: "playerCount", type: "count", condition: function(field_values) { - return field_values['mode'] == 0 || field_values['mode'] == 3 || field_values['mode'] == 4; - }, 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" } } + { name: "name", type: "string", condition: {"field":"mode","values":[0,2]}}, + { name: "prefix", type: "string", condition: {"field":"mode","values":[0,2]}}, + { name: "suffix", type: "string", condition: {"field":"mode","values":[0,2]}}, + { name: "friendlyFire", type: "byte", condition: {"field":"mode","values":[0,2]}}, + { name: "nameTagVisibility", type: "string", condition: {"field":"mode","values":[0,2]}}, + { name: "color", type: "byte", condition: {"field":"mode","values":[0,2]}}, + { name: "playerCount", type: "count", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "short", countFor: "players" } }, + { name: "players", type: "array", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "string", count: "playerCount" } } ]}, custom_payload: {id: 0x3f, fields: [ { name: "channel", type: "string" }, @@ -572,66 +518,32 @@ module.exports= { ]}, 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) { - return field_values['event'] == 1 || field_values['event'] == 2; - } }, - { name: "message", type: "string", condition: function(field_values) { - return field_values['event'] == 2; - } } + { name: "duration", type: "varint", condition: {"field":"event","values":[1]}}, + { name: "playerId", type: "varint", condition: {"field":"event","values":[2]}}, + { name: "entityId", type: "int", condition: {"field":"event","values":[1,2]}}, + { name: "message", type: "string", condition: {"field":"event","values":[2]}} ]}, camera: { id: 0x43, fields: [ { name: "cameraId", type: "varint" } ]}, world_border: { id: 0x44, fields: [ { 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; - } }, - { name: "speed", type: "varint", condition: function(field_values) { - 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; - } } + { name: "radius", type: "double", condition: {"field":"action","values":[0]}}, + { name: "x", type: "double", condition: {"field":"action","values":[2,3]}}, + { name: "z", type: "double", condition: {"field":"action","values":[2,3]}}, + { name: "old_radius", type: "double", condition: {"field":"action","values":[1,3]}}, + { name: "new_radius", type: "double", condition: {"field":"action","values":[1,3]}}, + { name: "speed", type: "varint", condition: {"field":"action","values":[1,3]}}, + { name: "portalBoundary", type: "varint", condition: {"field":"action","values":[3]}}, + { name: "warning_time", type: "varint", condition: {"field":"action","values":[4,3]}}, + { name: "warning_blocks", type: "varint", condition: {"field":"action","values":[5,3]}} ]}, title: { id: 0x45, fields: [ { 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) { - return field_values['action'] == 2; - } }, - { 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; - } } + { name: "text", type: "string", condition: {"field":"action","values":[0,1]}}, + { name: "fadeIn", type: "int", condition: {"field":"action","values":[2]}}, + { name: "stay", type: "int", condition: {"field":"action","values":[2]}}, + { name: "fadeOut", type: "int", condition: {"field":"action","values":[2]}} ]}, set_compression: { id: 0x46, fields: [ { name: "threshold", type: "varint"} @@ -659,15 +571,9 @@ module.exports= { use_entity: {id: 0x02, fields: [ { name: "target", type: "varint" }, { 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; - }}, + { name: "x", type: "float", condition: {"field":"mouse","values":[2]}}, + { name: "y", type: "float", condition: {"field":"mouse","values":[2]}}, + { name: "z", type: "float", condition: {"field":"mouse","values":[2]}}, ]}, flying: {id: 0x03, fields: [ { name: "onGround", type: "bool" } @@ -757,9 +663,7 @@ module.exports= { tab_complete: {id: 0x14, fields: [ { name: "text", type: "string" }, { name: "hasPosition", type: "bool" }, - { name: "block", type: "position", condition: function(field_values) { - return field_values['hasPosition']; - } } + { name: "block", type: "position", condition: {"field":"hasPosition","values":[true]}} ]}, settings: {id: 0x15, fields: [ { name: "locale", type: "string" }, diff --git a/test/test.js b/test/test.js index a7f2ce8..8e41961 100644 --- a/test/test.js +++ b/test/test.js @@ -173,7 +173,7 @@ describe("packets", function() { // empty object uses default values var packet = {}; packetInfo.forEach(function(field) { - if (!field.hasOwnProperty("condition") || field.condition(packet)) { + if (!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) { var fieldVal = values[field.type]; if (typeof fieldVal === "undefined") { throw new Error("No value for type " + field.type); From 9abe7997156152928944d270df8079eeef137779 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:22:37 +0200 Subject: [PATCH 04/12] transform typeArgs function into object --- src/protocol.js | 12 ++++++++++-- src/protocol_def.js | 11 +++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index f3b9632..2b154ec 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -591,14 +591,22 @@ function readRestBuffer(buffer, offset, typeArgs, rootNode) { }; } +function evalCount(count,fields) +{ + if(fields[count["field"]] in count["map"]) + return count["map"][fields[count["field"]]]; + return count["default"]; +} + function readArray(buffer, offset, typeArgs, rootNode) { var results = { value: [], size: 0 } var count; - if (typeof typeArgs.count === "function") - count = typeArgs.count(rootNode); + if (typeof typeArgs.count === "object") { + count = evalCount(typeArgs.count,rootNode); + } else count = getField(typeArgs.count, rootNode); for (var i = 0; i < count; i++) { diff --git a/src/protocol_def.js b/src/protocol_def.js index 528d6db..3ff565f 100644 --- a/src/protocol_def.js +++ b/src/protocol_def.js @@ -358,14 +358,9 @@ module.exports= { { name: "offsetZ", type: "float" }, { name: "particleData", type: "float" }, { name: "particles", type: "int" }, - { name: "data", type: "array", typeArgs: { count: function(fields) { - if (fields.particleId === 36) - return 2; - else if (fields.particleId === 37 || fields.particleId === 38) - return 1; - else - return 0; - }, type: "varint" } } + { name: "data", type: "array", + typeArgs: { count: {"field":"particleId","map":{"36":2,"37":1,"38":1},"default":0}, + type: "varint" } } ]}, game_state_change: {id: 0x2b, fields: [ { name: "reason", type: "ubyte" }, From ea2e97d59fdf665845bdfb92db1f197d6d59d886 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:25:40 +0200 Subject: [PATCH 05/12] removing comments before transformation to json : they'll have to go somewhere else --- src/protocol_def.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/protocol_def.js b/src/protocol_def.js index 3ff565f..bbde67a 100644 --- a/src/protocol_def.js +++ b/src/protocol_def.js @@ -10,8 +10,6 @@ module.exports= { ]} }, }, - -// TODO : protocollib names aren't the best around here status: { toClient: { server_info: {id: 0x00, fields: [ @@ -90,7 +88,7 @@ module.exports= { { name: "item", type: "slot" } ]}, spawn_position: {id: 0x05, fields: [ - { name: "location", type: "position" } /* TODO: Implement position */ + { name: "location", type: "position" } ]}, update_health: {id: 0x06, fields: [ { name: "health", type: "float" }, @@ -109,7 +107,7 @@ module.exports= { { name: "z", type: "double" }, { name: "yaw", type: "float" }, { name: "pitch", type: "float" }, - { name: "flags", type: "byte" /* It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is set, the x value is relative and not absolute. */} + { name: "flags", type: "byte"} ]}, held_item_slot: {id: 0x09, fields: [ { name: "slot", type: "byte" } @@ -333,7 +331,7 @@ module.exports= { { name: "playerMotionY", type: "float" }, { name: "playerMotionZ", type: "float" } ]}, - world_event: {id: 0x28, fields: [ // TODO : kinda wtf naming there + world_event: {id: 0x28, fields: [ { name: "effectId", type: "int" }, { name: "location", type: "position" }, { name: "data", type: "int" }, @@ -393,7 +391,7 @@ module.exports= { { name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } }, { name: "items", type: "array", typeArgs: { type: "slot", count: "count" } } ]}, - craft_progress_bar: {id: 0x31, fields: [ /* TODO: Bad name for this packet imo */ + craft_progress_bar: {id: 0x31, fields: [ { name: "windowId", type: "ubyte" }, { name: "property", type: "short" }, { name: "value", type: "short" } @@ -479,7 +477,7 @@ module.exports= { { name: "displayText", type: "string", condition: {"field":"action","values":[0,2]}}, { name: "type", type: "string", condition: {"field":"action","values":[0,2]}} ]}, - scoreboard_score: {id: 0x3c, fields: [ /* TODO: itemName and scoreName may need to be switched */ + scoreboard_score: {id: 0x3c, fields: [ { name: "itemName", type: "string" }, { name: "action", type: "byte" }, { name: "scoreName", type: "string" }, @@ -671,7 +669,7 @@ module.exports= { { name: "payload", type: "varint" } ]}, custom_payload: {id: 0x17, fields: [ - { name: "channel", type: "string" }, /* TODO: wiki.vg sats no dataLength is needed? */ + { name: "channel", type: "string" }, { name: "data", type: "restBuffer"} ]}, spectate: { id: 0x18, fields: [ From b3502291603eb79ac45a13beb49cb29777a0fb75 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:37:42 +0200 Subject: [PATCH 06/12] hexa to hexa string from json convertion --- src/protocol.js | 2 +- src/protocol_def.js | 222 ++++++++++++++++++++++---------------------- 2 files changed, 112 insertions(+), 112 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index 2b154ec..3e68236 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -31,7 +31,7 @@ var packetStates = {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 id = parseInt(info.id); var fields = info.fields; assert(id !== undefined, 'missing id for packet '+name); diff --git a/src/protocol_def.js b/src/protocol_def.js index bbde67a..91fd661 100644 --- a/src/protocol_def.js +++ b/src/protocol_def.js @@ -2,7 +2,7 @@ module.exports= { handshaking: { toClient: {}, toServer: { - set_protocol: {id: 0x00, fields: [ + set_protocol: {id: "0x00", fields: [ { name: "protocolVersion", type: "varint" }, { name: "serverHost", type: "string" }, { name: "serverPort", type: "ushort" }, @@ -12,16 +12,16 @@ module.exports= { }, status: { toClient: { - server_info: {id: 0x00, fields: [ + server_info: {id: "0x00", fields: [ { name: "response", type: "ustring" } ]}, - ping: {id: 0x01, fields: [ + ping: {id: "0x01", fields: [ { name: "time", type: "long" } ]} }, toServer: { - ping_start: {id: 0x00, fields: []}, - ping: {id: 0x01, fields: [ + ping_start: {id: "0x00", fields: []}, + ping: {id: "0x01", fields: [ { name: "time", type: "long" } ]} } @@ -29,29 +29,29 @@ module.exports= { login: { toClient: { - disconnect: {id: 0x00, fields: [ + disconnect: {id: "0x00", fields: [ { name: "reason", type: "string" } ]}, - encryption_begin: {id: 0x01, fields: [ + encryption_begin: {id: "0x01", fields: [ { name: "serverId", type: "string" }, { name: "publicKeyLength", type: "count", typeArgs: { type: "varint", countFor: "publicKey" } }, { name: "publicKey", type: "buffer", typeArgs: { count: "publicKeyLength" } }, { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, ]}, - success: {id: 0x02, fields: [ + success: {id: "0x02", fields: [ { name: "uuid", type: "string" }, { name: "username", type: "string" } ]}, - compress: { id: 0x03, fields: [ + compress: { id: "0x03", fields: [ { name: "threshold", type: "varint"} ]} }, toServer: { - login_start: {id: 0x00, fields: [ + login_start: {id: "0x00", fields: [ { name: "username", type: "string" } ]}, - encryption_begin: {id: 0x01, fields: [ + encryption_begin: {id: "0x01", fields: [ { name: "sharedSecretLength", type: "count", typeArgs: { type: "varint", countFor: "sharedSecret" } }, { name: "sharedSecret", type: "buffer", typeArgs: { count: "sharedSecretLength" } }, { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, @@ -62,10 +62,10 @@ module.exports= { play: { toClient: { - keep_alive: {id: 0x00, fields: [ + keep_alive: {id: "0x00", fields: [ { name: "keepAliveId", type: "varint" }, ]}, - login: {id: 0x01, fields: [ + login: {id: "0x01", fields: [ { name: "entityId", type: "int" }, { name: "gameMode", type: "ubyte" }, { name: "dimension", type: "byte" }, @@ -74,34 +74,34 @@ module.exports= { { name: "levelType", type: "string" }, { name: "reducedDebugInfo", type: "bool"} ]}, - chat: {id: 0x02, fields: [ + chat: {id: "0x02", fields: [ { name: "message", type: "ustring" }, { name: "position", type: "byte" } ]}, - update_time: {id: 0x03, fields: [ + update_time: {id: "0x03", fields: [ { name: "age", type: "long" }, { name: "time", type: "long" }, ]}, - entity_equipment: {id: 0x04, fields: [ + entity_equipment: {id: "0x04", fields: [ { name: "entityId", type: "varint" }, { name: "slot", type: "short" }, { name: "item", type: "slot" } ]}, - spawn_position: {id: 0x05, fields: [ + spawn_position: {id: "0x05", fields: [ { name: "location", type: "position" } ]}, - update_health: {id: 0x06, fields: [ + update_health: {id: "0x06", fields: [ { name: "health", type: "float" }, { name: "food", type: "varint" }, { name: "foodSaturation", type: "float" } ]}, - respawn: {id: 0x07, fields: [ + respawn: {id: "0x07", fields: [ { name: "dimension", type: "int" }, { name: "difficulty", type: "ubyte" }, { name: "gamemode", type: "ubyte" }, { name: "levelType", type: "string" } ]}, - position: {id: 0x08, fields: [ + position: {id: "0x08", fields: [ { name: "x", type: "double" }, { name: "y", type: "double" }, { name: "z", type: "double" }, @@ -109,18 +109,18 @@ module.exports= { { name: "pitch", type: "float" }, { name: "flags", type: "byte"} ]}, - held_item_slot: {id: 0x09, fields: [ + held_item_slot: {id: "0x09", fields: [ { name: "slot", type: "byte" } ]}, - bed: {id: 0x0a, fields: [ + bed: {id: "0x0a", fields: [ { name: "entityId", type: "varint" }, { name: "location", type: "position" } ]}, - animation: {id: 0x0b, fields: [ + animation: {id: "0x0b", fields: [ { name: "entityId", type: "varint" }, { name: "animation", type: "ubyte" } ]}, - named_entity_spawn: {id: 0x0c, fields: [ + named_entity_spawn: {id: "0x0c", fields: [ { name: "entityId", type: "varint" }, { name: "playerUUID", type: "UUID"}, { name: "x", type: "int" }, @@ -131,11 +131,11 @@ module.exports= { { name: "currentItem", type: "short" }, { name: "metadata", type: "entityMetadata" } ]}, - collect: {id: 0x0d, fields: [ + collect: {id: "0x0d", fields: [ { name: "collectedEntityId", type: "varint" }, { name: "collectorEntityId", type: "varint" } ]}, - spawn_entity: {id: 0x0e, fields: [ + spawn_entity: {id: "0x0e", fields: [ { name: "entityId", type: "varint" }, { name: "type", type: "byte" }, { name: "x", type: "int" }, @@ -150,7 +150,7 @@ module.exports= { { name: "velocityZ", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}} ]}} ]}, - spawn_entity_living: {id: 0x0f, fields: [ + spawn_entity_living: {id: "0x0f", fields: [ { name: "entityId", type: "varint" }, { name: "type", type: "ubyte" }, { name: "x", type: "int" }, @@ -164,46 +164,46 @@ module.exports= { { name: "velocityZ", type: "short" }, { name: "metadata", type: "entityMetadata" }, ]}, - spawn_entity_painting: {id: 0x10, fields: [ + spawn_entity_painting: {id: "0x10", fields: [ { name: "entityId", type: "varint" }, { name: "title", type: "string" }, { name: "location", type: "position" }, { name: "direction", type: "ubyte" } ]}, - spawn_entity_experience_orb: {id: 0x11, fields: [ + spawn_entity_experience_orb: {id: "0x11", fields: [ { name: "entityId", type: "varint" }, { name: "x", type: "int" }, { name: "y", type: "int" }, { name: "z", type: "int" }, { name: "count", type: "short" } ]}, - entity_velocity: {id: 0x12, fields: [ + entity_velocity: {id: "0x12", fields: [ { name: "entityId", type: "varint" }, { name: "velocityX", type: "short" }, { name: "velocityY", type: "short" }, { name: "velocityZ", type: "short" } ]}, - entity_destroy: {id: 0x13, fields: [ + entity_destroy: {id: "0x13", fields: [ { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entityIds" } }, { name: "entityIds", type: "array", typeArgs: { type: "varint", count: "count" } } ]}, - entity: {id: 0x14, fields: [ + entity: {id: "0x14", fields: [ { name: "entityId", type: "varint" } ]}, - rel_entity_move: {id: 0x15, fields: [ + rel_entity_move: {id: "0x15", fields: [ { name: "entityId", type: "varint" }, { name: "dX", type: "byte" }, { name: "dY", type: "byte" }, { name: "dZ", type: "byte" }, { name: "onGround", type: "bool"} ]}, - entity_look: {id: 0x16, fields: [ + entity_look: {id: "0x16", fields: [ { name: "entityId", type: "varint" }, { name: "yaw", type: "byte" }, { name: "pitch", type: "byte" }, { name: "onGround", type: "bool"} ]}, - entity_move_look: {id: 0x17, fields: [ + entity_move_look: {id: "0x17", fields: [ { name: "entityId", type: "varint" }, { name: "dX", type: "byte" }, { name: "dY", type: "byte" }, @@ -212,7 +212,7 @@ module.exports= { { name: "pitch", type: "byte" }, { name: "onGround", type: "bool"} ]}, - entity_teleport: {id: 0x18, fields: [ + entity_teleport: {id: "0x18", fields: [ { name: "entityId", type: "varint" }, { name: "x", type: "int" }, { name: "y", type: "int" }, @@ -221,40 +221,40 @@ module.exports= { { name: "pitch", type: "byte" }, { name: "onGround", type: "bool"} ]}, - entity_head_rotation: {id: 0x19, fields: [ + entity_head_rotation: {id: "0x19", fields: [ { name: "entityId", type: "varint" }, { name: "headYaw", type: "byte" }, ]}, - entity_status: {id: 0x1a, fields: [ + entity_status: {id: "0x1a", fields: [ { name: "entityId", type: "int" }, { name: "entityStatus", type: "byte" } ]}, - attach_entity: {id: 0x1b, fields: [ + attach_entity: {id: "0x1b", fields: [ { name: "entityId", type: "int" }, { name: "vehicleId", type: "int" }, { name: "leash", type: "bool" } ]}, - entity_metadata: {id: 0x1c, fields: [ + entity_metadata: {id: "0x1c", fields: [ { name: "entityId", type: "varint" }, { name: "metadata", type: "entityMetadata" } ]}, - entity_effect: {id: 0x1d, fields: [ + entity_effect: {id: "0x1d", fields: [ { name: "entityId", type: "varint" }, { name: "effectId", type: "byte" }, { name: "amplifier", type: "byte" }, { name: "duration", type: "varint" }, { name: "hideParticles", type: "bool" } ]}, - remove_entity_effect: {id: 0x1e, fields: [ + remove_entity_effect: {id: "0x1e", fields: [ { name: "entityId", type: "varint" }, { name: "effectId", type: "byte" } ]}, - experience: {id: 0x1f, fields: [ + experience: {id: "0x1f", fields: [ { name: "experienceBar", type: "float" }, { name: "level", type: "varint" }, { name: "totalExperience", type: "varint" } ]}, - update_attributes: {id: 0x20, fields: [ + update_attributes: {id: "0x20", fields: [ { name: "entityId", type: "varint" }, { name: "count", type: "count", typeArgs: { type: "int", countFor: "properties" } }, { name: "properties", type: "array", typeArgs: { count: "count", @@ -271,7 +271,7 @@ module.exports= { ]} }} ]}, - map_chunk: {id: 0x21, fields: [ + map_chunk: {id: "0x21", fields: [ { name: "x", type: "int" }, { name: "z", type: "int" }, { name: "groundUp", type: "bool" }, @@ -279,7 +279,7 @@ module.exports= { { name: "chunkDataLength", type: "count", typeArgs: { type: "varint", countFor: "chunkData" } }, { name: "chunkData", type: "buffer", typeArgs: { count: "chunkDataLength" } }, ]}, - multi_block_change: {id: 0x22, fields: [ + multi_block_change: {id: "0x22", fields: [ { name: "chunkX", type: "int" }, { name: "chunkZ", type: "int" }, { name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } }, @@ -289,22 +289,22 @@ module.exports= { { name: "blockId", type: "varint" } ]}}} ]}, - block_change: {id: 0x23, fields: [ + block_change: {id: "0x23", fields: [ { name: "location", type: "position" }, { name: "type", type: "varint" }, ]}, - block_action: {id: 0x24, fields: [ + block_action: {id: "0x24", fields: [ { name: "location", type: "position" }, { name: "byte1", type: "ubyte" }, { name: "byte2", type: "ubyte" }, { name: "blockId", type: "varint" } ]}, - block_break_animation: {id: 0x25, fields: [ + block_break_animation: {id: "0x25", fields: [ { name: "entityId", type: "varint" }, { name: "location", type: "position" }, { name: "destroyStage", type: "byte" } ]}, - map_chunk_bulk: {id: 0x26, fields: [ + map_chunk_bulk: {id: "0x26", fields: [ { name: "skyLightSent", type: "bool" }, { name: "chunkColumnCount", type: "count", typeArgs: { type: "varint", countFor: "meta" } }, { name: "meta", type: "array", typeArgs: { count: "chunkColumnCount", type: "container", typeArgs: { fields: [ @@ -314,7 +314,7 @@ module.exports= { ]}}}, { name: "data", type: "restBuffer" } ]}, - explosion: {id: 0x27, fields: [ + explosion: {id: "0x27", fields: [ { name: "x", type: "float" }, { name: "y", type: "float" }, { name: "z", type: "float" }, @@ -331,13 +331,13 @@ module.exports= { { name: "playerMotionY", type: "float" }, { name: "playerMotionZ", type: "float" } ]}, - world_event: {id: 0x28, fields: [ + world_event: {id: "0x28", fields: [ { name: "effectId", type: "int" }, { name: "location", type: "position" }, { name: "data", type: "int" }, { name: "global", type: "bool" } ]}, - named_sound_effect: {id: 0x29, fields: [ + named_sound_effect: {id: "0x29", fields: [ { name: "soundName", type: "string" }, { name: "x", type: "int" }, { name: "y", type: "int" }, @@ -345,7 +345,7 @@ module.exports= { { name: "volume", type: "float" }, { name: "pitch", type: "ubyte" } ]}, - world_particles: {id: 0x2a, fields: [ + world_particles: {id: "0x2a", fields: [ { name: "particleId", type: "int" }, { name: "longDistance", type: "bool"}, { name: "x", type: "float" }, @@ -360,55 +360,55 @@ module.exports= { typeArgs: { count: {"field":"particleId","map":{"36":2,"37":1,"38":1},"default":0}, type: "varint" } } ]}, - game_state_change: {id: 0x2b, fields: [ + game_state_change: {id: "0x2b", fields: [ { name: "reason", type: "ubyte" }, { name: "gameMode", type: "float" } ]}, - spawn_entity_weather:{id: 0x2c, fields: [ + spawn_entity_weather:{id: "0x2c", fields: [ { name: "entityId", type: "varint" }, { name: "type", type: "byte" }, { name: "x", type: "int" }, { name: "y", type: "int" }, { name: "z", type: "int" } ]}, - open_window: {id: 0x2d, fields: [ + open_window: {id: "0x2d", fields: [ { name: "windowId", type: "ubyte" }, { name: "inventoryType", type: "string" }, { name: "windowTitle", type: "string" }, { name: "slotCount", type: "ubyte" }, { name: "entityId", type: "int", condition: {"field":"inventoryType","values":[11]}} ]}, - close_window: {id: 0x2e, fields: [ + close_window: {id: "0x2e", fields: [ { name: "windowId", type: "ubyte" } ]}, - set_slot: {id: 0x2f, fields: [ + set_slot: {id: "0x2f", fields: [ { name: "windowId", type: "byte" }, { name: "slot", type: "short" }, { name: "item", type: "slot" } ]}, - window_items: {id: 0x30, fields: [ + window_items: {id: "0x30", fields: [ { name: "windowId", type: "ubyte" }, { name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } }, { name: "items", type: "array", typeArgs: { type: "slot", count: "count" } } ]}, - craft_progress_bar: {id: 0x31, fields: [ + craft_progress_bar: {id: "0x31", fields: [ { name: "windowId", type: "ubyte" }, { name: "property", type: "short" }, { name: "value", type: "short" } ]}, - transaction:{id: 0x32, fields: [ + transaction:{id: "0x32", fields: [ { name: "windowId", type: "byte" }, { name: "action", type: "short" }, { name: "accepted", type: "bool" } ]}, - update_sign: {id: 0x33, fields: [ + update_sign: {id: "0x33", fields: [ { name: "location", type: "position" }, { name: "text1", type: "string" }, { name: "text2", type: "string" }, { name: "text3", type: "string" }, { name: "text4", type: "string" } ]}, - map: {id: 0x34, fields: [ + map: {id: "0x34", fields: [ { name: "itemDamage", type: "varint" }, { name: "scale", type: "byte" }, { name: "iconLength", type: "count", typeArgs: { type: "varint", countFor: "icons" } }, @@ -424,15 +424,15 @@ module.exports= { { name: "dataLength", type: "count", typeArgs: { countFor: "data", type: "varint" }, condition: {"field":"columns","values":[0],"different":true}}, { name: "data", type: "buffer", typeArgs: { count: "dataLength" }, condition: {"field":"columns","values":[0],"different":true}}, ]}, - tile_entity_data:{id: 0x35, fields: [ + tile_entity_data:{id: "0x35", fields: [ { name: "location", type: "position" }, { name: "action", type: "ubyte" }, { name: "nbtData", type: "restBuffer" } ]}, - open_sign_entity: {id: 0x36, fields: [ + open_sign_entity: {id: "0x36", fields: [ { name: "location", type: "position" }, ]}, - statistics: {id: 0x37, fields: [ + statistics: {id: "0x37", fields: [ { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entries" } }, { name: "entries", type: "array", typeArgs: { count: "count", type: "container", typeArgs: { fields: [ @@ -441,7 +441,7 @@ module.exports= { ]} }} ]}, - player_info: {id: 0x38, fields: [ + 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: [ @@ -462,32 +462,32 @@ module.exports= { { name: "displayName", type: "string", condition: {"field":"hasDisplayName","values":[true]}} ]}}} ]}, - abilities: {id: 0x39, fields: [ + abilities: {id: "0x39", fields: [ { name: "flags", type: "byte" }, { name: "flyingSpeed", type: "float" }, { name: "walkingSpeed", type: "float" } ]}, - tab_complete: {id: 0x3a, fields: [ + tab_complete: {id: "0x3a", fields: [ { name: "count", type: "count", typeArgs: { type: "varint", countFor: "matches" } }, { name: "matches", type: "array", typeArgs: { type: "string", count: "count" } } ]}, - scoreboard_objective: {id: 0x3b, fields: [ + scoreboard_objective: {id: "0x3b", fields: [ { name: "name", type: "string" }, { name: "action", type: "byte" }, { name: "displayText", type: "string", condition: {"field":"action","values":[0,2]}}, { name: "type", type: "string", condition: {"field":"action","values":[0,2]}} ]}, - scoreboard_score: {id: 0x3c, fields: [ + scoreboard_score: {id: "0x3c", fields: [ { name: "itemName", type: "string" }, { name: "action", type: "byte" }, { name: "scoreName", type: "string" }, { name: "value", type: "varint", condition: {"field":"action","values":[1],"different":true}} ]}, - scoreboard_display_objective: {id: 0x3d, fields: [ + scoreboard_display_objective: {id: "0x3d", fields: [ { name: "position", type: "byte" }, { name: "name", type: "string" } ]}, - scoreboard_team: {id: 0x3e, fields: [ + scoreboard_team: {id: "0x3e", fields: [ { name: "team", type: "string" }, { name: "mode", type: "byte" }, { name: "name", type: "string", condition: {"field":"mode","values":[0,2]}}, @@ -499,27 +499,27 @@ module.exports= { { name: "playerCount", type: "count", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "short", countFor: "players" } }, { name: "players", type: "array", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "string", count: "playerCount" } } ]}, - custom_payload: {id: 0x3f, fields: [ + custom_payload: {id: "0x3f", fields: [ { name: "channel", type: "string" }, { name: "data", type: "restBuffer" } ]}, - kick_disconnect: {id: 0x40, fields: [ + kick_disconnect: {id: "0x40", fields: [ { name: "reason", type: "string" } ]}, - difficulty: { id: 0x41, fields: [ + difficulty: { id: "0x41", fields: [ { name: "difficulty", type: "ubyte" } ]}, - combat_event: { id: 0x42, fields: [ + combat_event: { id: "0x42", fields: [ { name: "event", type: "varint"}, { name: "duration", type: "varint", condition: {"field":"event","values":[1]}}, { name: "playerId", type: "varint", condition: {"field":"event","values":[2]}}, { name: "entityId", type: "int", condition: {"field":"event","values":[1,2]}}, { name: "message", type: "string", condition: {"field":"event","values":[2]}} ]}, - camera: { id: 0x43, fields: [ + camera: { id: "0x43", fields: [ { name: "cameraId", type: "varint" } ]}, - world_border: { id: 0x44, fields: [ + world_border: { id: "0x44", fields: [ { name: "action", type: "varint"}, { name: "radius", type: "double", condition: {"field":"action","values":[0]}}, { name: "x", type: "double", condition: {"field":"action","values":[2,3]}}, @@ -531,58 +531,58 @@ module.exports= { { name: "warning_time", type: "varint", condition: {"field":"action","values":[4,3]}}, { name: "warning_blocks", type: "varint", condition: {"field":"action","values":[5,3]}} ]}, - title: { id: 0x45, fields: [ + title: { id: "0x45", fields: [ { name: "action", type: "varint"}, { name: "text", type: "string", condition: {"field":"action","values":[0,1]}}, { name: "fadeIn", type: "int", condition: {"field":"action","values":[2]}}, { name: "stay", type: "int", condition: {"field":"action","values":[2]}}, { name: "fadeOut", type: "int", condition: {"field":"action","values":[2]}} ]}, - set_compression: { id: 0x46, fields: [ + set_compression: { id: "0x46", fields: [ { name: "threshold", type: "varint"} ]}, - playerlist_header: { id: 0x47, fields: [ + playerlist_header: { id: "0x47", fields: [ { name: "header", type: "string" }, { name: "footer", type: "string" } ]}, - resource_pack_send: { id: 0x48, fields: [ + resource_pack_send: { id: "0x48", fields: [ { name: "url", type: "string" }, { name: "hash", type: "string" } ]}, - update_entity_nbt: { id: 0x49, fields: [ + update_entity_nbt: { id: "0x49", fields: [ { name: "entityId", type: "varint" }, { name: "tag", type: "restBuffer"} ]} }, toServer: { - keep_alive: {id: 0x00, fields: [ + keep_alive: {id: "0x00", fields: [ { name: "keepAliveId", type: "varint" } ]}, - chat: {id: 0x01, fields: [ + chat: {id: "0x01", fields: [ { name: "message", type: "string" } ]}, - use_entity: {id: 0x02, fields: [ + use_entity: {id: "0x02", fields: [ { name: "target", type: "varint" }, { name: "mouse", type: "varint" }, { name: "x", type: "float", condition: {"field":"mouse","values":[2]}}, { name: "y", type: "float", condition: {"field":"mouse","values":[2]}}, { name: "z", type: "float", condition: {"field":"mouse","values":[2]}}, ]}, - flying: {id: 0x03, fields: [ + flying: {id: "0x03", fields: [ { name: "onGround", type: "bool" } ]}, - position: {id: 0x04, fields: [ + position: {id: "0x04", fields: [ { name: "x", type: "double" }, { name: "y", type: "double" }, { name: "z", type: "double" }, { name: "onGround", type: "bool" } ]}, - look: {id: 0x05, fields: [ + look: {id: "0x05", fields: [ { name: "yaw", type: "float" }, { name: "pitch", type: "float" }, { name: "onGround", type: "bool" } ]}, - position_look: {id: 0x06, fields: [ + position_look: {id: "0x06", fields: [ { name: "x", type: "double" }, { name: "y", type: "double" }, { name: "z", type: "double" }, @@ -590,12 +590,12 @@ module.exports= { { name: "pitch", type: "float" }, { name: "onGround", type: "bool" } ]}, - block_dig: {id: 0x07, fields: [ + block_dig: {id: "0x07", fields: [ { name: "status", type: "byte" }, { name: "location", type: "position"}, { name: "face", type: "byte" } ]}, - block_place: {id: 0x08, fields: [ + block_place: {id: "0x08", fields: [ { name: "location", type: "position" }, { name: "direction", type: "byte" }, { name: "heldItem", type: "slot" }, @@ -603,24 +603,24 @@ module.exports= { { name: "cursorY", type: "byte" }, { name: "cursorZ", type: "byte" } ]}, - held_item_slot: {id: 0x09, fields: [ + held_item_slot: {id: "0x09", fields: [ { name: "slotId", type: "short" } ]}, - arm_animation: {id: 0x0a, fields: []}, - entity_action: {id: 0x0b, fields: [ + arm_animation: {id: "0x0a", fields: []}, + entity_action: {id: "0x0b", fields: [ { name: "entityId", type: "varint" }, { name: "actionId", type: "varint" }, { name: "jumpBoost", type: "varint" } ]}, - steer_vehicle: {id: 0x0c, fields: [ + steer_vehicle: {id: "0x0c", fields: [ { name: "sideways", type: "float" }, { name: "forward", type: "float" }, { name: "jump", type: "ubyte" } ]}, - close_window: {id: 0x0d, fields: [ + close_window: {id: "0x0d", fields: [ { name: "windowId", type: "byte" } ]}, - window_click: {id: 0x0e, fields: [ + window_click: {id: "0x0e", fields: [ { name: "windowId", type: "byte" }, { name: "slot", type: "short" }, { name: "mouseButton", type: "byte" }, @@ -628,54 +628,54 @@ module.exports= { { name: "mode", type: "byte" }, { name: "item", type: "slot" } ]}, - transaction: {id: 0x0f, fields: [ + transaction: {id: "0x0f", fields: [ { name: "windowId", type: "byte" }, { name: "action", type: "short" }, { name: "accepted", type: "bool" } ]}, - set_creative_slot: {id: 0x10, fields: [ + set_creative_slot: {id: "0x10", fields: [ { name: "slot", type: "short" }, { name: "item", type: "slot" } ]}, - enchant_item: {id: 0x11, fields: [ + enchant_item: {id: "0x11", fields: [ { name: "windowId", type: "byte" }, { name: "enchantment", type: "byte" } ]}, - update_sign: {id: 0x12, fields: [ + update_sign: {id: "0x12", fields: [ { name: "location", type: "position" }, { name: "text1", type: "string" }, { name: "text2", type: "string" }, { name: "text3", type: "string" }, { name: "text4", type: "string" } ]}, - abilities: {id: 0x13, fields: [ + abilities: {id: "0x13", fields: [ { name: "flags", type: "byte" }, { name: "flyingSpeed", type: "float" }, { name: "walkingSpeed", type: "float" } ]}, - tab_complete: {id: 0x14, fields: [ + tab_complete: {id: "0x14", fields: [ { name: "text", type: "string" }, { name: "hasPosition", type: "bool" }, { name: "block", type: "position", condition: {"field":"hasPosition","values":[true]}} ]}, - settings: {id: 0x15, fields: [ + settings: {id: "0x15", fields: [ { name: "locale", type: "string" }, { name: "viewDistance", type: "byte" }, { name: "chatFlags", type: "byte" }, { name: "chatColors", type: "bool" }, { name: "skinParts", type: "ubyte" } ]}, - client_command: {id: 0x16, fields: [ + client_command: {id: "0x16", fields: [ { name: "payload", type: "varint" } ]}, - custom_payload: {id: 0x17, fields: [ + custom_payload: {id: "0x17", fields: [ { name: "channel", type: "string" }, { name: "data", type: "restBuffer"} ]}, - spectate: { id: 0x18, fields: [ + spectate: { id: "0x18", fields: [ { name: "target", type: "UUID"} ]}, - resource_pack_receive: { id: 0x19, fields: [ + resource_pack_receive: { id: "0x19", fields: [ { name: "hash", type: "string" }, { name: "result", type: "varint" } ]} From a1ec6138b76c520086d559c3272d6ff49a2799f2 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:52:57 +0200 Subject: [PATCH 07/12] convert protocol_def.js to protocol.js --- protocol/protocol.json | 2771 ++++++++++++++++++++++++++++++++++++++++ src/protocol.js | 2 +- src/protocol_def.js | 684 ---------- 3 files changed, 2772 insertions(+), 685 deletions(-) create mode 100644 protocol/protocol.json delete mode 100644 src/protocol_def.js diff --git a/protocol/protocol.json b/protocol/protocol.json new file mode 100644 index 0000000..15f7202 --- /dev/null +++ b/protocol/protocol.json @@ -0,0 +1,2771 @@ +{ + "handshaking": { + "toClient": {}, + "toServer": { + "set_protocol": { + "id": "0x00", + "fields": [ + { + "name": "protocolVersion", + "type": "varint" + }, + { + "name": "serverHost", + "type": "string" + }, + { + "name": "serverPort", + "type": "ushort" + }, + { + "name": "nextState", + "type": "varint" + } + ] + } + } + }, + "status": { + "toClient": { + "server_info": { + "id": "0x00", + "fields": [ + { + "name": "response", + "type": "ustring" + } + ] + }, + "ping": { + "id": "0x01", + "fields": [ + { + "name": "time", + "type": "long" + } + ] + } + }, + "toServer": { + "ping_start": { + "id": "0x00", + "fields": [] + }, + "ping": { + "id": "0x01", + "fields": [ + { + "name": "time", + "type": "long" + } + ] + } + } + }, + "login": { + "toClient": { + "disconnect": { + "id": "0x00", + "fields": [ + { + "name": "reason", + "type": "string" + } + ] + }, + "encryption_begin": { + "id": "0x01", + "fields": [ + { + "name": "serverId", + "type": "string" + }, + { + "name": "publicKeyLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "publicKey" + } + }, + { + "name": "publicKey", + "type": "buffer", + "typeArgs": { + "count": "publicKeyLength" + } + }, + { + "name": "verifyTokenLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "verifyToken" + } + }, + { + "name": "verifyToken", + "type": "buffer", + "typeArgs": { + "count": "verifyTokenLength" + } + } + ] + }, + "success": { + "id": "0x02", + "fields": [ + { + "name": "uuid", + "type": "string" + }, + { + "name": "username", + "type": "string" + } + ] + }, + "compress": { + "id": "0x03", + "fields": [ + { + "name": "threshold", + "type": "varint" + } + ] + } + }, + "toServer": { + "login_start": { + "id": "0x00", + "fields": [ + { + "name": "username", + "type": "string" + } + ] + }, + "encryption_begin": { + "id": "0x01", + "fields": [ + { + "name": "sharedSecretLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "sharedSecret" + } + }, + { + "name": "sharedSecret", + "type": "buffer", + "typeArgs": { + "count": "sharedSecretLength" + } + }, + { + "name": "verifyTokenLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "verifyToken" + } + }, + { + "name": "verifyToken", + "type": "buffer", + "typeArgs": { + "count": "verifyTokenLength" + } + } + ] + } + } + }, + "play": { + "toClient": { + "keep_alive": { + "id": "0x00", + "fields": [ + { + "name": "keepAliveId", + "type": "varint" + } + ] + }, + "login": { + "id": "0x01", + "fields": [ + { + "name": "entityId", + "type": "int" + }, + { + "name": "gameMode", + "type": "ubyte" + }, + { + "name": "dimension", + "type": "byte" + }, + { + "name": "difficulty", + "type": "ubyte" + }, + { + "name": "maxPlayers", + "type": "ubyte" + }, + { + "name": "levelType", + "type": "string" + }, + { + "name": "reducedDebugInfo", + "type": "bool" + } + ] + }, + "chat": { + "id": "0x02", + "fields": [ + { + "name": "message", + "type": "ustring" + }, + { + "name": "position", + "type": "byte" + } + ] + }, + "update_time": { + "id": "0x03", + "fields": [ + { + "name": "age", + "type": "long" + }, + { + "name": "time", + "type": "long" + } + ] + }, + "entity_equipment": { + "id": "0x04", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "slot", + "type": "short" + }, + { + "name": "item", + "type": "slot" + } + ] + }, + "spawn_position": { + "id": "0x05", + "fields": [ + { + "name": "location", + "type": "position" + } + ] + }, + "update_health": { + "id": "0x06", + "fields": [ + { + "name": "health", + "type": "float" + }, + { + "name": "food", + "type": "varint" + }, + { + "name": "foodSaturation", + "type": "float" + } + ] + }, + "respawn": { + "id": "0x07", + "fields": [ + { + "name": "dimension", + "type": "int" + }, + { + "name": "difficulty", + "type": "ubyte" + }, + { + "name": "gamemode", + "type": "ubyte" + }, + { + "name": "levelType", + "type": "string" + } + ] + }, + "position": { + "id": "0x08", + "fields": [ + { + "name": "x", + "type": "double" + }, + { + "name": "y", + "type": "double" + }, + { + "name": "z", + "type": "double" + }, + { + "name": "yaw", + "type": "float" + }, + { + "name": "pitch", + "type": "float" + }, + { + "name": "flags", + "type": "byte" + } + ] + }, + "held_item_slot": { + "id": "0x09", + "fields": [ + { + "name": "slot", + "type": "byte" + } + ] + }, + "bed": { + "id": "0x0a", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "location", + "type": "position" + } + ] + }, + "animation": { + "id": "0x0b", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "animation", + "type": "ubyte" + } + ] + }, + "named_entity_spawn": { + "id": "0x0c", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "playerUUID", + "type": "UUID" + }, + { + "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" + } + ] + }, + "collect": { + "id": "0x0d", + "fields": [ + { + "name": "collectedEntityId", + "type": "varint" + }, + { + "name": "collectorEntityId", + "type": "varint" + } + ] + }, + "spawn_entity": { + "id": "0x0e", + "fields": [ + { + "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" + }, + { + "name": "objectData", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "intField", + "type": "int" + }, + { + "name": "velocityX", + "type": "short", + "condition": { + "field": "intField", + "values": [ + 0 + ], + "different": true, + "this": true + } + }, + { + "name": "velocityY", + "type": "short", + "condition": { + "field": "intField", + "values": [ + 0 + ], + "different": true, + "this": true + } + }, + { + "name": "velocityZ", + "type": "short", + "condition": { + "field": "intField", + "values": [ + 0 + ], + "different": true, + "this": true + } + } + ] + } + } + ] + }, + "spawn_entity_living": { + "id": "0x0f", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "type", + "type": "ubyte" + }, + { + "name": "x", + "type": "int" + }, + { + "name": "y", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "yaw", + "type": "byte" + }, + { + "name": "pitch", + "type": "byte" + }, + { + "name": "headPitch", + "type": "byte" + }, + { + "name": "velocityX", + "type": "short" + }, + { + "name": "velocityY", + "type": "short" + }, + { + "name": "velocityZ", + "type": "short" + }, + { + "name": "metadata", + "type": "entityMetadata" + } + ] + }, + "spawn_entity_painting": { + "id": "0x10", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "title", + "type": "string" + }, + { + "name": "location", + "type": "position" + }, + { + "name": "direction", + "type": "ubyte" + } + ] + }, + "spawn_entity_experience_orb": { + "id": "0x11", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "x", + "type": "int" + }, + { + "name": "y", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "count", + "type": "short" + } + ] + }, + "entity_velocity": { + "id": "0x12", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "velocityX", + "type": "short" + }, + { + "name": "velocityY", + "type": "short" + }, + { + "name": "velocityZ", + "type": "short" + } + ] + }, + "entity_destroy": { + "id": "0x13", + "fields": [ + { + "name": "count", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "entityIds" + } + }, + { + "name": "entityIds", + "type": "array", + "typeArgs": { + "type": "varint", + "count": "count" + } + } + ] + }, + "entity": { + "id": "0x14", + "fields": [ + { + "name": "entityId", + "type": "varint" + } + ] + }, + "rel_entity_move": { + "id": "0x15", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "dX", + "type": "byte" + }, + { + "name": "dY", + "type": "byte" + }, + { + "name": "dZ", + "type": "byte" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "entity_look": { + "id": "0x16", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "yaw", + "type": "byte" + }, + { + "name": "pitch", + "type": "byte" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "entity_move_look": { + "id": "0x17", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "dX", + "type": "byte" + }, + { + "name": "dY", + "type": "byte" + }, + { + "name": "dZ", + "type": "byte" + }, + { + "name": "yaw", + "type": "byte" + }, + { + "name": "pitch", + "type": "byte" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "entity_teleport": { + "id": "0x18", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "x", + "type": "int" + }, + { + "name": "y", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "yaw", + "type": "byte" + }, + { + "name": "pitch", + "type": "byte" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "entity_head_rotation": { + "id": "0x19", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "headYaw", + "type": "byte" + } + ] + }, + "entity_status": { + "id": "0x1a", + "fields": [ + { + "name": "entityId", + "type": "int" + }, + { + "name": "entityStatus", + "type": "byte" + } + ] + }, + "attach_entity": { + "id": "0x1b", + "fields": [ + { + "name": "entityId", + "type": "int" + }, + { + "name": "vehicleId", + "type": "int" + }, + { + "name": "leash", + "type": "bool" + } + ] + }, + "entity_metadata": { + "id": "0x1c", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "metadata", + "type": "entityMetadata" + } + ] + }, + "entity_effect": { + "id": "0x1d", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "effectId", + "type": "byte" + }, + { + "name": "amplifier", + "type": "byte" + }, + { + "name": "duration", + "type": "varint" + }, + { + "name": "hideParticles", + "type": "bool" + } + ] + }, + "remove_entity_effect": { + "id": "0x1e", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "effectId", + "type": "byte" + } + ] + }, + "experience": { + "id": "0x1f", + "fields": [ + { + "name": "experienceBar", + "type": "float" + }, + { + "name": "level", + "type": "varint" + }, + { + "name": "totalExperience", + "type": "varint" + } + ] + }, + "update_attributes": { + "id": "0x20", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "count", + "type": "count", + "typeArgs": { + "type": "int", + "countFor": "properties" + } + }, + { + "name": "properties", + "type": "array", + "typeArgs": { + "count": "count", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "key", + "type": "string" + }, + { + "name": "value", + "type": "double" + }, + { + "name": "listLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "this.modifiers" + } + }, + { + "name": "modifiers", + "type": "array", + "typeArgs": { + "count": "this.listLength", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "UUID", + "type": "UUID" + }, + { + "name": "amount", + "type": "double" + }, + { + "name": "operation", + "type": "byte" + } + ] + } + } + } + ] + } + } + } + ] + }, + "map_chunk": { + "id": "0x21", + "fields": [ + { + "name": "x", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "groundUp", + "type": "bool" + }, + { + "name": "bitMap", + "type": "ushort" + }, + { + "name": "chunkDataLength", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "chunkData" + } + }, + { + "name": "chunkData", + "type": "buffer", + "typeArgs": { + "count": "chunkDataLength" + } + } + ] + }, + "multi_block_change": { + "id": "0x22", + "fields": [ + { + "name": "chunkX", + "type": "int" + }, + { + "name": "chunkZ", + "type": "int" + }, + { + "name": "recordCount", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "records" + } + }, + { + "name": "records", + "type": "array", + "typeArgs": { + "count": "recordCount", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "horizontalPos", + "type": "ubyte" + }, + { + "name": "y", + "type": "ubyte" + }, + { + "name": "blockId", + "type": "varint" + } + ] + } + } + } + ] + }, + "block_change": { + "id": "0x23", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "type", + "type": "varint" + } + ] + }, + "block_action": { + "id": "0x24", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "byte1", + "type": "ubyte" + }, + { + "name": "byte2", + "type": "ubyte" + }, + { + "name": "blockId", + "type": "varint" + } + ] + }, + "block_break_animation": { + "id": "0x25", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "location", + "type": "position" + }, + { + "name": "destroyStage", + "type": "byte" + } + ] + }, + "map_chunk_bulk": { + "id": "0x26", + "fields": [ + { + "name": "skyLightSent", + "type": "bool" + }, + { + "name": "chunkColumnCount", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "meta" + } + }, + { + "name": "meta", + "type": "array", + "typeArgs": { + "count": "chunkColumnCount", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "x", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "bitMap", + "type": "ushort" + } + ] + } + } + }, + { + "name": "data", + "type": "restBuffer" + } + ] + }, + "explosion": { + "id": "0x27", + "fields": [ + { + "name": "x", + "type": "float" + }, + { + "name": "y", + "type": "float" + }, + { + "name": "z", + "type": "float" + }, + { + "name": "radius", + "type": "float" + }, + { + "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" + } + ] + } + } + }, + { + "name": "playerMotionX", + "type": "float" + }, + { + "name": "playerMotionY", + "type": "float" + }, + { + "name": "playerMotionZ", + "type": "float" + } + ] + }, + "world_event": { + "id": "0x28", + "fields": [ + { + "name": "effectId", + "type": "int" + }, + { + "name": "location", + "type": "position" + }, + { + "name": "data", + "type": "int" + }, + { + "name": "global", + "type": "bool" + } + ] + }, + "named_sound_effect": { + "id": "0x29", + "fields": [ + { + "name": "soundName", + "type": "string" + }, + { + "name": "x", + "type": "int" + }, + { + "name": "y", + "type": "int" + }, + { + "name": "z", + "type": "int" + }, + { + "name": "volume", + "type": "float" + }, + { + "name": "pitch", + "type": "ubyte" + } + ] + }, + "world_particles": { + "id": "0x2a", + "fields": [ + { + "name": "particleId", + "type": "int" + }, + { + "name": "longDistance", + "type": "bool" + }, + { + "name": "x", + "type": "float" + }, + { + "name": "y", + "type": "float" + }, + { + "name": "z", + "type": "float" + }, + { + "name": "offsetX", + "type": "float" + }, + { + "name": "offsetY", + "type": "float" + }, + { + "name": "offsetZ", + "type": "float" + }, + { + "name": "particleData", + "type": "float" + }, + { + "name": "particles", + "type": "int" + }, + { + "name": "data", + "type": "array", + "typeArgs": { + "count": { + "field": "particleId", + "map": { + "36": 2, + "37": 1, + "38": 1 + }, + "default": 0 + }, + "type": "varint" + } + } + ] + }, + "game_state_change": { + "id": "0x2b", + "fields": [ + { + "name": "reason", + "type": "ubyte" + }, + { + "name": "gameMode", + "type": "float" + } + ] + }, + "spawn_entity_weather": { + "id": "0x2c", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "type", + "type": "byte" + }, + { + "name": "x", + "type": "int" + }, + { + "name": "y", + "type": "int" + }, + { + "name": "z", + "type": "int" + } + ] + }, + "open_window": { + "id": "0x2d", + "fields": [ + { + "name": "windowId", + "type": "ubyte" + }, + { + "name": "inventoryType", + "type": "string" + }, + { + "name": "windowTitle", + "type": "string" + }, + { + "name": "slotCount", + "type": "ubyte" + }, + { + "name": "entityId", + "type": "int", + "condition": { + "field": "inventoryType", + "values": [ + 11 + ] + } + } + ] + }, + "close_window": { + "id": "0x2e", + "fields": [ + { + "name": "windowId", + "type": "ubyte" + } + ] + }, + "set_slot": { + "id": "0x2f", + "fields": [ + { + "name": "windowId", + "type": "byte" + }, + { + "name": "slot", + "type": "short" + }, + { + "name": "item", + "type": "slot" + } + ] + }, + "window_items": { + "id": "0x30", + "fields": [ + { + "name": "windowId", + "type": "ubyte" + }, + { + "name": "count", + "type": "count", + "typeArgs": { + "type": "short", + "countFor": "items" + } + }, + { + "name": "items", + "type": "array", + "typeArgs": { + "type": "slot", + "count": "count" + } + } + ] + }, + "craft_progress_bar": { + "id": "0x31", + "fields": [ + { + "name": "windowId", + "type": "ubyte" + }, + { + "name": "property", + "type": "short" + }, + { + "name": "value", + "type": "short" + } + ] + }, + "transaction": { + "id": "0x32", + "fields": [ + { + "name": "windowId", + "type": "byte" + }, + { + "name": "action", + "type": "short" + }, + { + "name": "accepted", + "type": "bool" + } + ] + }, + "update_sign": { + "id": "0x33", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "text1", + "type": "string" + }, + { + "name": "text2", + "type": "string" + }, + { + "name": "text3", + "type": "string" + }, + { + "name": "text4", + "type": "string" + } + ] + }, + "map": { + "id": "0x34", + "fields": [ + { + "name": "itemDamage", + "type": "varint" + }, + { + "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" + }, + { + "name": "x", + "type": "byte" + }, + { + "name": "y", + "type": "byte" + } + ] + } + } + }, + { + "name": "columns", + "type": "byte" + }, + { + "name": "rows", + "type": "byte", + "condition": { + "field": "columns", + "values": [ + 0 + ], + "different": true + } + }, + { + "name": "x", + "type": "byte", + "condition": { + "field": "columns", + "values": [ + 0 + ], + "different": true + } + }, + { + "name": "y", + "type": "byte", + "condition": { + "field": "columns", + "values": [ + 0 + ], + "different": true + } + }, + { + "name": "dataLength", + "type": "count", + "typeArgs": { + "countFor": "data", + "type": "varint" + }, + "condition": { + "field": "columns", + "values": [ + 0 + ], + "different": true + } + }, + { + "name": "data", + "type": "buffer", + "typeArgs": { + "count": "dataLength" + }, + "condition": { + "field": "columns", + "values": [ + 0 + ], + "different": true + } + } + ] + }, + "tile_entity_data": { + "id": "0x35", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "action", + "type": "ubyte" + }, + { + "name": "nbtData", + "type": "restBuffer" + } + ] + }, + "open_sign_entity": { + "id": "0x36", + "fields": [ + { + "name": "location", + "type": "position" + } + ] + }, + "statistics": { + "id": "0x37", + "fields": [ + { + "name": "count", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "entries" + } + }, + { + "name": "entries", + "type": "array", + "typeArgs": { + "count": "count", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "value", + "type": "varint" + } + ] + } + } + } + ] + }, + "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": [ + { + "name": "UUID", + "type": "UUID" + }, + { + "name": "name", + "type": "string", + "condition": { + "field": "action", + "values": [ + 0 + ] + } + }, + { + "name": "propertiesLength", + "type": "count", + "condition": { + "field": "action", + "values": [ + 0 + ] + }, + "typeArgs": { + "countFor": "this.properties", + "type": "varint" + } + }, + { + "name": "properties", + "type": "array", + "condition": { + "field": "action", + "values": [ + 0 + ] + }, + "typeArgs": { + "count": "this.propertiesLength", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "value", + "type": "ustring" + }, + { + "name": "isSigned", + "type": "bool" + }, + { + "name": "signature", + "type": "ustring", + "condition": { + "field": "isSigned", + "values": [ + true + ], + "this": true + } + } + ] + } + } + }, + { + "name": "gamemode", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 0, + 1 + ] + } + }, + { + "name": "ping", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "hasDisplayName", + "type": "bool", + "condition": { + "field": "action", + "values": [ + 0, + 3 + ] + } + }, + { + "name": "displayName", + "type": "string", + "condition": { + "field": "hasDisplayName", + "values": [ + true + ] + } + } + ] + } + } + } + ] + }, + "abilities": { + "id": "0x39", + "fields": [ + { + "name": "flags", + "type": "byte" + }, + { + "name": "flyingSpeed", + "type": "float" + }, + { + "name": "walkingSpeed", + "type": "float" + } + ] + }, + "tab_complete": { + "id": "0x3a", + "fields": [ + { + "name": "count", + "type": "count", + "typeArgs": { + "type": "varint", + "countFor": "matches" + } + }, + { + "name": "matches", + "type": "array", + "typeArgs": { + "type": "string", + "count": "count" + } + } + ] + }, + "scoreboard_objective": { + "id": "0x3b", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "action", + "type": "byte" + }, + { + "name": "displayText", + "type": "string", + "condition": { + "field": "action", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "type", + "type": "string", + "condition": { + "field": "action", + "values": [ + 0, + 2 + ] + } + } + ] + }, + "scoreboard_score": { + "id": "0x3c", + "fields": [ + { + "name": "itemName", + "type": "string" + }, + { + "name": "action", + "type": "byte" + }, + { + "name": "scoreName", + "type": "string" + }, + { + "name": "value", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 1 + ], + "different": true + } + } + ] + }, + "scoreboard_display_objective": { + "id": "0x3d", + "fields": [ + { + "name": "position", + "type": "byte" + }, + { + "name": "name", + "type": "string" + } + ] + }, + "scoreboard_team": { + "id": "0x3e", + "fields": [ + { + "name": "team", + "type": "string" + }, + { + "name": "mode", + "type": "byte" + }, + { + "name": "name", + "type": "string", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "prefix", + "type": "string", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "suffix", + "type": "string", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "friendlyFire", + "type": "byte", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "nameTagVisibility", + "type": "string", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "color", + "type": "byte", + "condition": { + "field": "mode", + "values": [ + 0, + 2 + ] + } + }, + { + "name": "playerCount", + "type": "count", + "condition": { + "field": "mode", + "values": [ + 0, + 3, + 4 + ] + }, + "typeArgs": { + "type": "short", + "countFor": "players" + } + }, + { + "name": "players", + "type": "array", + "condition": { + "field": "mode", + "values": [ + 0, + 3, + 4 + ] + }, + "typeArgs": { + "type": "string", + "count": "playerCount" + } + } + ] + }, + "custom_payload": { + "id": "0x3f", + "fields": [ + { + "name": "channel", + "type": "string" + }, + { + "name": "data", + "type": "restBuffer" + } + ] + }, + "kick_disconnect": { + "id": "0x40", + "fields": [ + { + "name": "reason", + "type": "string" + } + ] + }, + "difficulty": { + "id": "0x41", + "fields": [ + { + "name": "difficulty", + "type": "ubyte" + } + ] + }, + "combat_event": { + "id": "0x42", + "fields": [ + { + "name": "event", + "type": "varint" + }, + { + "name": "duration", + "type": "varint", + "condition": { + "field": "event", + "values": [ + 1 + ] + } + }, + { + "name": "playerId", + "type": "varint", + "condition": { + "field": "event", + "values": [ + 2 + ] + } + }, + { + "name": "entityId", + "type": "int", + "condition": { + "field": "event", + "values": [ + 1, + 2 + ] + } + }, + { + "name": "message", + "type": "string", + "condition": { + "field": "event", + "values": [ + 2 + ] + } + } + ] + }, + "camera": { + "id": "0x43", + "fields": [ + { + "name": "cameraId", + "type": "varint" + } + ] + }, + "world_border": { + "id": "0x44", + "fields": [ + { + "name": "action", + "type": "varint" + }, + { + "name": "radius", + "type": "double", + "condition": { + "field": "action", + "values": [ + 0 + ] + } + }, + { + "name": "x", + "type": "double", + "condition": { + "field": "action", + "values": [ + 2, + 3 + ] + } + }, + { + "name": "z", + "type": "double", + "condition": { + "field": "action", + "values": [ + 2, + 3 + ] + } + }, + { + "name": "old_radius", + "type": "double", + "condition": { + "field": "action", + "values": [ + 1, + 3 + ] + } + }, + { + "name": "new_radius", + "type": "double", + "condition": { + "field": "action", + "values": [ + 1, + 3 + ] + } + }, + { + "name": "speed", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 1, + 3 + ] + } + }, + { + "name": "portalBoundary", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 3 + ] + } + }, + { + "name": "warning_time", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 4, + 3 + ] + } + }, + { + "name": "warning_blocks", + "type": "varint", + "condition": { + "field": "action", + "values": [ + 5, + 3 + ] + } + } + ] + }, + "title": { + "id": "0x45", + "fields": [ + { + "name": "action", + "type": "varint" + }, + { + "name": "text", + "type": "string", + "condition": { + "field": "action", + "values": [ + 0, + 1 + ] + } + }, + { + "name": "fadeIn", + "type": "int", + "condition": { + "field": "action", + "values": [ + 2 + ] + } + }, + { + "name": "stay", + "type": "int", + "condition": { + "field": "action", + "values": [ + 2 + ] + } + }, + { + "name": "fadeOut", + "type": "int", + "condition": { + "field": "action", + "values": [ + 2 + ] + } + } + ] + }, + "set_compression": { + "id": "0x46", + "fields": [ + { + "name": "threshold", + "type": "varint" + } + ] + }, + "playerlist_header": { + "id": "0x47", + "fields": [ + { + "name": "header", + "type": "string" + }, + { + "name": "footer", + "type": "string" + } + ] + }, + "resource_pack_send": { + "id": "0x48", + "fields": [ + { + "name": "url", + "type": "string" + }, + { + "name": "hash", + "type": "string" + } + ] + }, + "update_entity_nbt": { + "id": "0x49", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "tag", + "type": "restBuffer" + } + ] + } + }, + "toServer": { + "keep_alive": { + "id": "0x00", + "fields": [ + { + "name": "keepAliveId", + "type": "varint" + } + ] + }, + "chat": { + "id": "0x01", + "fields": [ + { + "name": "message", + "type": "string" + } + ] + }, + "use_entity": { + "id": "0x02", + "fields": [ + { + "name": "target", + "type": "varint" + }, + { + "name": "mouse", + "type": "varint" + }, + { + "name": "x", + "type": "float", + "condition": { + "field": "mouse", + "values": [ + 2 + ] + } + }, + { + "name": "y", + "type": "float", + "condition": { + "field": "mouse", + "values": [ + 2 + ] + } + }, + { + "name": "z", + "type": "float", + "condition": { + "field": "mouse", + "values": [ + 2 + ] + } + } + ] + }, + "flying": { + "id": "0x03", + "fields": [ + { + "name": "onGround", + "type": "bool" + } + ] + }, + "position": { + "id": "0x04", + "fields": [ + { + "name": "x", + "type": "double" + }, + { + "name": "y", + "type": "double" + }, + { + "name": "z", + "type": "double" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "look": { + "id": "0x05", + "fields": [ + { + "name": "yaw", + "type": "float" + }, + { + "name": "pitch", + "type": "float" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "position_look": { + "id": "0x06", + "fields": [ + { + "name": "x", + "type": "double" + }, + { + "name": "y", + "type": "double" + }, + { + "name": "z", + "type": "double" + }, + { + "name": "yaw", + "type": "float" + }, + { + "name": "pitch", + "type": "float" + }, + { + "name": "onGround", + "type": "bool" + } + ] + }, + "block_dig": { + "id": "0x07", + "fields": [ + { + "name": "status", + "type": "byte" + }, + { + "name": "location", + "type": "position" + }, + { + "name": "face", + "type": "byte" + } + ] + }, + "block_place": { + "id": "0x08", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "direction", + "type": "byte" + }, + { + "name": "heldItem", + "type": "slot" + }, + { + "name": "cursorX", + "type": "byte" + }, + { + "name": "cursorY", + "type": "byte" + }, + { + "name": "cursorZ", + "type": "byte" + } + ] + }, + "held_item_slot": { + "id": "0x09", + "fields": [ + { + "name": "slotId", + "type": "short" + } + ] + }, + "arm_animation": { + "id": "0x0a", + "fields": [] + }, + "entity_action": { + "id": "0x0b", + "fields": [ + { + "name": "entityId", + "type": "varint" + }, + { + "name": "actionId", + "type": "varint" + }, + { + "name": "jumpBoost", + "type": "varint" + } + ] + }, + "steer_vehicle": { + "id": "0x0c", + "fields": [ + { + "name": "sideways", + "type": "float" + }, + { + "name": "forward", + "type": "float" + }, + { + "name": "jump", + "type": "ubyte" + } + ] + }, + "close_window": { + "id": "0x0d", + "fields": [ + { + "name": "windowId", + "type": "byte" + } + ] + }, + "window_click": { + "id": "0x0e", + "fields": [ + { + "name": "windowId", + "type": "byte" + }, + { + "name": "slot", + "type": "short" + }, + { + "name": "mouseButton", + "type": "byte" + }, + { + "name": "action", + "type": "short" + }, + { + "name": "mode", + "type": "byte" + }, + { + "name": "item", + "type": "slot" + } + ] + }, + "transaction": { + "id": "0x0f", + "fields": [ + { + "name": "windowId", + "type": "byte" + }, + { + "name": "action", + "type": "short" + }, + { + "name": "accepted", + "type": "bool" + } + ] + }, + "set_creative_slot": { + "id": "0x10", + "fields": [ + { + "name": "slot", + "type": "short" + }, + { + "name": "item", + "type": "slot" + } + ] + }, + "enchant_item": { + "id": "0x11", + "fields": [ + { + "name": "windowId", + "type": "byte" + }, + { + "name": "enchantment", + "type": "byte" + } + ] + }, + "update_sign": { + "id": "0x12", + "fields": [ + { + "name": "location", + "type": "position" + }, + { + "name": "text1", + "type": "string" + }, + { + "name": "text2", + "type": "string" + }, + { + "name": "text3", + "type": "string" + }, + { + "name": "text4", + "type": "string" + } + ] + }, + "abilities": { + "id": "0x13", + "fields": [ + { + "name": "flags", + "type": "byte" + }, + { + "name": "flyingSpeed", + "type": "float" + }, + { + "name": "walkingSpeed", + "type": "float" + } + ] + }, + "tab_complete": { + "id": "0x14", + "fields": [ + { + "name": "text", + "type": "string" + }, + { + "name": "hasPosition", + "type": "bool" + }, + { + "name": "block", + "type": "position", + "condition": { + "field": "hasPosition", + "values": [ + true + ] + } + } + ] + }, + "settings": { + "id": "0x15", + "fields": [ + { + "name": "locale", + "type": "string" + }, + { + "name": "viewDistance", + "type": "byte" + }, + { + "name": "chatFlags", + "type": "byte" + }, + { + "name": "chatColors", + "type": "bool" + }, + { + "name": "skinParts", + "type": "ubyte" + } + ] + }, + "client_command": { + "id": "0x16", + "fields": [ + { + "name": "payload", + "type": "varint" + } + ] + }, + "custom_payload": { + "id": "0x17", + "fields": [ + { + "name": "channel", + "type": "string" + }, + { + "name": "data", + "type": "restBuffer" + } + ] + }, + "spectate": { + "id": "0x18", + "fields": [ + { + "name": "target", + "type": "UUID" + } + ] + }, + "resource_pack_receive": { + "id": "0x19", + "fields": [ + { + "name": "hash", + "type": "string" + }, + { + "name": "result", + "type": "varint" + } + ] + } + } + } +} \ No newline at end of file diff --git a/src/protocol.js b/src/protocol.js index 3e68236..b0bdafd 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -14,7 +14,7 @@ var states = { "PLAY": "play" } -var packets=require("./protocol_def.js"); +var packets=require("../protocol/protocol"); var packetFields = {}; var packetNames = {}; diff --git a/src/protocol_def.js b/src/protocol_def.js deleted file mode 100644 index 91fd661..0000000 --- a/src/protocol_def.js +++ /dev/null @@ -1,684 +0,0 @@ -module.exports= { - handshaking: { - toClient: {}, - toServer: { - set_protocol: {id: "0x00", fields: [ - { name: "protocolVersion", type: "varint" }, - { name: "serverHost", type: "string" }, - { name: "serverPort", type: "ushort" }, - { name: "nextState", type: "varint" } - ]} - }, - }, - status: { - toClient: { - server_info: {id: "0x00", fields: [ - { name: "response", type: "ustring" } - ]}, - ping: {id: "0x01", fields: [ - { name: "time", type: "long" } - ]} - }, - toServer: { - ping_start: {id: "0x00", fields: []}, - ping: {id: "0x01", fields: [ - { name: "time", type: "long" } - ]} - } - }, - - login: { - toClient: { - disconnect: {id: "0x00", fields: [ - { name: "reason", type: "string" } - ]}, - encryption_begin: {id: "0x01", fields: [ - { name: "serverId", type: "string" }, - { name: "publicKeyLength", type: "count", typeArgs: { type: "varint", countFor: "publicKey" } }, - { name: "publicKey", type: "buffer", typeArgs: { count: "publicKeyLength" } }, - { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, - { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, - ]}, - success: {id: "0x02", fields: [ - { name: "uuid", type: "string" }, - { name: "username", type: "string" } - ]}, - compress: { id: "0x03", fields: [ - { name: "threshold", type: "varint"} - ]} - }, - toServer: { - login_start: {id: "0x00", fields: [ - { name: "username", type: "string" } - ]}, - encryption_begin: {id: "0x01", fields: [ - { name: "sharedSecretLength", type: "count", typeArgs: { type: "varint", countFor: "sharedSecret" } }, - { name: "sharedSecret", type: "buffer", typeArgs: { count: "sharedSecretLength" } }, - { name: "verifyTokenLength", type: "count", typeArgs: { type: "varint", countFor: "verifyToken" } }, - { name: "verifyToken", type: "buffer", typeArgs: { count: "verifyTokenLength" } }, - ]} - } - }, - - play: { - toClient: { - keep_alive: {id: "0x00", fields: [ - { name: "keepAliveId", type: "varint" }, - ]}, - login: {id: "0x01", fields: [ - { name: "entityId", type: "int" }, - { name: "gameMode", type: "ubyte" }, - { name: "dimension", type: "byte" }, - { name: "difficulty", type: "ubyte" }, - { name: "maxPlayers", type: "ubyte" }, - { name: "levelType", type: "string" }, - { name: "reducedDebugInfo", type: "bool"} - ]}, - chat: {id: "0x02", fields: [ - { name: "message", type: "ustring" }, - { name: "position", type: "byte" } - ]}, - update_time: {id: "0x03", fields: [ - { name: "age", type: "long" }, - { name: "time", type: "long" }, - ]}, - entity_equipment: {id: "0x04", fields: [ - { name: "entityId", type: "varint" }, - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - spawn_position: {id: "0x05", fields: [ - { name: "location", type: "position" } - ]}, - update_health: {id: "0x06", fields: [ - { name: "health", type: "float" }, - { name: "food", type: "varint" }, - { name: "foodSaturation", type: "float" } - ]}, - respawn: {id: "0x07", fields: [ - { name: "dimension", type: "int" }, - { name: "difficulty", type: "ubyte" }, - { name: "gamemode", type: "ubyte" }, - { name: "levelType", type: "string" } - ]}, - position: {id: "0x08", fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "flags", type: "byte"} - ]}, - held_item_slot: {id: "0x09", fields: [ - { name: "slot", type: "byte" } - ]}, - bed: {id: "0x0a", fields: [ - { name: "entityId", type: "varint" }, - { name: "location", type: "position" } - ]}, - animation: {id: "0x0b", fields: [ - { name: "entityId", type: "varint" }, - { name: "animation", type: "ubyte" } - ]}, - named_entity_spawn: {id: "0x0c", fields: [ - { name: "entityId", type: "varint" }, - { name: "playerUUID", type: "UUID"}, - { 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" } - ]}, - collect: {id: "0x0d", fields: [ - { name: "collectedEntityId", type: "varint" }, - { name: "collectorEntityId", type: "varint" } - ]}, - spawn_entity: {id: "0x0e", fields: [ - { 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" }, - { name: "objectData", type: "container", typeArgs: { fields: [ - { name: "intField", type: "int" }, - { name: "velocityX", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}}, - { name: "velocityY", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}}, - { name: "velocityZ", type: "short", condition: {"field":"intField","values":[0],"different":true,"this":true}} - ]}} - ]}, - spawn_entity_living: {id: "0x0f", fields: [ - { name: "entityId", type: "varint" }, - { name: "type", type: "ubyte" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "headPitch", type: "byte" }, - { name: "velocityX", type: "short" }, - { name: "velocityY", type: "short" }, - { name: "velocityZ", type: "short" }, - { name: "metadata", type: "entityMetadata" }, - ]}, - spawn_entity_painting: {id: "0x10", fields: [ - { name: "entityId", type: "varint" }, - { name: "title", type: "string" }, - { name: "location", type: "position" }, - { name: "direction", type: "ubyte" } - ]}, - spawn_entity_experience_orb: {id: "0x11", fields: [ - { name: "entityId", type: "varint" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "count", type: "short" } - ]}, - entity_velocity: {id: "0x12", fields: [ - { name: "entityId", type: "varint" }, - { name: "velocityX", type: "short" }, - { name: "velocityY", type: "short" }, - { name: "velocityZ", type: "short" } - ]}, - entity_destroy: {id: "0x13", fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entityIds" } }, - { name: "entityIds", type: "array", typeArgs: { type: "varint", count: "count" } } - ]}, - entity: {id: "0x14", fields: [ - { name: "entityId", type: "varint" } - ]}, - rel_entity_move: {id: "0x15", fields: [ - { name: "entityId", type: "varint" }, - { name: "dX", type: "byte" }, - { name: "dY", type: "byte" }, - { name: "dZ", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_look: {id: "0x16", fields: [ - { name: "entityId", type: "varint" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_move_look: {id: "0x17", fields: [ - { name: "entityId", type: "varint" }, - { name: "dX", type: "byte" }, - { name: "dY", type: "byte" }, - { name: "dZ", type: "byte" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_teleport: {id: "0x18", fields: [ - { name: "entityId", type: "varint" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "yaw", type: "byte" }, - { name: "pitch", type: "byte" }, - { name: "onGround", type: "bool"} - ]}, - entity_head_rotation: {id: "0x19", fields: [ - { name: "entityId", type: "varint" }, - { name: "headYaw", type: "byte" }, - ]}, - entity_status: {id: "0x1a", fields: [ - { name: "entityId", type: "int" }, - { name: "entityStatus", type: "byte" } - ]}, - attach_entity: {id: "0x1b", fields: [ - { name: "entityId", type: "int" }, - { name: "vehicleId", type: "int" }, - { name: "leash", type: "bool" } - ]}, - entity_metadata: {id: "0x1c", fields: [ - { name: "entityId", type: "varint" }, - { name: "metadata", type: "entityMetadata" } - ]}, - entity_effect: {id: "0x1d", fields: [ - { name: "entityId", type: "varint" }, - { name: "effectId", type: "byte" }, - { name: "amplifier", type: "byte" }, - { name: "duration", type: "varint" }, - { name: "hideParticles", type: "bool" } - ]}, - remove_entity_effect: {id: "0x1e", fields: [ - { name: "entityId", type: "varint" }, - { name: "effectId", type: "byte" } - ]}, - experience: {id: "0x1f", fields: [ - { name: "experienceBar", type: "float" }, - { name: "level", type: "varint" }, - { name: "totalExperience", type: "varint" } - ]}, - update_attributes: {id: "0x20", fields: [ - { name: "entityId", type: "varint" }, - { name: "count", type: "count", typeArgs: { type: "int", countFor: "properties" } }, - { name: "properties", type: "array", typeArgs: { count: "count", - type: "container", typeArgs: { fields: [ - { name: "key", type: "string" }, - { name: "value", type: "double" }, - { name: "listLength", type: "count", typeArgs: { type: "varint", countFor: "this.modifiers" } }, - { name: "modifiers", type: "array", typeArgs: { count: "this.listLength", - type: "container", typeArgs: { fields: [ - { name: "UUID", type: "UUID" }, - { name: "amount", type: "double" }, - { name: "operation", type: "byte" } - ]}}} - ]} - }} - ]}, - map_chunk: {id: "0x21", fields: [ - { name: "x", type: "int" }, - { name: "z", type: "int" }, - { name: "groundUp", type: "bool" }, - { name: "bitMap", type: "ushort" }, - { name: "chunkDataLength", type: "count", typeArgs: { type: "varint", countFor: "chunkData" } }, - { name: "chunkData", type: "buffer", typeArgs: { count: "chunkDataLength" } }, - ]}, - multi_block_change: {id: "0x22", fields: [ - { name: "chunkX", type: "int" }, - { name: "chunkZ", type: "int" }, - { name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } }, - { name: "records", type: "array", typeArgs: { count: "recordCount", type: "container", typeArgs: { fields: [ - { name: "horizontalPos", type: "ubyte" }, - { name: "y", type: "ubyte" }, - { name: "blockId", type: "varint" } - ]}}} - ]}, - block_change: {id: "0x23", fields: [ - { name: "location", type: "position" }, - { name: "type", type: "varint" }, - ]}, - block_action: {id: "0x24", fields: [ - { name: "location", type: "position" }, - { name: "byte1", type: "ubyte" }, - { name: "byte2", type: "ubyte" }, - { name: "blockId", type: "varint" } - ]}, - block_break_animation: {id: "0x25", fields: [ - { name: "entityId", type: "varint" }, - { name: "location", type: "position" }, - { name: "destroyStage", type: "byte" } - ]}, - map_chunk_bulk: {id: "0x26", fields: [ - { name: "skyLightSent", type: "bool" }, - { name: "chunkColumnCount", type: "count", typeArgs: { type: "varint", countFor: "meta" } }, - { name: "meta", type: "array", typeArgs: { count: "chunkColumnCount", type: "container", typeArgs: { fields: [ - { name: "x", type: "int" }, - { name: "z", type: "int" }, - { name: "bitMap", type: "ushort" }, - ]}}}, - { name: "data", type: "restBuffer" } - ]}, - explosion: {id: "0x27", fields: [ - { name: "x", type: "float" }, - { name: "y", type: "float" }, - { name: "z", type: "float" }, - { name: "radius", type: "float" }, - { 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" } - ] - }}}, - { name: "playerMotionX", type: "float" }, - { name: "playerMotionY", type: "float" }, - { name: "playerMotionZ", type: "float" } - ]}, - world_event: {id: "0x28", fields: [ - { name: "effectId", type: "int" }, - { name: "location", type: "position" }, - { name: "data", type: "int" }, - { name: "global", type: "bool" } - ]}, - named_sound_effect: {id: "0x29", fields: [ - { name: "soundName", type: "string" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" }, - { name: "volume", type: "float" }, - { name: "pitch", type: "ubyte" } - ]}, - world_particles: {id: "0x2a", fields: [ - { name: "particleId", type: "int" }, - { name: "longDistance", type: "bool"}, - { name: "x", type: "float" }, - { name: "y", type: "float" }, - { name: "z", type: "float" }, - { name: "offsetX", type: "float" }, - { name: "offsetY", type: "float" }, - { name: "offsetZ", type: "float" }, - { name: "particleData", type: "float" }, - { name: "particles", type: "int" }, - { name: "data", type: "array", - typeArgs: { count: {"field":"particleId","map":{"36":2,"37":1,"38":1},"default":0}, - type: "varint" } } - ]}, - game_state_change: {id: "0x2b", fields: [ - { name: "reason", type: "ubyte" }, - { name: "gameMode", type: "float" } - ]}, - spawn_entity_weather:{id: "0x2c", fields: [ - { name: "entityId", type: "varint" }, - { name: "type", type: "byte" }, - { name: "x", type: "int" }, - { name: "y", type: "int" }, - { name: "z", type: "int" } - ]}, - open_window: {id: "0x2d", fields: [ - { name: "windowId", type: "ubyte" }, - { name: "inventoryType", type: "string" }, - { name: "windowTitle", type: "string" }, - { name: "slotCount", type: "ubyte" }, - { name: "entityId", type: "int", condition: {"field":"inventoryType","values":[11]}} - ]}, - close_window: {id: "0x2e", fields: [ - { name: "windowId", type: "ubyte" } - ]}, - set_slot: {id: "0x2f", fields: [ - { name: "windowId", type: "byte" }, - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - window_items: {id: "0x30", fields: [ - { name: "windowId", type: "ubyte" }, - { name: "count", type: "count", typeArgs: { type: "short", countFor: "items" } }, - { name: "items", type: "array", typeArgs: { type: "slot", count: "count" } } - ]}, - craft_progress_bar: {id: "0x31", fields: [ - { name: "windowId", type: "ubyte" }, - { name: "property", type: "short" }, - { name: "value", type: "short" } - ]}, - transaction:{id: "0x32", fields: [ - { name: "windowId", type: "byte" }, - { name: "action", type: "short" }, - { name: "accepted", type: "bool" } - ]}, - update_sign: {id: "0x33", fields: [ - { name: "location", type: "position" }, - { name: "text1", type: "string" }, - { name: "text2", type: "string" }, - { name: "text3", type: "string" }, - { name: "text4", type: "string" } - ]}, - map: {id: "0x34", fields: [ - { name: "itemDamage", type: "varint" }, - { 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: {"field":"columns","values":[0],"different":true}}, - { name: "x", type: "byte", condition: {"field":"columns","values":[0],"different":true}}, - { name: "y", type: "byte", condition: {"field":"columns","values":[0],"different":true}}, - { name: "dataLength", type: "count", typeArgs: { countFor: "data", type: "varint" }, condition: {"field":"columns","values":[0],"different":true}}, - { name: "data", type: "buffer", typeArgs: { count: "dataLength" }, condition: {"field":"columns","values":[0],"different":true}}, - ]}, - tile_entity_data:{id: "0x35", fields: [ - { name: "location", type: "position" }, - { name: "action", type: "ubyte" }, - { name: "nbtData", type: "restBuffer" } - ]}, - open_sign_entity: {id: "0x36", fields: [ - { name: "location", type: "position" }, - ]}, - statistics: {id: "0x37", fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "entries" } }, - { name: "entries", type: "array", typeArgs: { count: "count", - type: "container", typeArgs: { fields: [ - { name: "name", type: "string" }, - { name: "value", type: "varint" } - ]} - }} - ]}, - 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: [ - { name: "UUID", type: "UUID" }, - { name: "name", type: "string", condition: {"field":"action","values":[0]}}, - { name: "propertiesLength", type: "count", condition: {"field":"action","values":[0]} - , typeArgs: { countFor: "this.properties", type: "varint" }}, - { name: "properties", type: "array", condition: {"field":"action","values":[0]} - , typeArgs: { count: "this.propertiesLength", type: "container", typeArgs: { fields: [ - { name: "name", type: "string" }, - { name: "value", type: "ustring" }, - { name: "isSigned", type: "bool" }, - { name: "signature", type: "ustring", condition: {"field":"isSigned","values":[true],"this":true}} - ]}}}, - { name: "gamemode", type: "varint", condition: {"field":"action","values":[0,1]}}, - { name: "ping", type: "varint", condition: {"field":"action","values":[0,2]}}, - { name: "hasDisplayName", type: "bool", condition: {"field":"action","values":[0,3]}}, - { name: "displayName", type: "string", condition: {"field":"hasDisplayName","values":[true]}} - ]}}} - ]}, - abilities: {id: "0x39", fields: [ - { name: "flags", type: "byte" }, - { name: "flyingSpeed", type: "float" }, - { name: "walkingSpeed", type: "float" } - ]}, - tab_complete: {id: "0x3a", fields: [ - { name: "count", type: "count", typeArgs: { type: "varint", countFor: "matches" } }, - { name: "matches", type: "array", typeArgs: { type: "string", count: "count" } } - ]}, - scoreboard_objective: {id: "0x3b", fields: [ - { name: "name", type: "string" }, - { name: "action", type: "byte" }, - { name: "displayText", type: "string", condition: {"field":"action","values":[0,2]}}, - { name: "type", type: "string", condition: {"field":"action","values":[0,2]}} - ]}, - scoreboard_score: {id: "0x3c", fields: [ - { name: "itemName", type: "string" }, - { name: "action", type: "byte" }, - { name: "scoreName", type: "string" }, - { name: "value", type: "varint", condition: {"field":"action","values":[1],"different":true}} - ]}, - scoreboard_display_objective: {id: "0x3d", fields: [ - { name: "position", type: "byte" }, - { name: "name", type: "string" } - ]}, - scoreboard_team: {id: "0x3e", fields: [ - { name: "team", type: "string" }, - { name: "mode", type: "byte" }, - { name: "name", type: "string", condition: {"field":"mode","values":[0,2]}}, - { name: "prefix", type: "string", condition: {"field":"mode","values":[0,2]}}, - { name: "suffix", type: "string", condition: {"field":"mode","values":[0,2]}}, - { name: "friendlyFire", type: "byte", condition: {"field":"mode","values":[0,2]}}, - { name: "nameTagVisibility", type: "string", condition: {"field":"mode","values":[0,2]}}, - { name: "color", type: "byte", condition: {"field":"mode","values":[0,2]}}, - { name: "playerCount", type: "count", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "short", countFor: "players" } }, - { name: "players", type: "array", condition: {"field":"mode","values":[0,3,4]}, typeArgs: { type: "string", count: "playerCount" } } - ]}, - custom_payload: {id: "0x3f", fields: [ - { name: "channel", type: "string" }, - { name: "data", type: "restBuffer" } - ]}, - kick_disconnect: {id: "0x40", fields: [ - { name: "reason", type: "string" } - ]}, - difficulty: { id: "0x41", fields: [ - { name: "difficulty", type: "ubyte" } - ]}, - combat_event: { id: "0x42", fields: [ - { name: "event", type: "varint"}, - { name: "duration", type: "varint", condition: {"field":"event","values":[1]}}, - { name: "playerId", type: "varint", condition: {"field":"event","values":[2]}}, - { name: "entityId", type: "int", condition: {"field":"event","values":[1,2]}}, - { name: "message", type: "string", condition: {"field":"event","values":[2]}} - ]}, - camera: { id: "0x43", fields: [ - { name: "cameraId", type: "varint" } - ]}, - world_border: { id: "0x44", fields: [ - { name: "action", type: "varint"}, - { name: "radius", type: "double", condition: {"field":"action","values":[0]}}, - { name: "x", type: "double", condition: {"field":"action","values":[2,3]}}, - { name: "z", type: "double", condition: {"field":"action","values":[2,3]}}, - { name: "old_radius", type: "double", condition: {"field":"action","values":[1,3]}}, - { name: "new_radius", type: "double", condition: {"field":"action","values":[1,3]}}, - { name: "speed", type: "varint", condition: {"field":"action","values":[1,3]}}, - { name: "portalBoundary", type: "varint", condition: {"field":"action","values":[3]}}, - { name: "warning_time", type: "varint", condition: {"field":"action","values":[4,3]}}, - { name: "warning_blocks", type: "varint", condition: {"field":"action","values":[5,3]}} - ]}, - title: { id: "0x45", fields: [ - { name: "action", type: "varint"}, - { name: "text", type: "string", condition: {"field":"action","values":[0,1]}}, - { name: "fadeIn", type: "int", condition: {"field":"action","values":[2]}}, - { name: "stay", type: "int", condition: {"field":"action","values":[2]}}, - { name: "fadeOut", type: "int", condition: {"field":"action","values":[2]}} - ]}, - set_compression: { id: "0x46", fields: [ - { name: "threshold", type: "varint"} - ]}, - playerlist_header: { id: "0x47", fields: [ - { name: "header", type: "string" }, - { name: "footer", type: "string" } - ]}, - resource_pack_send: { id: "0x48", fields: [ - { name: "url", type: "string" }, - { name: "hash", type: "string" } - ]}, - update_entity_nbt: { id: "0x49", fields: [ - { name: "entityId", type: "varint" }, - { name: "tag", type: "restBuffer"} - ]} - }, - toServer: { - keep_alive: {id: "0x00", fields: [ - { name: "keepAliveId", type: "varint" } - ]}, - chat: {id: "0x01", fields: [ - { name: "message", type: "string" } - ]}, - use_entity: {id: "0x02", fields: [ - { name: "target", type: "varint" }, - { name: "mouse", type: "varint" }, - { name: "x", type: "float", condition: {"field":"mouse","values":[2]}}, - { name: "y", type: "float", condition: {"field":"mouse","values":[2]}}, - { name: "z", type: "float", condition: {"field":"mouse","values":[2]}}, - ]}, - flying: {id: "0x03", fields: [ - { name: "onGround", type: "bool" } - ]}, - position: {id: "0x04", fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "onGround", type: "bool" } - ]}, - look: {id: "0x05", fields: [ - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "onGround", type: "bool" } - ]}, - position_look: {id: "0x06", fields: [ - { name: "x", type: "double" }, - { name: "y", type: "double" }, - { name: "z", type: "double" }, - { name: "yaw", type: "float" }, - { name: "pitch", type: "float" }, - { name: "onGround", type: "bool" } - ]}, - block_dig: {id: "0x07", fields: [ - { name: "status", type: "byte" }, - { name: "location", type: "position"}, - { name: "face", type: "byte" } - ]}, - block_place: {id: "0x08", fields: [ - { name: "location", type: "position" }, - { name: "direction", type: "byte" }, - { name: "heldItem", type: "slot" }, - { name: "cursorX", type: "byte" }, - { name: "cursorY", type: "byte" }, - { name: "cursorZ", type: "byte" } - ]}, - held_item_slot: {id: "0x09", fields: [ - { name: "slotId", type: "short" } - ]}, - arm_animation: {id: "0x0a", fields: []}, - entity_action: {id: "0x0b", fields: [ - { name: "entityId", type: "varint" }, - { name: "actionId", type: "varint" }, - { name: "jumpBoost", type: "varint" } - ]}, - steer_vehicle: {id: "0x0c", fields: [ - { name: "sideways", type: "float" }, - { name: "forward", type: "float" }, - { name: "jump", type: "ubyte" } - ]}, - close_window: {id: "0x0d", fields: [ - { name: "windowId", type: "byte" } - ]}, - window_click: {id: "0x0e", fields: [ - { name: "windowId", type: "byte" }, - { name: "slot", type: "short" }, - { name: "mouseButton", type: "byte" }, - { name: "action", type: "short" }, - { name: "mode", type: "byte" }, - { name: "item", type: "slot" } - ]}, - transaction: {id: "0x0f", fields: [ - { name: "windowId", type: "byte" }, - { name: "action", type: "short" }, - { name: "accepted", type: "bool" } - ]}, - set_creative_slot: {id: "0x10", fields: [ - { name: "slot", type: "short" }, - { name: "item", type: "slot" } - ]}, - enchant_item: {id: "0x11", fields: [ - { name: "windowId", type: "byte" }, - { name: "enchantment", type: "byte" } - ]}, - update_sign: {id: "0x12", fields: [ - { name: "location", type: "position" }, - { name: "text1", type: "string" }, - { name: "text2", type: "string" }, - { name: "text3", type: "string" }, - { name: "text4", type: "string" } - ]}, - abilities: {id: "0x13", fields: [ - { name: "flags", type: "byte" }, - { name: "flyingSpeed", type: "float" }, - { name: "walkingSpeed", type: "float" } - ]}, - tab_complete: {id: "0x14", fields: [ - { name: "text", type: "string" }, - { name: "hasPosition", type: "bool" }, - { name: "block", type: "position", condition: {"field":"hasPosition","values":[true]}} - ]}, - settings: {id: "0x15", fields: [ - { name: "locale", type: "string" }, - { name: "viewDistance", type: "byte" }, - { name: "chatFlags", type: "byte" }, - { name: "chatColors", type: "bool" }, - { name: "skinParts", type: "ubyte" } - ]}, - client_command: {id: "0x16", fields: [ - { name: "payload", type: "varint" } - ]}, - custom_payload: {id: "0x17", fields: [ - { name: "channel", type: "string" }, - { name: "data", type: "restBuffer"} - ]}, - spectate: { id: "0x18", fields: [ - { name: "target", type: "UUID"} - ]}, - resource_pack_receive: { id: "0x19", fields: [ - { name: "hash", type: "string" }, - { name: "result", type: "varint" } - ]} - } - } -}; From ffa11996c43b4ba209c64a783566e14a575ee343 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 04:05:46 +0200 Subject: [PATCH 08/12] remove function test in evalCondition : a condition is now never a function --- src/protocol.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index b0bdafd..c77065b 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -119,23 +119,14 @@ for (var n in entityMetadataTypes) { entityMetadataTypeBytes[entityMetadataTypes[n].type] = n; } -function isFunction(functionToCheck) { - var getType = {}; - return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; -} - function evalCondition(condition,field_values) { - if(!isFunction(condition)) - { - var field_value_to_test="this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field]; - var b=condition.values.some(function(value) {return field_value_to_test===value;}); - if("different" in condition && condition["different"]) - return !b; - else - return b; - } - return condition(field_values); + var field_value_to_test="this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field]; + var b=condition.values.some(function(value) {return field_value_to_test===value;}); + if("different" in condition && condition["different"]) + return !b; + else + return b; } function sizeOfEntityMetadata(value) { From 9c9f358a5d78eec9a60c6fb5eefcc73f02f02b38 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 9 May 2015 04:06:33 +0200 Subject: [PATCH 09/12] add current json schema (and a small validate.js test using jsonschema module) : makes it easier to fully understand the format --- package.json | 3 +- protocol/protocol_schema.json | 128 ++++++++++++++++++++++++++++++++++ test/validate.js | 16 +++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 protocol/protocol_schema.json create mode 100644 test/validate.js diff --git a/package.json b/package.json index 2919d14..e5108e9 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "mocha": "~1.8.2", "rimraf": "~2.1.1", "zfill": "0.0.1", - "batch": "~0.3.1" + "batch": "~0.3.1", + "jsonschema": "~1.0.1" }, "dependencies": { "buffer-equal": "0.0.0", diff --git a/protocol/protocol_schema.json b/protocol/protocol_schema.json new file mode 100644 index 0000000..7cd75fe --- /dev/null +++ b/protocol/protocol_schema.json @@ -0,0 +1,128 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "protocol", + + "definitions": { + "directions": { + "type": "object", + "properties":{ + "toClient":{"$ref" : "#/definitions/packets"}, + "toServer":{"$ref" : "#/definitions/packets"} + }, + "required":["toClient","toServer"], + "additionalProperties": false + }, + "packets": { + "type": "object", + "patternProperties": { + "^[a-z_]+$": {"$ref" : "#/definitions/packet"} + }, + "additionalProperties": false + }, + "packet": { + "type": "object", + "properties":{ + "id": {"$ref" : "#/definitions/id"}, + "fields": {"$ref" : "#/definitions/fields"} + }, + "required":["id","fields"], + "additionalProperties": false + }, + "id": { + "type": "string", + "pattern": "^0x[0-9a-f]{2}$" + }, + "fields" : { + "type": "array", + "items": {"$ref" : "#/definitions/field"}, + "additionalItems": false + }, + "field": { + "type": "object", + "properties": { + "name": {"$ref" : "#/definitions/fieldName"}, + "type": {"$ref" : "#/definitions/fieldType"}, + "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"}, + "condition": {"$ref" : "#/definitions/fieldCondition"} + }, + "required":["name","type"], + "additionalProperties": false + }, + "fieldName": { + "type":"string", + "pattern": "^[a-zA-Z0-9_]+$" + }, + "fieldType": { + "type":"string", + "pattern": "^[a-zA-Z]+$" + }, + "fieldTypeArgs": { + "type":"object", + "properties":{ + "type": {"$ref" : "#/definitions/fieldType"}, + "countFor": {"$ref" : "#/definitions/fieldTypeArgsCountFor"}, + "count": {"$ref" : "#/definitions/fieldTypeArgsCount"}, + "fields": {"$ref" : "#/definitions/fields"}, + "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"} + }, + "additionalProperties": false + }, + "fieldTypeArgsCountFor": { + "type":"string", + "pattern": "^(this\\.)?[a-zA-Z0-9_]+$" + }, + "fieldTypeArgsCount": { + "oneOf": [ + {"$ref" : "#/definitions/fieldTypeArgsCountFor"}, + { + "type":"object", + "properties":{ + "field":{"$ref" : "#/definitions/fieldName"}, + "map":{ + "type":"object", + "patternProperties":{ + "^[0-9]+$":{ + "type":"integer" + } + }, + "additionalProperties": false + }, + "default":{ + "type":"integer" + } + }, + "required":["field","map","default"], + "additionalProperties": false + } + ] + }, + "fieldCondition": { + "type": "object", + "properties":{ + "field": {"$ref" : "#/definitions/fieldName"}, + "values": { + "type": "array", + "items": { + "type": ["integer","boolean"] + }, + "additionalItems": false, + "minItems": 1 + }, + "different": { + "type": "boolean" + }, + "this": { + "type": "boolean" + } + }, + "required": ["field","values"], + "additionalProperties": false + } + }, + + "type": "object", + "patternProperties": { + "^[a-z]+$":{"$ref" : "#/definitions/directions"} + }, + "additionalProperties": false +} diff --git a/test/validate.js b/test/validate.js new file mode 100644 index 0000000..7447f9c --- /dev/null +++ b/test/validate.js @@ -0,0 +1,16 @@ +var assert = require('assert'); + +var Validator = require('jsonschema').Validator; +var v = new Validator(); + +Error.stackTraceLimit=0; + +describe("protocol schema", function() { + this.timeout(60 * 1000); + it("protocol.json is valid",function(){ + var instance = require('../protocol/protocol.json'); + var schema = require('../protocol/protocol_schema.json'); + var result = v.validate(instance, schema); + assert.strictEqual(result.errors.length,0,require('util').inspect(result.errors,{'depth':4})); + }); +}); From faed1766313d64dfc0248eb7fbd7b42ef55360b2 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 9 May 2015 05:58:12 +0200 Subject: [PATCH 10/12] add new condition type : * protocol.js : new read, write and sizeOf methods and add check for condition type inside read * update protocol.json * update protocol_schema.json * some fixes in test.js to handle the new condition type --- protocol/protocol.json | 343 +++++++++++++++++++--------------- protocol/protocol_schema.json | 17 +- src/protocol.js | 27 ++- test/test.js | 9 +- 4 files changed, 243 insertions(+), 153 deletions(-) diff --git a/protocol/protocol.json b/protocol/protocol.json index 15f7202..d4ddfc0 100644 --- a/protocol/protocol.json +++ b/protocol/protocol.json @@ -476,8 +476,9 @@ }, { "name": "velocityX", - "type": "short", - "condition": { + "type": "condition", + "typeArgs": { + "type": "short", "field": "intField", "values": [ 0 @@ -488,8 +489,9 @@ }, { "name": "velocityY", - "type": "short", - "condition": { + "type": "condition", + "typeArgs": { + "type": "short", "field": "intField", "values": [ 0 @@ -500,8 +502,9 @@ }, { "name": "velocityZ", - "type": "short", - "condition": { + "type": "condition", + "typeArgs": { + "type": "short", "field": "intField", "values": [ 0 @@ -1369,12 +1372,13 @@ }, { "name": "entityId", - "type": "int", - "condition": { + "type": "condition", + "typeArgs": { "field": "inventoryType", "values": [ 11 - ] + ], + "type": "int" } } ] @@ -1538,8 +1542,9 @@ }, { "name": "rows", - "type": "byte", - "condition": { + "type":"condition", + "typeArgs":{ + "type":"byte", "field": "columns", "values": [ 0 @@ -1549,8 +1554,9 @@ }, { "name": "x", - "type": "byte", - "condition": { + "type": "condition", + "typeArgs": { + "type": "byte", "field": "columns", "values": [ 0 @@ -1560,8 +1566,9 @@ }, { "name": "y", - "type": "byte", - "condition": { + "type": "condition", + "typeArgs": { + "type": "byte", "field": "columns", "values": [ 0 @@ -1571,12 +1578,13 @@ }, { "name": "dataLength", - "type": "count", + "type":"condition", "typeArgs": { - "countFor": "data", - "type": "varint" - }, - "condition": { + "type": "count", + "typeArgs": { + "countFor": "data", + "type": "varint" + }, "field": "columns", "values": [ 0 @@ -1586,11 +1594,12 @@ }, { "name": "data", - "type": "buffer", - "typeArgs": { - "count": "dataLength" - }, - "condition": { + "type": "condition", + "typeArgs":{ + "type": "buffer", + "typeArgs": { + "count": "dataLength" + }, "field": "columns", "values": [ 0 @@ -1688,8 +1697,9 @@ }, { "name": "name", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "action", "values": [ 0 @@ -1698,63 +1708,67 @@ }, { "name": "propertiesLength", - "type": "count", - "condition": { + "type": "condition", + "typeArgs": { + "type": "count", "field": "action", "values": [ 0 - ] - }, - "typeArgs": { - "countFor": "this.properties", - "type": "varint" + ], + "typeArgs": { + "countFor": "this.properties", + "type": "varint" + } } }, { "name": "properties", - "type": "array", - "condition": { + "type": "condition", + "typeArgs": { + "type": "array", "field": "action", "values": [ 0 - ] - }, - "typeArgs": { - "count": "this.propertiesLength", - "type": "container", + ], "typeArgs": { - "fields": [ - { - "name": "name", - "type": "string" - }, - { - "name": "value", - "type": "ustring" - }, - { - "name": "isSigned", - "type": "bool" - }, - { - "name": "signature", - "type": "ustring", - "condition": { - "field": "isSigned", - "values": [ - true - ], - "this": true + "count": "this.propertiesLength", + "type": "container", + "typeArgs": { + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "value", + "type": "ustring" + }, + { + "name": "isSigned", + "type": "bool" + }, + { + "name": "signature", + "type": "condition", + "typeArgs": { + "type": "ustring", + "field": "isSigned", + "values": [ + true + ], + "this": true + } } - } - ] + ] + } } } }, { "name": "gamemode", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 0, @@ -1764,8 +1778,9 @@ }, { "name": "ping", - "type": "varint", - "condition": { + "type":"condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 0, @@ -1775,8 +1790,9 @@ }, { "name": "hasDisplayName", - "type": "bool", - "condition": { + "type":"condition", + "typeArgs": { + "type": "bool", "field": "action", "values": [ 0, @@ -1786,8 +1802,9 @@ }, { "name": "displayName", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "hasDisplayName", "values": [ true @@ -1851,8 +1868,9 @@ }, { "name": "displayText", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "action", "values": [ 0, @@ -1862,8 +1880,9 @@ }, { "name": "type", - "type": "string", - "condition": { + "type":"condition", + "typeArgs":{ + "type": "string", "field": "action", "values": [ 0, @@ -1890,8 +1909,9 @@ }, { "name": "value", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs":{ + "type": "varint", "field": "action", "values": [ 1 @@ -1927,8 +1947,9 @@ }, { "name": "name", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "mode", "values": [ 0, @@ -1938,8 +1959,9 @@ }, { "name": "prefix", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "mode", "values": [ 0, @@ -1949,8 +1971,9 @@ }, { "name": "suffix", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "mode", "values": [ 0, @@ -1960,8 +1983,9 @@ }, { "name": "friendlyFire", - "type": "byte", - "condition": { + "type": "condition", + "typeArgs": { + "type": "byte", "field": "mode", "values": [ 0, @@ -1971,8 +1995,9 @@ }, { "name": "nameTagVisibility", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "mode", "values": [ 0, @@ -1982,8 +2007,9 @@ }, { "name": "color", - "type": "byte", - "condition": { + "type": "condition", + "typeArgs": { + "type": "byte", "field": "mode", "values": [ 0, @@ -1993,34 +2019,36 @@ }, { "name": "playerCount", + "type": "condition", + "typeArgs": { "type": "count", - "condition": { "field": "mode", "values": [ 0, 3, 4 - ] - }, - "typeArgs": { - "type": "short", - "countFor": "players" + ], + "typeArgs": { + "type": "short", + "countFor": "players" + } } }, { "name": "players", - "type": "array", - "condition": { + "type": "condition", + "typeArgs": { + "type": "array", "field": "mode", "values": [ 0, 3, 4 - ] - }, - "typeArgs": { - "type": "string", - "count": "playerCount" + ], + "typeArgs": { + "type": "string", + "count": "playerCount" + } } } ] @@ -2065,8 +2093,9 @@ }, { "name": "duration", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "event", "values": [ 1 @@ -2075,8 +2104,9 @@ }, { "name": "playerId", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "event", "values": [ 2 @@ -2085,8 +2115,9 @@ }, { "name": "entityId", - "type": "int", - "condition": { + "type": "condition", + "typeArgs": { + "type": "int", "field": "event", "values": [ 1, @@ -2096,8 +2127,9 @@ }, { "name": "message", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "event", "values": [ 2 @@ -2124,8 +2156,9 @@ }, { "name": "radius", - "type": "double", - "condition": { + "type": "condition", + "typeArgs": { + "type": "double", "field": "action", "values": [ 0 @@ -2134,8 +2167,9 @@ }, { "name": "x", - "type": "double", - "condition": { + "type": "condition", + "typeArgs": { + "type": "double", "field": "action", "values": [ 2, @@ -2145,8 +2179,9 @@ }, { "name": "z", - "type": "double", - "condition": { + "type": "condition", + "typeArgs": { + "type": "double", "field": "action", "values": [ 2, @@ -2156,8 +2191,9 @@ }, { "name": "old_radius", - "type": "double", - "condition": { + "type": "condition", + "typeArgs": { + "type": "double", "field": "action", "values": [ 1, @@ -2167,8 +2203,9 @@ }, { "name": "new_radius", - "type": "double", - "condition": { + "type": "condition", + "typeArgs": { + "type": "double", "field": "action", "values": [ 1, @@ -2178,8 +2215,9 @@ }, { "name": "speed", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 1, @@ -2189,8 +2227,9 @@ }, { "name": "portalBoundary", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 3 @@ -2199,8 +2238,9 @@ }, { "name": "warning_time", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 4, @@ -2210,8 +2250,9 @@ }, { "name": "warning_blocks", - "type": "varint", - "condition": { + "type": "condition", + "typeArgs": { + "type": "varint", "field": "action", "values": [ 5, @@ -2230,8 +2271,9 @@ }, { "name": "text", - "type": "string", - "condition": { + "type": "condition", + "typeArgs": { + "type": "string", "field": "action", "values": [ 0, @@ -2241,8 +2283,9 @@ }, { "name": "fadeIn", - "type": "int", - "condition": { + "type": "condition", + "typeArgs": { + "type": "int", "field": "action", "values": [ 2 @@ -2251,8 +2294,9 @@ }, { "name": "stay", - "type": "int", - "condition": { + "type": "condition", + "typeArgs": { + "type": "int", "field": "action", "values": [ 2 @@ -2261,8 +2305,9 @@ }, { "name": "fadeOut", - "type": "int", - "condition": { + "type": "condition", + "typeArgs": { + "type": "int", "field": "action", "values": [ 2 @@ -2352,8 +2397,9 @@ }, { "name": "x", - "type": "float", - "condition": { + "type": "condition", + "typeArgs": { + "type": "float", "field": "mouse", "values": [ 2 @@ -2362,8 +2408,9 @@ }, { "name": "y", - "type": "float", - "condition": { + "type": "condition", + "typeArgs": { + "type": "float", "field": "mouse", "values": [ 2 @@ -2372,8 +2419,9 @@ }, { "name": "z", - "type": "float", - "condition": { + "type": "condition", + "typeArgs": { + "type": "float", "field": "mouse", "values": [ 2 @@ -2687,8 +2735,9 @@ }, { "name": "block", - "type": "position", - "condition": { + "type": "condition", + "typeArgs": { + "type": "position", "field": "hasPosition", "values": [ true @@ -2768,4 +2817,4 @@ } } } -} \ No newline at end of file +} diff --git a/protocol/protocol_schema.json b/protocol/protocol_schema.json index 7cd75fe..ccbe31d 100644 --- a/protocol/protocol_schema.json +++ b/protocol/protocol_schema.json @@ -63,7 +63,22 @@ "countFor": {"$ref" : "#/definitions/fieldTypeArgsCountFor"}, "count": {"$ref" : "#/definitions/fieldTypeArgsCount"}, "fields": {"$ref" : "#/definitions/fields"}, - "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"} + "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"}, + "field": {"$ref" : "#/definitions/fieldName"}, + "values": { + "type": "array", + "items": { + "type": ["integer","boolean"] + }, + "additionalItems": false, + "minItems": 1 + }, + "different": { + "type": "boolean" + }, + "this": { + "type": "boolean" + } }, "additionalProperties": false }, diff --git a/src/protocol.js b/src/protocol.js index c77065b..6bc45c1 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -76,6 +76,7 @@ var types = { 'slot': [readSlot, writeSlot, sizeOfSlot], 'nbt': [readNbt, writeBuffer, sizeOfBuffer], 'entityMetadata': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata], + 'condition': [readCondition, writeCondition, sizeOfCondition] }; var debug; @@ -119,6 +120,28 @@ for (var n in entityMetadataTypes) { entityMetadataTypeBytes[entityMetadataTypes[n].type] = n; } +function readCondition(buffer,offset,typeArgs, rootNode) +{ + if(!evalCondition(typeArgs,rootNode)) + return null; + return read(buffer, offset, { type: typeArgs.type, typeArgs:typeArgs.typeArgs }, rootNode); +} + +function writeCondition(value, buffer, offset, typeArgs, rootNode) { + if(!evalCondition(typeArgs,rootNode)) + return offset; + + return write(value, buffer, offset, { type: typeArgs.type, typeArgs:typeArgs.typeArgs }, rootNode); +} + +function sizeOfCondition(value, fieldInfo, rootNode) { + if(!evalCondition(fieldInfo,rootNode)) + return 0; + + return sizeOf(value,fieldInfo, rootNode); +} + + function evalCondition(condition,field_values) { var field_value_to_test="this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field]; @@ -645,8 +668,6 @@ function writeCount(value, buffer, offset, typeArgs, 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); } @@ -661,7 +682,7 @@ function read(buffer, cursor, fieldInfo, rootNodes) { }; } var readResults = type[0](buffer, cursor, fieldInfo.typeArgs, rootNodes); - if (readResults == null) { + if (readResults == null && fieldInfo.type!=="condition") { throw new Error("Reader returned null : " + JSON.stringify(fieldInfo)); } if (readResults && readResults.error) return { error: readResults.error }; diff --git a/test/test.js b/test/test.js index 8e41961..3cd6ec5 100644 --- a/test/test.js +++ b/test/test.js @@ -120,7 +120,12 @@ var values = { }, 'UUID': [42, 42, 42, 42], 'position': { x: 12, y: 332, z: 4382821 }, - 'restBuffer': new Buffer(0) + 'restBuffer': new Buffer(0), + 'condition' : function(typeArgs) { + // check whether this should return undefined if needed instead ? (using evalCondition) + // probably not since we only send values that respect the condition + return values[typeArgs.type]; + } }; describe("packets", function() { @@ -173,7 +178,7 @@ describe("packets", function() { // empty object uses default values var packet = {}; packetInfo.forEach(function(field) { - if (!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) { + if ((!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) && (field.type!=="condition" || protocol.evalCondition(field.typeArgs,packet))) { var fieldVal = values[field.type]; if (typeof fieldVal === "undefined") { throw new Error("No value for type " + field.type); From 63b332ce382dc358e96d899030d97e1272430d12 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 9 May 2015 06:21:39 +0200 Subject: [PATCH 11/12] remove condition property checks, also remove that property from the schema --- protocol/protocol_schema.json | 25 +------------------------ src/protocol.js | 14 +++----------- test/test.js | 2 +- 3 files changed, 5 insertions(+), 36 deletions(-) diff --git a/protocol/protocol_schema.json b/protocol/protocol_schema.json index ccbe31d..c2e8644 100644 --- a/protocol/protocol_schema.json +++ b/protocol/protocol_schema.json @@ -42,8 +42,7 @@ "properties": { "name": {"$ref" : "#/definitions/fieldName"}, "type": {"$ref" : "#/definitions/fieldType"}, - "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"}, - "condition": {"$ref" : "#/definitions/fieldCondition"} + "typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"} }, "required":["name","type"], "additionalProperties": false @@ -110,28 +109,6 @@ "additionalProperties": false } ] - }, - "fieldCondition": { - "type": "object", - "properties":{ - "field": {"$ref" : "#/definitions/fieldName"}, - "values": { - "type": "array", - "items": { - "type": ["integer","boolean"] - }, - "additionalItems": false, - "minItems": 1 - }, - "different": { - "type": "boolean" - }, - "this": { - "type": "boolean" - } - }, - "required": ["field","values"], - "additionalProperties": false } }, diff --git a/src/protocol.js b/src/protocol.js index 6bc45c1..00a4b6f 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -559,7 +559,8 @@ function writeContainer(value, buffer, offset, typeArgs, rootNode) { var context = value.this ? value.this : value; rootNode.this = value; for (var index in typeArgs.fields) { - if (!context.hasOwnProperty(typeArgs.fields[index].name) && typeArgs.fields[index].type != "count" && !typeArgs.fields[index].condition) + if (!context.hasOwnProperty(typeArgs.fields[index].name) && typeArgs.fields[index].type != "count" && + (typeArgs.fields[index].type !="condition" || evalCondition(typeArgs.fields[index].typeArgs,rootNode))) { debug(new Error("Missing Property " + typeArgs.fields[index].name).stack); console.log(context); @@ -672,9 +673,6 @@ function sizeOfCount(value, typeArgs, rootNode) { } function read(buffer, cursor, fieldInfo, rootNodes) { - if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNodes)) { - return null; - } var type = types[fieldInfo.type]; if (!type) { return { @@ -690,9 +688,6 @@ function read(buffer, cursor, fieldInfo, rootNodes) { } function write(value, buffer, offset, fieldInfo, rootNode) { - if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNode)) { - return offset; - } var type = types[fieldInfo.type]; if (!type) { return { @@ -703,9 +698,6 @@ function write(value, buffer, offset, fieldInfo, rootNode) { } function sizeOf(value, fieldInfo, rootNode) { - if (fieldInfo.condition && !evalCondition(fieldInfo.condition,rootNode)) { - return 0; - } var type = types[fieldInfo.type]; if (!type) { throw new Error("missing data type: " + fieldInfo.type); @@ -756,7 +748,7 @@ function createPacketBuffer(packetId, state, params, isServer) { packet.forEach(function(fieldInfo) { var value = params[fieldInfo.name]; // TODO : A better check is probably needed - if(typeof value === "undefined" && fieldInfo.type != "count" && !fieldInfo.condition) + if(typeof value === "undefined" && fieldInfo.type != "count" && (fieldInfo.type !="condition" || evalCondition(fieldInfo.typeArgs,params))) debug(new Error("Missing Property " + fieldInfo.name).stack); offset = write(value, buffer, offset, fieldInfo, params); }); diff --git a/test/test.js b/test/test.js index 3cd6ec5..b05a355 100644 --- a/test/test.js +++ b/test/test.js @@ -178,7 +178,7 @@ describe("packets", function() { // empty object uses default values var packet = {}; packetInfo.forEach(function(field) { - if ((!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) && (field.type!=="condition" || protocol.evalCondition(field.typeArgs,packet))) { + if (field.type!=="condition" || protocol.evalCondition(field.typeArgs,packet)) { var fieldVal = values[field.type]; if (typeof fieldVal === "undefined") { throw new Error("No value for type " + field.type); From 04a3f72130135f4fd3ee1d830cfb37f7d78b9e55 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 9 May 2015 16:08:54 +0200 Subject: [PATCH 12/12] readCondition now return value==null when the condition is not met instead of returning null (when read returns null it means there was an error), check if value is null in readContainer and parsePacketData and don't include it if so, improve "field x missing" error message in test.js a bit --- src/protocol.js | 8 ++++---- test/test.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index 00a4b6f..5444447 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -123,7 +123,7 @@ for (var n in entityMetadataTypes) { function readCondition(buffer,offset,typeArgs, rootNode) { if(!evalCondition(typeArgs,rootNode)) - return null; + return { value: null, size: 0 }; return read(buffer, offset, { type: typeArgs.type, typeArgs:typeArgs.typeArgs }, rootNode); } @@ -546,7 +546,7 @@ function readContainer(buffer, offset, typeArgs, rootNode) { rootNode.this = results.value; for (var index in typeArgs.fields) { var readResults = read(buffer, offset, typeArgs.fields[index], rootNode); - if (readResults == null) { continue; } + if (readResults == null || readResults.value==null) { continue; } results.size += readResults.size; offset += readResults.size; results.value[typeArgs.fields[index].name] = readResults.value; @@ -680,7 +680,7 @@ function read(buffer, cursor, fieldInfo, rootNodes) { }; } var readResults = type[0](buffer, cursor, fieldInfo.typeArgs, rootNodes); - if (readResults == null && fieldInfo.type!=="condition") { + if (readResults == null) { throw new Error("Reader returned null : " + JSON.stringify(fieldInfo)); } if (readResults && readResults.error) return { error: readResults.error }; @@ -829,7 +829,7 @@ function parsePacketData(buffer, state, isServer, packetsToParse) { results: results }; }*/ - if (readResults === null) continue; + if (readResults === null || readResults.value==null) continue; if (readResults.error) { return readResults; } diff --git a/test/test.js b/test/test.js index b05a355..d1456fa 100644 --- a/test/test.js +++ b/test/test.js @@ -213,10 +213,10 @@ describe("packets", function() { }); var field; for (field in p1) { - assert.ok(field in p2, "field " + field + " missing in p2"); + assert.ok(field in p2, "field " + field + " missing in p2, in p1 it has value "+JSON.stringify(p1[field])); } for (field in p2) { - assert.ok(field in p1, "field " + field + " missing in p1"); + assert.ok(field in p1, "field " + field + " missing in p1, in p2 it has value "+JSON.stringify(p2[field])); } } });