コード例 #1
0
        public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
        {
            if (stream.Length - stream.Position >= 2)
            {
                short length = stream.ReadShort();
                if (stream.Length - stream.Position >= length)
                {
                    client.Username = stream.ReadString(length);

                    //response

                    // - = no password
                    // + = password
                    // hash = check with minecraft.net

                    if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Online)
                    {
                        Random random = new Random();
                        client.Hash = random.Next().ToString("X");
                        client.Send(MinecraftPacketCreator.GetHandshake(client.Hash));
                    }
                    else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Offline)
                    {
                        client.Send(MinecraftPacketCreator.GetHandshake("-"));
                    }
                    else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Password)
                    {
                        client.Send(MinecraftPacketCreator.GetHandshake("+"));
                    }
                    return true;
                }
            }
            return false;
        }
コード例 #2
0
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     // NOTE: Maybe bad practice... Probably a better idea to store data after packet 100% received...
     // Although I don't see anything that might go wrong... Since the server and client is essentially "syncronized"
     if (stream.Length - stream.Position >= 8)
     {
         client.Player.Position.X = stream.ReadDouble();
         if (stream.Length - stream.Position >= 8)
         {
             client.Player.Stance = stream.ReadDouble();
             if (stream.Length - stream.Position >= 8)
             {
                 client.Player.Position.Y = stream.ReadDouble();
                 if (stream.Length - stream.Position >= 8)
                 {
                     client.Player.Position.Z = stream.ReadDouble();
                     if (stream.Length - stream.Position >= 4)
                     {
                         client.Player.Rotation.Yaw = stream.ReadFloat();
                         if (stream.Length - stream.Position >= 4)
                         {
                             client.Player.Rotation.Pitch = stream.ReadFloat();
                             if (stream.Length - stream.Position >= 1)
                             {
                                 client.Player.OnGround = stream.ReadBool();
                                 return true;
                             }
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
コード例 #3
0
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     if (stream.Length - stream.Position >= 8)
     {
         double x = stream.ReadDouble();
         if (stream.Length - stream.Position >= 8)
         {
             double y = stream.ReadDouble();
             if (stream.Length - stream.Position >= 8)
             {
                 double stance = stream.ReadDouble();
                 if (stream.Length - stream.Position >= 8)
                 {
                     double z = stream.ReadDouble();
                     if (stream.Length - stream.Position >= 1)
                     {
                         bool onGround = stream.ReadBool();
                         client.Player.Position.X = x;
                         client.Player.Position.Y = y;
                         client.Player.Stance = stance;
                         client.Player.Position.Z = z;
                         client.Player.OnGround = onGround;
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
コード例 #4
0
        public static byte[] GetMapChunk(Chunk c)
        {
            using (MinecraftPacketStream stream = new MinecraftPacketStream())
            {
                stream.WriteByte((byte)MinecraftOpcode.MapChunk);
                stream.WriteInt(c.Position.X);
                stream.WriteShort(c.Position.Y);
                stream.WriteInt(c.Position.Z);
                stream.WriteByte(15);
                stream.WriteByte(127);
                stream.WriteByte(15);

                using (MemoryStream mStream = new MemoryStream())
                {
                    using (ZOutputStream zStream = new ZOutputStream(mStream, zlibConst.Z_BEST_COMPRESSION))
                    {
                        zStream.Write(c.Data, 0, c.Data.Length);
                        stream.WriteInt((int)mStream.Length);
                        mStream.Position = 0;
                        mStream.CopyTo(stream);
                    }
                }
                return stream.ToArray();
            }
        }
コード例 #5
0
 public static byte[] GetHandshake(string hash)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.Handshake);
         stream.WriteString(hash);
         return stream.ToArray();
     }
 }
コード例 #6
0
 public static byte[] GetDisconnect(string message)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.Disconnect);
         stream.WriteString(message);
         return stream.ToArray();
     }
 }
コード例 #7
0
 public static byte[] GetLoginRequest()
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.LoginRequest);
         stream.WriteUint(MinecraftServer.Instance.Entity++);
         stream.WriteString("");
         stream.WriteString("");
         stream.WriteLong(MinecraftServer.Instance.RandomSeed);
         stream.WriteByte(MinecraftServer.Instance.Dimension);
         return stream.ToArray();
     }
 }
コード例 #8
0
 public static byte[] GetChatMessage(string username, string message)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.ChatMessage);
         StringBuilder builder = new StringBuilder();
         builder.Append("<");
         builder.Append(username);
         builder.Append("> ");
         builder.Append(message);
         stream.WriteString(builder.ToString());
         return stream.ToArray();
     }
 }
コード例 #9
0
ファイル: ChatMessageHandler.cs プロジェクト: Skeer/Minecraft
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     if (stream.Length - stream.Position >= 2)
     {
         short length = stream.ReadShort();
         if (stream.Length - stream.Position >= length)
         {
             string message = stream.ReadString(length);
             if (message.Substring(0, 1) == "/")
             {
                 string[] splitted = message.Substring(1).Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                 if (MinecraftServer.Instance.CommandManager.CommandExists(splitted[0].ToLower()))
                 {
                     MinecraftServer.Instance.CommandManager.RunCommand(client, splitted[0].ToLower(), splitted);
                 }
                 else if (splitted[0].ToLower() == "help")
                 {
                     client.Send(MinecraftPacketCreator.GetChatMessage("Availiable commands are:"));
                     foreach (string key in MinecraftServer.Instance.CommandManager.GetCommands())
                     {
                         client.Send(MinecraftPacketCreator.GetChatMessage(key));
                     }
                 }
                 else
                 {
                     client.Send(MinecraftPacketCreator.GetChatMessage("Command " + splitted[0] + " does not exist."));
                 }
             }
             else
             {
                 foreach (MinecraftClient c in MinecraftServer.Instance.Clients)
                 {
                     c.Send(MinecraftPacketCreator.GetChatMessage(client.Username, message));
                 }
             }
             return true;
         }
     }
     return false;
 }
コード例 #10
0
        public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
        {
            if (stream.Length - stream.Position >= 4)
            {
                int version = stream.ReadInt();
                if (version == MinecraftServer.Instance.Version)
                {
                    if (stream.Length - stream.Position >= 2)
                    {
                        short ulength = stream.ReadShort();
                        if (stream.Length - stream.Position >= ulength)
                        {
                            string username = stream.ReadString(ulength);
                            if (stream.Length - stream.Position >= 2)
                            {
                                short plength = stream.ReadShort();
                                if (stream.Length - stream.Position >= plength)
                                {
                                    string password = stream.ReadString(plength);
                                    if (stream.Length - stream.Position >= 8)
                                    {
                                        stream.ReadLong();
                                        if (stream.Length - stream.Position >= 1)
                                        {
                                            stream.ReadByte();

                                            if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Online)
                                            {
                                                //REFRACTOR?
                                                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.minecraft.net/game/checkserver.jsp?user="******"&serverId=" + client.Hash);
                                                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                                                byte[] buffer = new byte[3];
                                                response.GetResponseStream().Read(buffer, 0, buffer.Length);
                                                if (UTF8Encoding.UTF8.GetString(buffer) == "YES")
                                                {
                                                    client.Send(MinecraftPacketCreator.GetLoginRequest());
                                                    client.Load();
                                                    return true;
                                                }
                                                else
                                                {
                                                    client.Disconnect("Failed to verify username.");
                                                }
                                            }
                                            else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Offline)
                                            {
                                                client.Send(MinecraftPacketCreator.GetLoginRequest());
                                                client.Load();
                                                return true;
                                            }
                                            else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Password)
                                            {
                                                // Insert validation function here...
                                                // Password doesn't even work.
                                                client.Disconnect("Password authentication not implemented.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    client.Disconnect("Invalid version.");
                }
            }
            return false;
        }
コード例 #11
0
ファイル: KeepAliveHandler.cs プロジェクト: Skeer/Minecraft
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     client.ResetConnectionTimer();
     return true;
 }
コード例 #12
0
ファイル: MinecraftClient.cs プロジェクト: Skeer/Minecraft
        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    if (KeepAliveTimer != null)
                    {
                        KeepAliveTimer.Dispose();
                    }

                    if (ConnectionTimer != null)
                    {
                        ConnectionTimer.Dispose();
                    }

                    if (Client != null)
                    {
                        Client.Dispose();
                    }

                    if (Received != null)
                    {
                        Received.Dispose();
                    }

                    if(MinecraftServer.Instance.Clients.Contains(this))
                    {
                        MinecraftServer.Instance.Clients.Remove(this);
                    }

                    //Remove client for Server's client list?
                }

                Received = null;
                KeepAliveTimer = null;
                ConnectionTimer = null;
                Client = null;
                Disposed = true;
            }
        }
コード例 #13
0
 public static byte[] GetSpawnPosition(PointInt position)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.SpawnPosition);
         stream.WriteInt(position.X);
         stream.WriteInt(position.Y);
         stream.WriteInt(position.Z);
         return stream.ToArray();
     }
 }
コード例 #14
0
 public static byte[] GetPreChunk(Chunk c)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.PreChunk);
         stream.WriteInt(c.Position.X);
         stream.WriteInt(c.Position.Z);
         stream.WriteBool(true);
         return stream.ToArray();
     }
 }
コード例 #15
0
 public static byte[] GetPositionLook(PointDouble position, Rotation rotation, bool onGround)
 {
     using (MinecraftPacketStream stream = new MinecraftPacketStream())
     {
         stream.WriteByte((byte)MinecraftOpcode.PlayerPositionLook);
         stream.WriteDouble(position.X);
         stream.WriteDouble(position.Y);
         stream.WriteDouble(position.Y + 1.62); // dunno what 1.62 means...
         stream.WriteDouble(position.Z);
         stream.WriteFloat(rotation.Yaw);
         stream.WriteFloat(rotation.Pitch);
         stream.WriteBool(onGround);
         return stream.ToArray();
     }
 }