Terraria.UI.ChestUI.Restock C# (CSharp) Method

Restock() public static method

public static Restock ( ) : void
return void
        public static void Restock()
        {
            Player player = Main.player[Main.myPlayer];
            Item[] inv = player.inventory;
            Item[] objArray = player.bank.item;
            if (player.chest > -1)
                objArray = Main.chest[player.chest].item;
            else if (player.chest == -2)
                objArray = player.bank.item;
            else if (player.chest == -3)
                objArray = player.bank2.item;
            HashSet<int> hashSet = new HashSet<int>();
            List<int> list1 = new List<int>();
            List<int> list2 = new List<int>();
            for (int index = 57; index >= 0; --index)
            {
                if ((index < 50 || index >= 54) && (inv[index].itemId < 71 || inv[index].itemId > 74))
                {
                    if (inv[index].stack > 0 && inv[index].maxStack > 1 && (int)inv[index].prefix == 0)
                    {
                        hashSet.Add(inv[index].netID);
                        if (inv[index].stack < inv[index].maxStack)
                            list1.Add(index);
                    }
                    else if (inv[index].stack == 0 || inv[index].netID == 0 || inv[index].itemId == 0)
                        list2.Add(index);
                }
            }
            bool flag1 = false;
            for (int index1 = 0; index1 < objArray.Length; ++index1)
            {
                if (objArray[index1].stack >= 1 && (int)objArray[index1].prefix == 0 && hashSet.Contains(objArray[index1].netID))
                {
                    bool flag2 = false;
                    for (int index2 = 0; index2 < list1.Count; ++index2)
                    {
                        int slot = list1[index2];
                        int context = 0;
                        if (slot >= 50)
                            context = 2;
                        if (inv[slot].netID == objArray[index1].netID && ItemSlot.PickItemMovementAction(inv, context, slot, objArray[index1]) != -1)
                        {
                            int num = objArray[index1].stack;
                            if (inv[slot].maxStack - inv[slot].stack < num)
                                num = inv[slot].maxStack - inv[slot].stack;
                            inv[slot].stack += num;
                            objArray[index1].stack -= num;
                            flag1 = true;
                            if (inv[slot].stack == inv[slot].maxStack)
                            {
                                if (Main.netMode == 1 && Main.player[Main.myPlayer].chest > -1)
                                    NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)index1, 0.0f, 0.0f, 0, 0, 0);
                                list1.RemoveAt(index2);
                                --index2;
                            }
                            if (objArray[index1].stack == 0)
                            {
                                objArray[index1] = new Item();
                                flag2 = true;
                                if (Main.netMode == 1 && Main.player[Main.myPlayer].chest > -1)
                                {
                                    NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)index1, 0.0f, 0.0f, 0, 0, 0);
                                    break;
                                }
                                break;
                            }
                        }
                    }
                    if (!flag2 && list2.Count > 0 && objArray[index1].ammo != 0)
                    {
                        for (int index2 = 0; index2 < list2.Count; ++index2)
                        {
                            int context = 0;
                            if (list2[index2] >= 50)
                                context = 2;
                            if (ItemSlot.PickItemMovementAction(inv, context, list2[index2], objArray[index1]) != -1)
                            {
                                Utils.Swap<Item>(ref inv[list2[index2]], ref objArray[index1]);
                                list1.Add(list2[index2]);
                                list2.RemoveAt(index2);
                                flag1 = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (!flag1)
                return;
            Main.PlaySound(7, -1, -1, 1);
        }

Usage Example

Ejemplo n.º 1
0
        private static void DrawButton(SpriteBatch spriteBatch, int ID, int X, int Y)
        {
            Player player = Main.player[Main.myPlayer];

            if (ID == 4 && player.chest < -1 || ID == 5 && !Main.editChest)
            {
                ChestUI.UpdateHover(ID, false);
            }
            else
            {
                Y += ID * 26;
                float  num  = ChestUI.ButtonScale[ID];
                string text = "";
                switch (ID)
                {
                case 0:
                    text = Lang.inter[29];
                    break;

                case 1:
                    text = Lang.inter[30];
                    break;

                case 2:
                    text = Lang.inter[31];
                    break;

                case 3:
                    text = Lang.inter[82];
                    break;

                case 4:
                    text = Lang.inter[Main.editChest ? 47 : 61];
                    break;

                case 5:
                    text = Lang.inter[63];
                    break;
                }
                Vector2 vector2   = Main.fontMouseText.MeasureString(text);
                Color   color     = new Color((int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor) * num;
                Color   baseColor = Color.White * 0.97f * (float)(1.0 - ((double)byte.MaxValue - (double)Main.mouseTextColor) / (double)byte.MaxValue * 0.5);
                baseColor.A = byte.MaxValue;
                X          += (int)((double)vector2.X * (double)num / 2.0);
                ChatManager.DrawColorCodedStringWithShadow(spriteBatch, Main.fontMouseText, text, new Vector2((float)X, (float)Y), baseColor, 0.0f, vector2 / 2f, new Vector2(num), -1f, 1.5f);
                vector2 *= num;
                if (!Utils.FloatIntersect((float)Main.mouseX, (float)Main.mouseY, 0.0f, 0.0f, (float)X - vector2.X / 2f, (float)Y - vector2.Y / 2f, vector2.X, vector2.Y))
                {
                    ChestUI.UpdateHover(ID, false);
                }
                else
                {
                    ChestUI.UpdateHover(ID, true);
                    player.mouseInterface = true;
                    if (!Main.mouseLeft || !Main.mouseLeftRelease)
                    {
                        return;
                    }
                    switch (ID)
                    {
                    case 0:
                        ChestUI.LootAll();
                        break;

                    case 1:
                        ChestUI.DepositAll();
                        break;

                    case 2:
                        ChestUI.QuickStack();
                        break;

                    case 3:
                        ChestUI.Restock();
                        break;

                    case 4:
                        ChestUI.RenameChest();
                        break;

                    case 5:
                        ChestUI.RenameChestCancel();
                        break;
                    }
                    Recipe.FindRecipes();
                }
            }
        }