From 9c1617c9ee51a0f6b93242fe8fc789336957460c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 11 Apr 2018 21:15:16 -0400 Subject: drop support for Stardew Valley 1.2 (#453) --- .../Framework/Commands/Player/AddCommand.cs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index a6f42b98..ff11da1e 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -54,11 +54,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // apply quality if (match.Item is Object obj) -#if STARDEW_VALLEY_1_3 obj.Quality = quality; -#else - obj.quality = quality; -#endif else if (match.Item is Tool tool) tool.UpgradeLevel = quality; -- cgit From 7f4941167e58f27f43b073db2e37d14b7fee2543 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 15 Apr 2018 22:13:26 -0500 Subject: initial player_add changes --- .../Framework/Commands/Player/AddCommand.cs | 96 ++++++++++++++++++---- 1 file changed, 81 insertions(+), 15 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index ff11da1e..09b9585c 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; using StardewValley; @@ -30,24 +31,25 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// The command arguments. public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // read arguments - if (!args.TryGet(0, "item type", out string rawType, oneOf: Enum.GetNames(typeof(ItemType)))) - return; - if (!args.TryGetInt(1, "item ID", out int id, min: 0)) + SearchableItem match; + int optionalArgStartIndex; + + if (!args.TryGet(0, "item type or name", out string typeOrName, required: true)) return; - if (!args.TryGetInt(2, "count", out int count, min: 1, required: false)) - count = 1; - if (!args.TryGetInt(3, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) - quality = Object.lowQuality; - ItemType type = (ItemType)Enum.Parse(typeof(ItemType), rawType, ignoreCase: true); - // find matching item - SearchableItem match = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); + // read arguments + if (Enum.GetNames(typeof(ItemType)).Contains(typeOrName, StringComparer.InvariantCultureIgnoreCase)) + this.FindItemByTypeAndId(monitor, args, typeOrName, out optionalArgStartIndex, out match); + else + this.FindItemByName(monitor, args, typeOrName, out optionalArgStartIndex, out match); + if (match == null) - { - monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error); return; - } + + if (!args.TryGetInt(optionalArgStartIndex, "count", out int count, min: 1, required: false)) + count = 1; + if (!args.TryGetInt(optionalArgStartIndex + 1, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) + quality = Object.lowQuality; // apply count match.Item.Stack = count; @@ -63,6 +65,69 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player monitor.Log($"OK, added {match.Name} ({match.Type} #{match.ID}) to your inventory.", LogLevel.Info); } + /// + /// Finds a matching item by item type and id. + /// + /// Writes messages to the console and log file. + /// The command arguments. + /// The raw item type. + /// The index of subsequent arguments. + /// The matching item. + private void FindItemByTypeAndId(IMonitor monitor, ArgumentParser args, string rawType, out int nextArgIndex, out SearchableItem match) + { + match = null; + nextArgIndex = 2; + + // read arguments + if (!args.TryGetInt(1, "item ID", out int id, min: 0)) + return; + + ItemType type = (ItemType)Enum.Parse(typeof(ItemType), rawType, ignoreCase: true); + + // find matching item + match = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); + + if (match == null) + { + monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error); + } + } + + /// + /// Finds a matching item by name. + /// + /// Writes messages to the console and log file. + /// The command arguments. + /// The item name. + /// The index of subsequent arguments. + /// The matching item. + private void FindItemByName(IMonitor monitor, ArgumentParser args, string name, out int nextArgIndex, out SearchableItem match) + { + match = null; + nextArgIndex = 1; + + // interpret names with underscores as spaces + name = name.Replace('_', ' '); + + // find matching items + IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.Equals(name, StringComparison.CurrentCultureIgnoreCase)); + int numberOfMatches = matching.Count(); + + // handle unique requirement + if (numberOfMatches == 0) + { + monitor.Log($"There's no item with name {name}.", LogLevel.Error); + } + else if (numberOfMatches == 1) + { + match = matching.ElementAt(0); + } + else + { + monitor.Log($"There are {numberOfMatches} items with name {name}. Try specifying a type and id instead.", LogLevel.Error); + } + } + /********* ** Private methods *********/ @@ -71,9 +136,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player string[] typeValues = Enum.GetNames(typeof(ItemType)); return "Gives the player an item.\n" + "\n" - + "Usage: player_add [count] [quality]\n" + + "Usage: player_add ( |) [count] [quality]\n" + $"- type: the item type (one of {string.Join(", ", typeValues)}).\n" + "- item: the item ID (use the 'list_items' command to see a list).\n" + + "- name: the display name of the item (only if there is exactly one such item).\n" + "- count (optional): how many of the item to give.\n" + $"- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).\n" + "\n" -- cgit From a6e1ea0a4f87c621edc74d789eb76ce70893b5d1 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Mon, 16 Apr 2018 00:42:57 -0500 Subject: fix private method comment location --- .../Framework/Commands/Player/AddCommand.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 09b9585c..6c48d8df 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -65,6 +65,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player monitor.Log($"OK, added {match.Name} ({match.Type} #{match.ID}) to your inventory.", LogLevel.Info); } + /********* + ** Private methods + *********/ + /// /// Finds a matching item by item type and id. /// @@ -128,9 +132,6 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } } - /********* - ** Private methods - *********/ private static string GetDescription() { string[] typeValues = Enum.GetNames(typeof(ItemType)); -- cgit From d362843706c012867324dc9723e7c7a49a7a2621 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Mon, 16 Apr 2018 03:15:25 -0500 Subject: add Name as new type, show more helpful info on multiple matches --- .../Framework/Commands/Player/AddCommand.cs | 43 +++++++++++----------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 6c48d8df..47ba9e8d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -16,6 +16,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Provides methods for searching and constructing items. private readonly ItemRepository Items = new ItemRepository(); + private readonly string[] ItemTypeAndName = Enum.GetNames(typeof(ItemType)).Union(new string[] { "Name" }).ToArray(); /********* ** Public methods @@ -32,23 +33,21 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { SearchableItem match; - int optionalArgStartIndex; - if (!args.TryGet(0, "item type or name", out string typeOrName, required: true)) + //read arguments + if (!args.TryGet(0, "item type", out string typeOrName, oneOf: this.ItemTypeAndName)) return; - - // read arguments if (Enum.GetNames(typeof(ItemType)).Contains(typeOrName, StringComparer.InvariantCultureIgnoreCase)) - this.FindItemByTypeAndId(monitor, args, typeOrName, out optionalArgStartIndex, out match); + this.FindItemByTypeAndId(monitor, args, typeOrName, out match); else - this.FindItemByName(monitor, args, typeOrName, out optionalArgStartIndex, out match); - + this.FindItemByName(monitor, args, out match); + if (match == null) return; - if (!args.TryGetInt(optionalArgStartIndex, "count", out int count, min: 1, required: false)) + if (!args.TryGetInt(2, "count", out int count, min: 1, required: false)) count = 1; - if (!args.TryGetInt(optionalArgStartIndex + 1, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) + if (!args.TryGetInt(3, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) quality = Object.lowQuality; // apply count @@ -75,12 +74,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Writes messages to the console and log file. /// The command arguments. /// The raw item type. - /// The index of subsequent arguments. /// The matching item. - private void FindItemByTypeAndId(IMonitor monitor, ArgumentParser args, string rawType, out int nextArgIndex, out SearchableItem match) + private void FindItemByTypeAndId(IMonitor monitor, ArgumentParser args, string rawType, out SearchableItem match) { match = null; - nextArgIndex = 2; // read arguments if (!args.TryGetInt(1, "item ID", out int id, min: 0)) @@ -103,18 +100,17 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Writes messages to the console and log file. /// The command arguments. /// The item name. - /// The index of subsequent arguments. /// The matching item. - private void FindItemByName(IMonitor monitor, ArgumentParser args, string name, out int nextArgIndex, out SearchableItem match) + private void FindItemByName(IMonitor monitor, ArgumentParser args, out SearchableItem match) { match = null; - nextArgIndex = 1; - // interpret names with underscores as spaces - name = name.Replace('_', ' '); + // read arguments + if (!args.TryGet(1, "item name", out string name)) + return; // find matching items - IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.Equals(name, StringComparison.CurrentCultureIgnoreCase)); + IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) != -1); int numberOfMatches = matching.Count(); // handle unique requirement @@ -128,7 +124,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } else { - monitor.Log($"There are {numberOfMatches} items with name {name}. Try specifying a type and id instead.", LogLevel.Error); + string options = this.GetTableString(matching, new string[] { "type", "name", "command" }, item => new string[] { item.Type.ToString(), item.DisplayName, $"player_add {item.Type} {item.ID}" }); + + monitor.Log($"Found multiple item names containing '{name}'. Type one of these commands for the one you want:", LogLevel.Error); + monitor.Log($"\n{options}", LogLevel.Info); } } @@ -137,10 +136,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player string[] typeValues = Enum.GetNames(typeof(ItemType)); return "Gives the player an item.\n" + "\n" - + "Usage: player_add ( |) [count] [quality]\n" - + $"- type: the item type (one of {string.Join(", ", typeValues)}).\n" + + "Usage: player_add (|) [count] [quality]\n" + + $"- type: the item type (either Name or one of {string.Join(", ", typeValues)}).\n" + "- item: the item ID (use the 'list_items' command to see a list).\n" - + "- name: the display name of the item (only if there is exactly one such item).\n" + + "- name: the display name of the item (when using type Name).\n" + "- count (optional): how many of the item to give.\n" + $"- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).\n" + "\n" -- cgit From 96753c35fd3d6baed73a933c552e5f0a5a8fa02c Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Mon, 16 Apr 2018 03:39:08 -0500 Subject: add world ready check and more helpful error messages --- .../Framework/Commands/Player/AddCommand.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 47ba9e8d..3d55b425 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -32,6 +32,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// The command arguments. public override void Handle(IMonitor monitor, string command, ArgumentParser args) { + // validate + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + SearchableItem match; //read arguments @@ -111,16 +118,22 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // find matching items IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) != -1); + SearchableItem exactMatch = matching.FirstOrDefault(item => item.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase)); + int numberOfMatches = matching.Count(); // handle unique requirement - if (numberOfMatches == 0) + if (exactMatch != null) { - monitor.Log($"There's no item with name {name}.", LogLevel.Error); + match = matching.ElementAt(0); + } + else if (numberOfMatches == 0) + { + monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); } else if (numberOfMatches == 1) { - match = matching.ElementAt(0); + monitor.Log($"There's no item with name '{name}'. Did you mean '{matching.ElementAt(0).DisplayName}'? If so, type 'player_add name {matching.ElementAt(0).DisplayName}'.", LogLevel.Error); } else { -- cgit From 43487a40e391978221df00ba86f2b7628ed8d343 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Tue, 17 Apr 2018 15:35:22 -0500 Subject: refactor finding items by name slightly --- .../Framework/Commands/Player/AddCommand.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 3d55b425..453b8e32 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -118,16 +118,17 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // find matching items IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) != -1); - SearchableItem exactMatch = matching.FirstOrDefault(item => item.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase)); - - int numberOfMatches = matching.Count(); + match = matching.FirstOrDefault(item => item.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase)); // handle unique requirement - if (exactMatch != null) + if (match != null) { - match = matching.ElementAt(0); + return; } - else if (numberOfMatches == 0) + + int numberOfMatches = matching.Count(); + + if (numberOfMatches == 0) { monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); } -- cgit From f451e172e2d2cf1959c86fdb24c708ca5b1924f7 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Thu, 19 Apr 2018 01:35:15 -0500 Subject: update documentation format and document field --- .../Framework/Commands/Player/AddCommand.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 453b8e32..803ae7f6 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -16,6 +16,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Provides methods for searching and constructing items. private readonly ItemRepository Items = new ItemRepository(); + /// All possible item types along with Name. private readonly string[] ItemTypeAndName = Enum.GetNames(typeof(ItemType)).Union(new string[] { "Name" }).ToArray(); /********* @@ -75,9 +76,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player ** Private methods *********/ - /// - /// Finds a matching item by item type and id. - /// + /// Finds a matching item by item type and id. /// Writes messages to the console and log file. /// The command arguments. /// The raw item type. @@ -101,9 +100,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } } - /// - /// Finds a matching item by name. - /// + /// Finds a matching item by name. /// Writes messages to the console and log file. /// The command arguments. /// The item name. -- cgit From 0134f0b28d355766a17b1c9da89b8173f978d195 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 25 Apr 2018 13:16:25 -0400 Subject: update release notes, refactor a bit (#474) --- docs/release-notes.md | 1 + .../Framework/Commands/Player/AddCommand.cs | 123 +++++++++------------ .../Framework/ItemData/SearchableItem.cs | 21 +++- src/SMAPI/Framework/CommandManager.cs | 21 ++-- 4 files changed, 85 insertions(+), 81 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/docs/release-notes.md b/docs/release-notes.md index 308007bb..6842c941 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -11,6 +11,7 @@ * For modders: * Added code analysis to mod build config package to flag common issues as warnings. * Added `Context.IsMultiplayer` and `Context.IsMainPlayer` flags. + * Added `player_add name` console command, which lets you add items to your inventory by name instead of ID. * Fixed assets loaded by temporary content managers not being editable. * Fixed issue where assets didn't reload correctly when the player switches language. * Fixed user command input not saved to log file. diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 803ae7f6..71c3ff98 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; using StardewValley; @@ -16,16 +15,16 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Provides methods for searching and constructing items. private readonly ItemRepository Items = new ItemRepository(); - /// All possible item types along with Name. - private readonly string[] ItemTypeAndName = Enum.GetNames(typeof(ItemType)).Union(new string[] { "Name" }).ToArray(); + /// The type names recognised by this command. + private readonly string[] ValidTypes = Enum.GetNames(typeof(ItemType)).Concat(new[] { "Name" }).ToArray(); + /********* ** Public methods *********/ /// Construct an instance. public AddCommand() - : base("player_add", AddCommand.GetDescription()) - { } + : base("player_add", AddCommand.GetDescription()) { } /// Handle the command. /// Writes messages to the console and log file. @@ -40,24 +39,21 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player return; } - SearchableItem match; - - //read arguments - if (!args.TryGet(0, "item type", out string typeOrName, oneOf: this.ItemTypeAndName)) - return; - if (Enum.GetNames(typeof(ItemType)).Contains(typeOrName, StringComparer.InvariantCultureIgnoreCase)) - this.FindItemByTypeAndId(monitor, args, typeOrName, out match); - else - this.FindItemByName(monitor, args, out match); - - if (match == null) + // read arguments + if (!args.TryGet(0, "item type", out string type, oneOf: this.ValidTypes)) return; - if (!args.TryGetInt(2, "count", out int count, min: 1, required: false)) count = 1; if (!args.TryGetInt(3, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) quality = Object.lowQuality; + // find matching item + SearchableItem match = Enum.TryParse(type, true, out ItemType itemType) + ? this.FindItemByID(monitor, args, itemType) + : this.FindItemByName(monitor, args); + if (match == null) + return; + // apply count match.Item.Stack = count; @@ -72,90 +68,81 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player monitor.Log($"OK, added {match.Name} ({match.Type} #{match.ID}) to your inventory.", LogLevel.Info); } + /********* ** Private methods *********/ - - /// Finds a matching item by item type and id. + /// Get a matching item by its ID. /// Writes messages to the console and log file. /// The command arguments. - /// The raw item type. - /// The matching item. - private void FindItemByTypeAndId(IMonitor monitor, ArgumentParser args, string rawType, out SearchableItem match) + /// The item type. + private SearchableItem FindItemByID(IMonitor monitor, ArgumentParser args, ItemType type) { - match = null; - // read arguments if (!args.TryGetInt(1, "item ID", out int id, min: 0)) - return; - - ItemType type = (ItemType)Enum.Parse(typeof(ItemType), rawType, ignoreCase: true); + return null; // find matching item - match = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); - - if (match == null) - { + SearchableItem item = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); + if (item == null) monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error); - } + return item; } - /// Finds a matching item by name. + /// Get a matching item by its name. /// Writes messages to the console and log file. /// The command arguments. - /// The item name. - /// The matching item. - private void FindItemByName(IMonitor monitor, ArgumentParser args, out SearchableItem match) + private SearchableItem FindItemByName(IMonitor monitor, ArgumentParser args) { - match = null; - // read arguments if (!args.TryGet(1, "item name", out string name)) - return; + return null; // find matching items - IEnumerable matching = this.Items.GetAll().Where(p => p.DisplayName.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) != -1); - match = matching.FirstOrDefault(item => item.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase)); - - // handle unique requirement - if (match != null) - { - return; - } - - int numberOfMatches = matching.Count(); - - if (numberOfMatches == 0) - { - monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); - } - else if (numberOfMatches == 1) + SearchableItem[] matches = this.Items.GetAll().Where(p => p.NameContains(name)).ToArray(); + switch (matches.Length) { - monitor.Log($"There's no item with name '{name}'. Did you mean '{matching.ElementAt(0).DisplayName}'? If so, type 'player_add name {matching.ElementAt(0).DisplayName}'.", LogLevel.Error); - } - else - { - string options = this.GetTableString(matching, new string[] { "type", "name", "command" }, item => new string[] { item.Type.ToString(), item.DisplayName, $"player_add {item.Type} {item.ID}" }); - - monitor.Log($"Found multiple item names containing '{name}'. Type one of these commands for the one you want:", LogLevel.Error); - monitor.Log($"\n{options}", LogLevel.Info); + // none found + case 0: + monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); + return null; + + // exact match + case 1 when matches[0].NameEquivalentTo(name): + return matches[0]; + + // list matches + default: + string options = this.GetTableString( + data: matches, + header: new[] { "type", "name", "command" }, + getRow: item => new[] { item.Type.ToString(), item.DisplayName, $"player_add {item.Type} {item.ID}" } + ); + monitor.Log($"There's no item with name '{name}'. Do you mean one of these?\n\n{options}", LogLevel.Info); + return null; } } + /// Get the command description. private static string GetDescription() { string[] typeValues = Enum.GetNames(typeof(ItemType)); return "Gives the player an item.\n" + "\n" - + "Usage: player_add (|) [count] [quality]\n" - + $"- type: the item type (either Name or one of {string.Join(", ", typeValues)}).\n" + + "Usage: player_add [count] [quality]\n" + + $"- type: the item type (one of {string.Join(", ", typeValues)}).\n" + "- item: the item ID (use the 'list_items' command to see a list).\n" - + "- name: the display name of the item (when using type Name).\n" + "- count (optional): how many of the item to give.\n" + $"- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).\n" + "\n" - + "This example adds the galaxy sword to your inventory:\n" - + " player_add weapon 4"; + + "Usage: player_add name \"\" [count] [quality]\n" + + "- name: the item name to search (use the 'list_items' command to see a list). This will add the item immediately if it's an exact match, else show a table of matching items.\n" + + "- count (optional): how many of the item to give.\n" + + $"- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).\n" + + "\n" + + "These examples both add the galaxy sword to your inventory:\n" + + " player_add weapon 4\n" + + " player_add name \"Galaxy Sword\""; } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs index 3eede413..b618a308 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs @@ -1,4 +1,5 @@ -using StardewValley; +using System; +using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData { @@ -37,5 +38,23 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData this.ID = id; this.Item = item; } + + /// Get whether the item name contains a case-insensitive substring. + /// The substring to find. + public bool NameContains(string substring) + { + return + this.Name.IndexOf(substring, StringComparison.InvariantCultureIgnoreCase) != -1 + || this.DisplayName.IndexOf(substring, StringComparison.InvariantCultureIgnoreCase) != -1; + } + + /// Get whether the item name is exactly equal to a case-insensitive string. + /// The substring to find. + public bool NameEquivalentTo(string name) + { + return + this.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) + || this.DisplayName.Equals(name, StringComparison.InvariantCultureIgnoreCase); + } } } diff --git a/src/SMAPI/Framework/CommandManager.cs b/src/SMAPI/Framework/CommandManager.cs index 0c48d8ec..f9651ed9 100644 --- a/src/SMAPI/Framework/CommandManager.cs +++ b/src/SMAPI/Framework/CommandManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; namespace StardewModdingAPI.Framework { @@ -103,31 +104,27 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ - /// - /// Parses a string into an array of arguments. - /// + /// Parse a string into command arguments. /// The string to parse. private string[] ParseArgs(string input) { bool inQuotes = false; IList args = new List(); - IList currentArg = new List(); - foreach (char c in input) + StringBuilder currentArg = new StringBuilder(); + foreach (char ch in input) { - if (c == '"') - { + if (ch == '"') inQuotes = !inQuotes; - } - else if (!inQuotes && char.IsWhiteSpace(c)) + else if (!inQuotes && char.IsWhiteSpace(ch)) { - args.Add(string.Concat(currentArg)); + args.Add(currentArg.ToString()); currentArg.Clear(); } else - currentArg.Add(c); + currentArg.Append(ch); } - args.Add(string.Concat(currentArg)); + args.Add(currentArg.ToString()); return args.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray(); } -- cgit From bd0800f2609c14e30e8822e48323d4eb92c67bb4 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 10 Jun 2018 12:13:47 -0700 Subject: find item if there is exactly one exact name match --- .../Framework/Commands/Player/AddCommand.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 71c3ff98..f1b573cd 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; using StardewValley; @@ -100,6 +101,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // find matching items SearchableItem[] matches = this.Items.GetAll().Where(p => p.NameContains(name)).ToArray(); + + // if exactly one item with the exact same name, use that item + SearchableItem[] exactNameMatches = matches.Where(p => p.NameEquivalentTo(name)).ToArray(); + if (exactNameMatches.Length == 1) + return exactNameMatches[0]; + switch (matches.Length) { // none found @@ -107,10 +114,6 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); return null; - // exact match - case 1 when matches[0].NameEquivalentTo(name): - return matches[0]; - // list matches default: string options = this.GetTableString( -- cgit From 580fd687b0f676b9ddac3ee7938b4e0ac5a987c3 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 10 Jun 2018 12:21:04 -0700 Subject: remove unused import --- src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index f1b573cd..5bc97225 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; using StardewValley; -- cgit From 645a64cd24146edba44b89f7493ba5ea9950c3c8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Jun 2018 19:09:00 -0400 Subject: refactor player_add command a bit (#542) --- .../Framework/Commands/Player/AddCommand.cs | 38 ++++++++++------------ 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 5bc97225..37f4719e 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -100,29 +100,25 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // find matching items SearchableItem[] matches = this.Items.GetAll().Where(p => p.NameContains(name)).ToArray(); - - // if exactly one item with the exact same name, use that item - SearchableItem[] exactNameMatches = matches.Where(p => p.NameEquivalentTo(name)).ToArray(); - if (exactNameMatches.Length == 1) - return exactNameMatches[0]; - - switch (matches.Length) + if (!matches.Any()) { - // none found - case 0: - monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); - return null; - - // list matches - default: - string options = this.GetTableString( - data: matches, - header: new[] { "type", "name", "command" }, - getRow: item => new[] { item.Type.ToString(), item.DisplayName, $"player_add {item.Type} {item.ID}" } - ); - monitor.Log($"There's no item with name '{name}'. Do you mean one of these?\n\n{options}", LogLevel.Info); - return null; + monitor.Log($"There's no item with name '{name}'. You can use the 'list_items [name]' command to search for items.", LogLevel.Error); + return null; } + + // handle single exact match + SearchableItem[] exactMatches = matches.Where(p => p.NameEquivalentTo(name)).ToArray(); + if (exactMatches.Length == 1) + return exactMatches[0]; + + // handle ambiguous results + string options = this.GetTableString( + data: matches, + header: new[] { "type", "name", "command" }, + getRow: item => new[] { item.Type.ToString(), item.DisplayName, $"player_add {item.Type} {item.ID}" } + ); + monitor.Log($"There's no item with name '{name}'. Do you mean one of these?\n\n{options}", LogLevel.Info); + return null; } /// Get the command description. -- cgit