using System;
using System.Linq;
using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData;
using StardewValley;
using Object = StardewValley.Object;
namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
{
/// A command which adds an item to the player inventory.
internal class AddCommand : TrainerCommand
{
/*********
** Properties
*********/
/// Provides methods for searching and constructing items.
private readonly ItemRepository Items = new ItemRepository();
/*********
** Public methods
*********/
/// Construct an instance.
public AddCommand()
: base("player_add", AddCommand.GetDescription())
{ }
/// Handle the command.
/// Writes messages to the console and log file.
/// The command name.
/// 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))
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;
// 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;
// add to inventory
Game1.player.addItemByMenuIfNecessary(match.Item);
monitor.Log($"OK, added {match.Name} ({match.Type} #{match.ID}) to your inventory.", LogLevel.Info);
}
/*********
** Private methods
*********/
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 (one of {string.Join(", ", typeValues)}).\n"
+ "- item: the item ID (use the 'list_items' command to see a list).\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";
}
}
}