private static bool TryPurchasingCustomCurrency(int price, List <Item[]> inv, List <Point> slotCoins, List <Point> slotsEmpty, List <Point> slotEmptyBank, List <Point> slotEmptyBank2, List <Point> slotEmptyBank3, List <Point> slotEmptyBank4, Player player, CustomCurrencySystem system)
    {
        int priceRemaining = price;

        foreach (Item pItem in player.inventory)
        {
            if (pItem.ModItem is Wallet wallet)
            {
                ItemStorage storage = wallet.GetItemStorage();

                for (var i = 0; i < storage.Count; i++)
                {
                    Item item = storage[i];
                    if (system.Accepts(item))
                    {
                        int toRemove = Math.Min(priceRemaining, item.stack);
                        storage.ModifyStackSize(player, i, -toRemove);
                        priceRemaining -= toRemove;

                        if (priceRemaining <= 0)
                        {
                            return(true);
                        }
                    }
                }
            }
        }

        return(system.TryPurchasing(priceRemaining, inv, slotCoins, slotsEmpty, slotEmptyBank, slotEmptyBank2, slotEmptyBank3, slotEmptyBank4));
    }
    private static void QuickBuff(ILContext il)
    {
        ILCursor cursor = new ILCursor(il);

        if (cursor.TryGotoNext(MoveType.AfterLabel, i => i.MatchLdloca(0), i => i.MatchCall(typeof(SoundStyle?).GetMethod("get_HasValue", ReflectionUtility.DefaultFlags)), i => i.MatchBrfalse(out _)))
        {
            cursor.Emit(OpCodes.Ldarg, 0);
            cursor.Emit(OpCodes.Ldloca, 0);

            cursor.EmitDelegate <QuickBuff_Del>((Player player, ref SoundStyle? sound) =>
            {
                if (!ModContent.GetInstance <PortableStorageConfig>().AlchemistBagQuickBuff)
                {
                    return;
                }

                if (player.CountBuffs() == Player.MaxBuffs)
                {
                    return;
                }

                foreach (AlchemistBag bag in GetAlchemistBags(player))
                {
                    ItemStorage storage = bag.GetItemStorage();

                    for (int i = 0; i < AlchemistBag.PotionSlots; i++)
                    {
                        Item item = storage[i];

                        if (item.IsAir || item.buffType <= 0 || item.DamageType == DamageClass.Summon)
                        {
                            continue;
                        }

                        int buffType = item.buffType;
                        bool canUse  = CombinedHooks.CanUseItem(player, item) && QuickBuff_ShouldBotherUsingThisBuff.Invoke <bool>(player, buffType);
                        if (item.mana > 0 && canUse)
                        {
                            if (player.CheckMana(item, -1, true, true))
                            {
                                player.manaRegenDelay = (int)player.maxRegenDelay;
                            }
                        }

                        if (player.whoAmI == Main.myPlayer && item.type == ItemID.Carrot && !Main.runningCollectorsEdition)
                        {
                            canUse = false;
                        }

                        if (buffType == 27)
                        {
                            buffType = Main.rand.Next(3);
                            if (buffType == 0)
                            {
                                buffType = 27;
                            }
                            if (buffType == 1)
                            {
                                buffType = 101;
                            }
                            if (buffType == 2)
                            {
                                buffType = 102;
                            }
                        }

                        if (!canUse)
                        {
                            continue;
                        }

                        ItemLoader.UseItem(item, player);
                        sound        = item.UseSound;
                        int buffTime = item.buffTime;
                        if (buffTime == 0)
                        {
                            buffTime = 3600;
                        }

                        player.AddBuff(buffType, buffTime);
                        if (item.consumable && ItemLoader.ConsumeItem(item, player))
                        {
                            storage.ModifyStackSize(player, i, -1);
                        }

                        if (player.CountBuffs() == Player.MaxBuffs)
                        {
                            return;
                        }
                    }
                }
            });
        }
    }
    private static void QuickMana(ILContext il)
    {
        ILCursor cursor = new ILCursor(il);
        ILLabel  label  = cursor.DefineLabel();

        if (cursor.TryGotoNext(MoveType.AfterLabel, i => i.MatchLdcI4(0), i => i.MatchStloc(0)))
        {
            cursor.Emit(OpCodes.Ldarg, 0);

            cursor.EmitDelegate <Func <Player, bool> >(player =>
            {
                if (!ModContent.GetInstance <PortableStorageConfig>().AlchemistBagQuickMana)
                {
                    return(false);
                }

                foreach (AlchemistBag bag in GetAlchemistBags(player))
                {
                    ItemStorage storage = bag.GetItemStorage();

                    for (int i = 0; i < AlchemistBag.PotionSlots; i++)
                    {
                        Item item = storage[i];
                        if (item.IsAir || item.healMana <= 0 || (player.potionDelay > 0 && item.potion) || !CombinedHooks.CanUseItem(player, item))
                        {
                            continue;
                        }

                        if (item.UseSound != null)
                        {
                            SoundEngine.PlaySound(item.UseSound.Value, player.position);
                        }
                        if (item.potion)
                        {
                            if (item.type == ItemID.RestorationPotion)
                            {
                                player.potionDelay = player.restorationDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                            else if (item.type == ItemID.Mushroom)
                            {
                                player.potionDelay = player.mushroomDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                            else
                            {
                                player.potionDelay = player.potionDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                        }

                        ItemLoader.UseItem(item, player);
                        int healLife     = player.GetHealLife(item, true);
                        int healMana     = player.GetHealMana(item, true);
                        player.statLife += healLife;
                        player.statMana += healMana;

                        if (player.statLife > player.statLifeMax2)
                        {
                            player.statLife = player.statLifeMax2;
                        }
                        if (player.statMana > player.statManaMax2)
                        {
                            player.statMana = player.statManaMax2;
                        }

                        if (healLife > 0 && Main.myPlayer == player.whoAmI)
                        {
                            player.HealEffect(healLife);
                        }

                        if (healMana > 0)
                        {
                            player.AddBuff(94, Player.manaSickTime);
                            if (Main.myPlayer == player.whoAmI)
                            {
                                player.ManaEffect(healMana);
                            }
                        }

                        if (ItemLoader.ConsumeItem(item, player))
                        {
                            storage.ModifyStackSize(player, i, -1);
                        }

                        Recipe.FindRecipes();

                        return(true);
                    }
                }

                return(false);
            });

            cursor.Emit(OpCodes.Brfalse, label);
            cursor.Emit(OpCodes.Ret);
            cursor.MarkLabel(label);
        }
    }