public MinecraftClient(string hostname, ushort port) { Hostname = hostname; Port = port; Console.WriteLine("Connecting to client on {0}:{1}...", hostname, port); _client = new TcpClient(hostname, port); _network = _client.GetStream(); _notchian = new NotchianStream(_network); Console.WriteLine("Connected!"); }
public static void PrintPacket(IPacket packet) { using (var memory = new MemoryStream()) using (var notchian = new NotchianStream(memory)) { packet.Write(in notchian); Header2(">>Packet (properties):"); var properties = new List <(string type, string name, object value)>(); foreach (PropertyInfo property in packet.GetType().GetProperties()) { properties.Add(( type: property.PropertyType.Name, name: property.Name, value: property.GetValue(packet) )); } int widthType = properties.Max(p => p.type.Length) + 2; int widthName = properties.Max(p => p.name.Length) + 2; foreach ((string type, string name, object value) in properties) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(type); Console.Write(new string(' ', widthType - type.Length)); Console.ForegroundColor = ConsoleColor.Gray; Console.Write(name); Console.Write(new string(' ', widthName - name.Length)); PrintType(value); } Console.ResetColor(); Header2(">>Packet (hex):"); byte[] array = memory.ToArray(); int arrayCeil = 8 * (int)Math.Ceiling(array.Length / 8d); for (var row = 0; row < arrayCeil; row += 8) { for (int i = row; i < row + 8; i++) { if (i < array.Length) { Console.ForegroundColor = IsASCIIChar((char)array[i]) ? ConsoleColor.White : ConsoleColor.DarkGray; Console.Write("{0:x2} ", array[i]); } else { Console.Write(" "); } } Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write(" | "); for (int i = row; i < row + 8; i++) { if (i < array.Length) { Console.ForegroundColor = IsASCIIChar((char)array[i]) ? ConsoleColor.White : ConsoleColor.DarkGray; Console.Write((char)array[i]); } else { Console.Write(" "); } } Console.WriteLine(); } Console.ResetColor(); } }