MrWolfRanger

Untitled

May 24th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.64 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Text;
  7. using Facepunch;
  8. using Facepunch.Math;
  9. using Newtonsoft.Json;
  10. using Oxide.Core;
  11. using Oxide.Core.Plugins;
  12. using Oxide.Game.Rust.Cui;
  13. using Rust;
  14. using UnityEngine;
  15. using ProtoBuf;
  16.  
  17. /**********************************************************************
  18. *
  19. * 2.1.0 - Npc displaynames will now be propperly used
  20. * - Npc without names will use their translated prefab name or custom name
  21. * - Fixed -1 notification when blown up
  22. * - When blown up will show what explosive is used
  23. * - Removed old Blownup sequence and using the regular kill messages,
  24. * since the weaponcheck check is updated with explosives
  25. * - Added Animal kills to the feed (by example of crazyR17)
  26. * 2.1.1 - Removed obsolete Blown messages from language file
  27. * - Reverted distance check
  28. * - Hotfix
  29. * 2.1.2 - More checks in GetWeaponName
  30. * - Added Bradley shell kills and feed
  31. * - Added direct falling deaths to the feed
  32. * - For animals : instead of entity prefab check on each death it just checks for BaseAnimalNPC
  33. * then does the check listed in cfg (this should improve performance when animal feed is used)
  34. * - Added SimpleShark to the feed
  35. * - Fixed occasional error on suicide
  36. * - Possible fix for npc suicides
  37. * + "MsgFeedKillFall", "<color=#ff686b>{0}</color> died by <color=orange>Fall</color>"
  38. * + "MsgFeedKillBrad", "<color=#ff686b>{0}</color> was killed by a <color=orange>Shell</color>"
  39. *
  40. ***********************************************************************/
  41. namespace Oxide.Plugins
  42. {
  43. [Info("Simple Kill Feed", "Krungh Crow", "2.1.2")]
  44. [Description("A simple kill feed, that displays in the top right corner various kill events.")]
  45. public class SimpleKillFeed : RustPlugin
  46. {
  47. #region Fields
  48.  
  49. [PluginReference] private Plugin Clans;
  50. private bool _isClansReborn;
  51. private bool _isClansIo;
  52. private readonly Dictionary<uint, string> _itemNameMapping = new Dictionary<uint, string>();
  53. private GameObject _holder;
  54. private KillQueue _killQueue;
  55. private static SKFConfig _config;
  56. private static SimpleKillFeedData _data;
  57.  
  58. #endregion
  59.  
  60. #region Config
  61.  
  62. private class SKFConfig
  63. {
  64. [JsonProperty("Show Traps and Entitys in Kill Feed")]
  65. public bool EnableEntityFeed;
  66. [JsonProperty("Show Animals in Kill Feed")]
  67. public bool EnableAnimalFeed;
  68. [JsonProperty("Show Npcs in Kill Feed")]
  69. public bool EnableNpcFeed;
  70. [JsonProperty("Show suicides in KillFeed (Default: true)")]
  71. public bool EnableSuicides;
  72. [JsonProperty("Show radiation kills in KillFeed (Default: true)")]
  73. public bool EnableRadiationKills;
  74. [JsonProperty("Chat Icon Id (Steam profile ID)")]
  75. public ulong IconId;
  76. [JsonProperty("Max messages in feed (Default: 5)")]
  77. public int MaxFeedMessages;
  78. [JsonProperty("Max player name length in feed (Default: 18)")]
  79. public int MaxPlayerNameLength;
  80. [JsonProperty("Feed message TTL in seconds (Default: 7)")]
  81. public int FeedMessageTtlSec;
  82. [JsonProperty("Allow kill messages in chat along with kill feed")]
  83. public bool EnableChatFeed;
  84. [JsonProperty("Log PvP Kill events")]
  85. public bool EnableLogging;
  86. [JsonProperty("Height ident (space between messages). Default: 0.0185")]
  87. public float HeightIdent;
  88. [JsonProperty("Feed Position - Anchor Max. (Default: 0.995 0.986")]
  89. public string AnchorMax;
  90. [JsonProperty("Feed Position - Anchor Min. (Default: 0.723 0.964")]
  91. public string AnchorMin;
  92. [JsonProperty("Font size of kill feed (Default: 12)")]
  93. public int FontSize;
  94. [JsonProperty("Outline Text Size (Default: 0.5 0.5)")]
  95. public string OutlineSize;
  96. [JsonProperty("Default color for distance (if too far from any from the list). Default: #FF8000")]
  97. public string DefaultDistanceColor;
  98. [JsonProperty("Distance Colors List (Certain color will apply if distance is <= than specified)")]
  99. public DistanceColor[] DistanceColors;
  100. [JsonProperty("Custom Entity Names, you can remove or add more!")]
  101. public Dictionary<string, string> Ents = new Dictionary<string, string>();
  102. [JsonProperty("Custom Animal Names, you can remove or add more!")]
  103. public Dictionary<string, string> Animal = new Dictionary<string, string>();
  104. [JsonProperty("Custom Weapon Names, you can add more!")]
  105. public Dictionary<string, string> Weapons = new Dictionary<string, string>();
  106. [JsonProperty("Custom Npc Names, you can add more!")]
  107. public Dictionary<string, string> Npcs = new Dictionary<string, string>();
  108.  
  109. [OnDeserialized]
  110. internal void OnDeserialized(StreamingContext ctx) => Array.Sort(DistanceColors, (o1, o2) => o1.DistanceThreshold.CompareTo(o2.DistanceThreshold));
  111.  
  112. public class DistanceColor
  113. {
  114. public int DistanceThreshold;
  115. public string Color;
  116. public bool TestDistance(int distance) => distance <= DistanceThreshold;
  117. }
  118. }
  119.  
  120. protected override void LoadConfig()
  121. {
  122. base.LoadConfig();
  123. _config = Config.ReadObject<SKFConfig>();
  124. }
  125.  
  126. protected override void LoadDefaultConfig()
  127. {
  128. _config = new SKFConfig
  129. {
  130. EnableEntityFeed = true,
  131. EnableAnimalFeed = true,
  132. EnableNpcFeed = true,
  133. EnableSuicides = true,
  134. EnableRadiationKills = true,
  135. IconId = 76561197960839785UL,
  136. MaxFeedMessages = 5,
  137. MaxPlayerNameLength = 18,
  138. FeedMessageTtlSec = 7,
  139. EnableChatFeed = true,
  140. EnableLogging = false,
  141. HeightIdent = 0.0185f,
  142. AnchorMax = "0.995 0.986",
  143. AnchorMin = "0.723 0.964",
  144. FontSize = 12,
  145. OutlineSize = "0.5 0.5",
  146. DefaultDistanceColor = "#FF8000",
  147. DistanceColors = new[]
  148. {
  149. new SKFConfig.DistanceColor
  150. {
  151. Color = "#FFFFFF",
  152. DistanceThreshold = 50
  153. },
  154. new SKFConfig.DistanceColor
  155. {
  156. Color = "#91D6FF",
  157. DistanceThreshold = 100
  158. },
  159. new SKFConfig.DistanceColor
  160. {
  161. Color = "#FFFF00",
  162. DistanceThreshold = 150
  163. }
  164. },
  165. Ents = new Dictionary<string, string>()
  166. {
  167. { "autoturret_deployed","Auto Turret" },
  168. { "flameturret.deployed","Flame Turret"},
  169. { "guntrap.deployed","Gun Trap"},
  170. { "landmine","Landmine"},
  171. { "beartrap","Bear Trap"},
  172. { "sam_site_turret_deployed","Sam Site Turret"},
  173. { "patrolhelicopter","Helicopter"},
  174. { "bradleyapc","Bradley APC"}
  175. },
  176. Animal = new Dictionary<string, string>()
  177. {
  178. { "bear","Bear" },
  179. { "polarbear","PolarBear" },
  180. { "wolf","Wolf" },
  181. { "stag","Stag"},
  182. { "boar","Boar" },
  183. { "chicken","Chicken" },
  184. { "horse","Horse"},
  185. { "simpleshark","Shark" }
  186. },
  187. Weapons = new Dictionary<string, string>()
  188. {
  189. { "Assault Rifle","Ak-47" },
  190. { "LR-300 Assault Rifle","LR-300" },
  191. { "L96 Rifle","L96" },
  192. { "Bolt Action Rifle","Bolt" },
  193. { "Semi-Automatic Rifle","Semi-AR" },
  194. { "Semi-Automatic Pistol","Semi-AP" },
  195. { "Spas-12 Shotgun","Spas-12" },
  196. { "M92 Pistol","M92" }
  197. },
  198. Npcs = new Dictionary<string, string>()
  199. {
  200. { "scientist","Scientist Npc" }
  201. }
  202. };
  203. }
  204.  
  205. protected override void SaveConfig() => Config.WriteObject(_config);
  206.  
  207. #endregion
  208.  
  209. #region Data (ProtoBuf)
  210.  
  211. [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
  212. private class SimpleKillFeedData
  213. {
  214. public HashSet<ulong> DisabledUsers = new HashSet<ulong>();
  215. }
  216.  
  217. private void LoadData()
  218. {
  219. _data = ProtoStorage.Load<SimpleKillFeedData>(nameof(SimpleKillFeed)) ?? new SimpleKillFeedData();
  220. }
  221.  
  222. private void SaveData()
  223. {
  224. if (_data == null) return;
  225. ProtoStorage.Save(_data, nameof(SimpleKillFeed));
  226. }
  227.  
  228. #endregion
  229.  
  230. #region ChatCommand
  231.  
  232. [ChatCommand("feed")]
  233. private void ToggleFeed(BasePlayer player)
  234. {
  235. if (!_data.DisabledUsers.Contains(player.userID))
  236. {
  237. _data.DisabledUsers.Add(player.userID);
  238. Player.Message(player, _("Disabled", player), null, _config.IconId);
  239. }
  240. else
  241. {
  242. _data.DisabledUsers.Remove(player.userID);
  243. Player.Message(player, _("Enabled", player), null, _config.IconId);
  244. }
  245. }
  246.  
  247. #endregion
  248.  
  249. #region Oxide Hooks
  250.  
  251. private void OnServerInitialized()
  252. {
  253. _isClansReborn = Clans != null && Clans.Author.Contains("k1lly0u");
  254. _isClansIo = Clans != null && Clans.Author.Contains("playrust.io / dcode");
  255. foreach (var blueprint in ItemManager.bpList.Where(bp => bp.targetItem.category == ItemCategory.Weapon || bp.targetItem.category == ItemCategory.Tool))
  256. {
  257. var md = blueprint.targetItem.GetComponent<ItemModEntity>();
  258. if (!md)
  259. continue;
  260. if (!_itemNameMapping.ContainsKey(md.entityPrefab.resourceID))
  261. _itemNameMapping.Add(md.entityPrefab.resourceID, blueprint.targetItem.displayName.english);
  262. }
  263. _holder = new GameObject("SKFHolder");
  264. UnityEngine.Object.DontDestroyOnLoad(_holder);
  265. _killQueue = _holder.AddComponent<KillQueue>();
  266. }
  267.  
  268. private void Init() => LoadData();
  269.  
  270. private void Unload()
  271. {
  272. _killQueue = null;
  273. UnityEngine.Object.Destroy(_holder);
  274. _holder = null;
  275. for (var i = 0; i < _config.MaxFeedMessages; i++)
  276. KillQueue.RemoveKillCui($"kf-{i}");
  277. _config = null;
  278. SaveData();
  279. _data = null;
  280. }
  281.  
  282. #endregion
  283.  
  284. #region Oxide Hooks (Deaths)
  285.  
  286. private void OnEntityDeath(BaseAnimalNPC victim, HitInfo hitInfo)
  287. {
  288. if (victim == null || victim is BasePlayer || !IsAnimal(victim)) return;
  289.  
  290. BasePlayer attacker = hitInfo.InitiatorPlayer;
  291.  
  292. if (attacker == null || attacker.IsNpc || IsAnimal(attacker)) return;
  293. if (!_config.EnableAnimalFeed) return;
  294.  
  295. string VictimName = _config.Animal[victim.ShortPrefabName];
  296. string AttackerName = SanitizeName(GetClan(attacker) + attacker.displayName);
  297. string WeaponName = GetCustomWeaponName(hitInfo);
  298. var distance = (int)Vector3.Distance(attacker.transform.position, victim.transform.position);
  299. _killQueue.OnDeath(attacker, null, string.Format(_("MsgFeedKillAnimalFromPlayer"), AttackerName, VictimName, WeaponName, GetDistanceColor(distance), distance));
  300. }
  301.  
  302. private void OnEntityDeath(SimpleShark victim, HitInfo hitInfo)
  303. {
  304. if (victim == null || victim is BasePlayer || !IsAnimal(victim)) return;
  305.  
  306. BasePlayer attacker = hitInfo.InitiatorPlayer;
  307.  
  308. if (attacker == null || attacker.IsNpc || IsAnimal(attacker)) return;
  309. if (!_config.EnableAnimalFeed) return;
  310.  
  311. string VictimName = _config.Animal[victim.ShortPrefabName];
  312. string AttackerName = SanitizeName(GetClan(attacker) + attacker.displayName);
  313. string WeaponName = GetCustomWeaponName(hitInfo);
  314. var distance = (int)Vector3.Distance(attacker.transform.position, victim.transform.position);
  315. _killQueue.OnDeath(attacker, null, string.Format(_("MsgFeedKillAnimalFromPlayer"), AttackerName, VictimName, WeaponName, GetDistanceColor(distance), distance));
  316. }
  317.  
  318. private void OnPlayerDeath(BasePlayer victim, HitInfo hitInfo)
  319. {
  320. if (victim == null) return;
  321. try
  322. {
  323. if (hitInfo.damageTypes.GetMajorityDamageType() == DamageType.Fall) { DeathByFall(victim); return; }
  324.  
  325. if (hitInfo == null)
  326. {
  327. var wAttacker = victim.lastAttacker?.ToPlayer();
  328. if (wAttacker != null && victim.IsWounded())
  329. {
  330. OnDeathFromWounds(wAttacker, victim);
  331. }
  332. return;
  333. }
  334.  
  335. if (victim.lastAttacker?.ToPlayer() == victim)
  336. {
  337. if (!_config.EnableSuicides) return;
  338. OnSuicide(victim); return;
  339. return;
  340. }
  341.  
  342. if (hitInfo.Initiator is HotAirBalloon) return;
  343.  
  344. if (IsTrap(hitInfo.Initiator))
  345. {
  346. if (!_config.EnableEntityFeed) return;
  347. OnKilledByEnt(hitInfo.Initiator, victim); return;
  348. }
  349.  
  350. if (hitInfo.Initiator is BaseAnimalNPC && IsAnimal(hitInfo.Initiator))
  351. {
  352. if (!_config.EnableAnimalFeed) return;
  353. OnKilledByAnimal(hitInfo.Initiator, victim); return;
  354. }
  355.  
  356. if (hitInfo.WeaponPrefab.prefabID == 3032863244)//Cannon Shell prefab id
  357. {
  358. if (!_config.EnableEntityFeed) return;
  359. OnKilledByBradley(victim); return;
  360. }
  361.  
  362. if (IsRadiation(hitInfo))
  363. {
  364. if (!_config.EnableRadiationKills) return;
  365. OnKilledByRadiation(victim); return;
  366. }
  367.  
  368. var distance = !hitInfo.IsProjectile() ? (int)Vector3.Distance(hitInfo.PointStart, hitInfo.HitPositionWorld) : (int)hitInfo.ProjectileDistance;
  369.  
  370. var attacker = hitInfo.InitiatorPlayer;
  371.  
  372. if (attacker == null) return;
  373.  
  374. else if (IsFlame(hitInfo))
  375. {
  376. OnBurned(attacker, victim);
  377. }
  378.  
  379. if (victim.IsNpc && IsExplosion(hitInfo)) return;
  380. else
  381. {
  382. OnKilled(attacker, victim, hitInfo, distance);
  383. }
  384. }
  385. catch {}
  386. }
  387.  
  388. #endregion
  389.  
  390. #region LanguageAPI
  391.  
  392. protected override void LoadDefaultMessages() => lang.RegisterMessages(new Dictionary<string, string>
  393. {
  394. {"MsgAttacker", "You killed <color=#ff686b>{0}</color> from {1}m in <color=#ff686b>{2}</color>."},
  395. {"MsgVictim", "<color=#ff686b>{0}</color> killed you from {1}m with their {2} to <color=#ff686b>{3}</color>."},
  396. {"MsgFeedKill", "<color=#00ff00>{0}</color> killed <color=#ff686b>{1}</color>, <color=#ff686b>{2}</color>, <color=#ff686b>{3}</color><color={4}>({5}m)</color>"},
  397. {"MsgFeedKillNpc", "<color=#00ff00>{0}</color> killed <color=#ff686b>{1}</color>, <color={2}>({3}m)</color>"},
  398. {"MsgFeedKillAnimalFromPlayer", "<color=#00ff00>{0}</color> killed a <color=#ff686b>{1}</color>, <color=#ff686b>{2}</color>, <color={3}>({4}m)</color>"},
  399.  
  400. {"MsgAtkWounded", "You wounded <color=#ff686b>{0}</color> till death."},
  401. {"MsgVictimWounded", "<color=#ff686b>{0}</color> has wounded you till death."},
  402. {"MsgFeedWounded", "<color=#00ff00>{0}</color> finished <color=#ff686b>{1}</color>"},
  403.  
  404. {"MsgAtkBurned", "You burned <color=#ff686b>{0}</color> alive!"},
  405. {"MsgVictimBurned", "<color=#ff686b>{0}</color> has burned you alive!"},
  406. {"MsgFeedBurned", "<color=#00ff00>{0}</color> burned <color=#ff686b>{1}</color>!"},
  407.  
  408. {"MsgFeedKillBrad", "<color=#ff686b>{0}</color> was killed by a <color=orange>Shell</color>"},
  409. {"MsgFeedKillEnt", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
  410. {"MsgFeedKillAnimal", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
  411.  
  412. {"MsgFeedKillSuicide", "<color=#ff686b>{0}</color> committed <color=orange>Suicide</color>"},
  413. {"MsgFeedKillRadiation", "<color=#ff686b>{0}</color> died to <color=orange>Radiation</color>"},
  414. {"MsgFeedKillFall", "<color=#ff686b>{0}</color> died by <color=orange>Fall</color>"},
  415.  
  416. {"Enabled", "KillFeed Enabled"},
  417. {"Disabled", "KillFeed Disabled"}
  418. }, this);
  419.  
  420. #endregion
  421.  
  422. #region Kill Events
  423.  
  424. private void OnKilled(BasePlayer attacker, BasePlayer victim, HitInfo hitInfo, int dist)
  425. {
  426. var HitBone = hitInfo.boneArea.ToString();
  427. if (HitBone == "-1") HitBone = "Body";
  428.  
  429. if (attacker.IsNpc)
  430. {
  431. if (!_config.EnableNpcFeed) return;
  432. var npc = attacker;
  433. _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillNpc"), CustomNpcName(npc), SanitizeName(GetClan(victim) + victim.displayName), GetDistanceColor(dist), dist));
  434. return;
  435. }
  436.  
  437. if (victim.IsNpc)
  438. {
  439. if (!_config.EnableNpcFeed) return;
  440. var npc = victim;
  441. _killQueue.OnDeath(attacker, null, string.Format(_("MsgFeedKill"), SanitizeName(GetClan(attacker) + attacker.displayName), CustomNpcName(npc), GetCustomWeaponName(hitInfo), HitBone, GetDistanceColor(dist), dist));
  442. return;
  443. }
  444.  
  445. if (_config.EnableChatFeed)
  446. {
  447. if (!_data.DisabledUsers.Contains(attacker.userID))
  448. Player.Message(attacker, _("MsgAttacker", attacker), null, _config.IconId, GetClan(victim) + victim.displayName, dist, HitBone);
  449. if (!_data.DisabledUsers.Contains(victim.userID))
  450. Player.Message(victim, _("MsgVictim", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName, dist, GetCustomWeaponName(hitInfo), HitBone);
  451. }
  452. _killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedKill"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName), GetCustomWeaponName(hitInfo), HitBone, GetDistanceColor(dist), dist));
  453.  
  454. if (!_config.EnableLogging) return;
  455. var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} killed ({victim.UserIDString}){victim.displayName} from {dist}m in {HitBone}");
  456. LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
  457. }
  458.  
  459. private void OnSuicide(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillSuicide"), SanitizeName(GetClan(victim) + victim.displayName)));
  460.  
  461. private void OnKilledByRadiation(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillRadiation"), SanitizeName(GetClan(victim) + victim.displayName)));
  462.  
  463. private void OnKilledByEnt(BaseEntity attacker, BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillEnt"), SanitizeName(GetClan(victim) + victim.displayName), CustomEntName(attacker)));
  464.  
  465. private void OnKilledByBradley(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillBrad"), SanitizeName(GetClan(victim) + victim.displayName)));
  466.  
  467. private void OnKilledByAnimal(BaseEntity attacker, BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillAnimal"), SanitizeName(GetClan(victim) + victim.displayName), CustomAnimalName(attacker)));
  468.  
  469. private void DeathByFall(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillFall"), SanitizeName(GetClan(victim) + victim.displayName)));
  470.  
  471. private void OnDeathFromWounds(BasePlayer attacker, BasePlayer victim)
  472. {
  473. if (_config.EnableChatFeed)
  474. {
  475. if (!_data.DisabledUsers.Contains(attacker.userID) && attacker.userID.IsSteamId())
  476. Player.Message(attacker, _("MsgAtkWounded", attacker), null, _config.IconId, GetClan(victim) + victim.displayName);
  477. if (!_data.DisabledUsers.Contains(victim.userID))
  478. Player.Message(victim, _("MsgVictimWounded", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName);
  479. }
  480. _killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedWounded"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName)));
  481.  
  482. if (!_config.EnableLogging) return;
  483. var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} finished ({victim.UserIDString}){victim.displayName}");
  484. LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
  485. }
  486.  
  487. private void OnBurned(BasePlayer attacker, BasePlayer victim)
  488. {
  489. if (victim.IsNpc)
  490. {
  491. if (!_config.EnableNpcFeed) return;
  492. var npc = victim;
  493. _killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), SanitizeName(GetClan(attacker) + attacker.displayName), CustomNpcName(npc)));
  494. return;
  495. }
  496.  
  497. if (attacker.IsNpc)
  498. {
  499. if (!_config.EnableNpcFeed) return;
  500. var npc = attacker;
  501. _killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), CustomNpcName(npc), SanitizeName(GetClan(victim) + victim.displayName)));
  502. return;
  503. }
  504. if (_config.EnableChatFeed)
  505. {
  506. if (!_data.DisabledUsers.Contains(attacker.userID))
  507. Player.Message(attacker, _("MsgAtkBurned", attacker), null, _config.IconId, GetClan(victim) + victim.displayName);
  508. if (!_data.DisabledUsers.Contains(victim.userID))
  509. Player.Message(victim, _("MsgVictimBurned", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName);
  510. }
  511. _killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName)));
  512.  
  513. if (!_config.EnableLogging) return;
  514. var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} burned ({victim.UserIDString}){victim.displayName}");
  515. LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
  516. }
  517.  
  518. #endregion
  519.  
  520. #region UI
  521.  
  522. private class KillEvent : Pool.IPooled
  523. {
  524. public int DisplayUntil;
  525. public string Text;
  526.  
  527. public KillEvent Init(string text, int displayUntil)
  528. {
  529. Text = text;
  530. DisplayUntil = displayUntil;
  531. return this;
  532. }
  533.  
  534. public void EnterPool()
  535. {
  536. Text = null;
  537. DisplayUntil = 0;
  538. }
  539.  
  540. public void LeavePool() { }
  541. }
  542.  
  543. private class KillQueue : MonoBehaviour
  544. {
  545. private readonly WaitForSeconds _secondDelay = new WaitForSeconds(1f);
  546. private readonly Queue<KillEvent> _queue = new Queue<KillEvent>(_config.MaxFeedMessages);
  547. private readonly CuiOutlineComponent _outlineStatic = new CuiOutlineComponent { Distance = _config.OutlineSize, Color = "0 0 0 1" };
  548. private readonly CuiRectTransformComponent[] _rectTransformStatic = new CuiRectTransformComponent[_config.MaxFeedMessages];
  549. private readonly CuiTextComponent[] _textStatic = new CuiTextComponent[_config.MaxFeedMessages];
  550. private readonly CuiElementContainer _cui = new CuiElementContainer();
  551. private bool _needsRedraw;
  552. private int _currentlyDrawn;
  553.  
  554. public void OnDeath(BasePlayer victim, BasePlayer attacker, string text)
  555. {
  556. if (_queue.Count == _config.MaxFeedMessages)
  557. DequeueEvent(_queue.Dequeue());
  558. PushEvent(Pool.Get<KillEvent>().Init(text, Epoch.Current + _config.FeedMessageTtlSec));
  559. }
  560.  
  561. private void PushEvent(KillEvent evt)
  562. {
  563. _queue.Enqueue(evt);
  564. _needsRedraw = true;
  565. DoProccessQueue();
  566. }
  567.  
  568. private void Start()
  569. {
  570. for (var i = 0; i < _config.MaxFeedMessages; i++)
  571. {
  572. _rectTransformStatic[i] = new CuiRectTransformComponent
  573. {
  574. AnchorMax =
  575. $"{_config.AnchorMax.Split(Convert.ToChar(' '))[0]} {float.Parse(_config.AnchorMax.Split(Convert.ToChar(' '))[1]) - (_config.HeightIdent * i)}",
  576. AnchorMin =
  577. $"{_config.AnchorMin.Split(Convert.ToChar(' '))[0]} {float.Parse(_config.AnchorMin.Split(Convert.ToChar(' '))[1]) - (_config.HeightIdent * i)}"
  578. };
  579. _textStatic[i] = new CuiTextComponent { Align = TextAnchor.MiddleRight, FontSize = _config.FontSize, Text = string.Empty };
  580. }
  581. StartCoroutine(ProccessQueue());
  582. }
  583.  
  584. private void DequeueEvent(KillEvent evt)
  585. {
  586. Pool.Free(ref evt);
  587. _needsRedraw = true;
  588. }
  589.  
  590. private void DoProccessQueue()
  591. {
  592. while (_queue.Count > 0 && _queue.Peek().DisplayUntil < Epoch.Current)
  593. DequeueEvent(_queue.Dequeue());
  594.  
  595. if (!_needsRedraw)
  596. return;
  597. var toBeRemoved = _currentlyDrawn;
  598. _currentlyDrawn = 0;
  599. foreach (var killEvent in _queue)
  600. {
  601. var cuiText = _textStatic[_currentlyDrawn];
  602. cuiText.Text = killEvent.Text;
  603. _cui.Add(new CuiElement
  604. {
  605. Name = $"kf-{_currentlyDrawn}",
  606. Parent = "Hud",
  607. Components =
  608. {
  609. cuiText,
  610. _rectTransformStatic[_currentlyDrawn],
  611. _outlineStatic
  612. }
  613. });
  614. if (++_currentlyDrawn == _config.MaxFeedMessages)
  615. break;
  616. }
  617. _needsRedraw = false;
  618. SendKillCui(_cui, toBeRemoved);
  619. _cui.Clear();
  620. }
  621.  
  622. private IEnumerator ProccessQueue()
  623. {
  624. while (!Interface.Oxide.IsShuttingDown)
  625. {
  626. DoProccessQueue();
  627. yield return _secondDelay;
  628. }
  629. }
  630.  
  631. private static void SendKillCui(CuiElementContainer cui, int toBeRemoved)
  632. {
  633. var json = cui.ToJson();
  634. foreach (var plr in BasePlayer.activePlayerList)
  635. {
  636. for (var i = toBeRemoved; i > 0; i--)
  637. CuiHelper.DestroyUi(plr, $"kf-{i - 1}");
  638. if (!_data.DisabledUsers.Contains(plr.userID))
  639. CuiHelper.AddUi(plr, json);
  640. }
  641. }
  642.  
  643. public static void RemoveKillCui(string name)
  644. {
  645. foreach (var plr in BasePlayer.activePlayerList)
  646. CuiHelper.DestroyUi(plr, name);
  647. }
  648. }
  649.  
  650. #endregion
  651.  
  652. #region Helpers
  653.  
  654. private string _(string msg, BasePlayer player = null) => lang.GetMessage(msg, this, player?.UserIDString);
  655.  
  656. private string GetCustomWeaponName(HitInfo hitInfo)
  657. {
  658. var name = GetWeaponName(hitInfo);
  659. if (string.IsNullOrEmpty(name))
  660. return null;
  661.  
  662. string translatedName;
  663. if (_config.Weapons.TryGetValue(name, out translatedName))
  664. return translatedName;
  665.  
  666. _config.Weapons.Add(name, name);
  667. Config.WriteObject(_config);
  668. return name;
  669. }
  670.  
  671. private string CustomNpcName(BasePlayer npc)
  672. {
  673. var name = npc.ShortPrefabName;
  674. if (string.IsNullOrEmpty(name))
  675. return null;
  676. if (npc.displayName != npc.userID.ToString())
  677. return npc.displayName;
  678. string translatedName;
  679. if (_config.Npcs.TryGetValue(name, out translatedName))
  680. return translatedName;
  681.  
  682. _config.Npcs.Add(name, name);
  683. Config.WriteObject(_config);
  684. return name;
  685. }
  686.  
  687. private string CustomEntName(BaseEntity attacker)
  688. {
  689. var name = attacker.ShortPrefabName;
  690. if (string.IsNullOrEmpty(name))
  691. return null;
  692. string translatedName;
  693. if (_config.Ents.TryGetValue(name, out translatedName))
  694. return translatedName;
  695.  
  696. _config.Npcs.Add(name, name);
  697. Config.WriteObject(_config);
  698. return name;
  699. }
  700.  
  701. private string CustomAnimalName(BaseEntity attacker)
  702. {
  703. var name = attacker.ShortPrefabName;
  704. if (string.IsNullOrEmpty(name))
  705. return null;
  706. string translatedName;
  707. if (_config.Animal.TryGetValue(name, out translatedName))
  708. return translatedName;
  709.  
  710. _config.Npcs.Add(name, name);
  711. Config.WriteObject(_config);
  712. return name;
  713. }
  714.  
  715. private string GetWeaponName(HitInfo hitInfo)
  716. {
  717. var _WeaponName = "??Unknown??";
  718. if (hitInfo.Weapon == null)
  719. {
  720. if (hitInfo.WeaponPrefab.prefabID == 3898309212) _WeaponName = "C4";//3898309212
  721. if (hitInfo.WeaponPrefab.prefabID == 3046924118) _WeaponName = "Rocket";//3046924118
  722. if (hitInfo.WeaponPrefab.prefabID == 1217937936) _WeaponName = "HV Rocket";//1217937936
  723. if (hitInfo.WeaponPrefab.prefabID == 2742759844) _WeaponName = "Satchel";//1217937936
  724. if (hitInfo.WeaponPrefab.prefabID == 2144399804) _WeaponName = "Beancan";//2144399804
  725. if (hitInfo.WeaponPrefab.prefabID == 1859672190) _WeaponName = "Shell";//1217937936
  726. if (hitInfo.WeaponPrefab.prefabID == 1128089209) _WeaponName = "Grenade";//1128089209
  727. if (hitInfo.WeaponPrefab.prefabID == 3717106868) _WeaponName = "Flamethrower";//3717106868
  728. if (hitInfo.WeaponPrefab.prefabID == 3032863244) _WeaponName = "Cannon Shell";//3032863244
  729.  
  730. if (hitInfo.Initiator is GunTrap) _WeaponName = "GunTrap";
  731. if (hitInfo.Initiator is FlameTurret) _WeaponName = "FlameTurret";
  732. if (hitInfo.Initiator is AutoTurret) _WeaponName = "AutoTurret";
  733. else if (hitInfo.damageTypes.GetMajorityDamageType() == DamageType.Blunt) _WeaponName = "Blunt";
  734. else if (hitInfo.damageTypes.GetMajorityDamageType() == DamageType.Heat) _WeaponName = "Fire";
  735. else if (hitInfo.damageTypes.GetMajorityDamageType() == DamageType.Fun_Water) _WeaponName = "WaterGun";
  736. //Puts(hitInfo.WeaponPrefab.prefabID.ToString());
  737. return _WeaponName;
  738. }
  739.  
  740. if (hitInfo.Weapon != null)
  741. {
  742. var _Weapon = hitInfo.Weapon;
  743. var item = _Weapon.GetItem();
  744. if (item != null)
  745. _WeaponName = item.info.displayName.english;
  746. if (hitInfo.WeaponPrefab.prefabID == 1233562048) _WeaponName = "Grenade Launcher";
  747. }
  748. if (hitInfo.Initiator is GunTrap) _WeaponName = "GunTrap";
  749. if (hitInfo.Initiator is FlameTurret) _WeaponName = "FlameTurret";
  750. if (hitInfo.Initiator is AutoTurret) _WeaponName = "AutoTurret";
  751. if (hitInfo.Initiator is HotAirBalloon) _WeaponName = "Hot Air Balloon";
  752. return _WeaponName;
  753. }
  754.  
  755. private static bool IsExplosion(HitInfo hit) => (hit.WeaponPrefab != null && (hit.WeaponPrefab.ShortPrefabName.Contains("grenade") || hit.WeaponPrefab.ShortPrefabName.Contains("explosive")))
  756. || hit.damageTypes.GetMajorityDamageType() == DamageType.Explosion || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Explosion));
  757.  
  758. private static bool IsFlame(HitInfo hit) => hit.damageTypes.GetMajorityDamageType() == DamageType.Heat || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Heat));
  759.  
  760. private static bool IsRadiation(HitInfo hit) => hit.damageTypes.GetMajorityDamageType() == DamageType.Radiation || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Radiation));
  761.  
  762. private static bool IsTrap(BaseEntity ent) => ent != null && _config.Ents.ContainsKey(ent.ShortPrefabName);
  763.  
  764. private static bool IsAnimal(BaseEntity animal) => animal?.ShortPrefabName != null && _config.Animal.ContainsKey(animal.ShortPrefabName);
  765.  
  766. private static string GetDistanceColor(int dist)
  767. {
  768. foreach (var distanceColor in _config.DistanceColors)
  769. {
  770. if (distanceColor.TestDistance(dist))
  771. return distanceColor.Color;
  772. }
  773. return _config.DefaultDistanceColor ?? "white";
  774. }
  775.  
  776. private string GetClan(BasePlayer player)
  777. {
  778. if (_isClansReborn || _isClansIo || Clans == null) return null;
  779. var clan = (string)Clans.Call("GetClanOf", player.UserIDString);
  780. if (clan == null) return null;
  781. var format = string.Format("[" + clan + "] ");
  782. return format;
  783. }
  784.  
  785. private static string SanitizeName(string name)
  786. {
  787. if (name.Length > _config.MaxPlayerNameLength)
  788. name = name.Substring(0, _config.MaxPlayerNameLength).Trim();
  789. return name.Replace("\"", "''");
  790. }
  791. #endregion
  792. }
  793. }
  794.  
Advertisement
Add Comment
Please, Sign In to add comment