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

Last change on this file since 490 was 488, checked in by alloc, 5 months ago

27_32_48

  • V1.0 compatibility
File size: 2.8 KB
RevLine 
[324]1using System.Collections.Generic;
[345]2using AllocsFixes.PersistentData;
[455]3using JetBrains.Annotations;
[369]4using Platform.Steam;
[230]5
[325]6namespace AllocsFixes {
[455]7 [UsedImplicitly]
[324]8 public class API : IModApi {
[369]9 public void InitMod (Mod _modInstance) {
[337]10 ModEvents.GameStartDone.RegisterHandler (GameAwake);
[325]11 ModEvents.SavePlayerData.RegisterHandler (SavePlayerData);
12 ModEvents.PlayerSpawning.RegisterHandler (PlayerSpawning);
13 ModEvents.PlayerDisconnected.RegisterHandler (PlayerDisconnected);
[345]14 ModEvents.PlayerSpawnedInWorld.RegisterHandler (PlayerSpawned);
[325]15 ModEvents.ChatMessage.RegisterHandler (ChatMessage);
16 }
[230]17
[455]18 private static void GameAwake () {
[369]19 PersistentContainer.Load ();
[230]20 }
21
[455]22 private static void SavePlayerData (ClientInfo _cInfo, PlayerDataFile _playerDataFile) {
[446]23 PersistentContainer.Instance.Players.GetOrCreate (_cInfo.InternalId, _cInfo.PlatformId, _cInfo.CrossplatformId).Update (_cInfo, _playerDataFile);
[230]24 }
25
[455]26 private static void PlayerSpawning (ClientInfo _cInfo, int _chunkViewDim, PlayerProfile _playerProfile) {
[369]27 string owner = null;
28 if (_cInfo.PlatformId is UserIdentifierSteam identifierSteam) {
29 owner = identifierSteam.OwnerId.ToString ();
[345]30 }
[369]31
[455]32 Log.Out (
33 $"Player connected, entityid={_cInfo.entityId}, name={_cInfo.playerName}, pltfmid={_cInfo.PlatformId?.CombinedString ?? "<unknown>"}, crossid={_cInfo.CrossplatformId?.CombinedString ?? "<unknown/none>"}, steamOwner={owner ?? "<unknown/none>"}, ip={_cInfo.ip}"
[369]34 );
[230]35 }
[325]36
[455]37 private static void PlayerDisconnected (ClientInfo _cInfo, bool _bShutdown) {
[446]38 Player p = PersistentContainer.Instance.Players.GetByInternalId (_cInfo.InternalId);
[369]39 if (p != null) {
40 p.SetOffline ();
41 } else {
42 Log.Out ("Disconnected player not found in client list...");
43 }
[345]44
[369]45 PersistentContainer.Instance.Save ();
[230]46 }
47
[455]48 private static void PlayerSpawned (ClientInfo _cInfo, RespawnType _respawnReason, Vector3i _spawnPos) {
[488]49 if (_cInfo == null) {
50 return;
51 }
52
[446]53 PersistentContainer.Instance.Players.GetOrCreate (_cInfo.InternalId, _cInfo.PlatformId, _cInfo.CrossplatformId).SetOnline (_cInfo);
[369]54 PersistentContainer.Instance.Save ();
[345]55 }
56
[455]57 private const string ANSWER = " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
[345]58
[455]59 private static bool ChatMessage (ClientInfo _cInfo, EChatType _type, int _senderId, string _msg, string _mainName,
[488]60 List<int> _recipientEntityIds) {
[345]61 if (string.IsNullOrEmpty (_msg) || !_msg.EqualsCaseInsensitive ("/alloc")) {
62 return true;
63 }
64
65 if (_cInfo != null) {
[455]66 Log.Out ($"Sent chat hook reply to {_cInfo.InternalId}");
[488]67 _cInfo.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, ANSWER, "", null));
[345]68 } else {
[455]69 Log.Error ($"ChatHookExample: Argument _cInfo null on message: {_msg}");
[345]70 }
71
72 return false;
[238]73 }
[230]74 }
[325]75}
Note: See TracBrowser for help on using the repository browser.