source: binary-improvements/7dtd-server-fixes/src/API.cs@ 367

Last change on this file since 367 was 354, checked in by alloc, 5 years ago

A18 preps

File size: 3.1 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.PersistentData;
3using System;
4
5namespace AllocsFixes {
6 public class API : IModApi {
7 public void InitMod () {
8 ModEvents.GameStartDone.RegisterHandler (GameAwake);
9 ModEvents.GameShutdown.RegisterHandler (GameShutdown);
10 ModEvents.SavePlayerData.RegisterHandler (SavePlayerData);
11 ModEvents.PlayerSpawning.RegisterHandler (PlayerSpawning);
12 ModEvents.PlayerDisconnected.RegisterHandler (PlayerDisconnected);
13 ModEvents.PlayerSpawnedInWorld.RegisterHandler (PlayerSpawned);
14 ModEvents.ChatMessage.RegisterHandler (ChatMessage);
15 }
16
17 public void GameAwake () {
18 try {
19 PersistentContainer.Load ();
20 } catch (Exception e) {
21 Log.Out ("Error in StateManager.Awake: " + e);
22 }
23 }
24
25 public void GameShutdown () {
26 try {
27 Log.Out ("Server shutting down!");
28 PersistentContainer.Instance.Save ();
29 } catch (Exception e) {
30 Log.Out ("Error in StateManager.Shutdown: " + e);
31 }
32 }
33
34 public void SavePlayerData (ClientInfo _cInfo, PlayerDataFile _playerDataFile) {
35 try {
36 PersistentContainer.Instance.Players [_cInfo.playerId, true].Update (_playerDataFile);
37 } catch (Exception e) {
38 Log.Out ("Error in GM_SavePlayerData: " + e);
39 }
40 }
41
42 public void PlayerSpawning (ClientInfo _cInfo, int _chunkViewDim, PlayerProfile _playerProfile) {
43 try {
44 Log.Out ("Player connected" +
45 ", entityid=" + _cInfo.entityId +
46 ", name=" + _cInfo.playerName +
47 ", steamid=" + _cInfo.playerId +
48 ", steamOwner=" + _cInfo.ownerId +
49 ", ip=" + _cInfo.ip
50 );
51 } catch (Exception e) {
52 Log.Out ("Error in AllocsLogFunctions.RequestToSpawnPlayer: " + e);
53 }
54 }
55
56 public void PlayerDisconnected (ClientInfo _cInfo, bool _bShutdown) {
57 try {
58 Player p = PersistentContainer.Instance.Players [_cInfo.playerId, false];
59 if (p != null) {
60 p.SetOffline ();
61 } else {
62 Log.Out ("Disconnected player not found in client list...");
63 }
64
65 PersistentContainer.Instance.Save ();
66 } catch (Exception e) {
67 Log.Out ("Error in AllocsLogFunctions.PlayerDisconnected: " + e);
68 }
69 }
70
71 public void PlayerSpawned (ClientInfo _cInfo, RespawnType _respawnReason, Vector3i _spawnPos) {
72 try {
73 PersistentContainer.Instance.Players [_cInfo.playerId, true].SetOnline (_cInfo);
74 PersistentContainer.Instance.Save ();
75 } catch (Exception e) {
76 Log.Out ("Error in AllocsLogFunctions.PlayerSpawnedInWorld: " + e);
77 }
78 }
79
80 private const string ANSWER =
81 " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
82
83 public bool ChatMessage (ClientInfo _cInfo, EChatType _type, int _senderId, string _msg, string _mainName,
84 bool _localizeMain, List<int> _recipientEntityIds) {
85 if (string.IsNullOrEmpty (_msg) || !_msg.EqualsCaseInsensitive ("/alloc")) {
86 return true;
87 }
88
89 if (_cInfo != null) {
90 Log.Out ("Sent chat hook reply to {0}", _cInfo.playerId);
91 _cInfo.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, ANSWER, "", false, null));
92 } else {
93 Log.Error ("ChatHookExample: Argument _cInfo null on message: {0}", _msg);
94 }
95
96 return false;
97 }
98 }
99}
Note: See TracBrowser for help on using the repository browser.