Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CJBItemSpawner/Framework/CJBItemSpawnerAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using StardewValley;
using static CJBItemSpawner.ICJBItemSpawnerAPI;

namespace CJBItemSpawner.Framework;

/// <inheritdoc />
public sealed class CJBItemSpawnerAPI : ICJBItemSpawnerAPI
{
/*********
** Fields
*********/
/// <summary>Build an item spawner menu.</summary>
private readonly Func<ItemMenu> BuildMenu;

/// <summary>The item repositories which returns all spawnable items.</summary>
private readonly IList<Repository> ItemRepositories;

/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="buildMenu">Method for building an item spawner menu.</param>
internal CJBItemSpawnerAPI(Func<ItemMenu> buildMenu, IList<Repository> itemRepositories)
{
this.BuildMenu = buildMenu;
this.ItemRepositories = itemRepositories;
}

/// <inheritdoc />
public void OpenItemSpawnerMenu()
{
Game1.activeClickableMenu = this.BuildMenu();
}

/// <inheritdoc />
public void AddRepository(Repository repository)
{
this.ItemRepositories.Add(repository);
}
}
1 change: 0 additions & 1 deletion CJBItemSpawner/Framework/ItemData/SearchableItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using StardewValley;
using StardewValley.ItemTypeDefinitions;

namespace CJBItemSpawner.Framework.ItemData;

Expand Down
29 changes: 29 additions & 0 deletions CJBItemSpawner/ICJBItemSpawnerAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using StardewValley;
using StardewValley.ItemTypeDefinitions;

namespace CJBItemSpawner;

/// <summary>The API which lets other mods interact with CJB Item Spawner.</summary>
public interface ICJBItemSpawnerAPI
{
/// <summary>
/// Open the item spawner menu.
/// </summary>
void OpenItemSpawnerMenu();

/// <summary>Add an item repository.</summary>
/// <param name="repository">An item repository which returns all spawnable items.</param>
void AddRepository(Repository repository);

/// <summary>Get all spawnable items.</summary>
/// <param name="onlyType">Only include items for the given <see cref="IItemDataDefinition.Identifier"/>.</param>
/// <param name="includeVariants">Whether to include flavored variants like "Sunflower Honey".</param>
public delegate IEnumerable<(string Type, string Id, CreateItem CreateItem)> Repository(string? onlyType = null, bool includeVariants = true);

/// <summary>Create an item instance.</summary>
/// <param name="type">The item type.</param>
/// <param name="id">The unqualified item ID.</param>
public delegate Item CreateItem(string type, string id);
}
27 changes: 26 additions & 1 deletion CJBItemSpawner/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using static CJBItemSpawner.ICJBItemSpawnerAPI;

namespace CJBItemSpawner;

Expand All @@ -26,6 +27,9 @@ internal class ModEntry : Mod
/// <summary>The item category filters available in the item spawner menu.</summary>
private ModDataCategory[] Categories = null!; // set in Entry

/// <summary>The item repositories which returns all spawnable items.</summary>
private readonly IList<Repository> ItemRepositories = new List<Repository>();

/// <summary>Manages the gamepad text entry UI.</summary>
private readonly TextEntryManager TextEntryManager = new();

Expand Down Expand Up @@ -65,6 +69,11 @@ public override void Entry(IModHelper helper)
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
}

/// <inheritdoc />
public override object GetApi()
{
return new CJBItemSpawnerAPI(this.BuildMenu, this.ItemRepositories);
}

/*********
** Private methods
Expand Down Expand Up @@ -122,7 +131,23 @@ private ItemMenu BuildMenu()
/// <summary>Get the items which can be spawned.</summary>
private IEnumerable<SpawnableItem> GetSpawnableItems()
{
foreach (SearchableItem entry in new ItemRepository().GetAll())
IEnumerable<SearchableItem> 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));

Expand Down