Example #1
0
        public static void ReadArmAnimation(NetState ns, PacketReader packetReader)
        {
            int eid = packetReader.ReadInt32();
            byte animation = packetReader.ReadByte();

            // Send this to near players
        }
Example #2
0
        public void ProcessAccept(SocketAsyncEventArgs e)
        {
            try
            {
                NetState ns = new NetState();
                ns.Connection = e.AcceptSocket;
                ns.Connection.Blocking = false;
                //ns.Connection.NoDelay = true;
                ns.Owner = new Player();
                ns.Owner.State = ns;
                CXMineServer.Server.AddPlayer(ns.Owner);
                ns.Start();

                StartAccept(e);
            }
            catch (System.Exception ex)
            {
                CXMineServer.Log(ex.Message);
                Console.ReadLine();
            }
        }
Example #3
0
        public static void ReadDisconnect(NetState ns, PacketReader packetReader)
        {
            short length = packetReader.ReadInt16();

            if (packetReader.Index + length <= packetReader.Size)
            {
                string message = packetReader.ReadString(length);
            }
            else
            {
                packetReader.Failed = true;
                return;
            }

            CXMineServer.Log("Disposing " + ns.Owner.EntityID);
            ns.Dispose();
        }
Example #4
0
 public static void ReadCloseWindow(NetState ns, PacketReader packetReader)
 {
     byte id = packetReader.ReadByte();
 }
Example #5
0
        public static void ReadWindowClick(NetState ns, PacketReader packetReader)
        {
            byte windowId = packetReader.ReadByte();
            short slot = packetReader.ReadInt16();
            bool rightClick = packetReader.ReadBool();
            short actionNumber = packetReader.ReadInt16();
            short itemId = packetReader.ReadInt16();

            if(itemId != -1)
            {
                byte itemCount = 0;
                short itemUses = 0;
                if (packetReader.Index + 3 <= packetReader.Size)
                {
                    itemCount = packetReader.ReadByte();
                    itemUses = packetReader.ReadInt16();
                }
                else
                {
                    packetReader.Failed = true;
                    return;
                }

                // We need security controls, like controlling if we own the item, if the count and uses is correct or not, for now we say it's always ok
                ns.Transaction(windowId, actionNumber, true);

                if(windowId == 0)
                {
                    if(rightClick)
                    {
                        if(ns.Owner.MouseHoldingItem == null)
                        {
                            Inventory.Slot item = ns.Owner.inventory.GetItem(slot);
                            int prevCount = ns.Owner.inventory.GetItem(slot).Count;
                            int newCount = ns.Owner.inventory.GetItem(slot).Count = (short)(ns.Owner.inventory.GetItem(slot).Count > 1 ? ns.Owner.inventory.GetItem(slot).Count / 2 : 1);

                            ns.Owner.MouseHoldingItem = new Item();
                            ns.Owner.MouseHoldingItem.Count = prevCount - newCount;
                            ns.Owner.MouseHoldingItem.Uses = item.Uses;
                        }
                        else
                        {
                            Inventory.Slot item = ns.Owner.inventory.GetItem(slot);
                            if (item.Id == ns.Owner.MouseHoldingItem.Type && item.Count < 64)
                            {
                                ++item.Count;
                                --ns.Owner.MouseHoldingItem.Count;
                            }
                        }
                    }
                    else
                    {
                        Inventory.Slot item = ns.Owner.inventory.GetItem(slot);

                        if(item == null)
                            ns.Owner.inventory.AddToPosition(slot, itemId, itemCount, itemUses, false);

                        else if(ns.Owner.MouseHoldingItem == null)
                        {
                            ns.Owner.MouseHoldingItem = new Item();
                            ns.Owner.MouseHoldingItem.Count = item.Count;
                            ns.Owner.MouseHoldingItem.Uses = item.Uses;

                            item.Count = 0;
                        }
                        else if (item.Count < 64)
                        {
                            int stillHolding = ns.Owner.MouseHoldingItem.Count - (64 - item.Count);

                            int added = ns.Owner.MouseHoldingItem.Count - stillHolding;

                            if (stillHolding == 0)
                                ns.Owner.MouseHoldingItem = null;

                            item.Count += (short)added;
                        }
                    }
                }
            }
            else if(ns.Owner.MouseHoldingItem != null)
            {
                ns.Owner.inventory.AddToPosition(slot, (short)ns.Owner.MouseHoldingItem.Type, (short)ns.Owner.MouseHoldingItem.Count, (short)ns.Owner.MouseHoldingItem.Uses, false);
                ns.Owner.MouseHoldingItem = null;
            }
        }
Example #6
0
        public static void ReadPlayerPositionLook(NetState ns, PacketReader packetReader)
        {
            double x = packetReader.ReadDouble();
            double stance = packetReader.ReadDouble();
            double y = packetReader.ReadDouble();
            double z = packetReader.ReadDouble();

            float yaw = packetReader.ReadFloat();
            float pitch = packetReader.ReadFloat();

            ns.Owner.Yaw = yaw;
            ns.Owner.Pitch = pitch;

            ns.Owner.X = x;
            ns.Owner.Y = y;
            ns.Owner.Z = z;

            bool onGround = packetReader.ReadBool();

            ns.Owner.Moved = true;
        }
Example #7
0
 public static void ReadKeepAlive(NetState ns, PacketReader packetReader)
 {
     ns.KeepAlive();
 }
Example #8
0
        public static void ReadPlayerInventory(NetState ns, PacketReader packetReader)
        {
            int entityId = packetReader.ReadInt32();
            short slot = packetReader.ReadInt16();
            short itemId = packetReader.ReadInt16();
            short damage = packetReader.ReadInt16();

            if (slot < 0 || slot > 44)
                return;

            CXMineServer.Log("Received Player Inventory Packet");
        }
Example #9
0
        public static void ReadPlayerDigging(NetState ns, PacketReader packetReader)
        {
            int status = packetReader.ReadByte();

            int x = packetReader.ReadInt32();
            byte y = packetReader.ReadByte();
            int z = packetReader.ReadInt32();

            byte face = packetReader.ReadByte();

            if(status == 3)
                Chunk.DestroyBlock(ns, x, y, z, face);
        }
Example #10
0
        public static void ReadPlayerHolding(NetState ns, PacketReader packetReader)
        {
            int slotId = packetReader.ReadInt16();

            ns.Owner.inventory.HoldingPos = slotId;
        }
Example #11
0
        public static void ReadPlayerBlockPlace(NetState ns, PacketReader packetReader)
        {
            int x = packetReader.ReadInt32();
            byte y = packetReader.ReadByte();
            int z = packetReader.ReadInt32();

            byte direction = packetReader.ReadByte();
            int id;

            byte amount;
            short damage;

            if((id = packetReader.ReadInt16()) >= 0)
            {
                if(packetReader.Index + 3 <= packetReader.Size)
                {
                    amount = packetReader.ReadByte();
                    damage = packetReader.ReadInt16();
                }
                else
                {
                    packetReader.Failed = true;
                    return;
                }
            }

            if(id < 0)
            {
                // Check interaction with objects
            }
            else if(id < 100)
            {
                // The player placed a block
                Chunk.PlaceBlock(ns, id, x, y, z, direction);
            }
        }
Example #12
0
 public static void ReadPlayer(NetState ns, PacketReader packetReader)
 {
     byte extra = packetReader.ReadByte();
 }
Example #13
0
        public static void ReadLoginDetails(NetState ns, PacketReader packetReader)
        {
            int protocolVersion = packetReader.ReadInt32();

            if (protocolVersion != CXMineServer.ProtocolVersion)
            {
                ns.Disconnect();
                return;
            }

            short userNameLength = packetReader.ReadInt16();
            string username;

            if (packetReader.Index + userNameLength <= packetReader.Size)
            {
                username = packetReader.ReadString(userNameLength);
            }
            else
            {
                packetReader.Failed = true;
                return;
            }

            short passwordLength = packetReader.ReadInt16();
            string password;

            if (packetReader.Index + passwordLength <= packetReader.Size)
            {
                password = packetReader.ReadString(passwordLength);
            }

            if (username != ns.Owner.Username)
            {
                ns.Disconnect();
                return;
            }

            packetReader.ReadInt64();
            packetReader.ReadByte();

            ns.Login();
        }
Example #14
0
        public static void PlaceBlock(NetState from, int id, int x, int y, int z, int direction)
        {
            int meta = MetaHtN(direction);

            switch (direction)
            { // Direction
                case 0:
                    { // -Y
                        CXMineServer.Log("-Y");
                        y -= 1;
                        break;
                    }
                case 1:
                    { // +Y
                        CXMineServer.Log("+Y");
                        y += 1;
                        break;
                    }
                case 2:
                    { // -Z
                        CXMineServer.Log("-Z");
                        z -= 1;
                        break;
                    }
                case 3:
                    { // +Z
                        CXMineServer.Log("+Z");
                        z += 1;
                        break;
                    }
                case 4:
                    { // -X
                        CXMineServer.Log("-X");
                        x -= 1;
                        break;
                    }
                case 5:
                    { // +X
                        CXMineServer.Log("+X");
                        x += 1;
                        break;
                    }
            }

            int _x = x & 15, _z = z & 15;

            if (!Utility.IsInRange(from.Owner, x*32+16, z*32+16, 128) || !from.Owner.CanPlace(y) || (id == (int)BlockType.Torch && direction == 0))
            {
                // No need to rollback, no block exist here
                if(y > 127 || y < -128)
                    return;
                // Rollback to the precedent situation if the placement is invalid
                BlockType block = CXMineServer.Server.World.GetChunkAt(x, z).GetBlock(_x, y, _z);
                byte data = (byte)MetaHtN((int)CXMineServer.Server.World.GetChunkAt(x, z).GetData(_x, y, _z));
                from.BlockChange(x, (byte)y, z, (byte)block, data);

                Inventory.Slot slot = from.Owner.inventory.GetItem(from.Owner.inventory.HoldingPos + 36);
                from.SetSlot(0, (short)slot.Position, slot.Id, (byte)slot.Count, slot.Uses);
                return;
            }

            // Get the current chunk
            Chunk chunk = CXMineServer.Server.World.GetChunkAt(x, z);
            from.BlockChange(x, (byte)y, z, (byte)id, (byte)meta);
            // For each player using that chunk, update the block data
            foreach (Player p in CXMineServer.Server.PlayerList)
            {
                if (p == from.Owner)
                    continue;

                int chunkX = PlayerToChunkPosition(p.X);
                int chunkZ = PlayerToChunkPosition(p.Z);

                if (DistanceBetweenChunks(chunk, CXMineServer.Server.World.GetChunkAt(chunkX, chunkZ)) <= Map.visibleChunks)
                    from.BlockChange(x, (byte)y, z, (byte)id, (byte)meta);
            }

            // Update the chunk's data on the server
            try
            {
                chunk.SetBlock(_x, y, _z, (BlockType)id);
            }
            catch (System.Exception ex)
            {
                CXMineServer.Log("Exception: " + ex.Message + "\n\nXYZ: " + _x + " " + y + " " + _z);
            }

            // Decrement the inventory counter
            int pos = from.Owner.inventory.HoldingPos;
            from.Owner.inventory.Remove(pos + 36);

            // Handle Count == 0 and so ID == -1 (Needed by the different packet format)
            if (from.Owner.inventory.GetItem(pos).Id == -1)
                from.SetSlot(0, (short)pos, (short)-1);
            else
            {
                Inventory.Slot slot = from.Owner.inventory.GetItem(pos);
                from.SetSlot(0, (short)pos, slot.Id, (byte)slot.Count, slot.Uses);
            }
        }
Example #15
0
        public static void ReadPlayerLook(NetState ns, PacketReader packetReader)
        {
            float yaw = packetReader.ReadFloat();
            float pitch = packetReader.ReadFloat();

            ns.Owner.Yaw = yaw;
            ns.Owner.Pitch = pitch;

            bool onGround = packetReader.ReadBool();
        }
Example #16
0
        public static void DestroyBlock(NetState from, int x, int y, int z, int face)
        {
            // Get the chunk the player is digging in
            Chunk chunk = CXMineServer.Server.World.GetChunkAt(x, z);

            // Get a new EID for the spawn
            int eid = Server.getEID();
            // Get relative X and Z coordinate in the chunk
            int _x = x & 15, _z = z & 15;

            // Get the block data in the chunk
            BlockType block = chunk.GetBlock(_x, y, _z);
            // Update the chunk with the new block
            chunk.SetBlock(_x, y, _z, BlockType.Air);
            // Manage special spawn case where the destroyed block isn't the one to spawn
            if (block == BlockType.Grass)
                block = BlockType.Dirt;
            if (block == BlockType.Rock)
                block = BlockType.Cobblestone;

            int itemX = x * 32 + 16;
            int itemZ = z * 32 + 16;
            int itemY = y * 32;

            int prevDistance = Utility.DistanceBetweenEntities(from.Owner, itemX, itemZ); // It's the power of 2 distance
            Player picksUp = null;

            from.BlockChange(x, (byte)y, z, (byte)BlockType.Air, 0);
            from.PickupSpawn(eid, (short)block, 1, 0, itemX, itemY, itemZ, 0, 0, 0);

            foreach (Player p in CXMineServer.Server.PlayerList)
            {
                if (p == from.Owner)
                    continue;

                int chunkX = PlayerToChunkPosition(p.X);
                int chunkZ = PlayerToChunkPosition(p.Z);

                if (DistanceBetweenChunks(chunk, CXMineServer.Server.World.GetChunkAt(chunkX, chunkZ)) <= (Map.visibleChunks * Map.visibleChunks))
                    p.State.BlockChange(x, (byte)y, z, (byte)BlockType.Air, 0);

                if (DistanceBetweenChunks(chunk, CXMineServer.Server.World.GetChunkAt(chunkX, chunkZ)) <= 1)
                    p.State.PickupSpawn(eid, (short)block, 1, 0, itemX, itemY, itemZ, 0, 0, 0);

                int distance;
                if (Utility.IsInRange(p, itemX, itemZ, 40, out distance) && p.CanPick(itemY))
                {
                    if (distance < prevDistance)
                    {
                        prevDistance = distance;
                        picksUp = p;
                    }
                }
            }

            Item newItem = null;

            if (picksUp != null)
                picksUp.State.CollectItem(eid, picksUp.EntityID, (short)block, 1, 0);
            else if (prevDistance <= 1600 && from.Owner.CanPick(itemY))
                from.CollectItem(eid, from.Owner.EntityID, (short)block, 1, 0);
            else
            {
                newItem = new Item(chunk);
                newItem.Type = (int)block;
                newItem.X = itemX;
                newItem.Y = itemY;
                newItem.Z = itemZ;
                newItem.Yaw = 0.0f;
                newItem.Pitch = 0.0f;
                newItem.EId = eid;
                newItem.Uses = 0;
                newItem.Count = 1;

                chunk.Items.Add(newItem);
            }

            // Spawn a new object to collect
            /*Transmit(PacketType.PickupSpawn, eid, (short)block, (byte)1, (int)packet[2] * 32 + 16, (int)((byte)packet[3]) * 32, (int)packet[4] * 32 + 16, (byte)0, (byte)0, (byte)0);
            // Collect the block instantly (TODO: Collect the block if near the player)
            Transmit(PacketType.CollectItem, eid, _Player.EntityID);
            // Destroy the entity beacuse it's collected
            Transmit(PacketType.DestroyEntity, eid);
            // Update the inventory coherently
            int slot = _Player.inventory.Add((short)block);
            CXMineServer.Log("Sent to slot " + slot.ToString());
            Transmit(PacketType.SetSlot, (byte)0, slot, (short)block, (byte)_Player.inventory.GetItem(slot).Count, (byte)0);*/
        }
Example #17
0
        public static void ReadPlayerPosition(NetState ns, PacketReader packetReader)
        {
            double x = packetReader.ReadDouble();
            double stance = packetReader.ReadDouble();
            double y = packetReader.ReadDouble();
            double z = packetReader.ReadDouble();

            ns.Owner.X = x;
            ns.Owner.Y = y;
            ns.Owner.Z = z;

            bool onGround = packetReader.ReadBool();

            ns.Owner.Moved = true;
        }
Example #18
0
 public KeepAliveTimer(NetState ns)
     : base(TimeSpan.FromSeconds(2.0), false)
 {
     _NetState = ns;
 }
Example #19
0
        public static void ReadHandshake(NetState ns, PacketReader packetReader)
        {
            short length = packetReader.ReadInt16();

            if (packetReader.Index + length <= packetReader.Size)
            {
                string username = packetReader.ReadString(length);
                ns.Owner.Username = username;
            }
            else
            {
                packetReader.Failed = true;
                return;
            }

            ns.Handshake();
        }