Пример #1
0
        internal virtual void OnClicked(WindowClickPacket packet)
        {
            InterfaceClickedEventArgs e = new InterfaceClickedEventArgs(this, packet);

            if (Clicked != null)
            {
                Clicked.Invoke(this, e);
            }

            StartTransaction(); // Don't send update packets while we process this transaction, as that would be redundant.
            try
            {
                Interface target = null;
                switch (e.Location)
                {
                case ClickLocation.Void:
                    if (ItemHelper.IsVoid(Cursor))
                    {           // Empty click in void: ignore
                        e.Cancel();
                    }
                    switch (e.MouseButton)
                    {
                    case WindowClickPacket.MouseButtonClicked.Right:
                        // Right-click in void: drop item
                        var item = ItemHelper.GetInstance(Cursor.Type);
                        item.Durability = Cursor.Durability;
                        item.Damage     = Cursor.Damage;
                        item.Count      = 1;
                        Owner.Server.DropItem(Owner, item);
                        Cursor.Count--;
                        break;

                    case WindowClickPacket.MouseButtonClicked.Left:
                        // Left-click in void: drop stack
                        Owner.Server.DropItem(Owner, Cursor);
                        Cursor = ItemHelper.Void;
                        break;
                    }

                    return;

                case ClickLocation.Interface:
                    target = e.Interface;
                    break;

                case ClickLocation.Inventory:
                    target = Owner.Inventory;
                    break;
                }

                // Ensure a true void stack for our calculations
                if (ItemHelper.IsVoid(Cursor))
                {
                    Cursor = ItemHelper.Void;
                }
                if (ItemHelper.IsVoid(target._slots[e.Slot]))
                {
                    target[e.Slot] = ItemHelper.Void;
                }

                // The fun begins.
                if (target._slots[e.Slot].StacksWith(Cursor))
                {
                    if (ItemHelper.IsVoid(Cursor))
                    {   // Useless click
                        e.Cancel();
                    }
                    else if (e.MouseButton == WindowClickPacket.MouseButtonClicked.Right)
                    {     // Right-click on same item
                        if (target._slots[e.Slot].Count >= 64)
                        { // Stack is already full: ignore
                            e.Cancel();
                        }
                        else
                        {       // Increment stack
                            target[e.Slot].Count++;
                            Cursor.Count--;
                        }
                    }
                    else
                    {
                        // Left-click on same item
                        int total = target._slots[e.Slot].Count + Cursor.Count;
                        if (total <= 64)
                        {       // Move all items to stack
                            target[e.Slot].Count = unchecked ((sbyte)total);
                            Cursor.Count         = 0;
                        }
                        else
                        {       // Make stack 64, and put remainder in cursor
                            target[e.Slot].Count = 64;
                            Cursor.Count         = unchecked ((sbyte)(total - 64));
                        }
                    }
                }
                else if (!ItemHelper.IsVoid(Cursor) && (e.MouseButton == WindowClickPacket.MouseButtonClicked.Right) && ItemHelper.IsVoid(target._slots[e.Slot]))
                {
                    // Right-click on empty slot with items in cursor: drop one item from Cursor into slot

                    target[e.Slot]       = Cursor.Clone();
                    target[e.Slot].Count = 1;
                    Cursor.Count--;
                    if (Cursor.Count == 0)
                    {
                        Cursor = ItemHelper.Void;
                    }
                }
                else if (e.MouseButton == WindowClickPacket.MouseButtonClicked.Right && ItemHelper.IsVoid(Cursor))
                {       // Right-click with empty cursor: split stack in half
                    sbyte count = target._slots[e.Slot].Count;
                    target[e.Slot].Count /= 2;
                    count       -= target._slots[e.Slot].Count;
                    Cursor       = target._slots[e.Slot].Clone();
                    Cursor.Count = count;
                    //Cursor = new ItemStack(target.Slots[e.Slot].Type, (sbyte)count, target.Slots[e.Slot].Durability);
                }
                else if (e.MouseButton == WindowClickPacket.MouseButtonClicked.Right)
                {       // Right-click on different type: ignored click
                    e.Cancel();
                }
                else
                {       // Left-click on different type: swap stacks
                    var swap = target[e.Slot].Clone();
                    target[e.Slot] = Cursor;
                    Cursor         = swap;
                }
            }
            catch (Exception ex)
            {
                e.Cancel();
                Owner.Client.SendMessage("§cInventory Error: " + ex.Message);
                Owner.Client.Logger.Log(ex);
            }
            finally
            {
                Owner.Client.SendPacket(new TransactionPacket
                {
                    Accepted    = !e.Cancelled,
                    Transaction = e.Transaction,
                    WindowId    = e.Interface.Handle
                });
                EndTransaction();
            }
        }
Пример #2
0
        internal virtual void OnClicked(WindowClickPacket packet)
        {
            InterfaceClickedEventArgs e = new InterfaceClickedEventArgs(this, packet);
            if (Clicked != null)
                Clicked.Invoke(this, e);

            StartTransaction(); // Don't send update packets while we process this transaction, as that would be redundant.
            try
            {
                Interface target = null;
                switch (e.Location)
                {
                case ClickLocation.Void:
                    if (ItemStack.IsVoid(Cursor))
                    {	// Empty click in void: ignore
                        e.Cancel();
                    }
                    else if (e.RightClick)
                    {	// Right-click in void: drop item
                        Client.Server.DropItem(Client, new ItemStack(Cursor.Type, 1, Cursor.Durability));
                        Cursor.Count--;
                    }
                    else
                    {	// Left-click in void: drop stack
                        Client.Server.DropItem(Client, Cursor);
                        Cursor = ItemStack.Void;
                    }
                    return;

                case ClickLocation.Interface:
                    target = e.Interface;
                    break;

                case ClickLocation.Inventory:
                    target = Client.Inventory;
                    break;
                }

                // Ensure a true void stack for our calculations
                if (ItemStack.IsVoid(Cursor))
                    Cursor = ItemStack.Void;
                if (ItemStack.IsVoid(target.Slots[e.Slot]))
                    target[e.Slot] = ItemStack.Void;

                // The fun begins.
                if (target.Slots[e.Slot].StacksWith(Cursor))
                {
                    if (ItemStack.IsVoid(Cursor))
                    {	// Useless click
                        e.Cancel();
                    }
                    else if (e.RightClick)
                    {	// Right-click on same item
                        if (target.Slots[e.Slot].Count >= 64)
                        {	// Stack is already full: ignore
                            e.Cancel();
                        }
                        else
                        {	// Increment stack
                            target.Slots[e.Slot].Count++;
                            Cursor.Count--;
                        }
                    }
                    else
                    {	// Left-click on same item
                        int total = target.Slots[e.Slot].Count + Cursor.Count;
                        if (total <= 64)
                        {	// Move all items to stack
                            target.Slots[e.Slot].Count = unchecked((sbyte)total);
                            Cursor.Count = 0;
                        }
                        else
                        {	// Make stack 64, and put remainder in cursor
                            target.Slots[e.Slot].Count = 64;
                            Cursor.Count = unchecked((sbyte)(total - 64));
                        }
                    }
                }
                else if (!ItemStack.IsVoid(Cursor) && e.RightClick && ItemStack.IsVoid(target.Slots[e.Slot]))
                {
                    // Right-click on empty slot with items in cursor: drop one item from Cursor into slot
                    target.Slots[e.Slot].Type = Cursor.Type;
                    target.Slots[e.Slot].Durability = Cursor.Durability;
                    target.Slots[e.Slot].Count = 1;
                    Cursor.Count--;
                    if (Cursor.Count == 0)
                        Cursor = ItemStack.Void;
                }
                else if (e.RightClick && ItemStack.IsVoid(Cursor))
                {	// Right-click with empty cursor: split stack in half
                    int count = target.Slots[e.Slot].Count;
                    target.Slots[e.Slot].Count /= 2;
                    count -= target.Slots[e.Slot].Count;
                    Cursor = new ItemStack(target.Slots[e.Slot].Type, (sbyte)count, target.Slots[e.Slot].Durability);
                }
                else if (e.RightClick)
                {	// Right-click on different type: ignored click
                    e.Cancel();
                }
                else
                {	// Left-click on different type: swap stacks
                    ItemStack swap = target[e.Slot];
                    target[e.Slot] = Cursor;
                    Cursor = swap;
                }
            }
            catch (Exception ex)
            {
                e.Cancel();
                Client.SendMessage("§cInventory Error: " + ex.Message);
                Client.Logger.Log(ex);
            }
            finally
            {
                PacketHandler.SendPacket(new TransactionPacket
                {
                    Accepted = !e.Cancelled,
                    Transaction = e.Transaction,
                    WindowId = e.Interface.Handle
                });
                EndTransaction();
            }
        }