diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-19 19:09:00 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-19 19:09:00 -0400 |
commit | 645a64cd24146edba44b89f7493ba5ea9950c3c8 (patch) | |
tree | de7b1104c148b93a9d748d35e6d2607ccc16d716 /src | |
parent | 92bfc32e685da067a5cfb5a24bad162eebce50d3 (diff) | |
download | SMAPI-645a64cd24146edba44b89f7493ba5ea9950c3c8.tar.gz SMAPI-645a64cd24146edba44b89f7493ba5ea9950c3c8.tar.bz2 SMAPI-645a64cd24146edba44b89f7493ba5ea9950c3c8.zip |
refactor player_add command a bit (#542)
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs | 38 |
1 files changed, 17 insertions, 21 deletions
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; } /// <summary>Get the command description.</summary> |