-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUraniumExpandedModSystem.cs
More file actions
101 lines (88 loc) · 4.19 KB
/
UraniumExpandedModSystem.cs
File metadata and controls
101 lines (88 loc) · 4.19 KB
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
namespace UraniumExpanded.ModSystem
{
using HarmonyLib;
using System.Linq;
using System.Net.Sockets;
using UraniumExpanded.ModConfiguration;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
using Vintagestory.API.Util;
public class UraniumExpandedModSystem : ModSystem
{
private const string ConfigFileName = "uraniumexpanded.json";
private ICoreClientAPI capi;
private ICoreAPI api;
private IServerNetworkChannel serverChannel;
public override void StartPre(ICoreAPI api)
{
base.StartPre(api);
try
{
ModConfig fromDisk;
if ((fromDisk = api.LoadModConfig<ModConfig>(ConfigFileName)) == null)
{ api.StoreModConfig(ModConfig.Loaded, ConfigFileName); }
else
{ ModConfig.Loaded = fromDisk; }
}
catch
{ api.StoreModConfig(ModConfig.Loaded, ConfigFileName); }
// Set a property that JSON patches can check
api.World.Config.SetBool("EnableUraniumTools", ModConfig.Loaded.EnableUraniumTools);
api.World.Config.SetBool("EnableUraniumGlass", ModConfig.Loaded.EnableUraniumGlass);
if (api.Side == EnumAppSide.Server)
{
//this.Mod.Logger.Event($"EnableUraniumTools set to {ModConfig.Loaded.EnableUraniumTools} on server");
//this.Mod.Logger.Event($"EnableUraniumGlass set to {ModConfig.Loaded.EnableUraniumGlass} on server");
}
bool emLoaded = api.ModLoader.GetMod("em") != null;
api.World.Config.SetBool("EMisPresent", emLoaded);
//this.Mod.Logger.Event($"[UraniumExpanded] em loaded = {emLoaded}. EMisPresent = {emLoaded}");
bool gaLoaded = api.ModLoader.GetMod("geoaddons") != null;
api.World.Config.SetBool("GAisPresent", gaLoaded);
//this.Mod.Logger.Event($"[UraniumExpanded] ga loaded = {gaLoaded}. GAisPresent = {gaLoaded}");
var harmony = new Harmony("uraniumexpanded.harmony");
harmony.PatchAll();
}
public override void StartClientSide(ICoreClientAPI api)
{
base.StartClientSide(api);
this.capi = api;
capi.Network.RegisterChannel("uraniumexpanded")
.RegisterMessageType<SyncClientPacket>()
.SetMessageHandler<SyncClientPacket>(packet =>
{
ModConfig.Loaded.EnableUraniumTools = packet.EnableUraniumTools;
//this.Mod.Logger.Event($"Received EnableUraniumTools of {packet.EnableUraniumTools} from server");
ModConfig.Loaded.EnableUraniumGlass = packet.EnableUraniumGlass;
//this.Mod.Logger.Event($"Received EnableUraniumGlass of {packet.EnableUraniumGlass} from server");
});
}
public override void StartServerSide(ICoreServerAPI sapi)
{
// send connecting players the config settings
sapi.Event.PlayerJoin += this.OnPlayerJoin; // add method so we can remove it in dispose to prevent memory leaks
// register network channel to send data to clients
this.serverChannel = sapi.Network.RegisterChannel("uraniumexpanded")
.RegisterMessageType<SyncClientPacket>()
.SetMessageHandler<SyncClientPacket>((player, packet) => { /* do nothing. idk why this handler is even needed, but it is */ });
}
private void OnPlayerJoin(IServerPlayer player)
{
// send the connecting player the settings it needs to be synced
this.serverChannel.SendPacket(new SyncClientPacket
{
EnableUraniumTools = ModConfig.Loaded.EnableUraniumTools,
EnableUraniumGlass = ModConfig.Loaded.EnableUraniumGlass,
}, player);
}
public override void Dispose()
{
// remove our player join listener so we dont create memory leaks
if (this.api is ICoreServerAPI sapi)
{
sapi.Event.PlayerJoin -= this.OnPlayerJoin;
}
}
}
}