forked from Novex/stardew-remote-control
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModEntry.cs
99 lines (74 loc) · 3.15 KB
/
ModEntry.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections.Generic;
using HarmonyLib;
using RemoteControl.Commands;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using static RemoteControl.Utilities;
namespace RemoteControl
{
public partial class ModEntry : Mod
{
private ModConfig Config;
static List<ChatMessage> chatMessages = new List<ChatMessage>();
HostCommands hostCommands;
UserCommands userCommands;
AdminCommands adminCommands;
public override void Entry(IModHelper helper)
{
Utilities.Helper = helper;
this.Config = new ModConfig(Monitor, helper);
hostCommands = new HostCommands(Config);
userCommands = new UserCommands(Config);
adminCommands = new AdminCommands(Config);
helper.Events.GameLoop.OneSecondUpdateTicked += this.OnOneSecondUpdateTicked;
helper.Events.GameLoop.Saved += this.OnSaved;
Harmony harmony = new (this.ModManifest.UniqueID);
harmony.Patch(
original: AccessTools.Method(typeof(StardewValley.Menus.ChatBox), nameof(StardewValley.Menus.ChatBox.receiveChatMessage)),
postfix: new HarmonyMethod(typeof(ModEntry), nameof(ModEntry.receiveChatMessage_Postfix))
);
helper = this.Helper;
}
public override object GetApi()
{
return new ModApi(hostCommands.AddCommand, adminCommands.AddCommand, userCommands.AddCommand);
}
private void OnOneSecondUpdateTicked(object sender, OneSecondUpdateTickedEventArgs e)
{
if (!Context.IsWorldReady)
return;
Config.assignFirstAdminIfNeeded();
// Process chat messages that came in
foreach (ChatMessage chatMessage in chatMessages)
{
if (chatMessage.chatKind == ChatMessage.ChatKinds.ChatMessage || chatMessage.chatKind == ChatMessage.ChatKinds.PrivateMessage)
{
Farmer farmer = getFarmer(chatMessage.sourceFarmer);
if (farmer is null)
{
this.Monitor.Log($"{chatMessage.sourceFarmer} not found!", LogLevel.Info);
continue;
}
hostCommands.ParseCommand(farmer, chatMessage.message);
adminCommands.ParseCommand(farmer, chatMessage.message);
userCommands.ParseCommand(farmer, chatMessage.message);
}
}
chatMessages.Clear();
}
private void OnSaved(object sender, SavedEventArgs e)
{
hostCommands.OnSaved(sender, e);
}
internal static void receiveChatMessage_Postfix(long sourceFarmer, int chatKind, LocalizedContentManager.LanguageCode language, string message)
{
ChatMessage msg = new ChatMessage();
msg.sourceFarmer = sourceFarmer;
msg.chatKind = (ChatMessage.ChatKinds)chatKind;
msg.language = language;
msg.message = message;
chatMessages.Add(msg);
}
}
}