Beispiel #1
0
        public void HandleClick(Player p, short slot, ClickType click, short ActionID, bool Shift)
        {
            if (slot == -999)
            {
                p.inventory.HandleClick(slot, click, ActionID, Shift);
                return;
            }
            if (slot < 0 || slot > InventorySize + 35)
            {
                return;
            }
            if (Type == WindowType.Workbench || Type == WindowType.Furnace)
            {
                crafting.CheckCrafting(slot, items);
            }

            if (Shift)
            {
                if (slot >= InventorySize)
                {
                    if (Type == WindowType.Workbench || Type == WindowType.Furnace || Type == WindowType.EnchantmentTable || Type == WindowType.BrewingStand)
                    {
                        p.inventory.HandleClick((short)((slot - InventorySize) + 9), click, ActionID, Shift);
                        return;
                    }
                }

                Item item;
                bool useEmptySlot = false;
                if (slot < InventorySize)
                {
                    Item clickItem = items[slot];
                    if (clickItem.id != -1)
                    {
                        for (int i = 44; i >= 36; i--)
                        {
                            item = p.inventory.items[i];
                            if (useEmptySlot)
                            {
                                if (item.id == -1)
                                {
                                    p.inventory.items[i] = clickItem;
                                    items[slot]          = Item.Nothing;
                                    break;
                                }
                            }
                            else if (item.id == clickItem.id && item.meta == clickItem.meta)
                            {
                                byte stack = Inventory.isStackable(item.id);
                                byte avail = (byte)(stack - item.count);
                                if (avail < 1)
                                {
                                    continue;
                                }

                                if (clickItem.count <= avail)
                                {
                                    item.count += clickItem.count;
                                    items[slot] = Item.Nothing;
                                    break;
                                }
                                else
                                {
                                    item.count       = stack;
                                    clickItem.count -= avail;
                                }
                            }
                            if (i == 36 && !useEmptySlot)
                            {
                                useEmptySlot = true; i = 45;
                            }
                        }
                        if (items[slot].id != -1)
                        {
                            useEmptySlot = false;
                            for (int i = 35; i >= 9; i--)
                            {
                                item = p.inventory.items[i];
                                if (useEmptySlot)
                                {
                                    if (item.id == -1)
                                    {
                                        p.inventory.items[i] = clickItem;
                                        items[slot]          = Item.Nothing;
                                        break;
                                    }
                                }
                                else if (item.id == clickItem.id && item.meta == clickItem.meta)
                                {
                                    byte stack = Inventory.isStackable(item.id);
                                    byte avail = (byte)(stack - item.count);
                                    if (avail < 1)
                                    {
                                        continue;
                                    }

                                    if (clickItem.count <= avail)
                                    {
                                        item.count += clickItem.count;
                                        items[slot] = Item.Nothing;
                                        break;
                                    }
                                    else
                                    {
                                        item.count       = stack;
                                        clickItem.count -= avail;
                                    }
                                }
                                if (i == 9 && !useEmptySlot)
                                {
                                    useEmptySlot = true; i = 36;
                                }
                            }
                        }
                    }
                }
                else
                {
                    slot = (short)((slot - InventorySize) + 9);
                    Item clickItem = p.inventory.items[slot];
                    if (clickItem.id != -1)
                    {
                        for (int i = 0; i < InventorySize; i++)
                        {
                            item = items[i];
                            if (useEmptySlot)
                            {
                                if (item.id == -1)
                                {
                                    items[i] = clickItem;
                                    p.inventory.items[slot] = Item.Nothing;
                                    break;
                                }
                            }
                            else if (item.id == clickItem.id && item.meta == clickItem.meta)
                            {
                                byte stack = Inventory.isStackable(item.id);
                                byte avail = (byte)(stack - item.count);
                                if (avail < 1)
                                {
                                    continue;
                                }

                                if (clickItem.count <= avail)
                                {
                                    item.count += clickItem.count;
                                    p.inventory.items[slot] = Item.Nothing;
                                    break;
                                }
                                else
                                {
                                    item.count       = stack;
                                    clickItem.count -= avail;
                                }
                            }
                            if (i == InventorySize - 1 && !useEmptySlot)
                            {
                                useEmptySlot = true; i = -1;
                            }
                        }
                    }
                }
            }
            else
            {
                if (slot >= InventorySize)
                {
                    p.inventory.HandleClick((short)((slot - InventorySize) + 9), click, ActionID, Shift);
                    return;
                }

                Item clickItem = items[slot];
                if (p.OnMouse.id == -1)
                {
                    if (clickItem.id != -1)
                    {
                        if (click == ClickType.RightClick && clickItem.count > 1)
                        {
                            p.OnMouse        = new Item(clickItem);
                            p.OnMouse.count  = (byte)Math.Ceiling((float)p.OnMouse.count / 2F);
                            clickItem.count /= 2;
                        }
                        else
                        {
                            items[slot] = Item.Nothing;
                            p.OnMouse   = clickItem;
                        }
                    }
                }
                else
                {
                    if (clickItem.id != -1)
                    {
                        if (p.OnMouse.id == clickItem.id && p.OnMouse.meta == clickItem.meta)
                        {
                            byte stack = Inventory.isStackable(clickItem.id);
                            if (click == ClickType.RightClick && p.OnMouse.count > 1)
                            {
                                if (clickItem.count < stack)
                                {
                                    p.OnMouse.count--;
                                    clickItem.count++;
                                }
                            }
                            else
                            {
                                if (clickItem.count < stack)
                                {
                                    byte avail = (byte)(stack - clickItem.count);
                                    if (p.OnMouse.count <= avail)
                                    {
                                        clickItem.count += p.OnMouse.count;
                                        p.OnMouse        = Item.Nothing;
                                    }
                                    else
                                    {
                                        clickItem.count  = stack;
                                        p.OnMouse.count -= avail;
                                    }
                                }
                            }
                        }
                        else
                        {
                            items[slot] = p.OnMouse;
                            p.OnMouse   = clickItem;
                        }
                    }
                    else
                    {
                        if (click == ClickType.RightClick && p.OnMouse.count > 1)
                        {
                            items[slot]       = new Item(p.OnMouse);
                            items[slot].count = 1;
                            p.OnMouse.count--;
                        }
                        else
                        {
                            items[slot] = p.OnMouse;
                            p.OnMouse   = Item.Nothing;
                        }
                    }
                }
            }

            if (container != null)
            {
                container.UpdateContents(p);
            }
            List <Item> items2 = new List <Item>(items); items2.AddRange((Item[])p.inventory.items.TruncateStart(9));

            p.SendWindowItems(id, items2.ToArray());
            p.SendItem(255, -1, p.OnMouse);
        }