Advertisement
gamer931215

Minecraft.cs

Jan 2nd, 2012
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6.  
  7. namespace Minecraft_Client
  8. {
  9.     class Minecraft
  10.     {
  11.         public void Update(TcpClient c)
  12.         {
  13.             byte[] data = new byte[5];
  14.             data[0] = (byte)0;
  15.             byte[] id = new byte[1];
  16.             c.Client.Receive(id, 0, 1, SocketFlags.None);
  17.             if (id[0] == (byte)0)
  18.             {
  19.                 //Ping/alive package (send back!)
  20.                 Console.WriteLine("Client receiver keep-alive-package.");
  21.                 c.Client.Receive(data, 1, 4, SocketFlags.None);
  22.                 int kai = BitConverter.ToInt16(data, 1);
  23.                 Console.WriteLine("Sending keep-alive-package back server (KAI:" + kai.ToString() + ")");
  24.                 c.Client.Send(data);
  25.             }
  26.             else if (id[0] == (byte)3)
  27.             {
  28.                 short len;
  29.                 byte[] buffer = new byte[2];
  30.                 c.Client.Receive(buffer, 1, 3, SocketFlags.None);
  31.                 len = (short)BitConverter.ToInt16(buffer, 0);
  32.                 Console.WriteLine("Received chatmsg (len:" + len.ToString());
  33.             }
  34.         }
  35.  
  36.         public bool Handshake(string username,TcpClient c)
  37.         {
  38.             //array
  39.             byte[] data = new byte[3 + (username.Length *2)];
  40.  
  41.             //packet id
  42.             data[0] = (byte)2;
  43.  
  44.             //short len
  45.             byte[] sh = new byte[2];
  46.             sh = BitConverter.GetBytes((short)username.Length);
  47.             Array.Reverse(sh);
  48.             sh.CopyTo(data, 1);
  49.  
  50.             //username
  51.             byte[] name = Encoding.Unicode.GetBytes(username);
  52.             name.CopyTo(data, 3);
  53.  
  54.             c.Client.Send(data);
  55.  
  56.             byte[] pid = new byte[1];
  57.             c.Client.Receive(pid, 0, 1, SocketFlags.None);
  58.             if (pid[0] == (byte)2)
  59.             {
  60.                 return true;
  61.             }
  62.             else return false;
  63.         }
  64.  
  65.         public bool RequestLogin(string username,TcpClient c)
  66.         {
  67.             //array
  68.             byte id = (byte)1;
  69.             byte[] protecol = new byte[4]; protecol = BitConverter.GetBytes((int)22); Array.Reverse(protecol);//v1.0
  70.             byte[] len = new byte[2]; len = BitConverter.GetBytes((username.Length * 2)); Array.Reverse(len);
  71.             byte[] usrname = new byte[username.Length * 2]; usrname = Encoding.Unicode.GetBytes(username);
  72.             byte[] lon = new byte[8]; lon = BitConverter.GetBytes((long)0); Array.Reverse(lon);
  73.             byte[] intt = new byte[4]; intt = BitConverter.GetBytes((int)0); Array.Reverse(intt);
  74.             byte empty = (byte)0;
  75.  
  76.            
  77.  
  78.             //Creating byte array
  79.             byte[] data = new byte[23 + (username.Length * 2)];
  80.             //adding id
  81.             data[0] = id;
  82.             //adding protecol
  83.             protecol.CopyTo(data, 1);
  84.             //adding username length short
  85.             len.CopyTo(data, 5);
  86.             //adding username
  87.             usrname.CopyTo(data, 7);
  88.  
  89.             //new index calculation
  90.             int index = (7 + (username.Length * 2));
  91.  
  92.             //adding long
  93.             lon.CopyTo(data, index);
  94.             index = index + 8;
  95.             //adding int
  96.             intt.CopyTo(data, index);
  97.             index = index + 4;
  98.             //adding bytes
  99.             data[index] = empty;
  100.             data[index +1] = empty;
  101.             data[index +2] = empty;
  102.             data[index +3] = empty;
  103.  
  104.             c.Client.Send(data);
  105.             try
  106.             {
  107.                 byte[] pid = new byte[1];
  108.                 try
  109.                 {
  110.                     if (c.Connected)
  111.                     {
  112.                         c.Client.Receive(pid, 0, 1, SocketFlags.None);
  113.                     }
  114.                     else return false; //kick
  115.                 }
  116.                 catch
  117.                 {
  118.                     //server disconnected
  119.                     return false;
  120.                 }
  121.                 //Got package back ?
  122.                 return true;
  123.             }
  124.             catch
  125.             {
  126.                 Console.ForegroundColor = ConsoleColor.Red;
  127.                 Console.WriteLine("Server closed connection.");
  128.                 while (true) { }
  129.             }
  130.         }
  131.  
  132.         public void SendChatMessage(string message, TcpClient c)
  133.         {
  134.             byte[] chat = new byte[3 + (message.Length * 2)];
  135.             chat[0] = (byte)3;
  136.  
  137.             byte[] msglen = new byte[2];
  138.             msglen = BitConverter.GetBytes(message.Length);
  139.             Array.Reverse(msglen);
  140.             msglen.CopyTo(chat, 1);
  141.  
  142.             Encoding.Unicode.GetBytes(message).CopyTo(chat, 3);
  143.             c.Client.Send(chat);
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement