add support for filtering the item repo

This isn't used by SMAPI itself, but is used by some mods like Lookup Anything that copy this code.
This commit is contained in:
Jesse Plamondon-Willard 2021-05-17 20:52:01 -04:00
parent fe25a122f4
commit 75d7c10144
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 204 additions and 161 deletions

View File

@ -28,8 +28,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
** Public methods ** Public methods
*********/ *********/
/// <summary>Get all spawnable items.</summary> /// <summary>Get all spawnable items.</summary>
/// <param name="itemTypes">The item types to fetch (or null for any type).</param>
/// <param name="includeVariants">Whether to include flavored variants like "Sunflower Honey".</param>
[SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = "TryCreate invokes the lambda immediately.")] [SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = "TryCreate invokes the lambda immediately.")]
public IEnumerable<SearchableItem> GetAll() public IEnumerable<SearchableItem> GetAll(ItemType[] itemTypes = null, bool includeVariants = true)
{ {
// //
// //
@ -41,10 +43,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
// //
// //
IEnumerable<SearchableItem> GetAllRaw() IEnumerable<SearchableItem> GetAllRaw()
{ {
HashSet<ItemType> types = itemTypes?.Any() == true ? new HashSet<ItemType>(itemTypes) : null;
bool ShouldGet(ItemType type) => types == null || types.Contains(type);
// get tools // get tools
if (ShouldGet(ItemType.Tool))
{
for (int q = Tool.stone; q <= Tool.iridium; q++) for (int q = Tool.stone; q <= Tool.iridium; q++)
{ {
int quality = q; int quality = q;
@ -60,26 +66,44 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 1, _ => new Shears()); yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 1, _ => new Shears());
yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 2, _ => new Pan()); yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 2, _ => new Pan());
yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 3, _ => new Wand()); yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset + 3, _ => new Wand());
}
// clothing // clothing
if (ShouldGet(ItemType.Clothing))
{
foreach (int id in this.GetShirtIds()) foreach (int id in this.GetShirtIds())
yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID)); yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID));
}
// wallpapers // wallpapers
if (ShouldGet(ItemType.Wallpaper))
{
for (int id = 0; id < 112; id++) for (int id = 0; id < 112; id++)
yield return this.TryCreate(ItemType.Wallpaper, id, p => new Wallpaper(p.ID) { Category = SObject.furnitureCategory }); yield return this.TryCreate(ItemType.Wallpaper, id, p => new Wallpaper(p.ID) { Category = SObject.furnitureCategory });
}
// flooring // flooring
if (ShouldGet(ItemType.Flooring))
{
for (int id = 0; id < 56; id++) for (int id = 0; id < 56; id++)
yield return this.TryCreate(ItemType.Flooring, id, p => new Wallpaper(p.ID, isFloor: true) { Category = SObject.furnitureCategory }); yield return this.TryCreate(ItemType.Flooring, id, p => new Wallpaper(p.ID, isFloor: true) { Category = SObject.furnitureCategory });
}
// equipment // equipment
if (ShouldGet(ItemType.Boots))
{
foreach (int id in this.TryLoad<int, string>("Data\\Boots").Keys) foreach (int id in this.TryLoad<int, string>("Data\\Boots").Keys)
yield return this.TryCreate(ItemType.Boots, id, p => new Boots(p.ID)); yield return this.TryCreate(ItemType.Boots, id, p => new Boots(p.ID));
}
if (ShouldGet(ItemType.Hat))
{
foreach (int id in this.TryLoad<int, string>("Data\\hats").Keys) foreach (int id in this.TryLoad<int, string>("Data\\hats").Keys)
yield return this.TryCreate(ItemType.Hat, id, p => new Hat(p.ID)); yield return this.TryCreate(ItemType.Hat, id, p => new Hat(p.ID));
}
// weapons // weapons
if (ShouldGet(ItemType.Weapon))
{
foreach (int id in this.TryLoad<int, string>("Data\\weapons").Keys) foreach (int id in this.TryLoad<int, string>("Data\\weapons").Keys)
{ {
yield return this.TryCreate(ItemType.Weapon, id, p => (p.ID >= 32 && p.ID <= 34) yield return this.TryCreate(ItemType.Weapon, id, p => (p.ID >= 32 && p.ID <= 34)
@ -87,22 +111,33 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
: new MeleeWeapon(p.ID) : new MeleeWeapon(p.ID)
); );
} }
}
// furniture // furniture
if (ShouldGet(ItemType.Furniture))
{
foreach (int id in this.TryLoad<int, string>("Data\\Furniture").Keys) foreach (int id in this.TryLoad<int, string>("Data\\Furniture").Keys)
yield return this.TryCreate(ItemType.Furniture, id, p => Furniture.GetFurnitureInstance(p.ID)); yield return this.TryCreate(ItemType.Furniture, id, p => Furniture.GetFurnitureInstance(p.ID));
}
// craftables // craftables
if (ShouldGet(ItemType.BigCraftable))
{
foreach (int id in Game1.bigCraftablesInformation.Keys) foreach (int id in Game1.bigCraftablesInformation.Keys)
yield return this.TryCreate(ItemType.BigCraftable, id, p => new SObject(Vector2.Zero, p.ID)); yield return this.TryCreate(ItemType.BigCraftable, id, p => new SObject(Vector2.Zero, p.ID));
}
// objects // objects
if (ShouldGet(ItemType.Object) || ShouldGet(ItemType.Ring))
{
foreach (int id in Game1.objectInformation.Keys) foreach (int id in Game1.objectInformation.Keys)
{ {
string[] fields = Game1.objectInformation[id]?.Split('/'); string[] fields = Game1.objectInformation[id]?.Split('/');
// secret notes // secret notes
if (id == 79) if (id == 79)
{
if (ShouldGet(ItemType.Object))
{ {
foreach (int secretNoteId in this.TryLoad<int, string>("Data\\SecretNotes").Keys) foreach (int secretNoteId in this.TryLoad<int, string>("Data\\SecretNotes").Keys)
{ {
@ -114,13 +149,17 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
}); });
} }
} }
}
// ring // ring
else if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring else if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring
{
if (ShouldGet(ItemType.Ring))
yield return this.TryCreate(ItemType.Ring, id, p => new Ring(p.ID)); yield return this.TryCreate(ItemType.Ring, id, p => new Ring(p.ID));
}
// item // item
else else if (ShouldGet(ItemType.Object))
{ {
// spawn main item // spawn main item
SObject item = null; SObject item = null;
@ -135,6 +174,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
continue; continue;
// flavored items // flavored items
if (includeVariants)
{
switch (item.Category) switch (item.Category)
{ {
// fruit products // fruit products
@ -244,6 +285,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
} }
} }
} }
}
}
return GetAllRaw().Where(p => p != null); return GetAllRaw().Where(p => p != null);
} }