summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/SChatBox.cs
blob: e000d1cdf9759d20d4a6aa6949b225dcd171ac4e (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
using StardewValley;
using StardewValley.Menus;

namespace StardewModdingAPI.Framework
{
    /// <summary>SMAPI's implementation of the chatbox which intercepts errors for logging.</summary>
    internal class SChatBox : ChatBox
    {
        /*********
        ** Fields
        *********/
        /// <summary>Encapsulates monitoring and logging.</summary>
        private readonly IMonitor Monitor;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        public SChatBox(IMonitor monitor)
        {
            this.Monitor = monitor;
        }

        /// <inheritdoc />
        protected override void runCommand(string command)
        {
            this.Monitor.Log($"> chat command: {command}");
            base.runCommand(command);
        }

        /// <inheritdoc />
        public override void receiveChatMessage(long sourceFarmer, int chatKind, LocalizedContentManager.LanguageCode language, string message)
        {
            if (chatKind == ChatBox.errorMessage)
            {
                // log error
                this.Monitor.Log(message, LogLevel.Error);

                // add event details if applicable
                if (Game1.CurrentEvent != null && message.StartsWith("Event script error:"))
                    this.Monitor.Log($"In event #{Game1.CurrentEvent.id} for location {Game1.currentLocation?.NameOrUniqueName}", LogLevel.Error);
            }

            base.receiveChatMessage(sourceFarmer, chatKind, language, message);
        }
    }
}