//Thanks to aEnigmatic
        public void InjectItems()
        {
            var page = Game1.activeClickableMenu;

            if (page == null)
            {
                return;
            }

            // Find nearby chests
            var nearbyChests = _chestController.GetChests().ToList();

            if (!nearbyChests.Any())
            {
                return;
            }

            if (page is CraftingPage craftingPage)
            {
                craftingPage._materialContainers.AddRange(nearbyChests);
            }
            else
            {
                ModEntry.Instance.Monitor.Log($"Failed to inject items into: {page.GetType()}. Is it from an incompatible mod?",
                                              LogLevel.Warn);
            }
        }
예제 #2
0
        //Thanks to aEnigmatic
        public void InjectItems()
        {
            var page = Game1.activeClickableMenu;

            if (page == null)
            {
                return;
            }

            // Find nearby chests
            var nearbyChests = _chestController.GetChests().ToList();

            if (!nearbyChests.Any())
            {
                return;
            }

            // Add them as material containers to current CraftingPage
            var prop = page.GetType().GetField("_materialContainers", BindingFlags.NonPublic | BindingFlags.Instance);

            if (prop == null)
            {
                ModEntry.Instance.Log($"CraftFromChests failed: {page.GetType()}._materialContainers not found.");
                return;
            }

            var original = prop.GetValue(page) as List <Chest>;
            var modified = new List <Chest>();

            if (original?.Count > 0)
            {
                modified.AddRange(original);
            }
            modified.AddRange(nearbyChests);

            prop.SetValue(page, modified.Distinct().ToList());
        }