diff --git a/CJBItemSpawner/Framework/CJBItemSpawnerAPI.cs b/CJBItemSpawner/Framework/CJBItemSpawnerAPI.cs new file mode 100644 index 00000000..11608139 --- /dev/null +++ b/CJBItemSpawner/Framework/CJBItemSpawnerAPI.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using StardewValley; +using static CJBItemSpawner.ICJBItemSpawnerAPI; + +namespace CJBItemSpawner.Framework; + +/// +public sealed class CJBItemSpawnerAPI : ICJBItemSpawnerAPI +{ + /********* + ** Fields + *********/ + /// Build an item spawner menu. + private readonly Func BuildMenu; + + /// The item repositories which returns all spawnable items. + private readonly IList ItemRepositories; + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Method for building an item spawner menu. + internal CJBItemSpawnerAPI(Func buildMenu, IList itemRepositories) + { + this.BuildMenu = buildMenu; + this.ItemRepositories = itemRepositories; + } + + /// + public void OpenItemSpawnerMenu() + { + Game1.activeClickableMenu = this.BuildMenu(); + } + + /// + public void AddRepository(Repository repository) + { + this.ItemRepositories.Add(repository); + } +} diff --git a/CJBItemSpawner/Framework/ItemData/SearchableItem.cs b/CJBItemSpawner/Framework/ItemData/SearchableItem.cs index 33e050d3..dc4aa5fd 100644 --- a/CJBItemSpawner/Framework/ItemData/SearchableItem.cs +++ b/CJBItemSpawner/Framework/ItemData/SearchableItem.cs @@ -1,6 +1,5 @@ using System; using StardewValley; -using StardewValley.ItemTypeDefinitions; namespace CJBItemSpawner.Framework.ItemData; diff --git a/CJBItemSpawner/ICJBItemSpawnerAPI.cs b/CJBItemSpawner/ICJBItemSpawnerAPI.cs new file mode 100644 index 00000000..cb7a4bc2 --- /dev/null +++ b/CJBItemSpawner/ICJBItemSpawnerAPI.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using StardewValley; +using StardewValley.ItemTypeDefinitions; + +namespace CJBItemSpawner; + +/// The API which lets other mods interact with CJB Item Spawner. +public interface ICJBItemSpawnerAPI +{ + /// + /// Open the item spawner menu. + /// + void OpenItemSpawnerMenu(); + + /// Add an item repository. + /// An item repository which returns all spawnable items. + void AddRepository(Repository repository); + + /// Get all spawnable items. + /// Only include items for the given . + /// Whether to include flavored variants like "Sunflower Honey". + public delegate IEnumerable<(string Type, string Id, CreateItem CreateItem)> Repository(string? onlyType = null, bool includeVariants = true); + + /// Create an item instance. + /// The item type. + /// The unqualified item ID. + public delegate Item CreateItem(string type, string id); +} diff --git a/CJBItemSpawner/ModEntry.cs b/CJBItemSpawner/ModEntry.cs index 4a6b8e6f..4622b9ee 100644 --- a/CJBItemSpawner/ModEntry.cs +++ b/CJBItemSpawner/ModEntry.cs @@ -8,6 +8,7 @@ using StardewModdingAPI; using StardewModdingAPI.Events; using StardewValley; +using static CJBItemSpawner.ICJBItemSpawnerAPI; namespace CJBItemSpawner; @@ -26,6 +27,9 @@ internal class ModEntry : Mod /// The item category filters available in the item spawner menu. private ModDataCategory[] Categories = null!; // set in Entry + /// The item repositories which returns all spawnable items. + private readonly IList ItemRepositories = new List(); + /// Manages the gamepad text entry UI. private readonly TextEntryManager TextEntryManager = new(); @@ -65,6 +69,11 @@ public override void Entry(IModHelper helper) helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked; } + /// + public override object GetApi() + { + return new CJBItemSpawnerAPI(this.BuildMenu, this.ItemRepositories); + } /********* ** Private methods @@ -122,7 +131,23 @@ private ItemMenu BuildMenu() /// Get the items which can be spawned. private IEnumerable GetSpawnableItems() { - foreach (SearchableItem entry in new ItemRepository().GetAll()) + IEnumerable GetSpawnableItemsRaw() + { + foreach (SearchableItem entry in new ItemRepository().GetAll()) + { + yield return entry; + } + + foreach (Repository getAll in this.ItemRepositories) + { + foreach ((string type, string id, CreateItem createItem) in getAll()) + { + yield return new SearchableItem(type, id, p => createItem(p.Type, p.Id)); + } + } + } + + foreach (SearchableItem entry in GetSpawnableItemsRaw()) { ModDataCategory? category = this.Categories.FirstOrDefault(rule => rule.IsMatch(entry));