Ejemplo n.º 1
0
    public override void DropData(Vector2 Pos, object Data)
    {
        if (Data is int FromSlot && ParentMenu.Source != null)
        {
            if (Source == ParentMenu.Source.Source && Slot == FromSlot)
            {
                return;                 //Same source and slot, we dropped on ourself
            }
            Items.Instance Moving = ParentMenu.Source.Source.Inventory[FromSlot];
            Assert.ActualAssert(Moving != null);
            if (Moving == null)
            {
                return;                            //Makes Roslyn happy
            }
            Items.Instance Original = Source.Inventory[Slot];

            int  RetrieveCount = ParentMenu.CalcRetrieveCount(Moving.Count);
            bool EmptyMoving   = RetrieveCount == Moving.Count; //If we are moving all, empty the the source slot

            if (Original == null)                               //Replace (no item at target)
            {
                if (Net.Work.IsNetworkServer())
                {
                    Source.NetUpdateInventorySlot(Slot, Moving.Id, RetrieveCount);
                    if (EmptyMoving)
                    {
                        ParentMenu.Source.Source.NetEmptyInventorySlot(FromSlot);
                    }
                    else
                    {
                        ParentMenu.Source.Source.NetUpdateInventorySlot(FromSlot, Moving.Id, Moving.Count - RetrieveCount);
                    }
                }
                else
                {
                    Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), Slot, Moving.Id, RetrieveCount);
                    if (EmptyMoving)
                    {
                        ParentMenu.Source.Source.RpcId(Net.ServerId, nameof(IHasInventory.NetEmptyInventorySlot), FromSlot);
                    }
                    else
                    {
                        ParentMenu.Source.Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), FromSlot, Moving.Id, Moving.Count - RetrieveCount);
                    }
                }
            }
            else
            {
                if (Moving.Id == Original.Id)                //Combine at target
                {
                    if (Net.Work.IsNetworkServer())
                    {
                        Source.NetUpdateInventorySlot(Slot, Original.Id, Original.Count + RetrieveCount);
                        if (EmptyMoving)
                        {
                            ParentMenu.Source.Source.NetEmptyInventorySlot(FromSlot);
                        }
                        else
                        {
                            ParentMenu.Source.Source.NetUpdateInventorySlot(FromSlot, Moving.Id, Moving.Count - RetrieveCount);
                        }
                    }
                    else
                    {
                        Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), Slot, Original.Id, Original.Count + RetrieveCount);
                        if (EmptyMoving)
                        {
                            ParentMenu.Source.Source.RpcId(Net.ServerId, nameof(IHasInventory.NetEmptyInventorySlot), FromSlot);
                        }
                        else
                        {
                            ParentMenu.Source.Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), FromSlot, Moving.Id, Moving.Count - RetrieveCount);
                        }
                    }
                }
                else if (RetrieveCount == Moving.Count)                //Swap, only if we are moving all from the source slot
                {
                    if (Net.Work.IsNetworkServer())
                    {
                        Source.NetUpdateInventorySlot(Slot, Moving.Id, Moving.Count);
                        ParentMenu.Source.Source.NetUpdateInventorySlot(FromSlot, Original.Id, Original.Count);
                    }
                    else
                    {
                        Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), Slot, Moving.Id, Moving.Count);
                        ParentMenu.Source.Source.RpcId(Net.ServerId, nameof(IHasInventory.NetUpdateInventorySlot), FromSlot, Original.Id, Original.Count);
                    }
                }
            }
        }
    }