Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Eleon;
  8. using Eleon.Modding;
  9. using PrimS.Telnet;
  10.  
  11. namespace JCMod
  12. {
  13.     // Client mod
  14.  
  15.     // Class implementing both the new IMod and the legacy ModInterface interfaces as both are available on dedi side
  16.     public class JCLocalChatMod :  ModInterface
  17.     {
  18.         IModApi modApi;
  19.         ModGameAPI legacyModApi;
  20.         List<PlayerInfo> players;
  21.  
  22.         //telnet
  23.         private CancellationTokenSource cancellationTokenSource;
  24.         Client telnetClient;
  25.  
  26.         // ----- IMod methods -------------------------------------------------
  27.  
  28.         // Called once early when the host process starts - treat this like a constructor for your mod
  29.         public void Init(IModApi modApi)
  30.         {
  31.             this.modApi = modApi;
  32.  
  33.             modApi.Log("Local Chat mod by JCD initialized. My vision is augmented.");
  34.  
  35.             modApi.Log($"GameTime (ticks): {modApi.Application.GameTicks}");
  36.  
  37.             cancellationTokenSource = new CancellationTokenSource();
  38.             telnetClient = new Client("hostname", 0, cancellationTokenSource.Token);
  39.         }
  40.  
  41.         // Called once just before the game is shut down
  42.         // You may use this like a Dispose method for your mod to release unmanaged resources
  43.         public void Shutdown()
  44.         {
  45.             modApi.Log("Local Chat mod by JCD shutdown. Such a shame.");
  46.         }
  47.  
  48.  
  49.         // ----- ModInterface methods -----------------------------------------
  50.  
  51.         // Called once early when the host process starts - treat this like a constructor for your mod
  52.         public void Game_Start(ModGameAPI legacyModApi)
  53.         {
  54.             this.legacyModApi = legacyModApi;
  55.         }
  56.  
  57.         // Called once just before the game is shut down
  58.         // You may use this like a Dispose method for your mod to release unmanaged resources
  59.         public void Game_Exit()
  60.         {
  61.         }
  62.  
  63.         public void Game_Event(CmdId eventId, ushort seqNr, object data)
  64.         {
  65.             try
  66.  
  67.             {
  68.  
  69.                 switch (eventId)
  70.  
  71.                 {
  72.                     case CmdId.Event_Player_Info:
  73.  
  74.                         if (players.Where(e => e.entityId == ((PlayerInfo)data).entityId).Count() == 0)
  75.  
  76.                         {
  77.  
  78.                             players.Add((PlayerInfo)data);
  79.  
  80.                         }
  81.  
  82.                         break;
  83.  
  84.                     case CmdId.Event_Player_Connected:
  85.  
  86.                         legacyModApi.Game_Request(CmdId.Request_Player_Info, (ushort)CmdId.Request_Player_Info, (Id)data);
  87.  
  88.                         break;
  89.  
  90.                     case CmdId.Event_Player_Disconnected:
  91.  
  92.                         players.Remove(players.FirstOrDefault(e => e.entityId == ((Id)data).id));
  93.  
  94.                         break;
  95.  
  96.                     case CmdId.Event_ChatMessage:
  97.  
  98.                         ChatInfo ci = (ChatInfo)data;
  99.  
  100.                         if (ci == null) { break; }
  101.  
  102.                         PlayerInfo sender = players.Find(player => player.entityId == ci.playerId);
  103.  
  104.                         for(var i = 0; i < players.Count; i ++)
  105.                         {
  106.                             var cp = players[i];
  107.                             if (cp.playfield == sender.playfield)
  108.                             {
  109.                                 if (PV3Distance(cp.pos,sender.pos) < 20f)
  110.                                 {
  111.                                     telnetClient.Write("say p:" + cp.clientId + " {" + sender.playerName + "}: " + ci.msg);
  112.                                 }
  113.                             }
  114.                         }
  115.  
  116.                         break;
  117.  
  118.                     default:
  119.  
  120.                         break;
  121.  
  122.                 }
  123.  
  124.             }
  125.  
  126.             catch (Exception ex)
  127.  
  128.             {
  129.  
  130.                 legacyModApi.Console_Write(ex.Message);
  131.  
  132.             }
  133.         }
  134.  
  135.         // called each frame - don't waste time here!
  136.         public void Game_Update()
  137.         {
  138.         }
  139.  
  140.  
  141.         private float PV3Distance(PVector3 me, PVector3 other)
  142.         {
  143.             return (float)Math.Sqrt(Math.Pow(me.x - other.x, 2) + Math.Pow(me.y - other.y, 2) + Math.Pow(me.z - other.z, 2));
  144.         }
  145.     }
  146. }