/// <summary>
        /// Shows any custom letter waiting in the mail box.
        /// Don't do anything if there is already a letter being shown.
        /// </summary>
        public static void ShowLetter()
        {
            if (ShownLetter.Value != null)
            {
                IList <string> mailbox = new List <string>(Game1.player.mailbox);
                mailbox.Insert(0, CustomMailId);
                Game1.player.mailbox.Set(mailbox);
            }
            else
            {
                if (Letters.Value.Count > 0 && NextLetterId.Value == CustomMailId)
                {
                    ShownLetter.Value = Letters.Value.First();
                }
            }

            if (ShownLetter.Value != null)
            {
                var activeClickableMenu = new LetterViewerMenuExtended(ShownLetter.Value.Text.Replace("@", Game1.player.Name), ShownLetter.Value.Id);
                MailFrameworkModEntry.ModHelper.Reflection.GetField <int>(activeClickableMenu, "whichBG").SetValue(ShownLetter.Value.WhichBG);
                if (ShownLetter.Value.LetterTexture != null)
                {
                    activeClickableMenu.letterTexture = ShownLetter.Value.LetterTexture;
                }
                activeClickableMenu.TextColor = ShownLetter.Value.TextColor;
                if (ShownLetter.Value.UpperRightCloseButtonTexture != null &&
                    activeClickableMenu.upperRightCloseButton != null)
                {
                    activeClickableMenu.upperRightCloseButton = new ClickableTextureComponent(new Rectangle(activeClickableMenu.xPositionOnScreen + activeClickableMenu.width - 36, activeClickableMenu.yPositionOnScreen - 8, 48, 48), ShownLetter.Value.UpperRightCloseButtonTexture, new Rectangle(0, 0, 12, 12), 4f, false);
                }

                Game1.activeClickableMenu = activeClickableMenu;
                List <Item> attachments = new List <Item>();
                if (ShownLetter.Value.Items != null)
                {
                    attachments.AddRange(ShownLetter.Value.Items);
                }

                List <Item> dynamicItems = ShownLetter.Value.DynamicItems?.Invoke(ShownLetter.Value);
                if (dynamicItems != null)
                {
                    attachments.AddRange(dynamicItems);
                }
                attachments.ForEach(
                    (i) =>
                {
                    var item   = i.getOne();
                    item.Stack = i.Stack;
                    activeClickableMenu.itemsToGrab.Add
                    (
                        new ClickableComponent
                        (
                            new Rectangle
                            (
                                activeClickableMenu.xPositionOnScreen + activeClickableMenu.width / 2 -
                                12 * Game1.pixelZoom
                                , activeClickableMenu.yPositionOnScreen + activeClickableMenu.height -
                                Game1.tileSize / 2 - 24 * Game1.pixelZoom
                                , 24 * Game1.pixelZoom
                                , 24 * Game1.pixelZoom
                            )
                            , item
                        )
                    {
                        myID            = 104,
                        leftNeighborID  = 101,
                        rightNeighborID = 102
                    }
                    );
                    activeClickableMenu.backButton.rightNeighborID   = 104;
                    activeClickableMenu.forwardButton.leftNeighborID = 104;
                    activeClickableMenu.populateClickableComponentList();
                    activeClickableMenu.snapToDefaultClickableComponent();
                });
                if (ShownLetter.Value.Recipe != null)
                {
                    string recipe = ShownLetter.Value.Recipe;
                    GetAndLearnRecipe(recipe, out string recipeString, out int dataArrayI18NSize, out string cookingOrCraftingText);

                    if (recipeString != null)
                    {
                        string learnedRecipe = recipe;

                        string[] strArray = recipeString.Split('/');
                        if (strArray.Length < dataArrayI18NSize)
                        {
                            if (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.en)
                            {
                                MailFrameworkModEntry.ModMonitor.Log($"The recipe '{recipe}' does not have a internationalized name. The default name will be used.", LogLevel.Warn);
                            }
                        }
                        else if (strArray[strArray.Length - 1] != "null")
                        {
                            learnedRecipe = strArray[strArray.Length - 1];
                        }

                        activeClickableMenu.cookingOrCrafting = cookingOrCraftingText;
                        activeClickableMenu.learnedRecipe     = learnedRecipe;
                    }
                }
                activeClickableMenu.exitFunction = (IClickableMenu.onExit)Delegate.Combine(activeClickableMenu.exitFunction, (IClickableMenu.onExit) delegate
                {
                    OnMenuClose(ShownLetter.Value);
                });
            }
            else
            {
                UpdateNextLetterId();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Shows any custom letter waiting in the mail box.
        /// Don't do anything if there is already a letter being shown.
        /// </summary>
        public static void ShowLetter()
        {
            if (_shownLetter == null)
            {
                if (Letters.Count > 0 && _nextLetterId == CustomMailId)
                {
                    _shownLetter = Letters.First();
                    var activeClickableMenu = new LetterViewerMenuExtended(_shownLetter.Text.Replace("@", Game1.player.Name), _shownLetter.Id);
                    MailFrameworkModEntry.ModHelper.Reflection.GetField <int>(activeClickableMenu, "whichBG").SetValue(_shownLetter.WhichBG);
                    if (_shownLetter.LetterTexture != null)
                    {
                        activeClickableMenu.letterTexture = _shownLetter.LetterTexture;
                    }
                    activeClickableMenu.TextColor = _shownLetter.TextColor;
                    if (_shownLetter.UpperRightCloseButtonTexture != null &&
                        activeClickableMenu.upperRightCloseButton != null)
                    {
                        activeClickableMenu.upperRightCloseButton = new ClickableTextureComponent(new Rectangle(activeClickableMenu.xPositionOnScreen + activeClickableMenu.width - 36, activeClickableMenu.yPositionOnScreen - 8, 48, 48), _shownLetter.UpperRightCloseButtonTexture, new Rectangle(0, 0, 12, 12), 4f, false);
                    }

                    Game1.activeClickableMenu = activeClickableMenu;
                    _shownLetter.Items?.ForEach(
                        (i) =>
                    {
                        var item   = i.getOne();
                        item.Stack = i.Stack;
                        activeClickableMenu.itemsToGrab.Add
                        (
                            new ClickableComponent
                            (
                                new Rectangle
                                (
                                    activeClickableMenu.xPositionOnScreen + activeClickableMenu.width / 2 -
                                    12 * Game1.pixelZoom
                                    , activeClickableMenu.yPositionOnScreen + activeClickableMenu.height -
                                    Game1.tileSize / 2 - 24 * Game1.pixelZoom
                                    , 24 * Game1.pixelZoom
                                    , 24 * Game1.pixelZoom
                                )
                                , item
                            )
                        {
                            myID            = 104,
                            leftNeighborID  = 101,
                            rightNeighborID = 102
                        }
                        );
                        activeClickableMenu.backButton.rightNeighborID   = 104;
                        activeClickableMenu.forwardButton.leftNeighborID = 104;
                        activeClickableMenu.populateClickableComponentList();
                        activeClickableMenu.snapToDefaultClickableComponent();
                    });
                    if (_shownLetter.Recipe != null)
                    {
                        string recipe = _shownLetter.Recipe;
                        Dictionary <string, string> cookingData  = MailFrameworkModEntry.ModHelper.Content.Load <Dictionary <string, string> >("Data\\CookingRecipes", ContentSource.GameContent);
                        Dictionary <string, string> craftingData = MailFrameworkModEntry.ModHelper.Content.Load <Dictionary <string, string> >("Data\\CraftingRecipes", ContentSource.GameContent);
                        string recipeString          = null;
                        int    dataArrayI18NSize     = 0;
                        string cookingOrCraftingText = null;
                        if (cookingData.ContainsKey(recipe))
                        {
                            if (!Game1.player.cookingRecipes.ContainsKey(recipe))
                            {
                                Game1.player.cookingRecipes.Add(recipe, 0);
                            }
                            recipeString          = cookingData[recipe];
                            dataArrayI18NSize     = 5;
                            cookingOrCraftingText = Game1.content.LoadString("Strings\\UI:LearnedRecipe_cooking");
                        }
                        else if (craftingData.ContainsKey(recipe))
                        {
                            if (!Game1.player.craftingRecipes.ContainsKey(recipe))
                            {
                                Game1.player.craftingRecipes.Add(recipe, 0);
                            }
                            recipeString          = craftingData[recipe];
                            dataArrayI18NSize     = 6;
                            cookingOrCraftingText = Game1.content.LoadString("Strings\\UI:LearnedRecipe_crafting");
                        }
                        else
                        {
                            MailFrameworkModEntry.ModMonitor.Log($"The recipe '{recipe}' was not found. The mail will ignore it.", LogLevel.Warn);
                        }

                        if (recipeString != null)
                        {
                            string learnedRecipe = recipe;
                            if (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.en)
                            {
                                string[] strArray = recipeString.Split('/');
                                if (strArray.Length < dataArrayI18NSize)
                                {
                                    MailFrameworkModEntry.ModMonitor.Log($"The recipe '{recipe}' does not have a internationalized name. The default name will be used.", LogLevel.Warn);
                                }
                                else
                                {
                                    learnedRecipe = strArray[strArray.Length - 1];
                                }
                            }

                            if (MailFrameworkModEntry.ModHelper.Reflection.GetMethod(activeClickableMenu, "getTextColor").Invoke <int>() == -1)
                            {
                                MailFrameworkModEntry.ModHelper.Reflection
                                .GetField <String>(activeClickableMenu, "cookingOrCrafting").SetValue(cookingOrCraftingText);
                                MailFrameworkModEntry.ModHelper.Reflection
                                .GetField <String>(activeClickableMenu, "learnedRecipe").SetValue(learnedRecipe);
                            }
                            else
                            {
                                activeClickableMenu.CookingOrCrafting = cookingOrCraftingText;
                                activeClickableMenu.LearnedRecipe     = learnedRecipe;
                            }
                        }
                    }

                    _events.Display.MenuChanged += OnMenuChanged;
                }
                else
                {
                    UpdateNextLetterId();
                }
            }
        }