From f9482906ae7ce4dfd41bb4236e094be5d4fa7689 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 2 Jul 2017 01:32:07 -0400 Subject: split TrainerMod commands into separate classes (#302) --- .../Framework/Commands/Player/AddItemCommand.cs | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs (limited to 'src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs') diff --git a/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs new file mode 100644 index 00000000..6d3cf968 --- /dev/null +++ b/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs @@ -0,0 +1,68 @@ +using System.Linq; +using StardewModdingAPI; +using StardewValley; + +namespace TrainerMod.Framework.Commands.Player +{ + /// A command which adds an item to the player inventory. + internal class AddItemCommand : TrainerCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public AddItemCommand() + : base("player_additem", $"Gives the player an item.\n\nUsage: player_additem [count] [quality]\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).") { } + + /// 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, string[] args) + { + // validate + if (!args.Any()) + { + this.LogArgumentsInvalid(monitor, command); + return; + } + if (!int.TryParse(args[0], out int itemID)) + { + this.LogUsageError(monitor, "The item ID must be an integer.", command); + return; + } + + // parse arguments + int count = 1; + int quality = 0; + if (args.Length > 1) + { + if (!int.TryParse(args[1], out count)) + { + this.LogUsageError(monitor, "The optional count is invalid.", command); + return; + } + } + if (args.Length > 2) + { + if (!int.TryParse(args[2], out quality)) + { + this.LogUsageError(monitor, "The optional quality is invalid.", command); + return; + } + } + + // spawn item + var item = new Object(itemID, count) { quality = quality }; + if (item.Name == "Error Item") + { + monitor.Log("There is no such item ID.", LogLevel.Error); + return; + } + + // add to inventory + Game1.player.addItemByMenuIfNecessary(item); + monitor.Log($"OK, added {item.Name} to your inventory.", LogLevel.Info); + } + } +} -- cgit