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') 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') 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') 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') 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 b990f81eda42395eff63d77093092663de751712 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Mon, 16 Apr 2018 22:39:33 -0500 Subject: support quoted strings in console commands --- src/SMAPI/Framework/CommandManager.cs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/SMAPI/Framework/CommandManager.cs b/src/SMAPI/Framework/CommandManager.cs index 79a23d03..78e03827 100644 --- a/src/SMAPI/Framework/CommandManager.cs +++ b/src/SMAPI/Framework/CommandManager.cs @@ -72,7 +72,7 @@ namespace StardewModdingAPI.Framework if (string.IsNullOrWhiteSpace(input)) return false; - string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string[] args = this.ParseArgs(input); string name = args[0]; args = args.Skip(1).ToArray(); @@ -103,6 +103,32 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ + /// + /// Parses a string into an array of 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) + { + if (c == '"') + { + inQuotes = !inQuotes; + } + else if (!inQuotes && char.IsWhiteSpace(c)) + { + args.Add(string.Concat(currentArg)); + currentArg.Clear(); + } + else + currentArg.Add(c); + } + return args.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray(); + } + /// Get a normalised command name. /// The command name. private string GetNormalisedName(string name) -- 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') 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') 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 f95c7f25f4447fd1a34400a273256b1e5b4df7b1 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Wed, 25 Apr 2018 02:06:05 -0500 Subject: fix not adding last arg --- src/SMAPI/Framework/CommandManager.cs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/SMAPI/Framework/CommandManager.cs b/src/SMAPI/Framework/CommandManager.cs index 78e03827..0c48d8ec 100644 --- a/src/SMAPI/Framework/CommandManager.cs +++ b/src/SMAPI/Framework/CommandManager.cs @@ -126,6 +126,9 @@ namespace StardewModdingAPI.Framework else currentArg.Add(c); } + + args.Add(string.Concat(currentArg)); + return args.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray(); } -- cgit