public static byte[] PrivateMessageCheck(WorldMgr.character c)
        {
            DB ms = new DB("SELECT * FROM message WHERE receiver='" + c.Information.Name + "'");
            PacketWriter Writer = new PacketWriter();
            int count = ms.Count();

            Writer.Create(OperationCode.SERVER_PM_MESSAGE);
            Writer.Byte(1);//Static
            Writer.Byte(Convert.ToByte(count));//Total count
            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    string pmfrom       = reader.GetString(1);
                    string pmto         = reader.GetString(2);
                    string pmmessage    = reader.GetString(3);
                    byte pmstatus       = reader.GetByte(4);
                    DateTime pmdate     = Convert.ToDateTime(reader.GetDateTime(5));

                    Writer.Text(pmfrom);            // Message From
                    Writer.DWord(0x8A070000);       // date
                    Writer.DWord(0xC7058401);       // date
                    Writer.Byte(pmstatus);          // Status (0 = Unread) (1 = Read)
                }
                ms.Close();
            }
            return Writer.GetBytes();
        }
 public void MakeAlias()
 {
     try
     {
         ///////////////////////////////////////////////////////////////////////////////////////////////////
         PacketReader Reader = new PacketReader(PacketInformation.buffer);
         int id = Reader.Int32();
         byte type = Reader.Byte();
         short nLenght = Reader.Int16();
         string name = Reader.String(nLenght);
         ///////////////////////////////////////////////////////////////////////////////////////////////////
         Console.WriteLine(name);
         DB ms = new DB("SELECT * FROM character_jobs WHERE job_alias='" + name + "'");
         int checkjob = ms.Count();
         ///////////////////////////////////////////////////////////////////////////////////////////////////
         client.Send(Packet.MakeAlias(name, type));
         if (checkjob == 0)
         {
             client.Send(Packet.MakeAlias(name, type));
             DB.query("UPDATE character_jobs SET job_alias='" + name + "' WHERE character_name='" + Character.Information.Name + "'");
         }
         else if (checkjob >= 0)
         {
             client.Send(Packet.MakeAliasError(name, type));
         }
     }
     catch (Exception ex)
     {
         Log.Exception(ex);
     }
 }
        public void JoinMerc()
        {
            try
            {
                DB ms = new DB("SELECT * FROM character_jobs WHERE character_name='" + Character.Information.Name + "'");
                int checkjob = ms.Count();

                if (checkjob == 0)
                {
                    /////////////////////////////////////////////////////////////////////////////////////
                    PacketReader Reader = new PacketReader(PacketInformation.buffer);
                    int id = Reader.Int32();
                    byte type = Reader.Byte();
                    /////////////////////////////////////////////////////////////////////////////////////
                    client.Send(Packet.InfoUpdate(1, Character.Information.CharacterID, 0));
                    client.Send(Packet.JoinMerchant(id, type));
                    /////////////////////////////////////////////////////////////////////////////////////
                    DB.query("INSERT INTO character_jobs (character_name, job_type) VALUES ('" + Character.Information.Name + "','2')");
                    ms.Close();
                }
                else
                {
                    // Not needed cant join job because excist
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Private Message Open
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void PrivateMessageOpen()
        {
            try
            {
                PacketReader Reader = new PacketReader(PacketInformation.buffer);
                /////////////////////////////////////////////////////////////////////////////////////
                byte type = Reader.Byte();
                short typeget = (short)type;
                Reader.Close();
                DB ms = new DB("SELECT * FROM message WHERE receiver='" + Character.Information.Name + "' AND idinfo ='" + typeget + "'");

                using (SqlDataReader reader = ms.Read())
                {
                    while (reader.Read())
                    {
                        string messageinfo = reader.GetString(3);
                        client.Send(Packet.PrivateMessageOpen(type, messageinfo));
                        DB.query("UPDATE message SET status='1' WHERE receiver='" + Character.Information.Name + "' AND idinfo ='" + type + "'");
                    }
                }
                ms.Close();
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public void LoadGuildMemberIds(int guildid, ref List<int> MemberIDs)
        {
            try
            {
                //Make sure we start with a clean list
                if (MemberIDs != null)
                    //If not null clear the list
                    MemberIDs.Clear();
                //Create new query to get guild member information
                DB ms = new DB("SELECT * FROM guild_members WHERE guild_id='" + guildid + "'");
                //Create sql data reader to read database content
                using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                {
                    //While the reader is reading
                    while (reader.Read())
                    {
                        //Add member id to the list
                        MemberIDs.Add(reader.GetInt32(2));
                    }
                }
            }
            //Catch any bad exception error
            catch (Exception ex)
            {
                //Write information to debug log

            }
        }
        public static void LoadBlues(WorldMgr.character c)
        {
            try
            {
                DB ms = new DB("SELECT * FROM char_items WHERE owner='"+ c.Information.CharacterID +"'");
                using (SqlDataReader reader = ms.Read())
                {
                    while (reader.Read())
                    {
                        int a = 10;
                        int b = 11;

                        int id = reader.GetInt32(0);
                        ObjData.itemblue it = new ObjData.itemblue();
                        it.blue = new ArrayList();
                        it.blueamount = new ArrayList();
                        it.totalblue = reader.GetInt32(9);
                        for (int i = 0; i <= it.totalblue; i++)
                        {
                            it.blue.Add(reader.GetString(a));
                            it.blueamount.Add(reader.GetInt32(b));
                            a += 2;
                            b += 2;
                        }

                        ObjData.Manager.ItemBlue[id] = it;

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Blue error: {0}",ex);
            }
        }
        public static ushort LoginUser(string aID, ref string aPass, ref WorldMgr.player aPlayer, bool localConnect)
        {
            if (Helpers.Manager.clients.Count >= Helpers.Manager.maxSlots)
            {
                return 0; // crowded
            }
            DB ms = new DB("SELECT * FROM users WHERE id = '" + aID + "'");
            if (ms.Count() == 0)
            {
                ms.Close();
                return 1;
            }
            ms = new DB("SELECT * FROM users WHERE id = '" + aID + "' AND password='******'");
            if (ms.Count() == 0)
            {
                ms.Close();
                return 5;
            }
            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    if (reader.GetString(1).ToLower() == aID.ToLower()) // id
                    {
                        if (reader.GetByte(3) == 1) // online
                        {
                            ms.Close();
                            return 2; // already online
                        }

                        if (reader.GetInt32(5) == 1) // banned
                        {
                            aPass = reader.GetString(4);
                            ms.Close();
                            return 3; // banned
                        }

                        if (aPlayer == null && localConnect) DB.query("UPDATE users SET online=1 WHERE userid='" + reader.GetInt32(0) + "'");
                        aPlayer = new WorldMgr.player();
                        aPlayer.AccountName = aID;
                        aPlayer.Password = aPass; // Nukei: ?? whats the reason for saving password in memory ?
                        aPlayer.ID = reader.GetInt32(0);
                        aPlayer.pGold = reader.GetInt64(7);
                        aPlayer.Silk = reader.GetInt32(6);
                        aPlayer.SilkPrem = reader.GetInt32(9);
                        aPlayer.wSlots = reader.GetByte(11);
                        ms.Close();
                        return 4;
                    }
                }
            }
            ms.Close();
            return 6; // Bilinmeyen geri dönüş:Özel bir durum oluşmadı. (Mecburi Gönderim)
        }
 /////////////////////////////////////////////////////////////////////////////////////
 // Honor rank
 /////////////////////////////////////////////////////////////////////////////////////
 public void HonorRank()
 {
     /////////////////////////////////////////////////////////////////////////////////////
     DB readsql = new DB("SELECT TOP 50 * FROM rank_honor");
     int count = readsql.Count();
     /////////////////////////////////////////////////////////////////////////////////////
     if (count > 0)
     {
         client.Send(Packet.HonorRank(Character));
     }
 }
        public static byte[] OpenWarehouse2(byte storageslots, WorldMgr.player c)
        {
            PacketWriter Writer = new PacketWriter();
            Writer.Create(OperationCode.SERVER_OPEN_WAREPROB);
            Writer.Byte(storageslots);

            DB ms = new DB("SELECT * FROM char_items WHERE storageacc='" + c.ID + "' AND storagetype='1'");

            Writer.Byte(ms.Count());
            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())

                    Item.AddItemPacket(Writer, reader.GetByte(5), reader.GetInt32(2), reader.GetByte(4), reader.GetInt16(6), reader.GetInt32(7), reader.GetInt32(0), reader.GetInt32(9),reader.GetInt32(30));
            }
            ms.Close();
            return Writer.GetBytes();
        }
 ///////////////////////////////////////////////////////////////////////////
 // Grab pet information packet
 ///////////////////////////////////////////////////////////////////////////
 public static byte[] Pet_Information_grab(CLGameServer.WorldMgr.pet_obj o, byte slot)
 {
     PacketWriter Writer = new PacketWriter();
     Writer.Create(OperationCode.SERVER_PET_INFORMATION);
     try
     {
         //////////////////////////////////////////////////////////////////////////////////////
         // Grabpet structure
         //////////////////////////////////////////////////////////////////////////////////////
         Writer.DWord(o.UniqueID);                   //Unique ID
         Writer.DWord(o.Model);                      //Pet Model
         Writer.DWord(0x00006D);                     //Settings
         Writer.DWord(0x00006D);                     //Settings
         Writer.DWord(0x000047);                     //Settings 0x47 grab pet active 0 disabled
         if (o.Petname != "No name")                 //###############
             Writer.Text(o.Petname);                 // Name region
         else                                        //
             Writer.Word(0);                         //###############
         Writer.Byte(o.Slots);                       //Slots count inventory pet
         //////////////////////////////////////////////////////////////////////////////////////
         // Grabpet item inventory
         //////////////////////////////////////////////////////////////////////////////////////
         DB ms = new DB("SELECT * FROM char_items WHERE owner='" + o.OwnerID + "' AND pet_storage_id='" + o.UniqueID + "'");
         Writer.Byte(ms.Count());
         using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
         {
             while (reader.Read())
                 Item.AddItemPacket(Writer, reader.GetByte(5), reader.GetInt32(2), reader.GetByte(4), reader.GetInt16(6), reader.GetInt32(7), reader.GetInt32(0), reader.GetInt32(9), reader.GetInt32(30));
         }
         ms.Close();
         //////////////////////////////////////////////////////////////////////////////////////
         // Other
         //////////////////////////////////////////////////////////////////////////////////////
         Writer.DWord(o.OwnerID);                    //Character ID
         Writer.Byte(slot);                          //Slot location of the pet
         //////////////////////////////////////////////////////////////////////////////////////
     }
     catch (Exception ex)
     {
         Log.Exception(ex);
     }
     return Writer.GetBytes();
 }
        public static byte[] HonorRank(WorldMgr.character c)
        {
            DB ms = new DB("SELECT * FROM rank_honor");
            PacketWriter Writer = new PacketWriter();
            int countinfo = ms.Count();

            Writer.Create(OperationCode.SERVER_HONOR_RANK);
            Writer.Byte(1);
            int i = 0;
            Writer.Byte(Convert.ToByte(countinfo));
            using (SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    byte rankicon = reader.GetByte(1);
                    string rankname = reader.GetString(2);
                    byte ranklevel = reader.GetByte(3);
                    byte ranklevelc = reader.GetByte(4);
                    int graduatesc = reader.GetInt32(5);
                    int rankposc = reader.GetInt32(6);

                    Writer.DWord(i + 1);
                    Writer.Byte(rankicon);
                    Writer.Text(rankname);
                    Writer.Byte(ranklevel);
                    Writer.Byte(ranklevelc);
                    Writer.DWord(graduatesc);
                    //If player has no guild, we write a 0 word value
                    if (c.Network.Guild.Name == null)
                    {
                        Writer.Word(0);
                    }
                    //If player has a guild we write the guild name.
                    else
                    {
                        Writer.Text(c.Network.Guild.Name);
                    }
                    i++;
                }
                ms.Close();
            }
            return Writer.GetBytes();
        }
 public static byte[] GuildStorageData(WorldMgr.character c)
 {
     DB getstorage = new DB("SELECT * FROM char_items WHERE guild_storage_id='" + c.Network.Guild.Guildid + "' AND storagetype='3'");
     int itemcount = getstorage.Count();
     PacketWriter Writer = new PacketWriter();
     Writer.Create(OperationCode.SERVER_GUILD_STORAGE3);
     Writer.Byte(c.Network.Guild.StorageSlots);
     Writer.Byte(itemcount);
     if (itemcount != 0)
     {
         using (System.Data.SqlClient.SqlDataReader reader = getstorage.Read())
         {
             while (reader.Read())
             {
                 Item.AddItemPacket(Writer, reader.GetByte(5), reader.GetInt32(2), reader.GetByte(4), reader.GetInt16(6), reader.GetInt32(7), reader.GetInt32(0), reader.GetInt32(9), reader.GetInt32(30));
             }
         }
     }
     getstorage.Close();
     return Writer.GetBytes();
 }
        public static void PrivateItemPacket(PacketWriter Writer, int id, byte max, byte avatar, bool mspawn)
        {
            try
            {
                List<byte> slots = new List<byte>();
                DB ms = new DB("SELECT * FROM char_items WHERE owner='" + id + "' AND slot >= '0' AND slot <= '" + max + "' AND inavatar='" + avatar + "' AND storagetype='0' AND quantity='1'");
                using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                {
                    int count = ms.Count();

                    Writer.Byte(count);

                    while (reader.Read())
                    {
                        byte slot = reader.GetByte(5);
                        byte plusvalue = reader.GetByte(4);
                        int itemid = reader.GetInt32(2);

                        if (itemid != 0)
                        {
                            if (!slots.Exists(delegate (byte bk)
                            {
                                return bk == slot;
                            }))
                            {
                                slots.Add(slot);
                            }
                            Writer.DWord(itemid);
                            Writer.Byte(plusvalue);
                        }
                    }
                }
                ms.Close();
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public static byte[] RankListsActivityHunter()
        {
            DB ms = new DB("SELECT TOP 50 * FROM rank_job_activity WHERE job_type='3'");
            PacketWriter Writer = new PacketWriter();
            int countinfo = ms.Count();
            int i = 0;

            Writer.Create(OperationCode.SERVER_RANK_LISTS);
            Writer.Byte(1);
            Writer.Byte(1);
            Writer.Byte(0);
            Writer.Byte(Convert.ToByte(countinfo));

            using (SqlDataReader reader = ms.Read())
            {
                for (i = 0; i < countinfo; )
                {
                    while (reader.Read())
                    {
                        byte rank = reader.GetByte(2);
                        string name = reader.GetString(3);
                        byte level = reader.GetByte(4);
                        int exp = reader.GetInt32(5);
                        byte title = reader.GetByte(6);

                        Writer.Byte(rank);
                        Writer.Text(name);
                        Writer.Byte(level);
                        Writer.DWord(exp);
                        Writer.Byte(title);
                        i++;
                    }
                }
                ms.Close();
            }
            return Writer.GetBytes();
        }
        public static void LoadBluesid(int idinfo)
        {
            try
            {
                DB ms = new DB("SELECT * FROM char_items WHERE id='"+ idinfo +"'");
                using (SqlDataReader reader = ms.Read())
                {
                    while (reader.Read())
                    {
                        int a = 10;
                        int b = 11;

                        int id = idinfo;
                        ObjData.itemblue it = new ObjData.itemblue();
                        it.blue = new ArrayList();
                        it.blueamount = new ArrayList();
                        it.totalblue = reader.GetInt32(9);

                        for (int d = 0; d < it.totalblue; d++)
                        {
                            it.blue.Add(reader.GetString(a));
                            it.blueamount.Add(reader.GetInt32(b));
                            a += 2;
                            b += 2;
                        }

                        ObjData.Manager.ItemBlue[id] = it;

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Blue error: {0}", ex);
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Character listening packet
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static byte[] CharacterListing(string name)
        {
            DB ms    = new DB("SELECT TOP 4 * FROM character WHERE account='" + name + "'");
            PacketWriter Writer = new PacketWriter();

            Writer.Create(OperationCode.SERVER_CHARACTERSCREEN);      // Select opcode
            Writer.Byte(2);                                     // Static byte 2
            Writer.Byte(1);                                     // Static byte 1
            Writer.Byte(ms.Count());                            // Byte WorldMgr.character count

            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    Writer.DWord(reader.GetInt32(3));           // DWord Skin Model
                    Writer.Text(reader.GetString(2));           // String Name

                    Writer.Byte(reader.GetByte(4));             // Byte Skin Volume
                    Writer.Byte(reader.GetByte(5));             // Byte Level

                    Writer.LWord(reader.GetInt64(12));          // Long Experience

                    Writer.Word(reader.GetInt16(6));            // Word STR
                    Writer.Word(reader.GetInt16(7));            // Word INT
                    Writer.Word(reader.GetInt16(8));            // Attribute points

                    Writer.DWord(reader.GetInt32(9));           // HP
                    Writer.DWord(reader.GetInt32(10));          // MP

                    TimeSpan ts = Convert.ToDateTime(reader.GetDateTime(43)) - DateTime.Now;
                    double time = ts.TotalMinutes;

                    if (Math.Round(time) > 0)
                    {
                        Writer.Byte(1);
                        Writer.DWord(Math.Round(time));
                    }
                    else
                    {
                        Writer.Byte(0);
                    }

                    if (Math.Round(time) < 0 && DateTime.Now != reader.GetDateTime(43))
                    {
                        DB.query("UPDATE character SET deleted='1' Where id='" + reader.GetInt32(0) + "'");
                    }

                    Writer.Word(0);
                    Writer.Byte(0);

                    Function.Items.PrivateItemPacket(Writer, reader.GetInt32(0), 8, 0,false);
                    Function.Items.PrivateItemPacket(Writer, reader.GetInt32(0), 5, 1,false);
                }
                //Jobtype information
                byte jobinfo = Convert.ToByte(DB.GetData("SELECT * FROM users WHERE id='"+ name +"'", "jobtype"));
                Writer.Byte(jobinfo);
            }
            ms.Close();

            return Writer.GetBytes();
        }
 void HandleAttackPet(byte slot, int ItemID)
 {
     try
     {
         //Check if player level is high enough to spawn.
         if (!CheckItemLevel(Character.Information.Level, ItemID))
         {
             client.Send(Packet.MoveItemError(0x6C, 0x18));
         }
         //If ok we continue to spawn the attack pet.
         else
         {
             //Our sql query
             DB ms = new DB("SELECT * FROM pets WHERE pet_itemid='" + ItemID + "' AND playerid='" + Character.Information.CharacterID + "'");
             //Create new pet object.
             WorldMgr.pet_obj o = new WorldMgr.pet_obj();
             //Open our data reader
             using (SqlDataReader reader = ms.Read())
             {
                 //Start reading data from the query above.
                 while (reader.Read())
                 {
                     Character.Attackpet.Uniqueid = reader.GetInt32(11);
                     Character.Attackpet.Spawned = true;
                     o.UniqueID                  = Character.Attackpet.Uniqueid;
                     o.Model                     = ObjData.objectdata.GetItem(ObjData.Manager.ItemBase[ItemID].ObjectName);                            o.Level                     = reader.GetByte(13);
                     o.exp                       = reader.GetInt64(14);
                     o.x = Character.Position.x + Rnd.Next(1, 3);
                     o.z                         = Character.Position.z;
                     o.y = Character.Position.y + Rnd.Next(1, 3);
                     o.xSec                      = Character.Position.xSec;
                     o.ySec                      = Character.Position.ySec;
                     o.OwnerID                   = Character.Information.CharacterID;
                     o.OwnerName                 = Character.Information.Name;
                     o.Walking                   = Character.Position.Walking;
                     o.Petname                   = reader.GetString(3);
                     o.Named                     = 3;
                     o.Run                       = Character.Speed.RunSpeed;
                     o.Walk                      = Character.Speed.WalkSpeed;
                     o.Zerk                      = Character.Speed.BerserkSpeed;
                 }
                 ms.Close();
             }
             //We set our pet active bool, so user cannot spawn multiple.
             Character.Attackpet.Active = true;
             o.Information = true;
             //Set all details above to definitions
             Character.Attackpet.Details = o;
             //Global spawn the pet
             Helpers.Manager.HelperObject.Add(o);
             //Spawn ourselfs
             o.SpawnMe();
             //Send then packet required (Pet information block).
             client.Send(Packet.AttackPetStats(o, slot));
             client.Send(Packet.AttackPetHGP(o));
             //Update pet status to active (For relog purposes).
             DB.query("UPDATE pets SET pet_active='1' WHERE pet_unique='" + Character.Grabpet.Grabpetid + "' AND playerid='" + Character.Information.CharacterID + "'");
         }
     }
     catch (Exception ex)
     {
         Log.Exception(ex);
     }
 }
        /////////////////////////////////////////////////////////////////////////////////
        // Arrow Item Check
        /////////////////////////////////////////////////////////////////////////////////
        bool ItemCheckArrow()
        {
            #region Check Arrow
            DB ms = new DB("SELECT * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' AND slot >= '13' AND slot <= '" + Character.Information.Slots + "' AND inavatar='0' AND itemid='62' AND storagetype='0'");
            if (ms.Count() == 0)
            {
                ms = new DB("SELECT * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' AND slot >= '13' AND slot <= '" + Character.Information.Slots + "' AND inavatar='0' AND itemid='3823' AND storagetype='0'");
            }
            else if (ms.Count() == 0)
            {
                ms = new DB("SELECT * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' AND slot >= '13' AND slot <= '" + Character.Information.Slots + "' AND inavatar='0' AND itemid='10302' AND storagetype='0'");
            }
            else if (ms.Count() == 0)
            {
                ms = new DB("SELECT * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' AND slot >= '13' AND slot <= '" + Character.Information.Slots + "' AND inavatar='0' AND itemid='10487' AND storagetype='0'");
            }
            if (ms.Count() == 0) return false;
            else
            {
                ObjData.slotItem items = null;
                using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                {
                    while (reader.Read())
                        items = ConvertToItem(reader.GetInt32(2), reader.GetByte(5), reader.GetInt16(6), 1);
                }
                ms.Close();
                DB.query("UPDATE char_items SET itemnumber='item" + 7 + "',slot='" + 7 + "' WHERE itemnumber='" + "item" + items.Slot + "' AND owner='" + Character.Information.CharacterID + "' AND itemid='" + items.ID + "'");
                client.Send(Packet.MoveItem(0, items.Slot, 7, items.Amount, 0, "MOVE_INSIDE_INVENTORY"));

                Character.Information.Item.sAmount = items.Amount;
                Character.Information.Item.sID = items.ID;
                ;
                return true;
            }
            #endregion
        }
        /////////////////////////////////////////////////////////////////////////////////
        // Get Slot Item Information
        /////////////////////////////////////////////////////////////////////////////////
        static ObjData.slotItem GetItem(uint id, byte slot, int type)
        {
            #region Slot item info

            try
            {
                if (id != 0)
                {
                    ObjData.slotItem slotItem = new ObjData.slotItem();
                    int row = type;

                    DB ms;
                    if (row == 1)
                    {
                        ms = new DB("SELECT * FROM char_items WHERE itemnumber='item" + slot + "' AND storageacc='" + id + "' AND storagetype='" + row + "' AND slot='" + slot + "'");
                    }
                    else if (row == 3)
                    {
                        ms = new DB("SELECT * FROM char_items WHERE itemnumber='item" + slot + "' AND storagetype='" + row + "' AND slot='" + slot + "'");
                    }
                    else
                    {
                        ms = new DB("SELECT * FROM char_items WHERE itemnumber='item" + slot + "' AND owner='" + id + "' AND storagetype='" + row + "' AND slot='" + slot + "'");
                    }

                    using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                    {
                        while (reader.Read())
                        {
                            slotItem.dbID = reader.GetInt32(0);
                            slotItem.ID = reader.GetInt32(2);
                            slotItem.PlusValue = reader.GetByte(4);
                            slotItem.Amount = reader.GetInt16(6);
                            slotItem.Durability = reader.GetInt32(7);
                            slotItem.Slot = slot;
                            LoadBluesid(slotItem.dbID);
                        }
                    }
                    ms.Close();
                    return slotItem;
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
            return null;
            #endregion
        }
 /////////////////////////////////////////////////////////////////////////////////
 // Get Max Slots
 /////////////////////////////////////////////////////////////////////////////////
 public byte GetFreeSlotMax()
 {
     #region Get Max Slots Available
     List<byte> ListSlot = new List<byte>(Character.Information.Slots);
     DB ms = new DB("SELECT * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' AND slot >= '13' AND slot <= '" + Character.Information.Slots + "' AND inavatar='0'");
     using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
     {
         while (reader.Read())
         {
             ListSlot.Add(reader.GetByte(5));
         }
     }
     ms.Close();
     byte add = 0;
     for (byte i = 13; i < Character.Information.Slots; i++)
     {
         if (!GetCheckFreeSlot(ListSlot, i)) add++;
     }
     return add;
     #endregion
 }
 /////////////////////////////////////////////////////////////////////////////////
 // Get player inventory items (We will use this for stacking and checking).
 // So herer's the simple one ive made we can use to check player inventory
 /////////////////////////////////////////////////////////////////////////////////
 public static List<byte> GetPlayerItems(WorldMgr.character c)
 {
     List<byte> items = new List<byte>();
     DB ms = new DB("SELECT * FROM char_items WHERE owner='" + c.Information.CharacterID + "'");
     using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
     {
         while (reader.Read())
         {
             items.Add(reader.GetByte(5));
         }
     }
     ms.Close();
     return items;
 }
 public static byte GetAmmoSlot(WorldMgr.character ch)
 {
     DB ms = new DB("SELECT * FROM char_items WHERE owner='" + ch.Information.CharacterID + "' AND (itemid='62' OR itemid='3655' OR itemid='10376' OR itemid='10727')");
     byte ammo = 0;
     using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
     {
         while (reader.Read())
         {
             ammo = reader.GetByte(5);
         }
         ms.Close();
         return Convert.ToByte(ammo);
     }
 }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Private Message Send
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void PrivateMessageSend()
        {
            try
            {
                /////////////////////////////////////////////////////////////////////////////////////
                PacketReader Reader = new PacketReader(PacketInformation.buffer);
                /////////////////////////////////////////////////////////////////////////////////////
                short touser = Reader.Int16();
                string tousername = Reader.String(touser);
                short messagel = Reader.Int16();
                string message = Reader.String(messagel);
                Reader.Close();
                /////////////////////////////////////////////////////////////////////////////////////
                int count = DB.GetRowsCount("SELECT * FROM character WHERE name='" + tousername + "'");
                int charid = Convert.ToInt32(Convert.ToInt32(DB.GetData("SELECT id FROM character WHERE name='" + tousername + "'", "id")));
                /////////////////////////////////////////////////////////////////////////////////////
                if (count == 0)//Check username
                {
                    client.Send(Packet.PrivateMessageMsg(1));
                    return;
                }
                if (count > 0)//Excisting username
                {
                    DB ms = new DB("SELECT * FROM message WHERE receiver='" + tousername + "' ORDER BY idinfo DESC");
                    int msgcount = ms.Count();
                    PlayerMgr sys = Helpers.GetInformation.GetPlayerMainid(charid);

                    if (msgcount == 0)
                    {
                        DB.query("INSERT INTO message (sender, receiver, message, status, time, idinfo) VALUES ('" + Character.Information.Name + "','" + tousername + "','" + message + "','0','10/10/2010 12:00:00 AM','0')");
                        client.Send(Packet.PrivateMessageMsg(2));
                        sys.Send(Packet.FriendData(sys.Character.Information.UniqueID, 5, tousername, Character, false));
                    }
                    if (msgcount > 0)
                    {
                        int messagenewid = msgcount + 1;
                        DB.query("INSERT INTO message (sender, receiver, message, status, time, idinfo) VALUES ('" + Character.Information.Name + "','" + tousername + "','" + message + "','0','10/10/2010 12:00:00 AM','" + Convert.ToByte(messagenewid) + "')");
                        client.Send(Packet.PrivateMessageMsg(2));
                        sys.Send(Packet.FriendData(sys.Character.Information.UniqueID, 5, tousername, Character, false));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public void LoginScreen()
        {
            //Wrap our function inside a catcher
            try
            {
                //Open our packet reader
                PacketReader Reader = new PacketReader(PacketInformation.buffer);
                string name = Reader.Text();
                Reader.Close();
                //Anti hack checking sql query
                DB ms = new DB("SELECT name FROM character WHERE account='" + Player.AccountName + "' AND name='" + name + "'");
                //Check if the player account and character belongs together (count row).
                int checkinfo = ms.Count();
                //If there's no result
                if (checkinfo == 0)
                {
                    //Disconnect the user if hack attempt
                    client.Disconnect(client.clientSocket);
                    return;
                }
                //If there's a result we continue loading
                else
                {

                    //Create new character definition details
                    Character = new WorldMgr.character();
                    //Set character name
                    Character.Information.Name = name;
                    //Set player id
                    Character.Account.ID = Player.ID;
                    //Load player data
                    PlayerDataLoad();
                    Character.Ids = new GenerateUniqueID(Character.Information.CharacterID, GenerateUniqueID.IDS.Player);
                    Character.Information.UniqueID = Character.Ids.GetUniqueID;

                    //Load job data
                    LoadJobData();
                    //Check same character
                    checkSameChar(name, Character.Information.UniqueID);
                    //Check character stats
                    CheckCharStats(Character);
                    //Add new cient
                    lock (Helpers.Manager.clients)
                    {
                        Helpers.Manager.clients.Add(this);
                    }
                    //Send packets required
                    client.Send(Packet.LoginScreen());
                    client.Send(Packet.StartPlayerLoad());
                    client.Send(Packet.CharacterDataLoad(Character));
                    client.Send(Packet.EndPlayerLoad());
                    client.Send(Packet.PlayerUnknowPack(Character.Information.UniqueID));
                    //client.Send(Packet.UnknownPacket()); //need to research this packet.
                    //Update online status
                    DB.query("UPDATE character SET online='1' WHERE id='" + Character.Information.CharacterID + "'");
                    //Update server information
                    Brain.ServerMgr.UpdateServerInfo();
                    //Open our timer
                    OpenTimer();
                    Character.InitializeCharacter();
                    //Load blue data
                    LoadBlues(Character);

                    #region equipped items
                    List<ObjData.slotItem> EquipedItems = new List<ObjData.slotItem>();
                    //For each equiped item add
                    for (byte q = 0; q < 13; q++)
                    {
                        EquipedItems.Add(GetItem((uint)Character.Information.CharacterID, q, 0));
                    }
                    /*
                    //Load default values for resistance
                    #region Load Resistance default values
                    Character.Blues.Luck = 100;
                    Character.Blues.Resist_All = 100;
                    Character.Blues.Resist_Burn = 100;
                    Character.Blues.Resist_CSMP = 100;
                    Character.Blues.Resist_Disease = 100;
                    Character.Blues.Resist_Eshock = 100;
                    Character.Blues.Resist_Fear = 100;
                    Character.Blues.Resist_Frostbite = 100;
                    Character.Blues.Resist_Poison = 100;
                    Character.Blues.Resist_Sleep = 100;
                    Character.Blues.Resist_Stun = 100;
                    Character.Blues.Resist_Zombie = 100;
                    Character.Blues.MonsterIgnorance = 100;
                    Character.Blues.UniqueDMGInc = 100;
                    #endregion
                    */
                    //Load blues for each item
                    foreach (ObjData.slotItem sitem in EquipedItems)
                    {
                        if (ObjData.Manager.ItemBlue.ContainsKey(sitem.dbID))
                        {
                            LoadBluesid(sitem.dbID);
                            if (ObjData.Manager.ItemBlue[sitem.dbID].totalblue != 0)
                                AddRemoveBlues(this, sitem, true);
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public static byte[] UnionInfo(PlayerMgr c)
        {
            PacketWriter Writer = new PacketWriter();
            Writer.Create(OperationCode.SERVER_GUILD_UNIONS);
            Writer.DWord(c.Character.Information.CharacterID);                // Union owner id
            Writer.DWord(7);                                                  // ??Need to check
            Writer.DWord(c.Character.Network.Guild.Guildid);                  // Union leader guild id
            Writer.Byte(c.Character.Network.Guild.Unions.Count);              // Count guilds in union
            //Get all guilds details
            foreach (int guild in c.Character.Network.Guild.Unions)
            {
                //Load other guild data
                DB guild_data = new DB("SELECT * FROM guild WHERE id='" + guild + "'");

                using (System.Data.SqlClient.SqlDataReader reader = guild_data.Read())
                {
                    while (reader.Read())
                    {
                        string Guildname = reader.GetString(1);
                        byte Guildlevel = reader.GetByte(2);
                        byte Guildmembercount = reader.GetByte(6);
                        int Ownerid = reader.GetInt32(9);

                        string Charname = DB.GetData("SELECT name FROM character WHERE id='" + Ownerid + "'", "name").ToString();
                        int Charmodel = Convert.ToInt32(DB.GetData("SELECT chartype FROM character WHERE id='" + Ownerid + "'", "chartype"));

                        Writer.DWord(guild);                        //Guild ID
                        Writer.Text(reader.GetString(1));           //Guildname
                        Writer.Byte(reader.GetByte(2));             //Guildlevel
                        Writer.Text(Charname);                      //Ownername
                        Writer.DWord(Charmodel);                    //Owner model
                        Writer.Byte(reader.GetByte(6));             //Guild member count

                        //Get guild details
                        PlayerMgr Guildmembers =Helpers.GetInformation.GetGuildPlayer(guild);
                        //Add clients that are online to union list
                        //Null check
                        if (Guildmembers != null)
                        {
                            foreach (int member in Guildmembers.Character.Network.Guild.Members)
                            {
                                //make sure member isnt 0
                                if (member != 0)
                                {
                                    //Get player details
                                    PlayerMgr getmember = Helpers.GetInformation.GetPlayerMainid(member);
                                    //Make sure that the player is there
                                    if (getmember != null)
                                    {
                                        //Add client to union list
                                        c.Character.Network.Guild.UnionMembers.Add(getmember.Character.Information.CharacterID);
                                        //Add to member
                                        if (c.Character.Information.CharacterID != getmember.Character.Information.CharacterID)
                                        {
                                            getmember.Character.Network.Guild.UnionMembers.Add(c.Character.Information.CharacterID);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return Writer.GetBytes();
        }
        //###########################################################################################
        // Reverse scrolls
        //###########################################################################################
        void HandleReverse(int itemid ,byte select, int objectlocationid)
        {
            try
            {
                //Remove loc // add loc to case 7
                if (Character.Position.Walking) return;
                if (Character.Action.PickUping) return;
                if (Character.Stall.Stallactive) return;
                Character.Information.Scroll = true;
                switch (select)
                {
                    //Move to return point
                    case 2:
                        Send(Packet.StatePack(Character.Information.UniqueID, 0x0B, 0x01, false));
                        StartScrollTimer(1000);
                        SavePlayerReturn();
                        break;
                    //Move to last recall point
                    case 3:
                        DB ms = new DB("SELECT * FROM character_rev WHERE charname='" + Character.Information.Name + "'");
                        using (SqlDataReader reader = ms.Read())
                        {
                            while (reader.Read())
                            {
                                byte xSec = reader.GetByte(2);
                                byte ySec = reader.GetByte(3);
                                float x = reader.GetInt32(4);
                                float z = reader.GetInt32(6);
                                float y = reader.GetInt32(5);

                                BuffAllClose();
                                DeSpawnMe();
                                ObjectDeSpawnCheck();
                                client.Send(Packet.TeleportOtherStart());

                                Character.Position.xSec = xSec;
                                Character.Position.ySec = ySec;
                                Character.Position.x = x;
                                Character.Position.z = z;
                                Character.Position.y = y;

                                UpdateXY();

                                client.Send(Packet.TeleportImage(xSec, ySec));
                                Character.Teleport = true;
                                Timer.Scroll = null;
                                Character.Information.Scroll = false;
                            }
                        }
                        ms.Close();
                        break;
                    //Teleport to map location
                    case 7:
                        BuffAllClose();
                        DeSpawnMe();
                        ObjectDeSpawnCheck();
                        client.Send(Packet.TeleportOtherStart());
                        //TODO: Check formula coords are a bit off
                        Character.Position.xSec = ObjData.Manager.ReverseData[objectlocationid].xSec;
                        Character.Position.ySec = ObjData.Manager.ReverseData[objectlocationid].ySec;
                        Character.Position.x = (float)ObjData.Manager.ReverseData[objectlocationid].x;
                        Character.Position.z = (float)ObjData.Manager.ReverseData[objectlocationid].z;
                        Character.Position.y = (float)ObjData.Manager.ReverseData[objectlocationid].y;

                        UpdateXY();

                        client.Send(Packet.TeleportImage(ObjData.Manager.ReverseData[objectlocationid].xSec, ObjData.Manager.ReverseData[objectlocationid].ySec));
                        Character.Teleport = true;
                        Timer.Scroll = null;
                        Character.Information.Scroll = false;
                        break;
                }
            }

            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        // Add items to database
        /////////////////////////////////////////////////////////////////////////////////
        public void AddItem(int itemid, short prob, byte slot, int id, int modelid)
        {
            #region Add item to db
            try
            {
                if (ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.BLADE ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.CH_SHIELD ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_SHIELD ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.BOW ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_AXE ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_CROSSBOW ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_DAGGER ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_DARKSTAFF ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_HARP ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_STAFF ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_SWORD ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_TSTAFF ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EU_TSWORD ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.GLAVIE ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.SPEAR ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.SWORD ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.EARRING ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.RING ||
                     ObjData.Manager.ItemBase[itemid].Itemtype == ObjData.item_database.ItemType.NECKLACE ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.ARMOR ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.GARMENT ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.GM ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.HEAVY ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.LIGHT ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.PROTECTOR ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.AVATAR ||
                     ObjData.Manager.ItemBase[itemid].Type == ObjData.item_database.ArmorType.ROBE)
                {
                    DB.query("Insert Into char_items (itemid,plusvalue,durability,owner,itemnumber,slot,storageacc,inavatar,storagetype,charbound) VALUES ('" + itemid + "','" + prob + "','" + ObjData.Manager.ItemBase[itemid].Defans.Durability + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','0','0','" + ObjData.Manager.ItemBase[itemid].SoulBound + "' )");

                }
                else if (ObjData.Manager.ItemBase[itemid].Pettype == ObjData.item_database.PetType.GRABPET)
                {
                    DB.query("Insert Into char_items (itemid,plusvalue,durability,owner,itemnumber,slot,storageacc,charbound) VALUES ('" + itemid + "','" + 0 + "','" + ObjData.Manager.ItemBase[itemid].Defans.Durability + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','" + ObjData.Manager.ItemBase[itemid].SoulBound + "')");

                    DB ms = new DB("SELECT TOP 1 * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' ORDER BY id DESC");
                    using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                    {
                        while (reader.Read())
                        {
                            int idinfo = reader.GetInt32(0);
                            DB.query("Insert Into pets (playerid, pet_type, pet_name, pet_state, pet_itemid, pet_active, pet_check, pet_unique) VALUES ('" + Character.Information.CharacterID + "','4','No name','1','" + itemid + "','0','item" + slot + "','" + idinfo + "')");
                        }
                    }
                    ms.Close();
                }
                else if (ObjData.Manager.ItemBase[itemid].Pettype == ObjData.item_database.PetType.ATTACKPET)
                {
                    DB.query("Insert Into char_items (itemid,plusvalue,durability,owner,itemnumber,slot,storageacc,charbound) VALUES ('" + itemid + "','" + 0 + "','" + ObjData.Manager.ItemBase[itemid].Defans.Durability + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','" + ObjData.Manager.ItemBase[itemid].SoulBound + "')");

                    DB ms = new DB("SELECT TOP 1 * FROM char_items WHERE owner='" + Character.Information.CharacterID + "' ORDER BY id DESC");
                    using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
                    {
                        while (reader.Read())
                        {
                            int idinfo = reader.GetInt32(0);
                            DB.query("Insert Into pets (playerid, pet_type, pet_name, pet_state, pet_itemid, pet_active, pet_check, pet_unique) VALUES ('" + Character.Information.CharacterID + "','2','No name','1','" + itemid + "','0','item" + slot + "','" + idinfo + "')");
                        }
                    }
                    ms.Close();
                }
                else if (ObjData.Manager.ItemBase[itemid].Etctype == ObjData.item_database.EtcType.STONES)
                {
                    DB.query("Insert Into char_items (itemid,plusvalue,durability,owner,itemnumber,slot,storageacc,inavatar,storagetype,charbound) VALUES ('" + itemid + "','" + 0 + "','" + 0 + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','0','0','" + ObjData.Manager.ItemBase[itemid].SoulBound + "' )");
                }
                else if (ObjData.Manager.ItemBase[itemid].Etctype == ObjData.item_database.EtcType.MONSTERMASK)
                {
                    DB.query("Insert Into char_items (itemid,plusvalue,durability,owner,itemnumber,slot,storageacc,modelid,charbound) VALUES ('" + itemid + "','" + 0 + "','" + 0 + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','" + modelid + "','" + ObjData.Manager.ItemBase[itemid].SoulBound + "' )");
                }
                else
                {
                    if (prob < 2) prob = 1;
                    DB.query("Insert Into char_items (itemid,quantity,owner,itemnumber,slot,storageacc,charbound) VALUES ('" + itemid + "','" + prob + "','" + id + "','item" + slot + "','" + slot + "','" + Player.ID + "','" + ObjData.Manager.ItemBase[itemid].SoulBound + "' )");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DB Item add error: ", ex);
            }
            #endregion
        }
        public static byte[] CharacterDataLoad(WorldMgr.character c)
        {
            PacketWriter Writer = new PacketWriter();
            Writer.Create(OperationCode.SERVER_PLAYERDATA);
            /////////////////////////////////////////////////////// Character basic info
            #region Basic info
            Writer.DWord(c.Ids.GetLoginID);
            Writer.DWord(c.Information.Model);
            Writer.Byte(c.Information.Volume);
            Writer.Byte(c.Information.Level);
            Writer.Byte(c.Information.Level);
            Writer.LWord(c.Information.XP);
            Writer.DWord(c.Information.SpBar);
            Writer.LWord(c.Information.Gold);
            Writer.DWord(c.Information.SkillPoint);
            Writer.Word(c.Information.Attributes);
            Writer.Byte(c.Information.BerserkBar);
            Writer.DWord(988);
            Writer.DWord(c.Stat.SecondHp);
            Writer.DWord(c.Stat.SecondMP);
            Writer.Bool(c.Information.Level < 20 ? true : false);
            #endregion
            /////////////////////////////////////////////////////// Character Player Kill Info
            #region Pk information
            //DB perfection reading with multiple data adapters... while this one is open i can still read anything else from the database
            //With no speed reduction...
            DB checkpk = new DB("SELECT * FROM character WHERE name ='" + c.Information.Name + "'");
            using (System.Data.SqlClient.SqlDataReader getinfo = checkpk.Read())
            {
                while (getinfo.Read())
                {
                    byte dailypk = getinfo.GetByte(48);
                    byte pklevel = getinfo.GetByte(49);
                    byte murderlevel = getinfo.GetByte(50);

                    Writer.Byte(dailypk);
                    Writer.Word(pklevel);
                    Writer.DWord(murderlevel);
                    if (murderlevel != 0) c.Information.Murderer = true;
                }
            }
            #endregion
            /////////////////////////////////////////////////////// Character Title
            #region Title
            Writer.Byte(c.Information.Title);
            #endregion
            /////////////////////////////////////////////////////// Character Pvpstate
            #region Pvp
            Writer.Byte(c.Information.Pvpstate);
            if (c.Information.Pvpstate > 0)
                c.Information.PvP = true;
            #endregion
            /////////////////////////////////////////////////////// Character Items
            #region Item

            Writer.Byte(c.Information.Slots);

            DB ms = new DB("SELECT * FROM char_items WHERE owner='" + c.Information.CharacterID + "' AND slot >= '0' AND slot <= '" + c.Information.Slots + "' AND inavatar='0' AND storagetype='0'");
            Writer.Byte(ms.Count());
            using (System.Data.SqlClient.SqlDataReader msreader = ms.Read())
            {

                while (msreader.Read())
                {

                    short amount = msreader.GetInt16(6);

                    if (amount < 1) amount = 1;
                    DB.query("UPDATE char_items SET quantity='" + amount + "' WHERE owner='" + c.Information.CharacterID + "' AND itemid='" + msreader.GetInt32(2) + "' AND id='" + msreader.GetInt32(0) + "' AND storagetype='0'");

                    if (msreader.GetByte(5) == 6)
                        c.Information.Item.wID = Convert.ToInt32(msreader.GetInt32(2));
                    if (msreader.GetByte(5) == 7)
                    {
                        c.Information.Item.sID = msreader.GetInt32(2);
                        c.Information.Item.sAmount = msreader.GetInt16(6);
                    }

                    Item.AddItemPacket(Writer, msreader.GetByte(5), msreader.GetInt32(2), msreader.GetByte(4), amount, msreader.GetInt32(7),msreader.GetInt32(0), msreader.GetInt32(9), msreader.GetInt32(30));
                }
            }
            ms.Close();

            //Avatar
            Writer.Byte(5);

            ms = new DB("SELECT * FROM char_items WHERE owner='" + c.Information.CharacterID + "' AND slot >= '0' AND slot <= '" + c.Information.Slots + "' AND inavatar='1' AND storagetype='0'");

            Writer.Byte(ms.Count());
            using (System.Data.SqlClient.SqlDataReader msreader = ms.Read())
            {
                while (msreader.Read())
                {
                    Item.AddItemPacket(Writer, msreader.GetByte(5), msreader.GetInt32(2), msreader.GetByte(4), msreader.GetInt16(6), msreader.GetInt32(7), msreader.GetInt32(0), msreader.GetInt32(9),msreader.GetInt32(30));
                }
            }
            ms.Close();

            Writer.Byte(0);

            // job mastery
            Writer.Byte(0x0B);
            Writer.Byte(0);
            Writer.Byte(0);

            #endregion
            ///////////////////////////////////////////////////////  Mastery
            #region Mastery
            if (c.Information.Model <= 12000)
            {
                for (byte i = 1; i <= 7; i++)
                {
                    Writer.Byte(1);
                    Writer.DWord(c.Stat.Skill.Mastery[i]);
                    Writer.Byte(c.Stat.Skill.Mastery_Level[i]);
                }
            }
            else
            {
                if (c.Information.Model >= 14000)
                {
                    for (byte i = 1; i < 7; i++)
                    {
                        Writer.Byte(1);
                        Writer.DWord(c.Stat.Skill.Mastery[i]);
                        Writer.Byte(c.Stat.Skill.Mastery_Level[i]);
                    }
                }
            }
            #endregion
            /////////////////////////////////////////////////////// Skills
            #region Skill
            Writer.Byte(2);
            Writer.Byte(0);
                for (int i = 1; i <= c.Stat.Skill.AmountSkill; i++)
                {
                    Writer.Byte(1);
                    Writer.DWord(c.Stat.Skill.Skill[i]);
                    Writer.Byte(1);
                }
            Writer.Byte(2);
            #endregion
            /////////////////////////////////////////////////////// Quests
            #region Quest
            Writer.Word(1); // how many Quest ids completed/aborted
            Writer.DWord(1);// Quest id
            Writer.Byte(0);//number of Quests that are live
            #endregion
            Writer.Byte(0);//? for now
            /////////////////////////////////////////////////////// Talisman
            #region Talisman
            Writer.DWord(1);//new
            Writer.DWord(1);//new
            Writer.DWord(0);//? for now
            Writer.DWord(0x0C);//new
            #endregion
            /////////////////////////////////////////////////////// Position + id + speed
            #region Character id / Position / Speed
            Writer.DWord(c.Information.UniqueID);
            Writer.Byte(c.Position.xSec);
            Writer.Byte(c.Position.ySec);
            if (!FileDB.CheckCave(c.Position.xSec, c.Position.ySec))
            {
                Writer.Float(Formule.packetx(c.Position.x, c.Position.xSec));
                Writer.Float(c.Position.z);
                Writer.Float(Formule.packety(c.Position.y, c.Position.ySec));
            }
            else
            {
                Writer.Float(Formule.cavepacketx(c.Position.x));// Added for cave Coords
                Writer.Float(c.Position.z);
                Writer.Float(Formule.cavepackety(c.Position.y));// Added for cave Coords

            }
            Writer.Word(0);							// Angle
            Writer.Byte(0);
            Writer.Byte(1);
            Writer.Byte(0);
            Writer.Word(0);							// Angle
            Writer.Word(0);
            Writer.Byte(0);
            Writer.Bool(false); //berserk

            Writer.Byte(0);//new ?

            Writer.Float(c.Speed.WalkSpeed);
            Writer.Float(c.Speed.RunSpeed);
            Writer.Float(c.Speed.BerserkSpeed);
            #endregion
            /////////////////////////////////////////////////////// Premium Tickets
            #region Premium ticket
            Writer.Byte(0); //ITEM_MALL_GOLD_TIME_SERVICE_TICKET_4W
            #endregion
            /////////////////////////////////////////////////////// GM Check + Name
            #region GM Check + Name
            Writer.Text(c.Information.Name);
            #endregion
            /////////////////////////////////////////////////////// Character jobs
            #region Character Job / hunter thief trader ( old job things )
                //Writer info with job name when on job
                /*if (c.Job.state == 1 && c.Job.Jobname != "0")
                {
                    Writer.Text(c.Job.Jobname);
                    Writer.Byte(3);
                    Writer.Byte(1);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                }
                //Write basic info noname
                if (c.Job.Jobname == "0")
                {
                    Writer.Word(0);
                    Writer.Byte(3);
                    Writer.Byte(1);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                }
                //Write no info
                else
                {
                    Writer.Word(0);
                    Writer.Byte(0);
                    Writer.Byte(1);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.DWord(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                    Writer.Byte(0);
                }*/
            #endregion
            #region New job system
            if (c.Job.state == 1)
            {
                Writer.Text(c.Job.Jobname);
                Writer.Byte(1);
                Writer.Byte(c.Job.level);//Level job
                Writer.Byte(c.Information.Level);//Level char
                Writer.Byte(1); // job level? myb
                Writer.LWord(0);// job exp probably y
                Writer.Byte(0);
                Writer.Byte(0);
                Writer.Byte(0);
                Writer.Byte(0);
            }
            else
            {

                Writer.Word(0);
                Writer.Byte(0);
                Writer.Byte(0);
                Writer.Byte(2); // job type
                Writer.Byte(1); // job level? myb
                Writer.LWord(0);// job exp probably y
                Writer.Byte(0);
                Writer.Byte(0);
                Writer.Byte(0);
                Writer.Byte(0);
            }

            #endregion
                /////////////////////////////////////////////////////// Pvp / Pk State
            #region Pvp / Pk State
            if (c.Information.Pvpstate == 1 || c.Information.Murderer)
            {
                Writer.Byte(0x22);
            }
            else if (c.Information.Pvpstate == 0 || !c.Information.Murderer)
            {
                Writer.Byte(0xFF);
            }
            #endregion
            /////////////////////////////////////////////////////// Guide Data
            #region Guide Data this data stacks on itself so if guide id is 0400000000000000 and next guide is 0300000000000000 the data to send is 0700000000000000

            for (int i = 0; i < 8; ++i)//Main Guide Packet Info
            {
                Writer.Byte(c.Guideinfo.G1[i]);//Reads From Int Array
            }
            #endregion
            /////////////////////////////////////////////////////// Account / Gm Check
            #region Account ID + Gm Check
            Writer.DWord(c.Account.ID);
            Writer.Byte(c.Information.GM);
            #endregion
            /////////////////////////////////////////////////////// Quickbar + Autopotion
            #region Bar information
            Writer.Byte(7);
            PlayerQuickBar(Writer, c.Information.CharacterID);
            PlayerAutoPot(Writer, c.Information.CharacterID);
            #endregion
            /////////////////////////////////////////////////////// Academy
            #region Academy
            Writer.Byte(0); // number of player in academy
            /* // if we have players there
             Writer.Byte(1);
             Writer.Text("asd");
             */
            Writer.Byte(0);//added byte today for 1.310
            Writer.Byte(0);
            Writer.Word(1);
            Writer.Word(1);
            Writer.Byte(0);
            Writer.Byte(1);
            #endregion
            return Writer.GetBytes();
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Job Ranks
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void RankList()
        {
            try
            {
                PacketReader Reader = new PacketReader(PacketInformation.buffer);
                /////////////////////////////////////////////////////////////////////////////////////
                int Notneeded = Reader.Int32();
                byte Type = Reader.Byte();
                byte Choice = Reader.Byte();
                /////////////////////////////////////////////////////////////////////////////////////

                /////////////////////////////////////////////////////////////////////////////////////
                // Rank Trader
                /////////////////////////////////////////////////////////////////////////////////////
                if (Type == 1)
                {
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank Trader Merchant Activity
                    /////////////////////////////////////////////////////////////////////////////////////
                    if (Choice == 0)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_activity WHERE job_type='1'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsActivityTrader());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank Trader Weekly Donation
                    /////////////////////////////////////////////////////////////////////////////////////
                    else if (Choice == 1)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_donate WHERE job_type='1'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsDonateTrader());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                }
                /////////////////////////////////////////////////////////////////////////////////////
                // Rank List Thief
                /////////////////////////////////////////////////////////////////////////////////////
                else if (Type == 3)
                {
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank List Thief Activity
                    /////////////////////////////////////////////////////////////////////////////////////
                    if (Choice == 0)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_activity WHERE job_type='3'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsActivityThief());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank List Thief Weekly Donation
                    /////////////////////////////////////////////////////////////////////////////////////
                    else if (Choice == 1)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_donate WHERE job_type='2'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsDonateThief());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                }
                /////////////////////////////////////////////////////////////////////////////////////
                // Rank List Hunter
                /////////////////////////////////////////////////////////////////////////////////////
                else if (Type == 2) //Type 2 = Hunter
                {
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank List Hunter Activity
                    /////////////////////////////////////////////////////////////////////////////////////
                    if (Choice == 0)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_activity WHERE job_type='2'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsActivityHunter());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                    /////////////////////////////////////////////////////////////////////////////////////
                    // Rank List Hunter Weekly Contribution
                    /////////////////////////////////////////////////////////////////////////////////////
                    else if (Choice == 1)
                    {
                        try
                        {
                            DB readsql = new DB("SELECT TOP 50 * FROM rank_job_donate WHERE job_type='3'");
                            int begin = 0;
                            int count = readsql.Count();
                            using (SqlDataReader readinfo = readsql.Read())
                            {
                                while (readinfo.Read())
                                {
                                    for (begin = 0; begin < count; )
                                    {
                                        begin++;
                                        if (begin.Equals(count))
                                        {
                                            client.Send(Packet.RankListsDonateHunter());
                                            return;
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public static byte[] SendFriendList(byte count, WorldMgr.character c)
        {
            PacketWriter Writer = new PacketWriter();
            Writer.Create(OperationCode.SERVER_SEND_FRIEND_LIST);
            //Groups
            DB ms = new DB("SELECT * FROM friends_groups WHERE playerid='"+ c.Information.CharacterID  +"'");
            int groupcount = ms.Count();
            groupcount = groupcount + 1;
            Writer.Byte((byte)groupcount);

            Writer.Word(0);
            Writer.Text("NonClassified");
            int groupid = 0;
            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    string groupname = reader.GetString(2);
                    groupid = groupid +1;
                    Writer.Word((short)groupid);
                    Writer.Text(groupname);
                }
            }
            //Friends
            Writer.Byte(count);
            ms = new DB("SELECT * FROM friends WHERE owner='"+ c.Information.CharacterID  +"'");
            using (System.Data.SqlClient.SqlDataReader reader = ms.Read())
            {
                while (reader.Read())
                {
                    int model       = Convert.ToInt32(DB.GetData("SELECT * FROM character WHERE id='" + reader.GetInt32(2) + "'", "chartype"));
                    int status      = Convert.ToInt32(DB.GetData("SELECT * FROM character WHERE id='" + reader.GetInt32(2) + "'", "Online"));
                    int charid = Convert.ToInt32(DB.GetData("SELECT * FROM character WHERE id='" + reader.GetInt32(2) + "'", "id"));
                    string charname = DB.GetData("SELECT * FROM character WHERE id='" + reader.GetInt32(2) + "'", "name");
                    string groupname = reader.GetString(4);

                    Writer.DWord(charid);               // Friend CharID
                    Writer.Text(charname);              // Friend Name
                    Writer.DWord(model);                // Friend Model Type
                    if (groupname == "none")
                        Writer.Word(0);
                    else
                        Writer.Word(groupid);

                    if (status != 0)
                    {
                        Writer.Byte(0);                 // Friend is online
                    }
                    else
                    {
                        Writer.Byte(1);                 // Inverted, Friend is offline
                    }
                }
                reader.Close();
            }
            ms.Close();
            return Writer.GetBytes();
        }