Modding Help How can I sell a certain number of seeds from the Pierre store like the Krobus store?

Discussion in 'Mods' started by jae ha, Dec 25, 2019.

  1. jae ha

    jae ha Seal Broken

    I want to sell only a fixed quantity of seeds in Pierre's store, like the Krobus store, but I don't know how.
    (ex. only 5 parsnip seeds per day)
    Is there a relevant mod or source code to reference?

    I need to get the item list through "shopmenu" and change it, but I don't know how.




    저는 피에르 상점의 씨앗들을 크로버스의 상점처럼 정해진 갯수만큼만 판매하게 하고싶은데 어떻게 해야할지 모르겠습니다.
    (ex. 파스닙 씨앗 하루에 5개만 판매)
    혹시 관련된 모드나 참고할만한 소스코드가 있을까요?


    I am not good at English so I wrote it through Google Translate.
    Meaning may not be conveyed properly.
     
    • jahangmar

      jahangmar Sandwich Man

      @jae ha You want to modify
      Code:
      shopMenu.itemPriceAndStock
      It's a Dictionary of type Dictionary<ISalable, int[]>. It maps an item to an array of type int. The index 0 of that array is the price of the item and the index 1 is the stock.
      Example:
      Code:
      parsnipSeed.Stack = YOUR_AMOUNT;
      shopMenu.itemPriceAndStock.Add(parsnipSeed, new int[2] { parsnipSeed.salePrice(), parsnipSeed.Stack });
      Adds the item parsnipSeed to the shop inventory with price parsnipSeed.salePrice() and stock parsnipSeed.Stack.
       
        jae ha likes this.
      • MouseyPounds

        MouseyPounds Cosmic Narwhal

        You'll need to hook the MenuChanged event, then verify it is a ShopMenu run by Pierre. After that, you'll use reflection to access the itemPriceAndStock field and change whatever amounts are listed from 2147483647 to whatever you want.
         
          jae ha likes this.
        • jahangmar

          jahangmar Sandwich Man

          Since Stardew Valley 1.4 the fields forSale and itemPriceAndStock are public so reflections are not needed anymore. Also the ShopMenu implementation is a bit inconsistent: Sometimes it checks the item.Stack value instead of the value in itemPriceAndStock.
           
            jae ha likes this.
          • jae ha

            jae ha Seal Broken

            Code:
            public class ModEntry : Mod
                {
                    Dictionary<ISalable, int[]> d1;
                    bool firstRun = true;
                    public override void Entry(IModHelper helper)
                    {
                        helper.Events.Display.MenuChanged += OnMenuChanged;
                        helper.Events.GameLoop.DayStarted += OnDayStarted;
                    }
                    private void OnDayStarted(object sender, DayStartedEventArgs e)
                    {
                        d1 = null;
                        firstRun = true;
                    }
                    private void OnMenuChanged(object sender, MenuChangedEventArgs e)
                    {
                        //check shopmenu
                        if (e.NewMenu is ShopMenu menu)
                        {
                            ShopMenu shopmenu = menu;
                            //check info               
                            this.Monitor.Log($"today : {Game1.year}, {Game1.currentSeason}. who : {shopmenu.portraitPerson.Name}", LogLevel.Debug);
                            if (shopmenu.portraitPerson.Name.Equals("Pierre"))
                            {
                                if (Game1.year <= 3 && firstRun == true)
                                {
                                    var target = shopmenu.itemPriceAndStock.ToList();
                                    for (int i = 0; i < shopmenu.itemPriceAndStock.Count; i++)
                                    {
                                        if (target[i].Key.Name.Contains("Seeds"))
                                        {
                                            target[i].Value[1] = 5;
                                            shopmenu.forSale[i].Stack = 5;
                                        }
                                        this.Monitor.Log($"이거 뭐임 타입 : {target[i].Key.GetType()}", LogLevel.Debug);
                                        this.Monitor.Log($"이거 뭐임2 : {target[i].Key.Name}", LogLevel.Debug);
                                    }
                                    d1 = shopmenu.itemPriceAndStock;
                                    firstRun = false;
                                }
                               
                                if(firstRun == false)
                                {
                                shopmenu.setItemPriceAndStock(d1);
                                }
                            }
                        }
                    }
                }
            
            I want to make Pierre sell 5 seeds a day like Krobus's store.(or like a Traveling Cart)
            So I wrote the code that way.
            But if you write code like that
            "Tulip Bulb" or
            This does not apply to seeds such as "Bean Starter".
            I want to apply it properly as a value that distinguishes Seeds from Object.
            How do I change the code?
            Please help
            I'm not good at English so I wrote it through Google Translate.
            It's hard to reply
            I appreciate the people who give me all the comments. Thank you

            It was very helpful. Thank you.

            It was very helpful. Thank you.
             
              Last edited by a moderator: Dec 27, 2019
            • jahangmar

              jahangmar Sandwich Man

              To Check if an item is a seed you can check if item.Category ==StardewValley.Object.SeedsCategory. The value of SeedsCategory is -74 and you can find those values here.
              I haven't tested your code but you make a copy of itemPriceAndStock by calling the toList() function, then you modify your copy (and not the original value) and you are setting itemPriceAndStock to the value of "d1" which is not modified.

              Edit:
              Tree saplings are also in the seeds category.
              You can check out the AddSlingshots() function of one of my mods which allows the slingshots to be bought.
               
                Last edited: Dec 27, 2019
                jae ha likes this.
              • jae ha

                jae ha Seal Broken

                Thank you
                정말 감사합니다. 많은 도움이 되었습니다. 감사합니다!
                 

                Share This Page