示例#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;
        }
 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
文件: List.cs 项目: Skeer/Minecraft
 public void Run(MinecraftServer server, MinecraftClient client, string[] args)
 {
     StringBuilder builder = new StringBuilder();
     builder.Append("Connected users: ");
     foreach (MinecraftClient c in server.Clients)
     {
         builder.Append(c.Username);
         builder.Append(", ");
     }
     builder.Remove(builder.Length - 2, 2);
     builder.Append(".");
     client.Send(MinecraftPacketCreator.GetChatMessage(builder.ToString()));
 }
示例#5
0
 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;
 }
示例#6
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;
        }
示例#7
0
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     client.ResetConnectionTimer();
     return true;
 }
示例#8
0
 public bool RunCommand(MinecraftClient client, string name, string[] args)
 {
     try
     {
         Run(client, name, args);
         return true;
     }
     catch
     {
         if (Compile(name))
         {
             try
             {
                 Run(client, name, args);
                 return true;
             }
             catch (Exception e)
             {
                 Log.Error(e, "Error running script {0}.", name);
                 return false;
             }
         }
         return false;
     }
 }
示例#9
0
 private void Run(MinecraftClient client, string name, string[] args)
 {
     AppDomain domain = AppDomain.CreateDomain("domain", null, Setup);
     try
     {
         ((ICommand)domain.CreateInstanceFromAndUnwrap(Path.Combine(CommandDirectory, name + ".dll"), "Minecraft.Commands." + Commands[name])).Run(MinecraftServer.Instance, client, args);
     }
     finally
     {
         AppDomain.Unload(domain);
     }
 }