示例#1
0
        public static void QuickStack(On.Terraria.UI.ChestUI.orig_QuickStack orig)
        {
            Player player = Main.player[Main.myPlayer];

            if (player.chest == -4)
            {
                ChestUI.MoveCoins(player.inventory, player.bank3.item);
            }
            else if (player.chest == -3)
            {
                ChestUI.MoveCoins(player.inventory, player.bank2.item);
            }
            else if (player.chest == -2)
            {
                ChestUI.MoveCoins(player.inventory, player.bank.item);
            }
            Item[] inventory = player.inventory;
            Item[] openChest = player.bank.item;
            if (player.chest > -1)
            {
                openChest = Main.chest[player.chest].item;
            }
            else if (player.chest == -2)
            {
                openChest = player.bank.item;
            }
            else if (player.chest == -3)
            {
                openChest = player.bank2.item;
            }
            else if (player.chest == -4)
            {
                openChest = player.bank3.item;
            }
            List <Item>            possibleStackItems    = new List <Item>();
            List <int>             possibleStackSlots    = new List <int>();
            List <int>             quickStackIgnoreSlots = new List <int>();
            Dictionary <int, Item> dictionary            = new Dictionary <int, Item>();
            List <int>             removeItems           = new List <int>();

            bool[] quickStacked = new bool[openChest.Length];
            for (int i = 0; i < 40; i++)
            {
                if (openChest[i].type > 0 && openChest[i].stack > 0 && openChest[i].maxStack > 1 && (openChest[i].type < 71 || openChest[i].type > 74))
                {
                    possibleStackSlots.Add(i);
                    possibleStackItems.Add(openChest[i]);
                }
                if (openChest[i].type == 0 || openChest[i].stack <= 0)
                {
                    quickStackIgnoreSlots.Add(i);
                }
            }
            int num = 50;

            if (player.chest <= -2)
            {
                num += 4;
            }
            for (int j = 0; j < num; j++)
            {
                foreach (Item itm in possibleStackItems)
                {
                    if (inventory[j].IsTheSameAs(itm) && !inventory[j].favorited)
                    {
                        dictionary.Add(j, inventory[j]);
                    }
                }
            }
            for (int k = 0; k < possibleStackSlots.Count; k++)
            {
                int  num2 = possibleStackSlots[k];
                Item itm2 = openChest[num2];
                foreach (KeyValuePair <int, Item> keyValuePair in dictionary)
                {
                    if (keyValuePair.Value.IsTheSameAs(itm2) && inventory[keyValuePair.Key].IsTheSameAs(itm2))
                    {
                        int num3 = inventory[keyValuePair.Key].stack;
                        int num4 = openChest[num2].maxStack - openChest[num2].stack;
                        if (num4 == 0)
                        {
                            break;
                        }
                        if (num3 > num4)
                        {
                            num3 = num4;
                        }
                        Main.PlaySound(7, -1, -1, 1, 1f, 0f);
                        openChest[num2].stack             += num3;
                        inventory[keyValuePair.Key].stack -= num3;
                        if (inventory[keyValuePair.Key].stack == 0)
                        {
                            inventory[keyValuePair.Key].SetDefaults(0, false);
                        }
                        quickStacked[num2] = true;
                    }
                }
            }
            foreach (KeyValuePair <int, Item> keyValuePair2 in dictionary)
            {
                if (inventory[keyValuePair2.Key].stack == 0)
                {
                    removeItems.Add(keyValuePair2.Key);
                }
            }
            foreach (int key in removeItems)
            {
                dictionary.Remove(key);
            }
            for (int l = 0; l < quickStackIgnoreSlots.Count; l++)
            {
                int  num5 = quickStackIgnoreSlots[l];
                bool flag = true;
                Item num6 = openChest[num5];
                foreach (KeyValuePair <int, Item> keyValuePair3 in dictionary)
                {
                    if ((keyValuePair3.Value.IsTheSameAs(num6) && inventory[keyValuePair3.Key].IsTheSameAs(num6)) || (flag && inventory[keyValuePair3.Key].stack > 0))
                    {
                        Main.PlaySound(7, -1, -1, 1, 1f, 0f);
                        if (flag)
                        {
                            num6            = keyValuePair3.Value;
                            openChest[num5] = inventory[keyValuePair3.Key];
                            inventory[keyValuePair3.Key] = new Item();
                        }
                        else
                        {
                            int num7 = inventory[keyValuePair3.Key].stack;
                            int num8 = openChest[num5].maxStack - openChest[num5].stack;
                            if (num8 == 0)
                            {
                                break;
                            }
                            if (num7 > num8)
                            {
                                num7 = num8;
                            }
                            openChest[num5].stack += num7;
                            inventory[keyValuePair3.Key].stack -= num7;
                            if (inventory[keyValuePair3.Key].stack == 0)
                            {
                                inventory[keyValuePair3.Key] = new Item();
                            }
                        }
                        quickStacked[num5] = true;
                        flag = false;
                    }
                }
            }
            if (Main.netMode == 1 && player.chest >= 0)
            {
                for (int m = 0; m < quickStacked.Length; m++)
                {
                    NetMessage.SendData(32, -1, -1, null, player.chest, (float)m, 0f, 0f, 0, 0, 0);
                }
            }
            possibleStackItems.Clear();
            possibleStackSlots.Clear();
            quickStackIgnoreSlots.Clear();
            dictionary.Clear();
            removeItems.Clear();
        }