summaryrefslogtreecommitdiff
path: root/src/TrainerMod/Framework/Commands/TrainerCommand.cs
blob: 4715aa04ca6e7fa2b4dc96b4d93ab56eb1ea4831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using StardewModdingAPI;

namespace TrainerMod.Framework.Commands
{
    /// <summary>The base implementation for a trainer command.</summary>
    internal abstract class TrainerCommand : ITrainerCommand
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The command name the user must type.</summary>
        public string Name { get; }

        /// <summary>The command description.</summary>
        public string Description { get; }

        /// <summary>Whether the command needs to perform logic when the game updates.</summary>
        public virtual bool NeedsUpdate { get; } = false;


        /*********
        ** Public methods
        *********/
        /// <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 abstract void Handle(IMonitor monitor, string command, ArgumentParser args);

        /// <summary>Perform any logic needed on update tick.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        public virtual void Update(IMonitor monitor) { }


        /*********
        ** Protected methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="name">The command name the user must type.</param>
        /// <param name="description">The command description.</param>
        protected TrainerCommand(string name, string description)
        {
            this.Name = name;
            this.Description = description;
        }

        /// <summary>Log an error indicating incorrect usage.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        /// <param name="error">A sentence explaining the problem.</param>
        protected void LogUsageError(IMonitor monitor, string error)
        {
            monitor.Log($"{error} Type 'help {this.Name}' for usage.", LogLevel.Error);
        }

        /// <summary>Log an error indicating a value must be an integer.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        protected void LogArgumentNotInt(IMonitor monitor)
        {
            this.LogUsageError(monitor, "The value must be a whole number.");
        }
    }
}