diff options
Diffstat (limited to 'src/TrainerMod')
4 files changed, 96 insertions, 40 deletions
diff --git a/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs b/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs new file mode 100644 index 00000000..5f14edbb --- /dev/null +++ b/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs @@ -0,0 +1,53 @@ +using System.Linq; +using StardewModdingAPI; +using TrainerMod.Framework.ItemData; + +namespace TrainerMod.Framework.Commands.Player +{ + /// <summary>A command which list item types.</summary> + internal class ListItemTypesCommand : 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 ListItemTypesCommand() + : base("list_item_types", "Lists item types you can filter in other commands.\n\nUsage: list_item_types") { } + + /// <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) + { + // validate + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + + // handle + ItemType[] matches = + ( + from item in this.Items.GetAll() + orderby item.Type.ToString() + select item.Type + ) + .Distinct() + .ToArray(); + string summary = "Searching...\n"; + if (matches.Any()) + monitor.Log(summary + this.GetTableString(matches, new[] { "type" }, val => new[] { val.ToString() }), LogLevel.Info); + else + monitor.Log(summary + "No item types found.", LogLevel.Info); + } + } +} diff --git a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs index 68adf8c2..30c3de3b 100644 --- a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs @@ -77,44 +77,5 @@ namespace TrainerMod.Framework.Commands.Player yield return weapon; } } - - /// <summary>Get an ASCII table for a set of tabular data.</summary> - /// <typeparam name="T">The data type.</typeparam> - /// <param name="data">The data to display.</param> - /// <param name="header">The table header.</param> - /// <param name="getRow">Returns a set of fields for a data value.</param> - private string GetTableString<T>(IEnumerable<T> data, string[] header, Func<T, string[]> 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<string[]> lines = new List<string[]>(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.PadRight(widths[i], ' ')).ToArray()) - ) - ); - } } } diff --git a/src/TrainerMod/Framework/Commands/TrainerCommand.cs b/src/TrainerMod/Framework/Commands/TrainerCommand.cs index 4715aa04..abe9ee41 100644 --- a/src/TrainerMod/Framework/Commands/TrainerCommand.cs +++ b/src/TrainerMod/Framework/Commands/TrainerCommand.cs @@ -1,4 +1,7 @@ -using StardewModdingAPI; +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI; namespace TrainerMod.Framework.Commands { @@ -58,5 +61,43 @@ namespace TrainerMod.Framework.Commands { this.LogUsageError(monitor, "The value must be a whole number."); } + + /// <summary>Get an ASCII table to show tabular data in the console.</summary> + /// <typeparam name="T">The data type.</typeparam> + /// <param name="data">The data to display.</param> + /// <param name="header">The table header.</param> + /// <param name="getRow">Returns a set of fields for a data value.</param> + protected string GetTableString<T>(IEnumerable<T> data, string[] header, Func<T, string[]> 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<string[]> lines = new List<string[]>(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.PadRight(widths[i], ' ')).ToArray()) + ) + ); + } } } diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index 18abf42f..8e4e2b2e 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -55,6 +55,7 @@ <Compile Include="Framework\Commands\Other\ShowDataFilesCommand.cs" /> <Compile Include="Framework\Commands\Other\ShowGameFilesCommand.cs" /> <Compile Include="Framework\Commands\Other\DebugCommand.cs" /> + <Compile Include="Framework\Commands\Player\ListItemTypesCommand.cs" /> <Compile Include="Framework\Commands\Player\ListItemsCommand.cs" /> <Compile Include="Framework\Commands\Player\AddWallpaperCommand.cs" /> <Compile Include="Framework\Commands\Player\AddFlooringCommand.cs" /> |