diff options
Diffstat (limited to 'src/TrainerMod/Framework')
7 files changed, 85 insertions, 224 deletions
diff --git a/src/TrainerMod/Framework/Commands/ArgumentParser.cs b/src/TrainerMod/Framework/Commands/ArgumentParser.cs index bce068f1..6bcd3ff8 100644 --- a/src/TrainerMod/Framework/Commands/ArgumentParser.cs +++ b/src/TrainerMod/Framework/Commands/ArgumentParser.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using StardewModdingAPI; @@ -58,7 +59,7 @@ namespace TrainerMod.Framework.Commands /// <param name="name">The argument name for error messages.</param> /// <param name="value">The parsed value.</param> /// <param name="required">Whether to show an error if the argument is missing.</param> - /// <param name="oneOf">Require that the argument match one of the given values.</param> + /// <param name="oneOf">Require that the argument match one of the given values (case-insensitive).</param> public bool TryGet(int index, string name, out string value, bool required = true, string[] oneOf = null) { value = null; @@ -70,7 +71,7 @@ namespace TrainerMod.Framework.Commands this.LogError($"Argument {index} ({name}) is required."); return false; } - if (oneOf?.Any() == true && !oneOf.Contains(this.Args[index])) + if (oneOf?.Any() == true && !oneOf.Contains(this.Args[index], StringComparer.InvariantCultureIgnoreCase)) { this.LogError($"Argument {index} ({name}) must be one of {string.Join(", ", oneOf)}."); return false; 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"; + } + } +} diff --git a/src/TrainerMod/Framework/Commands/Player/AddFlooringCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddFlooringCommand.cs deleted file mode 100644 index 1bc96466..00000000 --- a/src/TrainerMod/Framework/Commands/Player/AddFlooringCommand.cs +++ /dev/null @@ -1,33 +0,0 @@ -using StardewModdingAPI; -using StardewValley; -using StardewValley.Objects; - -namespace TrainerMod.Framework.Commands.Player -{ - /// <summary>A command which adds a floor item to the player inventory.</summary> - internal class AddFlooringCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - public AddFlooringCommand() - : base("player_addflooring", "Gives the player a flooring item.\n\nUsage: player_addflooring <flooring>\n- flooring: the flooring ID (ranges from 0 to 39).") { } - - /// <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.TryGetInt(0, "floor ID", out int floorID, min: 0, max: 39)) - return; - - // handle - Wallpaper wallpaper = new Wallpaper(floorID, isFloor: true); - Game1.player.addItemByMenuIfNecessary(wallpaper); - monitor.Log($"OK, added flooring {floorID} to your inventory.", LogLevel.Info); - } - } -} diff --git a/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs deleted file mode 100644 index 190d040a..00000000 --- a/src/TrainerMod/Framework/Commands/Player/AddItemCommand.cs +++ /dev/null @@ -1,43 +0,0 @@ -using StardewModdingAPI; -using StardewValley; - -namespace TrainerMod.Framework.Commands.Player -{ - /// <summary>A command which adds an item to the player inventory.</summary> - internal class AddItemCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - public AddItemCommand() - : base("player_additem", $"Gives the player an item.\n\nUsage: player_additem <item> [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).") { } - - /// <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.TryGetInt(0, "item ID", out int itemID, min: 0)) - return; - if (!args.TryGetInt(1, "count", out int count, min: 1, required: false)) - count = 1; - if (!args.TryGetInt(2, "quality", out int quality, min: Object.lowQuality, max: Object.bestQuality, required: false)) - quality = Object.lowQuality; - - // 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); - } - } -} diff --git a/src/TrainerMod/Framework/Commands/Player/AddRingCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddRingCommand.cs deleted file mode 100644 index 93c5b2a5..00000000 --- a/src/TrainerMod/Framework/Commands/Player/AddRingCommand.cs +++ /dev/null @@ -1,33 +0,0 @@ -using StardewModdingAPI; -using StardewValley; -using StardewValley.Objects; - -namespace TrainerMod.Framework.Commands.Player -{ - /// <summary>A command which adds a ring to the player inventory.</summary> - internal class AddRingCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - public AddRingCommand() - : base("player_addring", "Gives the player a ring.\n\nUsage: player_addring <item>\n- item: the ring ID (use the 'list_items' command to see a list).") { } - - /// <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) - { - // parse arguments - if (!args.TryGetInt(0, "ring ID", out int ringID, min: Ring.ringLowerIndexRange, max: Ring.ringUpperIndexRange)) - return; - - // handle - Ring ring = new Ring(ringID); - Game1.player.addItemByMenuIfNecessary(ring); - monitor.Log($"OK, added {ring.Name} to your inventory.", LogLevel.Info); - } - } -} diff --git a/src/TrainerMod/Framework/Commands/Player/AddWallpaperCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddWallpaperCommand.cs deleted file mode 100644 index dddb9ffd..00000000 --- a/src/TrainerMod/Framework/Commands/Player/AddWallpaperCommand.cs +++ /dev/null @@ -1,33 +0,0 @@ -using StardewModdingAPI; -using StardewValley; -using StardewValley.Objects; - -namespace TrainerMod.Framework.Commands.Player -{ - /// <summary>A command which adds a wallpaper item to the player inventory.</summary> - internal class AddWallpaperCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - public AddWallpaperCommand() - : base("player_addwallpaper", "Gives the player a wallpaper.\n\nUsage: player_addwallpaper <wallpaper>\n- wallpaper: the wallpaper ID (ranges from 0 to 111).") { } - - /// <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) - { - // parse arguments - if (!args.TryGetInt(0, "wallpaper ID", out int wallpaperID, min: 0, max: 111)) - return; - - // handle - Wallpaper wallpaper = new Wallpaper(wallpaperID); - Game1.player.addItemByMenuIfNecessary(wallpaper); - monitor.Log($"OK, added wallpaper {wallpaperID} to your inventory.", LogLevel.Info); - } - } -} diff --git a/src/TrainerMod/Framework/Commands/Player/AddWeaponCommand.cs b/src/TrainerMod/Framework/Commands/Player/AddWeaponCommand.cs deleted file mode 100644 index c4ea3d6f..00000000 --- a/src/TrainerMod/Framework/Commands/Player/AddWeaponCommand.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System.Collections.Generic; -using StardewModdingAPI; -using StardewValley; -using StardewValley.Tools; - -namespace TrainerMod.Framework.Commands.Player -{ - /// <summary>A command which adds a weapon to the player inventory.</summary> - internal class AddWeaponCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - public AddWeaponCommand() - : base("player_addweapon", "Gives the player a weapon.\n\nUsage: player_addweapon <item>\n- item: the weapon ID (use the 'list_items' command to see a list).") { } - - /// <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) - { - // parse arguments - if (!args.TryGetInt(0, "weapon ID", out int weaponID, min: 0)) - return; - - // get raw weapon data - if (!Game1.content.Load<Dictionary<int, string>>("Data\\weapons").TryGetValue(weaponID, out string data)) - { - monitor.Log("There is no such weapon ID.", LogLevel.Error); - return; - } - - // get raw weapon type - int type; - { - string[] fields = data.Split('/'); - string typeStr = fields.Length > 8 ? fields[8] : null; - if (!int.TryParse(typeStr, out type)) - { - monitor.Log("Could not parse the data for the weapon with that ID.", LogLevel.Error); - return; - } - } - - // get weapon - Tool weapon; - switch (type) - { - case MeleeWeapon.stabbingSword: - case MeleeWeapon.dagger: - case MeleeWeapon.club: - case MeleeWeapon.defenseSword: - weapon = new MeleeWeapon(weaponID); - break; - - case 4: - weapon = new Slingshot(weaponID); - break; - - default: - monitor.Log($"The specified weapon has unknown type '{type}' in the game data.", LogLevel.Error); - return; - } - - // validate weapon - if (weapon.Name == null) - { - monitor.Log("That weapon doesn't seem to be valid.", LogLevel.Error); - return; - } - - // add weapon - Game1.player.addItemByMenuIfNecessary(weapon); - monitor.Log($"OK, added {weapon.Name} to your inventory.", LogLevel.Info); - } - } -} |