/// <summary> /// Initialize a new Forge protocol handler /// </summary> /// <param name="forgeInfo">Forge Server Information</param> /// <param name="protocolVersion">Minecraft protocol version</param> /// <param name="dataTypes">Minecraft data types handler</param> public Protocol18Forge(ForgeInfo forgeInfo, int protocolVersion, DataTypes dataTypes, Protocol18Handler protocol18, IMinecraftComHandler mcHandler) { this.forgeInfo = forgeInfo; this.protocolversion = protocolVersion; this.dataTypes = dataTypes; this.protocol18 = protocol18; this.mcHandler = mcHandler; }
/// <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) { string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveBufferSize = 1024 * 1024; byte[] packet_id = getVarInt(0); byte[] protocol_version = getVarInt(4); byte[] server_adress_val = Encoding.UTF8.GetBytes(host); byte[] server_adress_len = getVarInt(server_adress_val.Length); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = getVarInt(1); byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state); byte[] tosend = concatBytes(getVarInt(packet.Length), packet); tcp.Client.Send(tosend, SocketFlags.None); byte[] status_request = getVarInt(0); byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request); tcp.Client.Send(request_packet, SocketFlags.None); int packetID = -1; byte[] packetData = new byte[] { }; Protocol18Handler ComTmp = new Protocol18Handler(tcp); ComTmp.readNextPacket(ref packetID, ref packetData); if (packetData.Length > 0) //Verify Response length { if (packetID == 0x00) //Read Packet ID { string result = ComTmp.readNextString(ref packetData); //Get the Json data if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\"")) { string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None); string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None); if (tmp_ver.Length >= 2 && tmp_name.Length >= 2) { protocolversion = atoi(tmp_ver[1]); version = tmp_name[1].Split('"')[0]; if (result.Contains("modinfo\":")) { //Server is running Forge (which is not supported) version = "Forge " + version; protocolversion = 0; } ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + ")."); return(true); } } } } return(false); }
/// <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; byte[] packet_id = getVarInt(0); byte[] protocol_version = getVarInt(4); byte[] server_adress_val = Encoding.UTF8.GetBytes(host); byte[] server_adress_len = getVarInt(server_adress_val.Length); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = getVarInt(1); byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state); byte[] tosend = concatBytes(getVarInt(packet.Length), packet); tcp.Client.Send(tosend, SocketFlags.None); byte[] status_request = getVarInt(0); byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request); tcp.Client.Send(request_packet, SocketFlags.None); Protocol18Handler ComTmp = new Protocol18Handler(tcp); int packetLength = ComTmp.readNextVarIntRAW(); if (packetLength > 0) //Read Response length { List<byte> packetData = new List<byte>(ComTmp.readDataRAW(packetLength)); if (readNextVarInt(packetData) == 0x00) //Read Packet ID { string result = 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 = atoi(versionData.Properties["protocol"].StringValue); //Automatic fix for BungeeCord 1.8 reporting itself as 1.7... if (protocolversion < 47 && version.Split(' ', '/').Contains("1.8")) protocolversion = ProtocolHandler.MCVer2ProtocolVersion("1.8.0"); // Check for forge on the server. if (jsonData.Properties.ContainsKey("modinfo") && jsonData.Properties["modinfo"].Type == Json.JSONData.DataType.Object) { Json.JSONData modData = jsonData.Properties["modinfo"]; if (modData.Properties.ContainsKey("type") && modData.Properties["type"].StringValue == "FML") { forgeInfo = new ForgeInfo(modData); if (forgeInfo.Mods.Any()) { if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Server is running Forge. Mod list:"); foreach (ForgeInfo.ForgeMod mod in forgeInfo.Mods) { ConsoleIO.WriteLineFormatted("§8 " + mod.ToString()); } } else ConsoleIO.WriteLineFormatted("§8Server is running Forge."); } else forgeInfo = null; } } ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + (forgeInfo != null ? ", with Forge)." : ").")); return true; } } } } return false; }