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> /// Read an integer from the network /// </summary> /// <returns>The integer</returns> public int ReadNextVarIntRAW(SocketWrapper socket) { int i = 0; int j = 0; int k = 0; while (true) { k = socket.ReadDataRAW(1)[0]; i |= (k & 0x7F) << j++ *7; if (j > 5) { throw new OverflowException("VarInt too big"); } if ((k & 0x80) != 128) { break; } } return(i); }
/// <summary> /// Ping a Minecraft server to get information about the server /// </summary> /// <returns>True if ping was successful</returns> public static bool doPing(string host, int port, ref int protocolversion, ref ForgeInfo forgeInfo) { string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveBufferSize = 1024 * 1024; SocketWrapper socketWrapper = new SocketWrapper(tcp); DataTypes dataTypes = new DataTypes(MC18Version); byte[] packet_id = dataTypes.GetVarInt(0); byte[] protocol_version = dataTypes.GetVarInt(-1); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(1); byte[] packet = dataTypes.ConcatBytes(packet_id, protocol_version, dataTypes.GetString(host), server_port, next_state); byte[] tosend = dataTypes.ConcatBytes(dataTypes.GetVarInt(packet.Length), packet); socketWrapper.SendDataRAW(tosend); byte[] status_request = dataTypes.GetVarInt(0); byte[] request_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(status_request.Length), status_request); socketWrapper.SendDataRAW(request_packet); int packetLength = dataTypes.ReadNextVarIntRAW(socketWrapper); if (packetLength > 0) //Read Response length { List <byte> packetData = new List <byte>(socketWrapper.ReadDataRAW(packetLength)); if (dataTypes.ReadNextVarInt(packetData) == 0x00) //Read Packet ID { string result = dataTypes.ReadNextString(packetData); //Get the Json data if (!String.IsNullOrEmpty(result) && result.StartsWith("{") && result.EndsWith("}")) { Json.JSONData jsonData = Json.ParseJson(result); if (jsonData.Type == Json.JSONData.DataType.Object && jsonData.Properties.ContainsKey("version")) { Json.JSONData versionData = jsonData.Properties["version"]; //Retrieve display name of the Minecraft version if (versionData.Properties.ContainsKey("name")) { version = versionData.Properties["name"].StringValue; } //Retrieve protocol version number for handling this server if (versionData.Properties.ContainsKey("protocol")) { protocolversion = dataTypes.Atoi(versionData.Properties["protocol"].StringValue); } // Check for forge on the server. Protocol18Forge.ServerInfoCheckForge(jsonData, ref forgeInfo); ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + (forgeInfo != null ? ", with Forge)." : ").")); return(true); } } } } return(false); }