summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-11-21 14:10:09 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-11-21 14:10:09 -0500
commit85b947dced10f5398055af36b5fdb6b8d32a6a6b (patch)
treeefc2f877a6f41869a40b6d0a320e6d5c4fd591ca /src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs
parente20d26adcf7276c7f12ab9ab6ea0311953aa5194 (diff)
parenta0cb83ed406fc0447e8edf00e680585005480d23 (diff)
downloadSMAPI-85b947dced10f5398055af36b5fdb6b8d32a6a6b.tar.gz
SMAPI-85b947dced10f5398055af36b5fdb6b8d32a6a6b.tar.bz2
SMAPI-85b947dced10f5398055af36b5fdb6b8d32a6a6b.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs')
-rw-r--r--src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs194
1 files changed, 127 insertions, 67 deletions
diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs
index 37f5f8d1..d1dd758b 100644
--- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs
+++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs
@@ -6,6 +6,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
using StardewValley;
+using StardewValley.GameData.FishPond;
using StardewValley.Menus;
using StardewValley.Objects;
using StardewValley.Tools;
@@ -30,47 +31,77 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
[SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = "TryCreate invokes the lambda immediately.")]
public IEnumerable<SearchableItem> GetAll()
{
+ //
+ //
+ // Be careful about closure variable capture here!
+ //
+ // SearchableItem stores the Func<Item> to create new instances later. Loop variables passed into the
+ // function will be captured, so every func in the loop will use the value from the last iteration. Use the
+ // TryCreate(type, id, entity => item) form to avoid the issue, or create a local variable to pass in.
+ //
+ //
+
+
IEnumerable<SearchableItem> GetAllRaw()
{
// get tools
- for (int quality = Tool.stone; quality <= Tool.iridium; quality++)
+ for (int q = Tool.stone; q <= Tool.iridium; q++)
{
- yield return this.TryCreate(ItemType.Tool, ToolFactory.axe, () => ToolFactory.getToolFromDescription(ToolFactory.axe, quality));
- yield return this.TryCreate(ItemType.Tool, ToolFactory.hoe, () => ToolFactory.getToolFromDescription(ToolFactory.hoe, quality));
- yield return this.TryCreate(ItemType.Tool, ToolFactory.pickAxe, () => ToolFactory.getToolFromDescription(ToolFactory.pickAxe, quality));
- yield return this.TryCreate(ItemType.Tool, ToolFactory.wateringCan, () => ToolFactory.getToolFromDescription(ToolFactory.wateringCan, quality));
+ int quality = q;
+
+ yield return this.TryCreate(ItemType.Tool, ToolFactory.axe, _ => ToolFactory.getToolFromDescription(ToolFactory.axe, quality));
+ yield return this.TryCreate(ItemType.Tool, ToolFactory.hoe, _ => ToolFactory.getToolFromDescription(ToolFactory.hoe, quality));
+ yield return this.TryCreate(ItemType.Tool, ToolFactory.pickAxe, _ => ToolFactory.getToolFromDescription(ToolFactory.pickAxe, quality));
+ yield return this.TryCreate(ItemType.Tool, ToolFactory.wateringCan, _ => ToolFactory.getToolFromDescription(ToolFactory.wateringCan, quality));
if (quality != Tool.iridium)
- yield return this.TryCreate(ItemType.Tool, ToolFactory.fishingRod, () => ToolFactory.getToolFromDescription(ToolFactory.fishingRod, quality));
+ yield return this.TryCreate(ItemType.Tool, ToolFactory.fishingRod, _ => ToolFactory.getToolFromDescription(ToolFactory.fishingRod, quality));
}
- yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset, () => new MilkPail()); // these don't have any sort of ID, so we'll just assign some arbitrary ones
- 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 + 3, () => new Wand());
+ yield return this.TryCreate(ItemType.Tool, this.CustomIDOffset, _ => new MilkPail()); // these don't have any sort of ID, so we'll just assign some arbitrary ones
+ 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 + 3, _ => new Wand());
// clothing
- foreach (int id in Game1.clothingInformation.Keys)
- yield return this.TryCreate(ItemType.Clothing, id, () => new Clothing(id));
+ {
+ // items
+ HashSet<int> clothingIds = new HashSet<int>();
+ foreach (int id in Game1.clothingInformation.Keys)
+ {
+ if (id < 0)
+ continue; // placeholder data for character customization clothing below
+
+ clothingIds.Add(id);
+ yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID));
+ }
+
+ // character customization shirts (some shirts in this range have no data, but game has special logic to handle them)
+ for (int id = 1000; id <= 1111; id++)
+ {
+ if (!clothingIds.Contains(id))
+ yield return this.TryCreate(ItemType.Clothing, id, p => new Clothing(p.ID));
+ }
+ }
// wallpapers
for (int id = 0; id < 112; id++)
- yield return this.TryCreate(ItemType.Wallpaper, id, () => new Wallpaper(id) { Category = SObject.furnitureCategory });
+ yield return this.TryCreate(ItemType.Wallpaper, id, p => new Wallpaper(p.ID) { Category = SObject.furnitureCategory });
// flooring
for (int id = 0; id < 56; id++)
- yield return this.TryCreate(ItemType.Flooring, id, () => new Wallpaper(id, isFloor: true) { Category = SObject.furnitureCategory });
+ yield return this.TryCreate(ItemType.Flooring, id, p => new Wallpaper(p.ID, isFloor: true) { Category = SObject.furnitureCategory });
// equipment
foreach (int id in this.TryLoad<int, string>("Data\\Boots").Keys)
- yield return this.TryCreate(ItemType.Boots, id, () => new Boots(id));
+ yield return this.TryCreate(ItemType.Boots, id, p => new Boots(p.ID));
foreach (int id in this.TryLoad<int, string>("Data\\hats").Keys)
- yield return this.TryCreate(ItemType.Hat, id, () => new Hat(id));
+ yield return this.TryCreate(ItemType.Hat, id, p => new Hat(p.ID));
// weapons
foreach (int id in this.TryLoad<int, string>("Data\\weapons").Keys)
{
- yield return this.TryCreate(ItemType.Weapon, id, () => (id >= 32 && id <= 34)
- ? (Item)new Slingshot(id)
- : new MeleeWeapon(id)
+ yield return this.TryCreate(ItemType.Weapon, id, p => (p.ID >= 32 && p.ID <= 34)
+ ? (Item)new Slingshot(p.ID)
+ : new MeleeWeapon(p.ID)
);
}
@@ -78,14 +109,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
foreach (int id in this.TryLoad<int, string>("Data\\Furniture").Keys)
{
if (id == 1466 || id == 1468 || id == 1680)
- yield return this.TryCreate(ItemType.Furniture, id, () => new TV(id, Vector2.Zero));
+ yield return this.TryCreate(ItemType.Furniture, id, p => new TV(p.ID, Vector2.Zero));
else
- yield return this.TryCreate(ItemType.Furniture, id, () => new Furniture(id, Vector2.Zero));
+ yield return this.TryCreate(ItemType.Furniture, id, p => new Furniture(p.ID, Vector2.Zero));
}
// craftables
foreach (int id in Game1.bigCraftablesInformation.Keys)
- yield return this.TryCreate(ItemType.BigCraftable, id, () => new SObject(Vector2.Zero, id));
+ yield return this.TryCreate(ItemType.BigCraftable, id, p => new SObject(Vector2.Zero, p.ID));
// objects
foreach (int id in Game1.objectInformation.Keys)
@@ -97,7 +128,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
{
foreach (int secretNoteId in this.TryLoad<int, string>("Data\\SecretNotes").Keys)
{
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset + secretNoteId, () =>
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset + secretNoteId, _ =>
{
SObject note = new SObject(79, 1);
note.name = $"{note.name} #{secretNoteId}";
@@ -108,18 +139,18 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
// ring
else if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring
- yield return this.TryCreate(ItemType.Ring, id, () => new Ring(id));
+ yield return this.TryCreate(ItemType.Ring, id, p => new Ring(p.ID));
// item
else
{
// spawn main item
SObject item = null;
- yield return this.TryCreate(ItemType.Object, id, () =>
+ yield return this.TryCreate(ItemType.Object, id, p =>
{
- return item = (id == 812 // roe
- ? new ColoredObject(id, 1, Color.White)
- : new SObject(id, 1)
+ return item = (p.ID == 812 // roe
+ ? new ColoredObject(p.ID, 1, Color.White)
+ : new SObject(p.ID, 1)
);
});
if (item == null)
@@ -131,7 +162,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
// fruit products
case SObject.FruitsCategory:
// wine
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 2 + id, () => new SObject(348, 1)
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 2 + item.ParentSheetIndex, _ => new SObject(348, 1)
{
Name = $"{item.Name} Wine",
Price = item.Price * 3,
@@ -140,7 +171,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
});
// jelly
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 3 + id, () => new SObject(344, 1)
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 3 + item.ParentSheetIndex, _ => new SObject(344, 1)
{
Name = $"{item.Name} Jelly",
Price = 50 + item.Price * 2,
@@ -152,7 +183,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
// vegetable products
case SObject.VegetableCategory:
// juice
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 4 + id, () => new SObject(350, 1)
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 4 + item.ParentSheetIndex, _ => new SObject(350, 1)
{
Name = $"{item.Name} Juice",
Price = (int)(item.Price * 2.25d),
@@ -161,7 +192,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
});
// pickled
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + id, () => new SObject(342, 1)
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + item.ParentSheetIndex, _ => new SObject(342, 1)
{
Name = $"Pickled {item.Name}",
Price = 50 + item.Price * 2,
@@ -172,7 +203,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
// flower honey
case SObject.flowersCategory:
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + id, () =>
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 5 + item.ParentSheetIndex, _ =>
{
SObject honey = new SObject(Vector2.Zero, 340, $"{item.Name} Honey", false, true, false, false)
{
@@ -185,40 +216,49 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
break;
// roe and aged roe (derived from FishPond.GetFishProduce)
- case SObject.sellAtFishShopCategory when id == 812:
- foreach (var pair in Game1.objectInformation)
+ case SObject.sellAtFishShopCategory when item.ParentSheetIndex == 812:
{
- // get input
- SObject input = this.TryCreate(ItemType.Object, -1, () => new SObject(pair.Key, 1))?.Item as SObject;
- if (input == null || input.Category != SObject.FishCategory)
- continue;
- Color color = this.GetRoeColor(input);
-
- // yield roe
- SObject roe = null;
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + id, () =>
- {
- roe = new ColoredObject(812, 1, color)
- {
- name = $"{input.Name} Roe",
- preserve = { Value = SObject.PreserveType.Roe },
- preservedParentSheetIndex = { Value = input.ParentSheetIndex }
- };
- roe.Price += input.Price / 2;
- return roe;
- });
-
- // aged roe
- if (roe != null && pair.Key != 698) // aged sturgeon roe is caviar, which is a separate item
+ this.GetRoeContextTagLookups(out HashSet<string> simpleTags, out List<List<string>> complexTags);
+
+ foreach (var pair in Game1.objectInformation)
{
- yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + id, () => new ColoredObject(447, 1, color)
+ // get input
+ SObject input = this.TryCreate(ItemType.Object, pair.Key, p => new SObject(p.ID, 1))?.Item as SObject;
+ var inputTags = input?.GetContextTags();
+ if (inputTags?.Any() != true)
+ continue;
+
+ // check if roe-producing fish
+ if (!inputTags.Any(tag => simpleTags.Contains(tag)) && !complexTags.Any(set => set.All(tag => input.HasContextTag(tag))))
+ continue;
+
+ // yield roe
+ SObject roe = null;
+ Color color = this.GetRoeColor(input);
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + item.ParentSheetIndex, _ =>
{
- name = $"Aged {input.Name} Roe",
- Category = -27,
- preserve = { Value = SObject.PreserveType.AgedRoe },
- preservedParentSheetIndex = { Value = input.ParentSheetIndex },
- Price = roe.Price * 2
+ roe = new ColoredObject(812, 1, color)
+ {
+ name = $"{input.Name} Roe",
+ preserve = { Value = SObject.PreserveType.Roe },
+ preservedParentSheetIndex = { Value = input.ParentSheetIndex }
+ };
+ roe.Price += input.Price / 2;
+ return roe;
});
+
+ // aged roe
+ if (roe != null && pair.Key != 698) // aged sturgeon roe is caviar, which is a separate item
+ {
+ yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + item.ParentSheetIndex, _ => new ColoredObject(447, 1, color)
+ {
+ name = $"Aged {input.Name} Roe",
+ Category = -27,
+ preserve = { Value = SObject.PreserveType.AgedRoe },
+ preservedParentSheetIndex = { Value = input.ParentSheetIndex },
+ Price = roe.Price * 2
+ });
+ }
}
}
break;
@@ -234,6 +274,26 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
/*********
** Private methods
*********/
+ /// <summary>Get optimized lookups to match items which produce roe in a fish pond.</summary>
+ /// <param name="simpleTags">A lookup of simple singular tags which match a roe-producing fish.</param>
+ /// <param name="complexTags">A list of tag sets which match roe-producing fish.</param>
+ private void GetRoeContextTagLookups(out HashSet<string> simpleTags, out List<List<string>> complexTags)
+ {
+ simpleTags = new HashSet<string>();
+ complexTags = new List<List<string>>();
+
+ foreach (FishPondData data in Game1.content.Load<List<FishPondData>>("Data\\FishPondData"))
+ {
+ if (data.ProducedItems.All(p => p.ItemID != 812))
+ continue; // doesn't produce roe
+
+ if (data.RequiredTags.Count == 1 && !data.RequiredTags[0].StartsWith("!"))
+ simpleTags.Add(data.RequiredTags[0]);
+ else
+ complexTags.Add(data.RequiredTags);
+ }
+ }
+
/// <summary>Try to load a data file, and return empty data if it's invalid.</summary>
/// <typeparam name="TKey">The asset key type.</typeparam>
/// <typeparam name="TValue">The asset value type.</typeparam>
@@ -255,13 +315,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework
/// <param name="type">The item type.</param>
/// <param name="id">The unique ID (if different from the item's parent sheet index).</param>
/// <param name="createItem">Create an item instance.</param>
- private SearchableItem TryCreate(ItemType type, int id, Func<Item> createItem)
+ private SearchableItem TryCreate(ItemType type, int id, Func<SearchableItem, Item> createItem)
{
try
{
- var item = createItem();
- item.getDescription(); // force-load item data, so it crashes here if it's invalid
- return new SearchableItem(type, id, item);
+ var item = new SearchableItem(type, id, createItem);
+ item.Item.getDescription(); // force-load item data, so it crashes here if it's invalid
+ return item;
}
catch
{