Beispiel #1
0
        public static void Cmd_Give(string s, string[] args)
        {
            const int defaultQuantity = 25;
            int       quantity        = args.Length == 0
                                ? defaultQuantity
                                : int.TryParse(args[0], out int argQuantity)
                                        ? argQuantity
                                        : defaultQuantity;

            Log.D($"Adding {quantity} of each unlocked raised bed. Use '{ModEntry.CommandPrefix}giveall' to add all varieties.");

            IEnumerable <string> unlockedKeys = Game1.player.craftingRecipes.Keys
                                                .Where(recipe => recipe.StartsWith(OutdoorPot.GenericName))
                                                .Select(recipe => OutdoorPot.GetVariantKeyFromName(recipe));

            if (!unlockedKeys.Any())
            {
                Log.D($"No raised bed recipes are unlocked! Use '{ModEntry.CommandPrefix}giveall' to add all varieties.");
            }
            else
            {
                foreach (string variantKey in unlockedKeys)
                {
                    ModEntry.Give(variantKey: variantKey, quantity: quantity);
                }
            }
        }
        /// <summary>
        /// Required to draw correct object sprites and strings in crafting menu.
        /// Event handlers on StardewModdingAPI.Events.Display.MenuChanged were inconsistent.
        /// </summary>
        public static void CraftingPage_LayoutRecipes_Postfix(
            CraftingPage __instance)
        {
            int unlockedCount = Game1.player.craftingRecipes.Keys.Count(c => c.StartsWith(OutdoorPot.GenericName));

            int[] matchesPerDict = new int[__instance.pagesOfCraftingRecipes.Count];
            int   i = 0;

            foreach (Dictionary <ClickableTextureComponent, CraftingRecipe> dict in __instance.pagesOfCraftingRecipes)
            {
                List <KeyValuePair <ClickableTextureComponent, CraftingRecipe> > matches = dict
                                                                                           .Where(pair => pair.Value.name.StartsWith(OutdoorPot.GenericName))
                                                                                           .ToList();
                matches.ForEach(pair =>
                {
                    string variantKey = OutdoorPot.GetVariantKeyFromName(name: pair.Value.name);

                    // Sprite
                    pair.Key.texture    = ModEntry.Sprites[ModEntry.ItemDefinitions[variantKey].SpriteKey];
                    pair.Key.sourceRect = OutdoorPot.GetSpriteSourceRectangle(spriteIndex: ModEntry.ItemDefinitions[variantKey].SpriteIndex);

                    // Strings
                    pair.Value.DisplayName = OutdoorPot.GetDisplayNameFromName(pair.Value.name);
                    pair.Value.description = OutdoorPot.GetRawDescription();
                });
                matchesPerDict[i++] = matches.Count();
            }
            Log.T($"Found {string.Join("/", matchesPerDict)} garden beds in crafting menu pages ({unlockedCount} unlocked).");
        }
        /// <summary>
        /// Replace logic for crafting objects in base game crafting menu to create the appropriate garden bed for the crafting recipe variant.
        /// </summary>
        public static bool CraftingPage_ClickCraftingRecipe_Prefix(
            CraftingPage __instance,
            int ___currentCraftingPage,
            ref Item ___heldItem,
            ClickableTextureComponent c,
            bool playSound = true)
        {
            try
            {
                // Fetch an instance of any clicked-on craftable in the crafting menu
                CraftingRecipe recipe = __instance.pagesOfCraftingRecipes[___currentCraftingPage][c];

                // Fall through to default method for any other craftables
                if (!recipe.name.StartsWith(OutdoorPot.GenericName))
                {
                    return(true);
                }

                OutdoorPot item = new OutdoorPot(
                    variantKey: OutdoorPot.GetVariantKeyFromName(recipe.name),
                    tileLocation: Vector2.Zero);

                // Behaviours as from base method
                recipe.consumeIngredients(null);
                if (playSound)
                {
                    Game1.playSound("coin");
                }
                if (___heldItem == null)
                {
                    ___heldItem = item;
                }
                else if (___heldItem.canStackWith(item))
                {
                    ___heldItem.addToStack(item);
                }
                if (Game1.player.craftingRecipes.ContainsKey(recipe.name))
                {
                    Game1.player.craftingRecipes[recipe.name] += recipe.numberProducedPerCraft;
                }
                Game1.stats.checkForCraftingAchievements();
                if (Game1.options.gamepadControls && ___heldItem != null && Game1.player.couldInventoryAcceptThisItem(___heldItem))
                {
                    Game1.player.addItemToInventoryBool(___heldItem);
                    ___heldItem = null;
                }

                return(false);
            }
            catch (Exception e)
            {
                HarmonyPatches.ErrorHandler(e);
            }
            return(true);
        }