-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModConfig.cs
113 lines (91 loc) · 3.21 KB
/
ModConfig.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI;
using StardewValley;
using static RemoteControl.Utilities;
namespace RemoteControl
{
public class ModConfig
{
public class SavedConfig
{
public class Admin
{
public long id;
public string name;
}
public bool everyoneIsAdmin { get; set; } = false;
public List<Admin> admins { get; set; } = new List<Admin>();
public bool shouldAssignAdminToFirstCabinFarmer { get; set; } = false;
}
private IMonitor Monitor;
private IModHelper Helper;
public SavedConfig json;
public ModConfig(IMonitor monitor, IModHelper helper)
{
Monitor = monitor;
Helper = helper;
json = Helper.ReadConfig<SavedConfig>();
}
public void assignFirstAdminIfNeeded()
{
if (!json.shouldAssignAdminToFirstCabinFarmer)
{
return;
}
foreach (Farmer farmer in getAllPlayers())
{
Monitor.Log($"Assigning first farmer {farmer}", LogLevel.Debug);
Monitor.Log($"{farmer.UniqueMultiplayerID} = farmhand id", LogLevel.Debug);
Monitor.Log($"{farmer.Name} = farmhand name", LogLevel.Debug);
json.shouldAssignAdminToFirstCabinFarmer = false;
addAdmin(farmer); // This will save both settings
break;
}
}
// Add an admin
public void addAdmin(Farmer farmer)
{
if (farmer is null)
{
return;
}
Monitor.Log($"Adding {farmer.Name} ({farmer.UniqueMultiplayerID}) to admin list", LogLevel.Debug);
SavedConfig.Admin newAdmin = new SavedConfig.Admin();
newAdmin.id = farmer.UniqueMultiplayerID;
newAdmin.name = farmer.Name;
json.admins.Add(newAdmin);
Helper.WriteConfig<SavedConfig>(json);
}
// Remove admins by either username or id (so they don't have to be online)
public void removeAdmin(string farmerDetails)
{
Monitor.Log($"Trying to remove {farmerDetails} from admin list", LogLevel.Debug);
foreach (SavedConfig.Admin admin in json.admins.ToList())
{
if (admin.id.ToString() == farmerDetails || admin.name == farmerDetails)
{
Monitor.Log($"Removing {admin.name} ({admin.id}) from admin list", LogLevel.Debug);
json.admins.Remove(admin);
Helper.WriteConfig<SavedConfig>(json);
return;
}
}
}
public bool isAdmin(Farmer fromPlayer)
{
if (isHost(fromPlayer) || json.everyoneIsAdmin)
{
return true;
}
foreach (SavedConfig.Admin admin in json.admins.ToList())
{
if (admin.id == fromPlayer.UniqueMultiplayerID)
{
return true;
}
}
return false;
}
}
}