diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 17:37:30 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 17:37:30 -0400 |
commit | 40e8d3da0e204117d0a6de91b368ef420eb31df0 (patch) | |
tree | 8c1d2f1149b9eff23db90c7dafad0f05de279028 /src/TrainerMod/Framework/Commands | |
parent | a0c4746c27656d6617853890d270072c13843c64 (diff) | |
download | SMAPI-40e8d3da0e204117d0a6de91b368ef420eb31df0.tar.gz SMAPI-40e8d3da0e204117d0a6de91b368ef420eb31df0.tar.bz2 SMAPI-40e8d3da0e204117d0a6de91b368ef420eb31df0.zip |
migrate list_items command to new item repository (#302)
Diffstat (limited to 'src/TrainerMod/Framework/Commands')
-rw-r--r-- | src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs | 55 |
1 files changed, 25 insertions, 30 deletions
diff --git a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs index 30c3de3b..7f4f454c 100644 --- a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using StardewModdingAPI; -using StardewValley; -using StardewValley.Objects; using TrainerMod.Framework.ItemData; namespace TrainerMod.Framework.Commands.Player @@ -12,6 +10,13 @@ namespace TrainerMod.Framework.Commands.Player internal class ListItemsCommand : TrainerCommand { /********* + ** Properties + *********/ + /// <summary>Provides methods for searching and constructing items.</summary> + private readonly ItemRepository Items = new ItemRepository(); + + + /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> @@ -24,12 +29,24 @@ namespace TrainerMod.Framework.Commands.Player /// <param name="args">The command arguments.</param> public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - var matches = this.GetItems(args.ToArray()).ToArray(); + // validate + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } - // show matches + // handle + SearchableItem[] matches = + ( + from item in this.GetItems(args.ToArray()) + orderby item.Type.ToString(), item.Name + select item + ) + .ToArray(); string summary = "Searching...\n"; if (matches.Any()) - monitor.Log(summary + this.GetTableString(matches, new[] { "type", "id", "name" }, val => new[] { val.Type.ToString(), val.ID.ToString(), val.Name }), LogLevel.Info); + monitor.Log(summary + this.GetTableString(matches, new[] { "type", "name", "id" }, val => new[] { val.Type.ToString(), val.Name, val.ID.ToString() }), LogLevel.Info); else monitor.Log(summary + "No items found", LogLevel.Info); } @@ -40,7 +57,7 @@ namespace TrainerMod.Framework.Commands.Player *********/ /// <summary>Get all items which can be searched and added to the player's inventory through the console.</summary> /// <param name="searchWords">The search string to find.</param> - private IEnumerable<ISearchItem> GetItems(string[] searchWords) + private IEnumerable<SearchableItem> GetItems(string[] searchWords) { // normalise search term searchWords = searchWords?.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); @@ -49,33 +66,11 @@ namespace TrainerMod.Framework.Commands.Player // find matches return ( - from item in this.GetItems() - let term = $"{item.ID}|{item.Type}|{item.Name}" + from item in this.Items.GetAll() + let term = $"{item.ID}|{item.Type}|{item.Name}|{item.DisplayName}" where searchWords == null || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) select item ); } - - /// <summary>Get all items which can be searched and added to the player's inventory through the console.</summary> - private IEnumerable<ISearchItem> GetItems() - { - // objects - foreach (int id in Game1.objectInformation.Keys) - { - ISearchItem obj = id >= Ring.ringLowerIndexRange && id <= Ring.ringUpperIndexRange - ? new SearchableRing(id) - : (ISearchItem)new SearchableObject(id); - if (obj.IsValid) - yield return obj; - } - - // weapons - foreach (int id in Game1.content.Load<Dictionary<int, string>>("Data\\weapons").Keys) - { - ISearchItem weapon = new SearchableWeapon(id); - if (weapon.IsValid) - yield return weapon; - } - } } } |