/// <summary> /// Start network encryption. Automatically called by Login() if the server requests encryption. /// </summary> /// <returns>True if encryption was successful</returns> private bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey) { System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey); byte[] secretKey = CryptoHandler.GenerateAESPrivateKey(); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated."); } if (serverIDhash != "-") { Console.WriteLine("Checking Session..."); if (!ProtocolHandler.SessionCheck(uuid, sessionID, CryptoHandler.getServerHash(serverIDhash, serverKey, secretKey))) { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, "Failed to check session."); return(false); } } //Encrypt the data byte[] key_enc = dataTypes.GetArray(RSAService.Encrypt(secretKey, false)); byte[] token_enc = dataTypes.GetArray(RSAService.Encrypt(token, false)); //Encryption Response packet SendPacket(0x01, dataTypes.ConcatBytes(key_enc, token_enc)); //Start client-side encryption socketWrapper.SwitchToEncrypted(secretKey); //Process the next packet int packetID = -1; List <byte> packetData = new List <byte>(); while (true) { ReadNextPacket(ref packetID, packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); } else if (packetID == 0x02) //Login successful { login_phase = false; if (!pForge.CompleteForgeHandshake()) { return(false); } StartUpdating(); return(true); } else { HandlePacket(packetID, packetData); } } }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; if (Handler.GetTerrainEnabled()) { ConsoleIO.WriteLineFormatted("§e[错误]§c世界交互 暂不支持此版本"); Handler.SetTerrainEnabled(false); } if (handler.GetInventoryEnabled()) { ConsoleIO.WriteLineFormatted("§e[错误]§c物品栏交互暂不支持此版本"); handler.SetInventoryEnabled(false); } if (handler.GetEntityHandlingEnabled()) { ConsoleIO.WriteLineFormatted("§e[错误]§c实体交互暂不支持此版本"); handler.SetEntityHandlingEnabled(false); } }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; if (Handler.GetTerrainEnabled()) { Translations.WriteLineFormatted("extra.terrainandmovement_disabled"); Handler.SetTerrainEnabled(false); } if (handler.GetInventoryEnabled()) { Translations.WriteLineFormatted("extra.inventory_disabled"); handler.SetInventoryEnabled(false); } if (handler.GetEntityHandlingEnabled()) { Translations.WriteLineFormatted("extra.entity_disabled"); handler.SetEntityHandlingEnabled(false); } }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; if (Handler.GetTerrainEnabled()) { ConsoleIO.WriteLineFormatted("§8Terrain & Movements currently not handled for that MC version."); Handler.SetTerrainEnabled(false); } if (handler.GetInventoryEnabled()) { ConsoleIO.WriteLineFormatted("§8Inventories are currently not handled for that MC version."); handler.SetInventoryEnabled(false); } if (handler.GetEntityHandlingEnabled()) { ConsoleIO.WriteLineFormatted("§8Entities are currently not handled for that MC version."); handler.SetEntityHandlingEnabled(false); } }
/// <summary> /// Handle the given packet /// </summary> /// <param name="packetID">Packet ID</param> /// <param name="packetData">Packet contents</param> /// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns> private bool handlePacket(int packetID, byte[] packetData) { if (login_phase) { switch (packetID) //Packet IDs are different while logging in { case 0x03: compression_treshold = readNextVarInt(ref packetData); break; default: return(false); //Ignored packet } } else //Regular in-game packets { switch (packetID) { case 0x00: SendPacket(0x00, getVarInt(readNextVarInt(ref packetData))); break; case 0x02: handler.OnTextReceived(ChatParser.ParseText(readNextString(ref packetData))); break; case 0x3A: int autocomplete_count = readNextVarInt(ref packetData); string tab_list = ""; for (int i = 0; i < autocomplete_count; i++) { autocomplete_result = readNextString(ref packetData); if (autocomplete_result != "") { tab_list = tab_list + autocomplete_result + " "; } } autocomplete_received = true; tab_list = tab_list.Trim(); if (tab_list.Length > 0) { ConsoleIO.WriteLineFormatted("§8" + tab_list, false); } break; case 0x40: handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData))); return(false); case 0x46: compression_treshold = readNextVarInt(ref packetData); break; default: return(false); //Ignored packet } } return(true); //Packet processed }
public Protocol17Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); ChatParser.InitTranslations(); this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; }
/// <summary> /// Start network encryption. Automatically called by Login() if the server requests encryption. /// </summary> /// <returns>True if encryption was successful</returns> private bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey) { System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey); byte[] secretKey = CryptoHandler.GenerateAESPrivateKey(); ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated."); if (serverIDhash != "-") { Console.WriteLine("Checking Session..."); if (!ProtocolHandler.SessionCheck(uuid, sessionID, CryptoHandler.getServerHash(serverIDhash, serverKey, secretKey))) { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, "Failed to check session."); return(false); } } //Encrypt the data byte[] key_enc = RSAService.Encrypt(secretKey, false); byte[] token_enc = RSAService.Encrypt(token, false); byte[] key_len = getVarInt(key_enc.Length); byte[] token_len = getVarInt(token_enc.Length); //Encryption Response packet SendPacket(0x01, concatBytes(key_len, key_enc, token_len, token_enc)); //Start client-side encryption s = CryptoHandler.getAesStream(c.GetStream(), secretKey, this); encrypted = true; //Process the next packet int packetID = -1; byte[] packetData = new byte[] { }; while (true) { readNextPacket(ref packetID, ref packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString(ref packetData))); return(false); } else if (packetID == 0x02) //Login successful { login_phase = false; StartUpdating(); return(true); } else { handlePacket(packetID, packetData); } } }
/// <summary> /// Do the Minecraft login. /// </summary> /// <returns>True if login successful</returns> public bool Login() { byte[] protocol_version = dataTypes.GetVarInt(protocolversion); string server_address = pForge.GetServerAddress(handler.GetServerHost()); byte[] server_port = BitConverter.GetBytes((ushort)handler.GetServerPort()); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(2); byte[] handshake_packet = dataTypes.ConcatBytes(protocol_version, dataTypes.GetString(server_address), server_port, next_state); SendPacket(0x00, handshake_packet); byte[] login_packet = dataTypes.GetString(handler.GetUsername()); SendPacket(0x00, login_packet); int packetID = -1; List <byte> packetData = new List <byte>(); while (true) { ReadNextPacket(ref packetID, packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); } else if (packetID == 0x01) //Encryption request { string serverID = dataTypes.ReadNextString(packetData); byte[] Serverkey = dataTypes.ReadNextByteArray(packetData); byte[] token = dataTypes.ReadNextByteArray(packetData); return(StartEncryption(handler.GetUserUUID(), handler.GetSessionID(), token, serverID, Serverkey)); } else if (packetID == 0x02) //Login successful { ConsoleIO.WriteLineFormatted("§8Server is in offline mode."); login_phase = false; if (!pForge.CompleteForgeHandshake()) { return(false); } StartUpdating(); return(true); //No need to check session or start encryption } else { HandlePacket(packetID, packetData); } } }
/// <summary> /// Start network encryption. Automatically called by Login() if the server requests encryption. /// </summary> /// <returns>True if encryption was successful</returns> private bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey) { System.Security.Cryptography.RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverKey); byte[] secretKey = CryptoHandler.GenerateAESPrivateKey(); ConsoleIO.WriteLineFormatted("§8Crypto keys & hash generated."); if (serverIDhash != "-") { Console.WriteLine("Checking Session..."); if (!ProtocolHandler.SessionCheck(uuid, sessionID, CryptoHandler.getServerHash(serverIDhash, serverKey, secretKey))) { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, "Failed to check session."); return(false); } } //Encrypt the data byte[] key_enc = RSAService.Encrypt(secretKey, false); byte[] token_enc = RSAService.Encrypt(token, false); byte[] key_len = BitConverter.GetBytes((short)key_enc.Length); Array.Reverse(key_len); byte[] token_len = BitConverter.GetBytes((short)token_enc.Length); Array.Reverse(token_len); //Encryption Response packet byte[] packet_id = getVarInt(0x01); byte[] encryption_response = concatBytes(packet_id, key_len, key_enc, token_len, token_enc); byte[] encryption_response_tosend = concatBytes(getVarInt(encryption_response.Length), encryption_response); Send(encryption_response_tosend); //Start client-side encryption s = CryptoHandler.getAesStream(c.GetStream(), secretKey, this); encrypted = true; //Read and skip the next packet int received_packet_size = readNextVarInt(); int received_packet_id = readNextVarInt(); bool encryption_success = (received_packet_id == 0x02); if (received_packet_id == 0) { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString())); } else { readData(received_packet_size - getVarInt(received_packet_id).Length); } if (encryption_success) { StartUpdating(); } return(encryption_success); }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; ConsoleIO.WriteLineFormatted("§8Terrain & Movements currently not handled for that MC version."); //Null obj pattern? }
/// <summary> /// Do the Minecraft login. /// </summary> /// <returns>True if login successful</returns> public bool Login() { byte[] protocol_version = getVarInt(protocolversion); byte[] server_adress_val = Encoding.UTF8.GetBytes(handler.getServerHost()); byte[] server_adress_len = getVarInt(server_adress_val.Length); byte[] server_port = BitConverter.GetBytes((ushort)handler.getServerPort()); Array.Reverse(server_port); byte[] next_state = getVarInt(2); byte[] handshake_packet = concatBytes(protocol_version, server_adress_len, server_adress_val, server_port, next_state); SendPacket(0x00, handshake_packet); byte[] username_val = Encoding.UTF8.GetBytes(handler.getUsername()); byte[] username_len = getVarInt(username_val.Length); byte[] login_packet = concatBytes(username_len, username_val); SendPacket(0x00, login_packet); int packetID = -1; byte[] packetData = new byte[] { }; while (true) { readNextPacket(ref packetID, ref packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString(ref packetData))); return(false); } else if (packetID == 0x01) //Encryption request { string serverID = readNextString(ref packetData); byte[] Serverkey = readNextByteArray(ref packetData); byte[] token = readNextByteArray(ref packetData); return(StartEncryption(handler.getUserUUID(), handler.getSessionID(), token, serverID, Serverkey)); } else if (packetID == 0x02) //Login successful { ConsoleIO.WriteLineFormatted("§8Server is in offline mode."); login_phase = false; StartUpdating(); return(true); //No need to check session or start encryption } else { handlePacket(packetID, packetData); } } }
public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo forgeInfo, Player player) { this.player = player; ConsoleIO.SetAutoCompleteEngine(this); ChatParser.InitTranslations(); connectionInfo = new ConnectionInfo(new SocketWrapper(Client), 0); this.dataTypes = new DataTypes(protocolVersion); this.protocolversion = protocolVersion; this.handler = handler; this.pForge = new Protocol18Forge(forgeInfo, protocolVersion, dataTypes, this, handler); this.pTerrain = new Protocol18Terrain(protocolVersion, dataTypes, handler); if (protocolversion > (int)McVersion.V1142) { ConsoleIO.WriteLineFormatted("§8Terrain & Movements currently not handled for that MC version."); //Modify client pool for terrain? } if (player.GetInventoryEnabled() && protocolversion > (int)McVersion.V114) { ConsoleIO.WriteLineFormatted("§8Inventories are currently not handled for that MC version."); player.SetInventoryEnabled(false); } if (protocolversion >= (int)McVersion.V18) { if (protocolVersion >= (int)McVersion.V114) { Block.Palette = new Palette114(); } else { Block.Palette = new Palette113(); } } else { Block.Palette = new Palette112(); } packetReadWriter = new Protocol18PacketReadWriter(connectionInfo, dataTypes, protocolVersion); packetHandler = new Protocol18PacketHandler(protocolVersion, dataTypes, handler, packetReadWriter, pTerrain, pForge, worldInfo, this, player); pForge.packetReadWriter = packetReadWriter; }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { //ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; /*if (Settings.TerrainAndMovements) * { * //ConsoleIO.WriteLineFormatted("§8Terrain & Movements currently not handled for that MC version."); * Settings.TerrainAndMovements = false; * }*/ }
public Protocol16Handler(TcpClient Client, int ProtocolVersion, IMinecraftComHandler Handler) { ConsoleIO.SetAutoCompleteEngine(this); if (protocolversion >= 72) { ChatParser.InitTranslations(); } this.c = Client; this.protocolversion = ProtocolVersion; this.handler = Handler; if (Settings.TerrainAndMovements) { ConsoleIO.WriteLineFormatted("§2[HtBot]§a Terreno e Movimentos não são suportados nessa versão do Minecraft."); Settings.TerrainAndMovements = false; } }
public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo forgeInfo) { ConsoleIO.SetAutoCompleteEngine(this); ChatParser.InitTranslations(); this.socketWrapper = new SocketWrapper(Client); this.dataTypes = new DataTypes(protocolVersion); this.protocolversion = protocolVersion; this.handler = handler; this.pForge = new Protocol18Forge(forgeInfo, protocolVersion, dataTypes, this, handler); this.pTerrain = new Protocol18Terrain(protocolVersion, dataTypes, handler); if (handler.GetTerrainEnabled() && protocolversion > MC1142Version) { ConsoleIO.WriteLineFormatted("§8Terrain & Movements currently not handled for that MC version."); handler.SetTerrainEnabled(false); } if (handler.GetInventoryEnabled() && protocolversion > MC114Version) { ConsoleIO.WriteLineFormatted("§8Inventories are currently not handled for that MC version."); handler.SetInventoryEnabled(false); } if (protocolversion >= MC113Version) { if (protocolVersion > MC1142Version && handler.GetTerrainEnabled()) { throw new NotImplementedException("Please update block types handling for this Minecraft version. See Material.cs"); } if (protocolVersion >= MC114Version) { Block.Palette = new Palette114(); } else { Block.Palette = new Palette113(); } } else { Block.Palette = new Palette112(); } }
/// <summary> /// Completes the Minecraft Forge handshake. /// </summary> /// <returns>Whether the handshake was successful.</returns> public bool CompleteForgeHandshake() { if (ForgeEnabled()) { while (fmlHandshakeState != FMLHandshakeClientState.DONE) { Packet packet = packetReadWriter.ReadNext(); if (packet.id == 0x40) // Disconnect { mcHandler.OnConnectionLost(DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packet.data))); return(false); } else { // Send back regular packet to the vanilla protocol handler protocol18.HandlePacket(packet); } } } return(true); }
/// <summary> /// Completes the Minecraft Forge handshake. /// </summary> /// <returns>Whether the handshake was successful.</returns> public bool CompleteForgeHandshake() { if (ForgeEnabled()) { int packetID = -1; List <byte> packetData = new List <byte>(); while (fmlHandshakeState != FMLHandshakeClientState.DONE) { protocol18.ReadNextPacket(ref packetID, packetData); if (packetID == 0x40) // Disconnect { mcHandler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); } else { // Send back regular packet to the vanilla protocol handler protocol18.HandlePacket(packetID, packetData); } } } return(true); }
/// <summary> /// Handle the given packet /// </summary> /// <param name="packetID">Packet ID</param> /// <param name="packetData">Packet contents</param> /// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns> private bool handlePacket(int packetID, byte[] packetData) { if (login_phase) { switch (packetID) //Packet IDs are different while logging in { case 0x03: compression_treshold = readNextVarInt(ref packetData); break; default: return(false); //Ignored packet } } else //Regular in-game packets { switch (packetID) { case 0x00: //Keep-Alive SendPacket(0x00, getVarInt(readNextVarInt(ref packetData))); break; case 0x02: //Chat message handler.OnTextReceived(ChatParser.ParseText(readNextString(ref packetData))); break; case 0x38: //Player List update int action = readNextVarInt(ref packetData); int numActions = readNextVarInt(ref packetData); Guid uuid = readNextUUID(ref packetData); switch (action) { case 0x00: //Player Join string name = readNextString(ref packetData); handler.OnPlayerJoin(uuid, name); break; case 0x04: //Player Leave handler.OnPlayerLeave(uuid); break; default: //Unknown player list item type break; } break; case 0x3A: //Tab-Complete Result int autocomplete_count = readNextVarInt(ref packetData); string tab_list = ""; for (int i = 0; i < autocomplete_count; i++) { autocomplete_result = readNextString(ref packetData); if (autocomplete_result != "") { tab_list = tab_list + autocomplete_result + " "; } } autocomplete_received = true; tab_list = tab_list.Trim(); if (tab_list.Length > 0) { ConsoleIO.WriteLineFormatted("§8" + tab_list, false); } break; case 0x40: //Kick Packet handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData))); return(false); case 0x46: //Network Compression Treshold Info compression_treshold = readNextVarInt(ref packetData); break; default: return(false); //Ignored packet } } return(true); //Packet processed }
/// <summary> /// Handle the given packet /// </summary> /// <param name="packetID">Packet ID</param> /// <param name="packetData">Packet contents</param> /// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns> internal bool HandlePacket(int packetID, List <byte> packetData) { try { if (login_phase) { switch (packetID) //Packet IDs are different while logging in { case 0x03: if (protocolversion >= MC18Version) { compression_treshold = dataTypes.ReadNextVarInt(packetData); } break; default: return(false); //Ignored packet } } // Regular in-game packets switch (Protocol18PacketTypes.GetPacketIncomingType(packetID, protocolversion)) { case PacketIncomingType.KeepAlive: SendPacket(PacketOutgoingType.KeepAlive, packetData); break; case PacketIncomingType.JoinGame: handler.OnGameJoined(); dataTypes.ReadNextInt(packetData); dataTypes.ReadNextByte(packetData); if (protocolversion >= MC191Version) { this.currentDimension = dataTypes.ReadNextInt(packetData); } else { this.currentDimension = (sbyte)dataTypes.ReadNextByte(packetData); } if (protocolversion < MC114Version) { dataTypes.ReadNextByte(packetData); // Difficulty - 1.13 and below } dataTypes.ReadNextByte(packetData); dataTypes.ReadNextString(packetData); if (protocolversion >= MC114Version) { dataTypes.ReadNextVarInt(packetData); // View distance - 1.14 and above } if (protocolversion >= MC18Version) { dataTypes.ReadNextBool(packetData); // Reduced debug info - 1.8 and above } break; case PacketIncomingType.ChatMessage: string message = dataTypes.ReadNextString(packetData); try { //Hide system messages or xp bar messages? byte messageType = dataTypes.ReadNextByte(packetData); if ((messageType == 1 && !Settings.DisplaySystemMessages) || (messageType == 2 && !Settings.DisplayXPBarMessages)) { break; } } catch (ArgumentOutOfRangeException) { /* No message type */ } handler.OnTextReceived(message, true); break; case PacketIncomingType.Respawn: this.currentDimension = dataTypes.ReadNextInt(packetData); if (protocolversion < MC114Version) { dataTypes.ReadNextByte(packetData); // Difficulty - 1.13 and below } dataTypes.ReadNextByte(packetData); dataTypes.ReadNextString(packetData); handler.OnRespawn(); break; case PacketIncomingType.PlayerPositionAndLook: if (handler.GetTerrainEnabled()) { double x = dataTypes.ReadNextDouble(packetData); double y = dataTypes.ReadNextDouble(packetData); double z = dataTypes.ReadNextDouble(packetData); float yaw = dataTypes.ReadNextFloat(packetData); float pitch = dataTypes.ReadNextFloat(packetData); byte locMask = dataTypes.ReadNextByte(packetData); if (protocolversion >= MC18Version) { Location location = handler.GetCurrentLocation(); location.X = (locMask & 1 << 0) != 0 ? location.X + x : x; location.Y = (locMask & 1 << 1) != 0 ? location.Y + y : y; location.Z = (locMask & 1 << 2) != 0 ? location.Z + z : z; handler.UpdateLocation(location, yaw, pitch); } else { handler.UpdateLocation(new Location(x, y, z), yaw, pitch); } } if (protocolversion >= MC19Version) { int teleportID = dataTypes.ReadNextVarInt(packetData); // Teleport confirm packet SendPacket(PacketOutgoingType.TeleportConfirm, dataTypes.GetVarInt(teleportID)); } break; case PacketIncomingType.ChunkData: if (handler.GetTerrainEnabled()) { int chunkX = dataTypes.ReadNextInt(packetData); int chunkZ = dataTypes.ReadNextInt(packetData); bool chunksContinuous = dataTypes.ReadNextBool(packetData); ushort chunkMask = protocolversion >= MC19Version ? (ushort)dataTypes.ReadNextVarInt(packetData) : dataTypes.ReadNextUShort(packetData); if (protocolversion < MC18Version) { ushort addBitmap = dataTypes.ReadNextUShort(packetData); int compressedDataSize = dataTypes.ReadNextInt(packetData); byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] decompressed = ZlibUtils.Decompress(compressed); pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new List <byte>(decompressed)); } else { if (protocolversion >= MC114Version) { dataTypes.ReadNextNbt(packetData); // Heightmaps - 1.14 and above } int dataSize = dataTypes.ReadNextVarInt(packetData); pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData); } } break; case PacketIncomingType.MultiBlockChange: if (handler.GetTerrainEnabled()) { int chunkX = dataTypes.ReadNextInt(packetData); int chunkZ = dataTypes.ReadNextInt(packetData); int recordCount = protocolversion < MC18Version ? (int)dataTypes.ReadNextShort(packetData) : dataTypes.ReadNextVarInt(packetData); for (int i = 0; i < recordCount; i++) { byte locationXZ; ushort blockIdMeta; int blockY; if (protocolversion < MC18Version) { blockIdMeta = dataTypes.ReadNextUShort(packetData); blockY = (ushort)dataTypes.ReadNextByte(packetData); locationXZ = dataTypes.ReadNextByte(packetData); } else { locationXZ = dataTypes.ReadNextByte(packetData); blockY = (ushort)dataTypes.ReadNextByte(packetData); blockIdMeta = (ushort)dataTypes.ReadNextVarInt(packetData); } int blockX = locationXZ >> 4; int blockZ = locationXZ & 0x0F; Block block = new Block(blockIdMeta); handler.GetWorld().SetBlock(new Location(chunkX, chunkZ, blockX, blockY, blockZ), block); } } break; case PacketIncomingType.BlockChange: if (handler.GetTerrainEnabled()) { if (protocolversion < MC18Version) { int blockX = dataTypes.ReadNextInt(packetData); int blockY = dataTypes.ReadNextByte(packetData); int blockZ = dataTypes.ReadNextInt(packetData); short blockId = (short)dataTypes.ReadNextVarInt(packetData); byte blockMeta = dataTypes.ReadNextByte(packetData); handler.GetWorld().SetBlock(new Location(blockX, blockY, blockZ), new Block(blockId, blockMeta)); } else { handler.GetWorld().SetBlock(dataTypes.ReadNextLocation(packetData), new Block((ushort)dataTypes.ReadNextVarInt(packetData))); } } break; case PacketIncomingType.MapChunkBulk: if (protocolversion < MC19Version && handler.GetTerrainEnabled()) { int chunkCount; bool hasSkyLight; List <byte> chunkData = packetData; //Read global fields if (protocolversion < MC18Version) { chunkCount = dataTypes.ReadNextShort(packetData); int compressedDataSize = dataTypes.ReadNextInt(packetData); hasSkyLight = dataTypes.ReadNextBool(packetData); byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] decompressed = ZlibUtils.Decompress(compressed); chunkData = new List <byte>(decompressed); } else { hasSkyLight = dataTypes.ReadNextBool(packetData); chunkCount = dataTypes.ReadNextVarInt(packetData); } //Read chunk records int[] chunkXs = new int[chunkCount]; int[] chunkZs = new int[chunkCount]; ushort[] chunkMasks = new ushort[chunkCount]; ushort[] addBitmaps = new ushort[chunkCount]; for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) { chunkXs[chunkColumnNo] = dataTypes.ReadNextInt(packetData); chunkZs[chunkColumnNo] = dataTypes.ReadNextInt(packetData); chunkMasks[chunkColumnNo] = dataTypes.ReadNextUShort(packetData); addBitmaps[chunkColumnNo] = protocolversion < MC18Version ? dataTypes.ReadNextUShort(packetData) : (ushort)0; } //Process chunk records for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) { pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, currentDimension, chunkData); } } break; case PacketIncomingType.UnloadChunk: if (protocolversion >= MC19Version && handler.GetTerrainEnabled()) { int chunkX = dataTypes.ReadNextInt(packetData); int chunkZ = dataTypes.ReadNextInt(packetData); handler.GetWorld()[chunkX, chunkZ] = null; } break; case PacketIncomingType.PlayerListUpdate: if (protocolversion >= MC18Version) { int action = dataTypes.ReadNextVarInt(packetData); int numActions = dataTypes.ReadNextVarInt(packetData); for (int i = 0; i < numActions; i++) { Guid uuid = dataTypes.ReadNextUUID(packetData); switch (action) { case 0x00: //Player Join string name = dataTypes.ReadNextString(packetData); int propNum = dataTypes.ReadNextVarInt(packetData); for (int p = 0; p < propNum; p++) { string key = dataTypes.ReadNextString(packetData); string val = dataTypes.ReadNextString(packetData); if (dataTypes.ReadNextBool(packetData)) { dataTypes.ReadNextString(packetData); } } dataTypes.ReadNextVarInt(packetData); dataTypes.ReadNextVarInt(packetData); if (dataTypes.ReadNextBool(packetData)) { dataTypes.ReadNextString(packetData); } handler.OnPlayerJoin(uuid, name); break; case 0x01: //Update gamemode case 0x02: //Update latency dataTypes.ReadNextVarInt(packetData); break; case 0x03: //Update display name if (dataTypes.ReadNextBool(packetData)) { dataTypes.ReadNextString(packetData); } break; case 0x04: //Player Leave handler.OnPlayerLeave(uuid); break; default: //Unknown player list item type break; } } } else //MC 1.7.X does not provide UUID in tab-list updates { string name = dataTypes.ReadNextString(packetData); bool online = dataTypes.ReadNextBool(packetData); short ping = dataTypes.ReadNextShort(packetData); Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray()); if (online) { handler.OnPlayerJoin(FakeUUID, name); } else { handler.OnPlayerLeave(FakeUUID); } } break; case PacketIncomingType.TabCompleteResult: if (protocolversion >= MC113Version) { autocomplete_transaction_id = dataTypes.ReadNextVarInt(packetData); dataTypes.ReadNextVarInt(packetData); // Start of text to replace dataTypes.ReadNextVarInt(packetData); // Length of text to replace } int autocomplete_count = dataTypes.ReadNextVarInt(packetData); autocomplete_result.Clear(); for (int i = 0; i < autocomplete_count; i++) { autocomplete_result.Add(dataTypes.ReadNextString(packetData)); if (protocolversion >= MC113Version) { // Skip optional tooltip for each tab-complete result if (dataTypes.ReadNextBool(packetData)) { dataTypes.ReadNextString(packetData); } } } autocomplete_received = true; break; case PacketIncomingType.PluginMessage: String channel = dataTypes.ReadNextString(packetData); // Length is unneeded as the whole remaining packetData is the entire payload of the packet. if (protocolversion < MC18Version) { pForge.ReadNextVarShort(packetData); } handler.OnPluginChannelMessage(channel, packetData.ToArray()); return(pForge.HandlePluginMessage(channel, packetData, ref currentDimension)); case PacketIncomingType.KickPacket: handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); case PacketIncomingType.NetworkCompressionTreshold: if (protocolversion >= MC18Version && protocolversion < MC19Version) { compression_treshold = dataTypes.ReadNextVarInt(packetData); } break; case PacketIncomingType.ResourcePackSend: string url = dataTypes.ReadNextString(packetData); string hash = dataTypes.ReadNextString(packetData); //Send back "accepted" and "successfully loaded" responses for plugins making use of resource pack mandatory byte[] responseHeader = new byte[0]; if (protocolversion < MC110Version) //MC 1.10 does not include resource pack hash in responses { responseHeader = dataTypes.ConcatBytes(dataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash)); } SendPacket(PacketOutgoingType.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(3))); //Accepted pack SendPacket(PacketOutgoingType.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(0))); //Successfully loaded break; default: return(false); //Ignored packet } return(true); //Packet processed } catch (Exception innerException) { throw new System.IO.InvalidDataException( String.Format("Failed to process incoming packet of type {0}. (PacketID: {1}, Protocol: {2}, LoginPhase: {3}, InnerException: {4}).", Protocol18PacketTypes.GetPacketIncomingType(packetID, protocolversion), packetID, protocolversion, login_phase, innerException.GetType()), innerException); } }
/// <summary> /// Read and data from the network. Should be called on a separate thread. /// </summary> /// <returns></returns> private bool Update() { handler.OnUpdate(); if (c.Client == null || !c.Connected) { return(false); } int id = 0, size = 0; try { while (c.Client.Available > 0) { size = readNextVarInt(); //Packet size id = readNextVarInt(); //Packet ID switch (id) { case 0x00: byte[] keepalive = new byte[4] { 0, 0, 0, 0 }; Receive(keepalive, 0, 4, SocketFlags.None); byte[] keepalive_packet = concatBytes(getVarInt(0x00), keepalive); byte[] keepalive_tosend = concatBytes(getVarInt(keepalive_packet.Length), keepalive_packet); Send(keepalive_tosend); break; case 0x02: handler.OnTextReceived(ChatParser.ParseText(readNextString())); break; case 0x38: string name = readNextString(); bool online = readNextBool(); short ping = readNextShort(); Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray()); if (online) { handler.OnPlayerJoin(FakeUUID, name); } else { handler.OnPlayerLeave(FakeUUID); } break; case 0x3A: int autocomplete_count = readNextVarInt(); string tab_list = ""; for (int i = 0; i < autocomplete_count; i++) { autocomplete_result = readNextString(); if (autocomplete_result != "") { tab_list = tab_list + autocomplete_result + " "; } } autocomplete_received = true; tab_list = tab_list.Trim(); if (tab_list.Length > 0) { ConsoleIO.WriteLineFormatted("§8" + tab_list, false); } break; case 0x40: handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString())); return(false); default: readData(size - getVarInt(id).Length); //Skip packet break; } } } catch (SocketException) { return(false); } return(true); }
private bool processPacket(byte id) { int nbr = 0; switch (id) { case 0x00: byte[] keepalive = new byte[5] { 0, 0, 0, 0, 0 }; Receive(keepalive, 1, 4, SocketFlags.None); Send(keepalive); break; case 0x01: readData(4); readNextString(); readData(5); break; case 0x02: readData(1); readNextString(); readNextString(); readData(4); break; case 0x03: string message = readNextString(); if (protocolversion >= 72) { message = ChatParser.ParseText(message); } handler.OnTextReceived(message); break; case 0x04: readData(16); break; case 0x05: readData(6); readNextItemSlot(); break; case 0x06: readData(12); break; case 0x07: readData(9); break; case 0x08: if (protocolversion >= 72) { readData(10); } else { readData(8); } break; case 0x09: readData(8); readNextString(); break; case 0x0A: readData(1); break; case 0x0B: readData(33); break; case 0x0C: readData(9); break; case 0x0D: readData(41); break; case 0x0E: readData(11); break; case 0x0F: readData(10); readNextItemSlot(); readData(3); break; case 0x10: readData(2); break; case 0x11: readData(14); break; case 0x12: readData(5); break; case 0x13: if (protocolversion >= 72) { readData(9); } else { readData(5); } break; case 0x14: readData(4); readNextString(); readData(16); readNextEntityMetaData(); break; case 0x16: readData(8); break; case 0x17: readData(19); readNextObjectData(); break; case 0x18: readData(26); readNextEntityMetaData(); break; case 0x19: readData(4); readNextString(); readData(16); break; case 0x1A: readData(18); break; case 0x1B: if (protocolversion >= 72) { readData(10); } break; case 0x1C: readData(10); break; case 0x1D: nbr = (int)readNextByte(); readData(nbr * 4); break; case 0x1E: readData(4); break; case 0x1F: readData(7); break; case 0x20: readData(6); break; case 0x21: readData(9); break; case 0x22: readData(18); break; case 0x23: readData(5); break; case 0x26: readData(5); break; case 0x27: if (protocolversion >= 72) { readData(9); } else { readData(8); } break; case 0x28: readData(4); readNextEntityMetaData(); break; case 0x29: readData(8); break; case 0x2A: readData(5); break; case 0x2B: readData(8); break; case 0x2C: if (protocolversion >= 72) { readNextEntityProperties(protocolversion); } break; case 0x33: readData(13); nbr = readNextInt(); readData(nbr); break; case 0x34: readData(10); nbr = readNextInt(); readData(nbr); break; case 0x35: readData(12); break; case 0x36: readData(14); break; case 0x37: readData(17); break; case 0x38: readNextChunkBulkData(); break; case 0x3C: readData(28); nbr = readNextInt(); readData(3 * nbr); readData(12); break; case 0x3D: readData(18); break; case 0x3E: readNextString(); readData(17); break; case 0x3F: if (protocolversion > 51) { readNextString(); readData(32); } break; case 0x46: readData(2); break; case 0x47: readData(17); break; case 0x64: readNextWindowData(protocolversion); break; case 0x65: readData(1); break; case 0x66: readData(7); readNextItemSlot(); break; case 0x67: readData(3); readNextItemSlot(); break; case 0x68: readData(1); for (nbr = readNextShort(); nbr > 0; nbr--) { readNextItemSlot(); } break; case 0x69: readData(5); break; case 0x6A: readData(4); break; case 0x6B: readData(2); readNextItemSlot(); break; case 0x6C: readData(2); break; case 0x82: readData(10); readNextString(); readNextString(); readNextString(); readNextString(); break; case 0x83: readData(4); nbr = readNextShort(); readData(nbr); break; case 0x84: readData(11); nbr = readNextShort(); if (nbr > 0) { readData(nbr); } break; case 0x85: if (protocolversion >= 74) { readData(13); } break; case 0xC8: if (readNextInt() == 2022) { handler.OnTextReceived("You are dead. Type /reco to respawn & reconnect."); } if (protocolversion >= 72) { readData(4); } else { readData(1); } break; case 0xC9: string name = readNextString(); bool online = readNextByte() != 0x00; readData(2); Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray()); if (online) { handler.OnPlayerJoin(FakeUUID, name); } else { handler.OnPlayerLeave(FakeUUID); } break; case 0xCA: if (protocolversion >= 72) { readData(9); } else { readData(3); } break; case 0xCB: autocomplete_result = readNextString(); autocomplete_received = true; break; case 0xCC: readNextString(); readData(4); break; case 0xCD: readData(1); break; case 0xCE: if (protocolversion > 51) { readNextString(); readNextString(); readData(1); } break; case 0xCF: if (protocolversion > 51) { readNextString(); readData(1); readNextString(); } readData(4); break; case 0xD0: if (protocolversion > 51) { readData(1); readNextString(); } break; case 0xD1: if (protocolversion > 51) { readNextTeamData(); } break; case 0xFA: readNextString(); nbr = readNextShort(); readData(nbr); break; case 0xFF: string reason = readNextString(); handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, reason); break; default: return(false); //unknown packet! } return(true); //packet has been successfully skipped }