summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs105
-rw-r--r--src/SMAPI/Framework/CommandManager.cs31
2 files changed, 121 insertions, 15 deletions
diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs
index ff11da1e..803ae7f6 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;
@@ -15,6 +16,8 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
/// <summary>Provides methods for searching and constructing items.</summary>
private readonly ItemRepository Items = new ItemRepository();
+ /// <summary>All possible item types along with Name.</summary>
+ private readonly string[] ItemTypeAndName = Enum.GetNames(typeof(ItemType)).Union(new string[] { "Name" }).ToArray();
/*********
** Public methods
@@ -30,24 +33,30 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
/// <param name="args">The command arguments.</param>
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))))
+ // validate
+ if (!Context.IsWorldReady)
+ {
+ monitor.Log("You need to load a save to use this command.", LogLevel.Error);
return;
- if (!args.TryGetInt(1, "item ID", out int id, min: 0))
+ }
+
+ 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)
+ 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);
- if (match == null)
- {
- monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error);
- return;
- }
// apply count
match.Item.Stack = count;
@@ -66,14 +75,82 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
/*********
** Private methods
*********/
+
+ /// <summary>Finds a matching item by item type and id.</summary>
+ /// <param name="monitor">Writes messages to the console and log file.</param>
+ /// <param name="args">The command arguments.</param>
+ /// <param name="rawType">The raw item type.</param>
+ /// <param name="match">The matching item.</param>
+ private void FindItemByTypeAndId(IMonitor monitor, ArgumentParser args, string rawType, out SearchableItem match)
+ {
+ 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);
+
+ // 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);
+ }
+ }
+
+ /// <summary>Finds a matching item by name.</summary>
+ /// <param name="monitor">Writes messages to the console and log file.</param>
+ /// <param name="args">The command arguments.</param>
+ /// <param name="name">The item name.</param>
+ /// <param name="match">The matching item.</param>
+ private void FindItemByName(IMonitor monitor, ArgumentParser args, out SearchableItem match)
+ {
+ match = null;
+
+ // read arguments
+ if (!args.TryGet(1, "item name", out string name))
+ return;
+
+ // find matching items
+ IEnumerable<SearchableItem> 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)
+ {
+ 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);
+ }
+ }
+
private static string GetDescription()
{
string[] typeValues = Enum.GetNames(typeof(ItemType));
return "Gives the player an item.\n"
+ "\n"
- + "Usage: player_add <type> <item> [count] [quality]\n"
- + $"- type: the item type (one of {string.Join(", ", typeValues)}).\n"
+ + "Usage: player_add <type> (<item>|<name>) [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 (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"
diff --git a/src/SMAPI/Framework/CommandManager.cs b/src/SMAPI/Framework/CommandManager.cs
index 79a23d03..0c48d8ec 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,35 @@ namespace StardewModdingAPI.Framework
/*********
** Private methods
*********/
+ /// <summary>
+ /// Parses a string into an array of arguments.
+ /// </summary>
+ /// <param name="input">The string to parse.</param>
+ private string[] ParseArgs(string input)
+ {
+ bool inQuotes = false;
+ IList<string> args = new List<string>();
+ IList<char> currentArg = new List<char>();
+ 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);
+ }
+
+ args.Add(string.Concat(currentArg));
+
+ return args.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray();
+ }
+
/// <summary>Get a normalised command name.</summary>
/// <param name="name">The command name.</param>
private string GetNormalisedName(string name)