forked from Novex/stardew-remote-control
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utilities.cs
77 lines (65 loc) · 2.79 KB
/
Utilities.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
using StardewModdingAPI;
using StardewValley;
using StardewValley.Menus;
namespace RemoteControl
{
public class Utilities
{
public static IModHelper Helper;
public static bool isHost(Farmer farmer)
{
return Game1.player == farmer;
}
public static string getPlayerName(long sourceFarmerId)
{
return getPlayerName(getFarmer(sourceFarmerId));
}
public static string getPlayerName(Farmer sourceFarmer)
=> sourceFarmer is not null
? ChatBox.formattedUserName(sourceFarmer)
: Game1.content.LoadString("Strings\\UI:Chat_UnknownUserName");
/// <summary>
/// Gets the farmer by ID.
/// Returns null if not found.
/// </summary>
/// <param name="farmerId">Farmer ID to search.</param>
/// <remarks>Game's Game1.getFarmer tries to substitute in the MasterPlayer if not found.</remarks>
public static Farmer? getFarmer(long farmerId)
{
if (farmerId == Game1.player.UniqueMultiplayerID)
return Game1.player;
if (Game1.otherFarmers.TryGetValue(farmerId, out Farmer farmer))
return farmer;
return null;
}
public static Farmer FirstFarmerByName(string name)
{
foreach (Farmer farmer in Game1.getOnlineFarmers())
{
if (farmer.Name == name)
{
return farmer;
}
}
return null;
}
// Sends a public chat message
public static void publishSystemMessage(string msg)
{
// send to everyone else
Multiplayer multiplayer = Helper.Reflection.GetField<Multiplayer>(typeof(Game1), "multiplayer").GetValue();
multiplayer.sendChatMessage(LocalizedContentManager.CurrentLanguageCode, msg, Multiplayer.AllPlayers);
// also show the message in our own message box
Game1.chatBox.receiveChatMessage(Game1.player.UniqueMultiplayerID, (int)ChatMessage.ChatKinds.UserNotification, LocalizedContentManager.CurrentLanguageCode, msg);
}
// Send a PM
public static void publishPrivateMessage(Farmer player, string msg)
{
// send to everyone else
Multiplayer multiplayer = Helper.Reflection.GetField<Multiplayer>(typeof(Game1), "multiplayer").GetValue();
multiplayer.sendChatMessage(LocalizedContentManager.CurrentLanguageCode, msg, player.UniqueMultiplayerID);
// also show the message in our own message box
Game1.chatBox.receiveChatMessage(Game1.player.UniqueMultiplayerID, (int)ChatMessage.ChatKinds.PrivateMessage, LocalizedContentManager.CurrentLanguageCode, msg);
}
}
}