Example #1
0
        public static void DoWithdrawItemForCraft(Recipe self, ref Item item, ref int required)
        {
            if (StoragePlayer.Get.StorageAccess.X < 0)
            {
                return;
            }

            var instance = MagicStoragePlus.Instance.StorageUI;

            bool changed = false;

            foreach (Item i in instance.items)
            {
                if (required <= 0)
                {
                    if (changed)
                    {
                        instance.RefreshItems();
                    }
                    return;
                }

                if (item.IsTheSameAs(i) || self.useWood(item.type, i.type) || self.useSand(item.type, i.type) || self.useIronBar(item.type, i.type) || self.usePressurePlate(item.type, i.type) || self.useFragment(item.type, i.type) || self.AcceptedByItemGroups(item.type, i.type))
                {
                    int count = Math.Min(required, item.stack);
                    required -= count;

                    var request = i.Clone();
                    request.stack = count;

                    TEStorageHeart heart = StoragePlayer.GetStorageHeart();
                    if (Main.netMode == 0)
                    {
                        request = heart.TryWithdraw(request);
                    }
                    else
                    {
                        NetHelper.SendWithdraw(heart.ID, request, NetHelper.StorageOp.WithdrawJustRemove);
                        request = new Item();
                    }

                    if (!request.IsAir)
                    {
                        request.TurnToAir();
                        changed = true;
                    }
                }
            }
            if (changed)
            {
                instance.RefreshItems();
            }
        }
Example #2
0
        static void DoDeposit(Item item)
        {
            TEStorageHeart heart = StoragePlayer.GetStorageHeart();

            if (Main.netMode == 0)
            {
                heart.DepositItem(item);
            }
            else
            {
                NetHelper.SendDeposit(heart.ID, item);
                item.SetDefaults(0, true);
            }
        }
Example #3
0
        static Item DoWithdraw(Item item, bool toInventory = false)
        {
            TEStorageHeart heart = StoragePlayer.GetStorageHeart();

            if (Main.netMode == 0)
            {
                return(heart.TryWithdraw(item));
            }
            else
            {
                var type = toInventory ? NetHelper.StorageOp.WithdrawToInventory : NetHelper.StorageOp.Withdraw;
                NetHelper.SendWithdraw(heart.ID, item, type);
                return(new Item());
            }
        }
Example #4
0
        public static bool TryDepositAll()
        {
            Player         player  = Main.player[Main.myPlayer];
            TEStorageHeart heart   = StoragePlayer.GetStorageHeart();
            bool           changed = false;

            if (Main.netMode == 0)
            {
                for (int k = 10; k < 50; k++)
                {
                    if (!player.inventory[k].IsAir && !player.inventory[k].favorited)
                    {
                        int oldStack = player.inventory[k].stack;
                        heart.DepositItem(player.inventory[k]);
                        if (oldStack != player.inventory[k].stack)
                        {
                            changed = true;
                        }
                    }
                }
            }
            else
            {
                List <Item> items = new List <Item>();
                for (int k = 10; k < 50; k++)
                {
                    if (!player.inventory[k].IsAir && !player.inventory[k].favorited)
                    {
                        items.Add(player.inventory[k]);
                    }
                }
                NetHelper.SendDepositAll(heart.ID, items);
                foreach (Item item in items)
                {
                    item.SetDefaults(0, true);
                }
                changed = true;
                items.Clear();
            }
            return(changed);
        }
Example #5
0
        public static void ReceiveOpResult(BinaryReader reader)
        {
            if (Main.netMode != 1)
            {
                return;
            }

            var op = (StorageOp)reader.ReadByte();

            if (op == StorageOp.Deposit || op == StorageOp.Withdraw || op == StorageOp.WithdrawToInventory)
            {
                Item item = ItemIO.Receive(reader, true);
                StoragePlayer.GetItem(item, op != StorageOp.WithdrawToInventory);
            }
            else if (op == StorageOp.DepositList)
            {
                int count = reader.ReadByte();
                for (int k = 0; k < count; k++)
                {
                    Item item = ItemIO.Receive(reader, true);
                    StoragePlayer.GetItem(item, false);
                }
            }
        }
Example #6
0
        public void RefreshItems()
        {
            items.Clear();

            TEStorageHeart heart = StoragePlayer.GetStorageHeart();

            if (heart == null)
            {
                return;
            }

            // @Volatile :SearchBars
            var searchByName = sideBar[0] as UISearchBar;
            var searchByMod  = sideBar[1] as UISearchBar;

            string nameFilter = "";

            if (searchByName.Text != searchByName.HintText)
            {
                nameFilter = searchByName.Text;
            }
            string modFilter = "";

            if (searchByMod.Text != searchByMod.HintText)
            {
                modFilter = searchByMod.Text;
            }

            var sort   = sideBar[2] as UIDropDown; // @Volatile :SortAndFilter
            var filter = sideBar[3] as UIDropDown; // @Volatile :SortAndFilter

            items.AddRange(Sorting.Items.FilterAndSort(heart.GetStoredItems(), (SortMode)sort.CurrentOption, (FilterMode)filter.CurrentOption, modFilter, nameFilter));
            items.ForEach(x => x.checkMat());

            Recipe.FindRecipes();
        }
Example #7
0
        public static void FindRecipesPatch(ref Dictionary <int, int> dict)
        {
            var heart = StoragePlayer.GetStorageHeart();

            if (heart != null)
            {
                foreach (Item item in heart.GetStoredItems())
                {
                    if (item.stack == 0)
                    {
                        continue;
                    }

                    if (dict.ContainsKey(item.netID))
                    {
                        dict[item.netID] += item.stack;
                    }
                    else
                    {
                        dict[item.netID] = item.stack;
                    }
                }
            }
        }
Example #8
0
        public static bool TryQuickStack(bool stackToCurrentlyOpenIfNotSearchForNearbyAccess)
        {
            Player player = Main.player[Main.myPlayer];

            if (player.IsStackingItems())
            {
                return(false);
            }

            TEStorageHeart heart = StoragePlayer.GetStorageHeart();

            if (!stackToCurrentlyOpenIfNotSearchForNearbyAccess)
            {
                // Vanilla quick stack to all chests is approximately a circle with radius 12.5
                for (int i = -13; i <= 13; i++)
                {
                    for (int j = -13; j <= 13; j++)
                    {
                        int x = (int)player.position.X / 16 + i;
                        int y = (int)player.position.Y / 16 + j;

                        Tile tile = Main.tile[x, y];
                        if (tile == null)
                        {
                            continue;
                        }

                        if (tile.frameX % 36 == 18)
                        {
                            x--;
                        }
                        if (tile.frameY % 36 == 18)
                        {
                            y--;
                        }

                        int     tileType = tile.type;
                        ModTile modTile  = TileLoader.GetTile(tileType);
                        if (modTile == null || !(modTile is StorageAccess))
                        {
                            continue;
                        }

                        if ((player.Center - new Vector2(x, y) * 16).Length() < 200)
                        {
                            heart = ((StorageAccess)modTile).GetHeart(x, y);
                        }
                    }
                }
            }
            if (heart == null)
            {
                return(false);
            }

            List <Item> items = new List <Item>();

            for (int k = 10; k < 50; k++)
            {
                var item = player.inventory[k];
                if (!item.IsAir && !item.favorited)
                {
                    if (heart.HasItem(item, true))
                    {
                        items.Add(player.inventory[k]);
                    }
                }
            }

            if (Main.netMode == 0)
            {
                foreach (Item item in items)
                {
                    heart.DepositItem(item);
                }
            }
            else
            {
                NetHelper.SendDepositAll(heart.ID, items);
                foreach (Item item in items)
                {
                    item.SetDefaults(0, true);
                }
            }

            // Play the stash sound
            // The if is here so minimize IL hacking in DrawInventory...
            if (items.Count != 0 && !stackToCurrentlyOpenIfNotSearchForNearbyAccess)
            {
                Main.PlaySound(7, -1, -1, 1, 1f, 0f);
            }
            return(items.Count != 0);
        }
Example #9
0
        // @Speed: We are calling this every frame. Do we need to?
        public void UpdateUI()
        {
            int padding = 2;

            if (dirty)
            {
                RebuildUI();
                dirty = false;
            }

            float slotWidth  = Main.inventoryBackTexture.Width * UI.InventoryScale;
            float slotHeight = Main.inventoryBackTexture.Height * UI.InventoryScale;

            float panelLeft       = 72;
            float panelHeight     = Main.screenHeight - Main.instance.invBottom - 40f;
            float innerPanelWidth = numColumns * (slotWidth + padding) + padding * 4;

            int rows = (int)(panelHeight / slotHeight) - 1;

            if (Crafting && rows > 4)
            {
                rows = 4;
            }

            panelHeight = rows * (slotHeight + padding) + padding * 2;
            if (Crafting)
            {
                UI.TrashSlotOffset = new Point16(4, (int)panelHeight);
            }
            else
            {
                UI.TrashSlotOffset = new Point16(4, (int)panelHeight + 7);
            }
            if (Main.recBigList)
            {
                UI.TrashSlotOffset = new Point16(3, 0);
            }

            Left.Set(panelLeft, 0f);
            Top.Set(Main.instance.invBottom, 0f);
            Width.Set(innerPanelWidth + 25, 0f);
            Height.Set(panelHeight, 0f);

            {
                slotZone.Width.Set(innerPanelWidth, 0);
                slotZone.Height.Set(0, 1);

                slotZone.SetDimensions(numColumns, rows);

                int noDisplayRows = (items.Count + numColumns - 1) / numColumns - rows; // @Magic

                var bar = slotZone.ScrollBar;
                bar.Left.Set(-22, 1f);
                bar.Top.Set(11, 0);
                bar.Height.Set(panelHeight - padding * 10, 0f);
                bar.SetView(1, noDisplayRows + 1);
            }
            {
                float sideBarLeft = innerPanelWidth + 35;

                for (int i = 0; i < sideBar.Count; i++)
                {
                    var element = sideBar[i];
                    int offset  = 0;
                    if (i > 2)
                    {
                        var sort = sideBar[2] as UIDropDown; // @Volatile :SortAndFilter
                        if (sort.Focused)
                        {
                            offset += sort.OptionsY + sort.OptionDiffY * (sort.Options.Count - 1);
                        }
                    }
                    if (i > 3)
                    {
                        var filter = sideBar[3] as UIDropDown; // @Volatile :SortAndFilter
                        if (filter.Focused)
                        {
                            offset += filter.OptionsY + filter.OptionDiffY * (filter.Options.Count - 1);
                        }
                    }
                    element.Left.Set(sideBarLeft, 0);
                    element.Top.Set(15 + 27 * i + offset, 0);
                }
            }
            {
                bottomBar.Top.Set(6, 1f);

                int numItems = 0;
                int capacity = 0;

                TEStorageHeart heart = StoragePlayer.GetStorageHeart();
                if (heart != null)
                {
                    foreach (var unit in heart.GetStorageUnits())
                    {
                        if (unit is TEStorageUnit)
                        {
                            var storageUnit = unit as TEStorageUnit;
                            numItems += storageUnit.NumItems;
                            capacity += storageUnit.Capacity;
                        }
                    }
                }
                string text = "";
                if (!Crafting)
                {
                    text = "• " + numItems + "/" + capacity + " Items";
                }

                capacityText.SetText(text);
            }
            Recalculate();
        }