Example #1
0
        /// <summary>Does the action.</summary>
        /// <param name="amount">Number of items.</param>
        /// <param name="clickLocation">Where the player clicked.</param>
        public override void PerformAction(int amount, Point clickLocation)
        {
            var heldItem           = this.Reflection.GetField <Item>(this.NativeShopMenu, "heldItem").GetValue();
            var priceAndStockField = this.Reflection.GetField <Dictionary <ISalable, int[]> >(this.NativeShopMenu, "itemPriceAndStock");
            var priceAndStockMap   = priceAndStockField.GetValue();

            Debug.Assert(priceAndStockMap.ContainsKey(this.ClickedItem));

            // Calculate the number to purchase
            int numInStock    = priceAndStockMap[this.ClickedItem][1];
            int itemPrice     = priceAndStockMap[this.ClickedItem][0];
            int currentMonies = ShopMenu.getPlayerCurrencyAmount(Game1.player, this.ShopCurrencyType);

            amount = Math.Min(Math.Min(amount, currentMonies / itemPrice), Math.Min(numInStock, this.ClickedItem.maximumStackSize()));

            // If we couldn't grab all that we wanted then only subtract the amount we were able to grab
            int numHeld  = heldItem?.Stack ?? 0;
            int overflow = Math.Max((numHeld + amount) - this.ClickedItem.maximumStackSize(), 0);

            amount -= overflow;

            this.Monitor.DebugLog($"Attempting to purchase {amount} of {this.ClickedItem.Name} for {itemPrice * amount}");

            if (amount <= 0)
            {
                return;
            }

            // Try to purchase the item - method returns true if it should be removed from the shop since there's no more.
            var purchaseMethodInfo = this.Reflection.GetMethod(this.NativeShopMenu, "tryToPurchaseItem");
            int index = BuyAction.GetClickedItemIndex(this.Reflection, this.NativeShopMenu, clickLocation);

            if (purchaseMethodInfo.Invoke <bool>(this.ClickedItem, heldItem, amount, clickLocation.X, clickLocation.Y, index))
            {
                this.Monitor.DebugLog($"Purchase of {this.ClickedItem.Name} successful");

                // remove the purchased item from the stock etc.
                priceAndStockMap.Remove(this.ClickedItem);
                priceAndStockField.SetValue(priceAndStockMap);

                var itemsForSaleField = this.Reflection.GetField <List <ISalable> >(this.NativeShopMenu, "forSale");
                var itemsForSale      = itemsForSaleField.GetValue();
                itemsForSale.Remove(this.ClickedItem);
                itemsForSaleField.SetValue(itemsForSale);
            }
        }
Example #2
0
        /// <summary>Creates an instance of the action.</summary>
        /// <param name="reflection">Reflection helper.</param>
        /// <param name="monitor">Monitor for logging.</param>
        /// <param name="shopMenu">Native shop menu.</param>
        /// <param name="mouse">Mouse position.</param>
        /// <returns>The instance or null if no valid item was selected.</returns>
        public new static ShopAction Create(IReflectionHelper reflection, IMonitor monitor, ShopMenu shopMenu, Point mouse)
        {
            var item = BuyAction.GetClickedShopItem(reflection, shopMenu, mouse);

            return(item != null ? new BuyAction(reflection, monitor, shopMenu, item) : null);
        }
 /// <summary>Main event that derived handlers use to setup necessary hooks and other things needed to take over how the stack is split.</summary>
 /// <returns>If the input was handled or consumed.</returns>
 protected override EInputHandled OpenSplitMenu()
 {
     this.CurrentShopAction = BuyAction.Create(this.Helper.Reflection, this.Monitor, this.NativeMenu, this.ClickItemLocation);
     return(TryOpenSplitMenu(this.CurrentShopAction));
 }