From cea8e557efef1be5466cbb7102b4e163c34394c2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 22 Jan 2021 20:24:13 -0500 Subject: remove inf options for player_sethealth/money/stamina --- .../Framework/Commands/Player/SetHealthCommand.cs | 36 ++++------------------ .../Framework/Commands/Player/SetMoneyCommand.cs | 36 ++++------------------ .../Framework/Commands/Player/SetStaminaCommand.cs | 36 ++++------------------ 3 files changed, 18 insertions(+), 90 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs index 59bda5dd..41a5b2af 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs @@ -6,19 +6,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// A command which edits the player's current health. internal class SetHealthCommand : TrainerCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's health at its maximum. - private bool InfiniteHealth; - - /********* ** Public methods *********/ /// Construct an instance. public SetHealthCommand() - : base("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth [value]\n- value: an integer amount, or 'inf' for infinite health.", mayNeedUpdate: true) { } + : base("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth [value]\n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +22,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // no-argument mode if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteHealth ? "infinite" : Game1.player.health.ToString())} health. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.health} health. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteHealth = true; - monitor.Log("OK, you now have infinite health.", LogLevel.Info); + Game1.player.health = amount; + monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); } else - { - this.InfiniteHealth = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.health = amount; - monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteHealth && Context.IsWorldReady) - Game1.player.health = Game1.player.maxHealth; + this.LogArgumentNotInt(monitor); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs index 6e3d68b6..e015f01a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs @@ -6,19 +6,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// A command which edits the player's current money. internal class SetMoneyCommand : TrainerCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's money at a set value. - private bool InfiniteMoney; - - /********* ** Public methods *********/ /// Construct an instance. public SetMoneyCommand() - : base("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount, or 'inf' for infinite money.", mayNeedUpdate: true) { } + : base("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +22,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // validate if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteMoney ? "infinite" : Game1.player.Money.ToString())} gold. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.Money} gold. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteMoney = true; - monitor.Log("OK, you now have infinite money.", LogLevel.Info); + Game1.player.Money = amount; + monitor.Log($"OK, you now have {Game1.player.Money} gold.", LogLevel.Info); } else - { - this.InfiniteMoney = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.Money = amount; - monitor.Log($"OK, you now have {Game1.player.Money} gold.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteMoney && Context.IsWorldReady) - Game1.player.Money = 999999; + this.LogArgumentNotInt(monitor); } } } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs index 60a1dcb1..a8adafce 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs @@ -6,19 +6,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// A command which edits the player's current stamina. internal class SetStaminaCommand : TrainerCommand { - /********* - ** Fields - *********/ - /// Whether to keep the player's stamina at its maximum. - private bool InfiniteStamina; - - /********* ** Public methods *********/ /// Construct an instance. public SetStaminaCommand() - : base("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina [value]\n- value: an integer amount, or 'inf' for infinite stamina.", mayNeedUpdate: true) { } + : base("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina [value]\n- value: an integer amount.") { } /// Handle the command. /// Writes messages to the console and log file. @@ -29,36 +22,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player // validate if (!args.Any()) { - monitor.Log($"You currently have {(this.InfiniteStamina ? "infinite" : Game1.player.Stamina.ToString())} stamina. Specify a value to change it.", LogLevel.Info); + monitor.Log($"You currently have {Game1.player.Stamina} stamina. Specify a value to change it.", LogLevel.Info); return; } // handle string amountStr = args[0]; - if (amountStr == "inf") + if (int.TryParse(amountStr, out int amount)) { - this.InfiniteStamina = true; - monitor.Log("OK, you now have infinite stamina.", LogLevel.Info); + Game1.player.Stamina = amount; + monitor.Log($"OK, you now have {Game1.player.Stamina} stamina.", LogLevel.Info); } else - { - this.InfiniteStamina = false; - if (int.TryParse(amountStr, out int amount)) - { - Game1.player.Stamina = amount; - monitor.Log($"OK, you now have {Game1.player.Stamina} stamina.", LogLevel.Info); - } - else - this.LogArgumentNotInt(monitor); - } - } - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public override void OnUpdated(IMonitor monitor) - { - if (this.InfiniteStamina && Context.IsWorldReady) - Game1.player.stamina = Game1.player.MaxStamina; + this.LogArgumentNotInt(monitor); } } } -- cgit From 4b5dd0f2c9ed530e97a783aedb3de73425d24516 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 22 Jan 2021 20:29:08 -0500 Subject: rename TrainerCommand to ConsoleCommand to match the rest of the code --- .../Framework/Commands/ConsoleCommand.cs | 115 +++++++++++++++++++++ .../Framework/Commands/IConsoleCommand.cs | 40 +++++++ .../Framework/Commands/ITrainerCommand.cs | 40 ------- .../Commands/Other/ApplySaveFixCommand.cs | 2 +- .../Framework/Commands/Other/DebugCommand.cs | 2 +- .../Commands/Other/ShowDataFilesCommand.cs | 2 +- .../Commands/Other/ShowGameFilesCommand.cs | 2 +- .../Framework/Commands/Other/TestInputCommand.cs | 2 +- .../Framework/Commands/Player/AddCommand.cs | 2 +- .../Commands/Player/ListItemTypesCommand.cs | 2 +- .../Framework/Commands/Player/ListItemsCommand.cs | 2 +- .../Framework/Commands/Player/SetColorCommand.cs | 2 +- .../Framework/Commands/Player/SetHealthCommand.cs | 2 +- .../Commands/Player/SetImmunityCommand.cs | 2 +- .../Commands/Player/SetMaxHealthCommand.cs | 2 +- .../Commands/Player/SetMaxStaminaCommand.cs | 2 +- .../Framework/Commands/Player/SetMoneyCommand.cs | 2 +- .../Framework/Commands/Player/SetNameCommand.cs | 2 +- .../Framework/Commands/Player/SetStaminaCommand.cs | 2 +- .../Framework/Commands/Player/SetStyleCommand.cs | 2 +- .../Framework/Commands/TrainerCommand.cs | 115 --------------------- .../Framework/Commands/World/ClearCommand.cs | 2 +- .../Commands/World/DownMineLevelCommand.cs | 2 +- .../Framework/Commands/World/FreezeTimeCommand.cs | 2 +- .../Framework/Commands/World/SetDayCommand.cs | 2 +- .../Commands/World/SetMineLevelCommand.cs | 2 +- .../Framework/Commands/World/SetSeasonCommand.cs | 2 +- .../Framework/Commands/World/SetTimeCommand.cs | 2 +- .../Framework/Commands/World/SetYearCommand.cs | 2 +- src/SMAPI.Mods.ConsoleCommands/ModEntry.cs | 20 ++-- 30 files changed, 190 insertions(+), 190 deletions(-) create mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs create mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs delete mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs delete mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs (limited to 'src/SMAPI.Mods.ConsoleCommands/Framework') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs new file mode 100644 index 00000000..01cab92e --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands +{ + /// The base implementation for a console command. + internal abstract class ConsoleCommand : IConsoleCommand + { + /********* + ** Accessors + *********/ + /// The command name the user must type. + public string Name { get; } + + /// The command description. + public string Description { get; } + + /// Whether the command may need to perform logic when the player presses a button. This value shouldn't change. + public bool MayNeedInput { get; } + + /// Whether the command may need to perform logic when the game updates. This value shouldn't change. + public bool MayNeedUpdate { get; } + + + /********* + ** Public methods + *********/ + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public abstract void Handle(IMonitor monitor, string command, ArgumentParser args); + + /// Perform any logic needed on update tick. + /// Writes messages to the console and log file. + public virtual void OnUpdated(IMonitor monitor) { } + + /// Perform any logic when input is received. + /// Writes messages to the console and log file. + /// The button that was pressed. + public virtual void OnButtonPressed(IMonitor monitor, SButton button) { } + + + /********* + ** Protected methods + *********/ + /// Construct an instance. + /// The command name the user must type. + /// The command description. + /// Whether the command may need to perform logic when the player presses a button. + /// Whether the command may need to perform logic when the game updates. + protected ConsoleCommand(string name, string description, bool mayNeedInput = false, bool mayNeedUpdate = false) + { + this.Name = name; + this.Description = description; + this.MayNeedInput = mayNeedInput; + this.MayNeedUpdate = mayNeedUpdate; + } + + /// Log an error indicating incorrect usage. + /// Writes messages to the console and log file. + /// A sentence explaining the problem. + protected void LogUsageError(IMonitor monitor, string error) + { + monitor.Log($"{error} Type 'help {this.Name}' for usage.", LogLevel.Error); + } + + /// Log an error indicating a value must be an integer. + /// Writes messages to the console and log file. + protected void LogArgumentNotInt(IMonitor monitor) + { + this.LogUsageError(monitor, "The value must be a whole number."); + } + + /// Get an ASCII table to show tabular data in the console. + /// The data type. + /// The data to display. + /// The table header. + /// Returns a set of fields for a data value. + protected string GetTableString(IEnumerable data, string[] header, Func getRow) + { + // get table data + int[] widths = header.Select(p => p.Length).ToArray(); + string[][] rows = data + .Select(item => + { + string[] fields = getRow(item); + if (fields.Length != widths.Length) + throw new InvalidOperationException($"Expected {widths.Length} columns, but found {fields.Length}: {string.Join(", ", fields)}"); + + for (int i = 0; i < fields.Length; i++) + widths[i] = Math.Max(widths[i], fields[i].Length); + + return fields; + }) + .ToArray(); + + // render fields + List lines = new List(rows.Length + 2) + { + header, + header.Select((value, i) => "".PadRight(widths[i], '-')).ToArray() + }; + lines.AddRange(rows); + + return string.Join( + Environment.NewLine, + lines.Select(line => string.Join(" | ", + line.Select((field, i) => field.PadLeft(widths[i], ' ')) + )) + ); + } + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs new file mode 100644 index 00000000..9c82bbd3 --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs @@ -0,0 +1,40 @@ +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands +{ + /// A console command to register. + internal interface IConsoleCommand + { + /********* + ** Accessors + *********/ + /// The command name the user must type. + string Name { get; } + + /// The command description. + string Description { get; } + + /// Whether the command may need to perform logic when the game updates. This value shouldn't change. + bool MayNeedUpdate { get; } + + /// Whether the command may need to perform logic when the player presses a button. This value shouldn't change. + bool MayNeedInput { get; } + + + /********* + ** Public methods + *********/ + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + void Handle(IMonitor monitor, string command, ArgumentParser args); + + /// Perform any logic needed on update tick. + /// Writes messages to the console and log file. + void OnUpdated(IMonitor monitor); + + /// Perform any logic when input is received. + /// Writes messages to the console and log file. + /// The button that was pressed. + void OnButtonPressed(IMonitor monitor, SButton button); + } +} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs deleted file mode 100644 index d4d36e5d..00000000 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ITrainerCommand.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands -{ - /// A console command to register. - internal interface ITrainerCommand - { - /********* - ** Accessors - *********/ - /// The command name the user must type. - string Name { get; } - - /// The command description. - string Description { get; } - - /// Whether the command may need to perform logic when the game updates. This value shouldn't change. - bool MayNeedUpdate { get; } - - /// Whether the command may need to perform logic when the player presses a button. This value shouldn't change. - bool MayNeedInput { get; } - - - /********* - ** Public methods - *********/ - /// Handle the command. - /// Writes messages to the console and log file. - /// The command name. - /// The command arguments. - void Handle(IMonitor monitor, string command, ArgumentParser args); - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - void OnUpdated(IMonitor monitor); - - /// Perform any logic when input is received. - /// Writes messages to the console and log file. - /// The button that was pressed. - void OnButtonPressed(IMonitor monitor, SButton button); - } -} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs index 8f59342e..957b0e75 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which runs one of the game's save migrations. - internal class ApplySaveFixCommand : TrainerCommand + internal class ApplySaveFixCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs index e4010111..1955c14e 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs @@ -3,7 +3,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which sends a debug command to the game. - internal class DebugCommand : TrainerCommand + internal class DebugCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs index 54d27185..27f6ce53 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs @@ -3,7 +3,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which shows the data files. - internal class ShowDataFilesCommand : TrainerCommand + internal class ShowDataFilesCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs index 0257892f..71093184 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs @@ -3,7 +3,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which shows the game files. - internal class ShowGameFilesCommand : TrainerCommand + internal class ShowGameFilesCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs index 11aa10c3..46583dc3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs @@ -3,7 +3,7 @@ using System; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { /// A command which logs the keys being pressed for 30 seconds once enabled. - internal class TestInputCommand : TrainerCommand + internal class TestInputCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 6cb2b624..0e8f7517 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -7,7 +7,7 @@ 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 + internal class AddCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs index a835455e..1f12e5f9 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs @@ -4,7 +4,7 @@ using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which list item types. - internal class ListItemTypesCommand : TrainerCommand + internal class ListItemTypesCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs index 4232ce16..67569298 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs @@ -6,7 +6,7 @@ using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which list items available to spawn. - internal class ListItemsCommand : TrainerCommand + internal class ListItemsCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs index f0815ef6..7b7cbf83 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the color of a player feature. - internal class SetColorCommand : TrainerCommand + internal class SetColorCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs index 41a5b2af..f27b336f 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current health. - internal class SetHealthCommand : TrainerCommand + internal class SetHealthCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs index 9c66c4fe..df90adf2 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current immunity. - internal class SetImmunityCommand : TrainerCommand + internal class SetImmunityCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs index f4ae0694..a5f7f444 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's maximum health. - internal class SetMaxHealthCommand : TrainerCommand + internal class SetMaxHealthCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs index 5bce5ea3..e3c2f011 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's maximum stamina. - internal class SetMaxStaminaCommand : TrainerCommand + internal class SetMaxStaminaCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs index e015f01a..787ce920 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current money. - internal class SetMoneyCommand : TrainerCommand + internal class SetMoneyCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs index e8cb0584..4911ad1c 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs @@ -3,7 +3,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's name. - internal class SetNameCommand : TrainerCommand + internal class SetNameCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs index a8adafce..c78378ef 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits the player's current stamina. - internal class SetStaminaCommand : TrainerCommand + internal class SetStaminaCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs index 31f4107d..98f6c330 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs @@ -3,7 +3,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player { /// A command which edits a player style. - internal class SetStyleCommand : TrainerCommand + internal class SetStyleCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs deleted file mode 100644 index 98daa906..00000000 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/TrainerCommand.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands -{ - /// The base implementation for a trainer command. - internal abstract class TrainerCommand : ITrainerCommand - { - /********* - ** Accessors - *********/ - /// The command name the user must type. - public string Name { get; } - - /// The command description. - public string Description { get; } - - /// Whether the command may need to perform logic when the player presses a button. This value shouldn't change. - public bool MayNeedInput { get; } - - /// Whether the command may need to perform logic when the game updates. This value shouldn't change. - public bool MayNeedUpdate { get; } - - - /********* - ** Public methods - *********/ - /// Handle the command. - /// Writes messages to the console and log file. - /// The command name. - /// The command arguments. - public abstract void Handle(IMonitor monitor, string command, ArgumentParser args); - - /// Perform any logic needed on update tick. - /// Writes messages to the console and log file. - public virtual void OnUpdated(IMonitor monitor) { } - - /// Perform any logic when input is received. - /// Writes messages to the console and log file. - /// The button that was pressed. - public virtual void OnButtonPressed(IMonitor monitor, SButton button) { } - - - /********* - ** Protected methods - *********/ - /// Construct an instance. - /// The command name the user must type. - /// The command description. - /// Whether the command may need to perform logic when the player presses a button. - /// Whether the command may need to perform logic when the game updates. - protected TrainerCommand(string name, string description, bool mayNeedInput = false, bool mayNeedUpdate = false) - { - this.Name = name; - this.Description = description; - this.MayNeedInput = mayNeedInput; - this.MayNeedUpdate = mayNeedUpdate; - } - - /// Log an error indicating incorrect usage. - /// Writes messages to the console and log file. - /// A sentence explaining the problem. - protected void LogUsageError(IMonitor monitor, string error) - { - monitor.Log($"{error} Type 'help {this.Name}' for usage.", LogLevel.Error); - } - - /// Log an error indicating a value must be an integer. - /// Writes messages to the console and log file. - protected void LogArgumentNotInt(IMonitor monitor) - { - this.LogUsageError(monitor, "The value must be a whole number."); - } - - /// Get an ASCII table to show tabular data in the console. - /// The data type. - /// The data to display. - /// The table header. - /// Returns a set of fields for a data value. - protected string GetTableString(IEnumerable data, string[] header, Func getRow) - { - // get table data - int[] widths = header.Select(p => p.Length).ToArray(); - string[][] rows = data - .Select(item => - { - string[] fields = getRow(item); - if (fields.Length != widths.Length) - throw new InvalidOperationException($"Expected {widths.Length} columns, but found {fields.Length}: {string.Join(", ", fields)}"); - - for (int i = 0; i < fields.Length; i++) - widths[i] = Math.Max(widths[i], fields[i].Length); - - return fields; - }) - .ToArray(); - - // render fields - List lines = new List(rows.Length + 2) - { - header, - header.Select((value, i) => "".PadRight(widths[i], '-')).ToArray() - }; - lines.AddRange(rows); - - return string.Join( - Environment.NewLine, - lines.Select(line => string.Join(" | ", - line.Select((field, i) => field.PadLeft(widths[i], ' ')) - )) - ); - } - } -} diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs index 29052be3..4b0e45a0 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs @@ -10,7 +10,7 @@ using SObject = StardewValley.Object; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which clears in-game objects. - internal class ClearCommand : TrainerCommand + internal class ClearCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs index 2cec0fd3..0aa9c9c3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs @@ -4,7 +4,7 @@ using StardewValley.Locations; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which moves the player to the next mine level. - internal class DownMineLevelCommand : TrainerCommand + internal class DownMineLevelCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs index 736a93a0..16faa2fe 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which freezes the current time. - internal class FreezeTimeCommand : TrainerCommand + internal class FreezeTimeCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs index 23c266ea..4028b3dc 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current day. - internal class SetDayCommand : TrainerCommand + internal class SetDayCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs index b4f6d5b3..40f4b19f 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs @@ -4,7 +4,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which moves the player to the given mine level. - internal class SetMineLevelCommand : TrainerCommand + internal class SetMineLevelCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs index 676369fe..a4cb35bb 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current season. - internal class SetSeasonCommand : TrainerCommand + internal class SetSeasonCommand : ConsoleCommand { /********* ** Fields diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs index 9eae6741..6782e38a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current time. - internal class SetTimeCommand : TrainerCommand + internal class SetTimeCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs index 648830c1..95401962 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs @@ -5,7 +5,7 @@ using StardewValley; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { /// A command which sets the current year. - internal class SetYearCommand : TrainerCommand + internal class SetYearCommand : ConsoleCommand { /********* ** Public methods diff --git a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs index 5c4f3bba..91437fd3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs +++ b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs @@ -13,13 +13,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands ** Fields *********/ /// The commands to handle. - private ITrainerCommand[] Commands; + private IConsoleCommand[] Commands; /// The commands which may need to handle update ticks. - private ITrainerCommand[] UpdateHandlers; + private IConsoleCommand[] UpdateHandlers; /// The commands which may need to handle input. - private ITrainerCommand[] InputHandlers; + private IConsoleCommand[] InputHandlers; /********* @@ -31,7 +31,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands { // register commands this.Commands = this.ScanForCommands().ToArray(); - foreach (ITrainerCommand command in this.Commands) + foreach (IConsoleCommand command in this.Commands) helper.ConsoleCommands.Add(command.Name, command.Description, (name, args) => this.HandleCommand(command, name, args)); // cache commands @@ -52,7 +52,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The event arguments. private void OnButtonPressed(object sender, ButtonPressedEventArgs e) { - foreach (ITrainerCommand command in this.InputHandlers) + foreach (IConsoleCommand command in this.InputHandlers) command.OnButtonPressed(this.Monitor, e.Button); } @@ -61,7 +61,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The event arguments. private void OnUpdateTicked(object sender, EventArgs e) { - foreach (ITrainerCommand command in this.UpdateHandlers) + foreach (IConsoleCommand command in this.UpdateHandlers) command.OnUpdated(this.Monitor); } @@ -69,19 +69,19 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The command to invoke. /// The command name specified by the user. /// The command arguments. - private void HandleCommand(ITrainerCommand command, string commandName, string[] args) + private void HandleCommand(IConsoleCommand command, string commandName, string[] args) { ArgumentParser argParser = new ArgumentParser(commandName, args, this.Monitor); command.Handle(this.Monitor, commandName, argParser); } /// Find all commands in the assembly. - private IEnumerable ScanForCommands() + private IEnumerable ScanForCommands() { return ( from type in this.GetType().Assembly.GetTypes() - where !type.IsAbstract && typeof(ITrainerCommand).IsAssignableFrom(type) - select (ITrainerCommand)Activator.CreateInstance(type) + where !type.IsAbstract && typeof(IConsoleCommand).IsAssignableFrom(type) + select (IConsoleCommand)Activator.CreateInstance(type) ); } } -- cgit