diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 18:17:20 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 18:17:20 -0400 |
commit | f904b3da9728ee51c76e95915b78623a7638de26 (patch) | |
tree | 31b20846e8b65567c5d18520cd12e1de004671e8 /src/TrainerMod/Framework/Commands/Player/AddCommand.cs | |
parent | 40e8d3da0e204117d0a6de91b368ef420eb31df0 (diff) | |
download | SMAPI-f904b3da9728ee51c76e95915b78623a7638de26.tar.gz SMAPI-f904b3da9728ee51c76e95915b78623a7638de26.tar.bz2 SMAPI-f904b3da9728ee51c76e95915b78623a7638de26.zip |
add unified player_add command which adds any item type (#302)
Diffstat (limited to 'src/TrainerMod/Framework/Commands/Player/AddCommand.cs')
-rw-r--r-- | src/TrainerMod/Framework/Commands/Player/AddCommand.cs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/TrainerMod/Framework/Commands/Player/AddCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddCommand.cs new file mode 100644 index 00000000..47840202 --- /dev/null +++ b/src/TrainerMod/Framework/Commands/Player/AddCommand.cs @@ -0,0 +1,81 @@ +using System; +using System.Linq; +using StardewModdingAPI; +using StardewValley; +using TrainerMod.Framework.ItemData; +using Object = StardewValley.Object; + +namespace TrainerMod.Framework.Commands.Player +{ + /// <summary>A command which adds an item to the player inventory.</summary> + internal class AddCommand : TrainerCommand + { + /********* + ** Properties + *********/ + /// <summary>Provides methods for searching and constructing items.</summary> + private readonly ItemRepository Items = new ItemRepository(); + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public AddCommand() + : base("player_add", AddCommand.GetDescription()) + { } + + /// <summary>Handle the command.</summary> + /// <param name="monitor">Writes messages to the console and log file.</param> + /// <param name="command">The command name.</param> + /// <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)))) + 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 & quality + match.Item.Stack = count; + if (match.Item is Object obj) + obj.quality = 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 <type> <item> [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"; + } + } +} |