Пример #1
0
        /// <summary>
        /// What the Client sends to the server.
        /// </summary>
        public override void FromClient(PacketFromClient packet)
        {
            while (phase == Phases.Handshake)
                Thread.Sleep(100);
            //Wait for handshake to complete
            if (phase == Phases.FinalClose)
                throw new SessionClosedException();
    
            if (packet.PacketID == PassThrough.ID)
            {
                SendToBackend(packet);
                return;
            }

            //Fix players eid to what is known for the server
            if (packet is IEntity)
            {
                IEntity ie = (IEntity)packet;
                if (ie.EID == Player.EntityID)
                {
                    ie.EID = EID;
                    packet.SetPacketBuffer(null);
                }
            }

            WorldRegion region = CurrentRegion;

            switch (packet.PacketID)
            {
                case PlayerPosition.ID:
                    if (AttachedEntity > 0)
                    {
                        //Ignore relative movements
                    }
                    else
                    {
                        var pp = ((PlayerPosition)packet);
                        if (pp.Position.Y > -900)
                        {
                            SetPosition(pp.Position, true);
                            OnGround = pp.OnGround != 0;
                        }
                    }
                    break;

                case PlayerPositionLookClient.ID:
                    if (AttachedEntity > 0)
                    {
                        //Ignore relative movements
                    }
                    else
                    {
                        PlayerPositionLookClient pp = ((PlayerPositionLookClient)packet);
                        if (pp.Position.Y > -900)
                        {
                            SetPosition(pp.Position, true);
                            OnGround = pp.OnGround;
                        }
                    }
                    Pitch = ((PlayerPositionLookClient)packet).Pitch;
                    Yaw = ((PlayerPositionLookClient)packet).Yaw;
                    break;

                case PlayerGround.ID:
                    OnGround = ((PlayerGround)packet).OnGround;
                    //Good but a few false positives
                    /*
                    if (Sprinting)//Hacked client: Invalid sprint
                    {
                        Chatting.Parser.TellAdmin(Player.Name + Chat.Gray + " sprinting standing still");
                    }*/
                    break;

                case  EntityAction.ID:
                    EntityAction ea = packet as EntityAction;
                    switch (ea.Action)
                    {
                        case EntityAction.Actions.LeaveBed:
                            Sleeping = false;
                            break;
                        case EntityAction.Actions.Crounch:
                            Crouched = true;
                            break;
                        case EntityAction.Actions.Uncrounch:
                            Crouched = false;
                            break;
                        case EntityAction.Actions.StartSprinting:
                            Sprinting = true;
                            break;
                        case EntityAction.Actions.StopSprinting:
                            Sprinting = false;
                            break;
                    }
                    break;

                case UseEntity.ID:
                    if (Mode == GameMode.Creative && (Player.Admin() == false))
                        return; //Donors can't hurt while in creative mode

                    if (UseEntityFromClient((UseEntity)packet))
                        return;
                    break;
                
                case HeldItemClient.ID:
                    //Active item
                    var hc = (HeldItemClient)packet;
                    if (hc.SlotID >= 0 && hc.SlotID <= 8)
                        ActiveInventoryIndex = hc.SlotID;
                    else
                        Log.Write(
                            new InvalidOperationException("Invalid holding slot id: " + hc.SlotID),
                            this.Player
                        );
                    break;
                        
            //Prevent non admins from getting items in creative
                case CreativeInventory.ID:
                    if (Player.Admin() == false)
                    {
                        Player.SendToClient(new EntityStatus(Player.EntityID, EntityStatuses.EntityHurt));
                        Player.TellSystem(Chat.Yellow, "Creative Inventory Disabled");
                        return;
                    }

                    CreativeInventory ci = (CreativeInventory)packet;
                    if (0 <= ci.Slot && ci.Slot <= Inventory.Length)
                        Inventory[ci.Slot] = ci.Item;
                    break;

            //If block action is done from another region
                case PlayerBlockPlacement.ID:
                    //AfkTime = DateTime.Now;

                    //Non admins can't place block
                    if (Mode == GameMode.Creative && (Player.Admin() == false))
                    {
                        Player.SendToClient(new EntityStatus(Player.EntityID, EntityStatuses.EntityHurt));
                        Player.TellSystem(Chat.Yellow, "Creative Build Disabled");
                        return;
                    }

                    PlayerBlockPlacement pb = (PlayerBlockPlacement)packet;
                    if (pb.BlockPosition.IsNull() == false)
                    {
                        CoordDouble pos = pb.BlockPosition.CloneDouble();
                        region = RegionCrossing.GetRegion(pos, Dimension, World.Regions);
                        //Remember the last position clicked so we can do better chest protection
                        LastClickRegion = region; //this is obsolete since we moved to blocking open across regions
                    }
                    else
                    {
                        if (pb.Item != null)
                            Charge = new ChargeState(pb.Item.ItemID);
                    }

                    if (FilterDirection(pb.BlockPosition))
                        return;

                    if (region == null)
                    {
                        if (Dimension == 0 && FilterLava(pb))
                            return;
                    }
                    if (Protected.ProtectBlockPlace(this, region, pb))
                        return;

                    break;
            
                case PlayerDigging.ID:
                    //AfkTime = DateTime.Now;
                    PlayerDigging pd = (PlayerDigging)packet;
                    if (pd.Status == PlayerDigging.StatusEnum.FinishedDigging || pd.Status == PlayerDigging.StatusEnum.StartedDigging)
                    {
                        CoordDouble pos = pd.Position.CloneDouble();
                        region = RegionCrossing.GetRegion(pos, Dimension, World.Regions);
                    }
                    //Log breaking blocks to determine if they found the diamond
                    if (pd.Status == PlayerDigging.StatusEnum.FinishedDigging)
                        OreTracker.BrokeBlock = DateTime.Now;

                    if (pd.Status == PlayerDigging.StatusEnum.ShootArrow)
                        Charge = null;

                    //Prevent non admin creative from digging
                    if (Mode == GameMode.Creative && Player.Admin() == false)
                        return;
                    if (FilterDirection(pd.Position))
                        return;

                    if (Protected.ProtectBlockBreak(this, region, pd))
                        return;
                    break;
            
                case WindowClick.ID:
                    var wc = packet as WindowClick;
                    if (wc.WindowID == 0)
                    {
                        //TODO: handle
                    }
                    //AfkTime = DateTime.Now;
                    if (Protected.ProtectChestsClick(this, wc))
                        return;

                    ///Workaround bug clicky items duplication
                    if (wc.Item != null)
                    {
                        if (wc.Item.Count > 64)
                            return;
                    }
                    break;

                case WindowCloseClient.ID:
                    WindowClose((WindowCloseClient)packet);
                    break;
            }

            if (region != null)
            {
                if (region.Type == SpawnRegion.Type)
                if (SpawnRegion.FilterClient(Player, packet))
                    return;
                if (region.Type == SpawnTimeRegion.Type)
                if (SpawnTimeRegion.FilterClient(region, Player, packet))
                    return;
            }

            SendToBackend(packet);
        }