-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleKillFeed.cs
759 lines (652 loc) · 32.6 KB
/
SimpleKillFeed.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Facepunch;
using Facepunch.Math;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
using ProtoBuf;
namespace Oxide.Plugins
{
[Info("Simple Kill Feed", "CosaNosatra", "2.0.1")]
[Description("A simple kill feed, that displays in the top right corner various kill events.")]
public class SimpleKillFeed : RustPlugin
{
#region Fields
[PluginReference] private Plugin Clans;
private bool _isClansReborn;
private bool _isClansIo;
private readonly Dictionary<uint, string> _itemNameMapping = new Dictionary<uint, string>();
private GameObject _holder;
private KillQueue _killQueue;
private static SKFConfig _config;
private static SimpleKillFeedData _data;
#endregion
#region Config
private class SKFConfig
{
[JsonProperty("Show Traps and Entitys in Kill Feed")]
public bool EnableEntityFeed;
[JsonProperty("Show Animals in Kill Feed")]
public bool EnableAnimalFeed;
[JsonProperty("Show Npcs in Kill Feed")]
public bool EnableNpcFeed;
[JsonProperty("Show suicides in KillFeed (Default: true)")]
public bool EnableSuicides;
[JsonProperty("Show radiation kills in KillFeed (Default: true)")]
public bool EnableRadiationKills;
[JsonProperty("Chat Icon Id (Steam profile ID)")]
public ulong IconId;
[JsonProperty("Max messages in feed (Default: 5)")]
public int MaxFeedMessages;
[JsonProperty("Max player name length in feed (Default: 18)")]
public int MaxPlayerNameLength;
[JsonProperty("Feed message TTL in seconds (Default: 7)")]
public int FeedMessageTtlSec;
[JsonProperty("Allow kill messages in chat along with kill feed")]
public bool EnableChatFeed;
[JsonProperty("Log PvP Kill events")]
public bool EnableLogging;
[JsonProperty("Height ident (space between messages). Default: 0.0185")]
public float HeightIdent;
[JsonProperty("Feed Position - Anchor Max. (Default: 0.995 0.986")]
public string AnchorMax;
[JsonProperty("Feed Position - Anchor Min. (Default: 0.723 0.964")]
public string AnchorMin;
[JsonProperty("Font size of kill feed (Default: 12)")]
public int FontSize;
[JsonProperty("Outline Text Size (Default: 0.5 0.5)")]
public string OutlineSize;
[JsonProperty("Default color for distance (if too far from any from the list). Default: #FF8000")]
public string DefaultDistanceColor;
[JsonProperty("Distance Colors List (Certain color will apply if distance is <= than specified)")]
public DistanceColor[] DistanceColors;
[JsonProperty("Custom Entity Names, you can remove or add more!")]
public Dictionary<string, string> Ents = new Dictionary<string, string>();
[JsonProperty("Custom Animal Names, you can remove or add more!")]
public Dictionary<string, string> Animal = new Dictionary<string, string>();
[JsonProperty("Custom Weapon Names, you can add more!")]
public Dictionary<string, string> Weapons = new Dictionary<string, string>();
[JsonProperty("Custom Npc Names, you can add more!")]
public Dictionary<string, string> Npcs = new Dictionary<string, string>();
[OnDeserialized]
internal void OnDeserialized(StreamingContext ctx) => Array.Sort(DistanceColors, (o1, o2) => o1.DistanceThreshold.CompareTo(o2.DistanceThreshold));
public class DistanceColor
{
public int DistanceThreshold;
public string Color;
public bool TestDistance(int distance) => distance <= DistanceThreshold;
}
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<SKFConfig>();
}
protected override void LoadDefaultConfig()
{
_config = new SKFConfig
{
EnableEntityFeed = true,
EnableAnimalFeed = true,
EnableNpcFeed = true,
EnableSuicides = true,
EnableRadiationKills = true,
IconId = 76561197960839785UL,
MaxFeedMessages = 5,
MaxPlayerNameLength = 18,
FeedMessageTtlSec = 7,
EnableChatFeed = true,
EnableLogging = false,
HeightIdent = 0.0185f,
AnchorMax = "0.995 0.986",
AnchorMin = "0.723 0.964",
FontSize = 12,
OutlineSize = "0.5 0.5",
DefaultDistanceColor = "#FF8000",
DistanceColors = new[]
{
new SKFConfig.DistanceColor
{
Color = "#FFFFFF",
DistanceThreshold = 50
},
new SKFConfig.DistanceColor
{
Color = "#91D6FF",
DistanceThreshold = 100
},
new SKFConfig.DistanceColor
{
Color = "#FFFF00",
DistanceThreshold = 150
}
},
Ents = new Dictionary<string, string>()
{
{ "autoturret_deployed","Auto Turret" },
{ "flameturret.deployed","Flame Turret"},
{ "guntrap.deployed","Gun Trap"},
{ "landmine","Landmine"},
{ "beartrap","Bear Trap"},
{ "sam_site_turret_deployed","Sam Site Turret"},
{ "patrolhelicopter","Helicopter"},
{ "bradleyapc","Bradley APC"}
},
Animal = new Dictionary<string, string>()
{
{ "bear","Bear" },
{ "wolf","Wolf" },
{ "stag","Stag"},
{ "boar","Boar" },
{ "chicken","Chicken" },
{ "horse","Horse"}
},
Weapons = new Dictionary<string, string>()
{
{ "Assault Rifle","Ak-47" },
{ "LR-300 Assault Rifle","LR-300" },
{ "L96 Rifle","L96" },
{ "Bolt Action Rifle","Bolt" },
{ "Semi-Automatic Rifle","Semi-AR" },
{ "Semi-Automatic Pistol","Semi-AP" },
{ "Spas-12 Shotgun","Spas-12" },
{ "M92 Pistol","M92" }
},
Npcs = new Dictionary<string, string>()
{
{ "scientist","Scientist Npc" }
}
};
}
protected override void SaveConfig() => Config.WriteObject(_config);
#endregion
#region Data (ProtoBuf)
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
private class SimpleKillFeedData
{
public HashSet<ulong> DisabledUsers = new HashSet<ulong>();
}
private void LoadData()
{
_data = ProtoStorage.Load<SimpleKillFeedData>(nameof(SimpleKillFeed)) ?? new SimpleKillFeedData();
}
private void SaveData()
{
if (_data == null) return;
ProtoStorage.Save(_data, nameof(SimpleKillFeed));
}
#endregion
#region ChatCommand
[ChatCommand("feed")]
private void ToggleFeed(BasePlayer player)
{
if (!_data.DisabledUsers.Contains(player.userID))
{
_data.DisabledUsers.Add(player.userID);
Player.Message(player, _("Disabled", player), null, _config.IconId);
}
else
{
_data.DisabledUsers.Remove(player.userID);
Player.Message(player, _("Enabled", player), null, _config.IconId);
}
}
#endregion
#region Hooks
private void OnServerInitialized()
{
_isClansReborn = Clans != null && Clans.Author.Contains("FuJiCuRa");
_isClansIo = Clans != null && Clans.Author.Contains("playrust.io / dcode");
foreach (var blueprint in ItemManager.bpList.Where(bp => bp.targetItem.category == ItemCategory.Weapon || bp.targetItem.category == ItemCategory.Tool))
{
var md = blueprint.targetItem.GetComponent<ItemModEntity>();
if (!md)
continue;
if (!_itemNameMapping.ContainsKey(md.entityPrefab.resourceID))
_itemNameMapping.Add(md.entityPrefab.resourceID, blueprint.targetItem.displayName.english);
}
_holder = new GameObject("SKFHolder");
UnityEngine.Object.DontDestroyOnLoad(_holder);
_killQueue = _holder.AddComponent<KillQueue>();
Pool.FillBuffer<KillEvent>(_config.MaxFeedMessages);
}
private void Init() => LoadData();
private void Unload()
{
_killQueue = null;
UnityEngine.Object.Destroy(_holder);
_holder = null;
for (var i = 0; i < _config.MaxFeedMessages; i++)
KillQueue.RemoveKillCui($"kf-{i}");
_config = null;
Pool.directory.Remove(typeof(KillEvent));
SaveData();
_data = null;
}
private void OnPlayerDeath(BasePlayer victim, HitInfo hitInfo)
{
if (victim == null) return;
if (hitInfo == null)
{
var wAttacker = victim.lastAttacker?.ToPlayer();
if (wAttacker != null && victim.IsWounded())
{
OnDeathFromWounds(wAttacker, victim);
}
return;
}
if (IsTrap(hitInfo.Initiator))
{
if (!_config.EnableEntityFeed) return;
OnKilledByEnt(hitInfo.Initiator, victim); return;
}
if (IsAnimal(hitInfo.Initiator))
{
if (!_config.EnableAnimalFeed) return;
OnKilledByAnimal(hitInfo.Initiator, victim); return;
}
if (IsRadiation(hitInfo))
{
if (!_config.EnableRadiationKills) return;
OnKilledByRadiation(victim); return;
}
var distance = !hitInfo.IsProjectile() ? (int)Vector3.Distance(hitInfo.PointStart, hitInfo.HitPositionWorld) : (int)hitInfo.ProjectileDistance;
var attacker = hitInfo.InitiatorPlayer;
if (attacker == null) return;
if (attacker == victim)
{
if (!_config.EnableSuicides)
return;
OnSuicide(victim); return;
}
if (IsExplosion(hitInfo))
{
OnExploded(attacker, victim);
}
else if (IsFlame(hitInfo))
{
OnBurned(attacker, victim);
}
else
{
OnKilled(attacker, victim, hitInfo, distance);
}
}
#endregion
#region Oxide Lang
protected override void LoadDefaultMessages() {
lang.RegisterMessages(new Dictionary<string, string>
{
{"MsgAttacker", "You killed <color=#ff686b>{0}</color> from {1}m in <color=#ff686b>{2}</color>."},
{"MsgVictim", "<color=#ff686b>{0}</color> killed you from {1}m with their {2} to <color=#ff686b>{3}</color>."},
{"MsgFeedKill", "<color=#00ff00>{0}</color> killed <color=#ff686b>{1}</color>, <color=#ff686b>{2}</color>, <color=#ff686b>{3}</color><color={4}>({5}m)</color>"},
{"MsgFeedKillNpc", "<color=#00ff00>{0}</color> killed <color=#ff686b>{1}</color>, <color={2}>({3}m)</color>"},
{"MsgAtkWounded", "You wounded <color=#ff686b>{0}</color> till death."},
{"MsgVictimWounded", "<color=#ff686b>{0}</color> has wounded you till death."},
{"MsgFeedWounded", "<color=#00ff00>{0}</color> finished <color=#ff686b>{1}</color>"},
{"MsgAttackerBlown", "You blown <color=#ff686b>{0}</color>!"},
{"MsgVictimBlown", "<color=#ff686b>{0}</color> has blown you!"},
{"MsgFeedBlown", "<color=#00ff00>{0}</color> blown up <color=#ff686b>{1}</color>!"},
{"MsgAtkBurned", "You burned <color=#ff686b>{0}</color> alive!"},
{"MsgVictimBurned", "<color=#ff686b>{0}</color> has burned you alive!"},
{"MsgFeedBurned", "<color=#00ff00>{0}</color> burned <color=#ff686b>{1}</color>!"},
{"MsgFeedKillEnt", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
{"MsgFeedKillAnimal", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
{"MsgFeedKillSuicide", "<color=#ff686b>{0}</color> committed <color=orange>Suicide</color>"},
{"MsgFeedKillRadiation", "<color=#ff686b>{0}</color> died to <color=orange>Radiation</color>"},
{"Enabled", "KillFeed Enabled"},
{"Disabled", "KillFeed Disabled"}
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
{"MsgAttacker", "You killed <color=#ff686b>{0}</color> from {1}m in <color=#ff686b>{2}</color>."},
{"MsgVictim", "<color=#ff686b>{0}</color> killed you from {1}m with their {2} to <color=#ff686b>{3}</color>."},
{"MsgFeedKill", "<color=#00ff00>{0}</color> убил <color=#ff686b>{1}</color>, <color=#ff686b>{2}</color>, <color=#ff686b>{3}</color><color={4}>({5}m)</color>"},
{"MsgFeedKillNpc", "<color=#00ff00>{0}</color> killed <color=#ff686b>{1}</color>, <color={2}>({3}m)</color>"},
{"MsgAtkWounded", "You wounded <color=#ff686b>{0}</color> till death."},
{"MsgVictimWounded", "<color=#ff686b>{0}</color> has wounded you till death."},
{"MsgFeedWounded", "<color=#00ff00>{0}</color> finished <color=#ff686b>{1}</color>"},
{"MsgAttackerBlown", "You blown <color=#ff686b>{0}</color>!"},
{"MsgVictimBlown", "<color=#ff686b>{0}</color> has blown you!"},
{"MsgFeedBlown", "<color=#00ff00>{0}</color> blown up <color=#ff686b>{1}</color>!"},
{"MsgAtkBurned", "You burned <color=#ff686b>{0}</color> alive!"},
{"MsgVictimBurned", "<color=#ff686b>{0}</color> has burned you alive!"},
{"MsgFeedBurned", "<color=#00ff00>{0}</color> burned <color=#ff686b>{1}</color>!"},
{"MsgFeedKillEnt", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
{"MsgFeedKillAnimal", "<color=#ff686b>{0}</color> was killed by <color=orange>{1}</color>"},
{"MsgFeedKillSuicide", "<color=#ff686b>{0}</color> committed <color=orange>Suicide</color>"},
{"MsgFeedKillRadiation", "<color=#ff686b>{0}</color> died to <color=orange>Radiation</color>"},
{"Enabled", "KillFeed Enabled"},
{"Disabled", "KillFeed Disabled"}
}, this, "ru");
}
#endregion
#region Kill Events
private void OnKilled(BasePlayer attacker, BasePlayer victim, HitInfo hitInfo, int dist)
{
if (attacker.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = attacker;
_killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillNpc"), CustomNpcName(npc), SanitizeName(GetClan(victim) + victim.displayName), GetDistanceColor(dist), dist));
return;
}
if (victim.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = victim;
_killQueue.OnDeath(attacker, null, string.Format(_("MsgFeedKill"), SanitizeName(GetClan(attacker) + attacker.displayName), CustomNpcName(npc), GetCustomWeaponName(hitInfo), hitInfo.boneArea, GetDistanceColor(dist), dist));
return;
}
if (_config.EnableChatFeed)
{
if (!_data.DisabledUsers.Contains(attacker.userID))
Player.Message(attacker, _("MsgAttacker", attacker), null, _config.IconId, GetClan(victim) + victim.displayName, dist, hitInfo.boneArea);
if (!_data.DisabledUsers.Contains(victim.userID))
Player.Message(victim, _("MsgVictim", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName, dist, GetCustomWeaponName(hitInfo), hitInfo.boneArea);
}
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedKill"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName), GetCustomWeaponName(hitInfo), hitInfo.boneArea, GetDistanceColor(dist), dist));
if (!_config.EnableLogging) return;
var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} killed ({victim.UserIDString}){victim.displayName} from {dist}m in {hitInfo.boneArea}");
LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
}
private void OnSuicide(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillSuicide"), SanitizeName(GetClan(victim) + victim.displayName)));
private void OnKilledByRadiation(BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillRadiation"), SanitizeName(GetClan(victim) + victim.displayName)));
private void OnKilledByEnt(BaseEntity attacker, BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillEnt"), SanitizeName(GetClan(victim) + victim.displayName), CustomEntName(attacker)));
private void OnKilledByAnimal(BaseEntity attacker, BasePlayer victim) => _killQueue.OnDeath(victim, null, string.Format(_("MsgFeedKillAnimal"), SanitizeName(GetClan(victim) + victim.displayName), CustomAnimalName(attacker)));
private void OnDeathFromWounds(BasePlayer attacker, BasePlayer victim)
{
if (_config.EnableChatFeed)
{
if (!_data.DisabledUsers.Contains(attacker.userID))
Player.Message(attacker, _("MsgAtkWounded", attacker), null, _config.IconId, GetClan(victim) + victim.displayName);
if (!_data.DisabledUsers.Contains(victim.userID))
Player.Message(victim, _("MsgVictimWounded", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName);
}
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedWounded"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName)));
if (!_config.EnableLogging) return;
var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} finished ({victim.UserIDString}){victim.displayName}");
LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
}
private void OnExploded(BasePlayer attacker, BasePlayer victim)
{
if (victim.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = victim;
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBlown"), SanitizeName(GetClan(attacker) + attacker.displayName), CustomNpcName(npc)));
return;
}
if (attacker.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = attacker;
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBlown"), CustomNpcName(npc), SanitizeName(GetClan(victim) + victim.displayName)));
return;
}
if (_config.EnableChatFeed)
{
if (!_data.DisabledUsers.Contains(attacker.userID))
Player.Message(attacker, _("MsgAttackerBlown", attacker), null, _config.IconId, GetClan(victim) + victim.displayName);
if (!_data.DisabledUsers.Contains(victim.userID))
Player.Message(victim, _("MsgVictimBlown", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName);
}
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBlown"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName)));
if (!_config.EnableLogging) return;
var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} blown ({victim.UserIDString}){victim.displayName}");
LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
}
private void OnBurned(BasePlayer attacker, BasePlayer victim)
{
if (victim.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = victim;
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), SanitizeName(GetClan(attacker) + attacker.displayName), CustomNpcName(npc)));
return;
}
if (attacker.IsNpc)
{
if (!_config.EnableNpcFeed) return;
var npc = attacker;
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), CustomNpcName(npc), SanitizeName(GetClan(victim) + victim.displayName)));
return;
}
if (_config.EnableChatFeed)
{
if (!_data.DisabledUsers.Contains(attacker.userID))
Player.Message(attacker, _("MsgAtkBurned", attacker), null, _config.IconId, GetClan(victim) + victim.displayName);
if (!_data.DisabledUsers.Contains(victim.userID))
Player.Message(victim, _("MsgVictimBurned", victim), null, _config.IconId, GetClan(attacker) + attacker.displayName);
}
_killQueue.OnDeath(victim, attacker, string.Format(_("MsgFeedBurned"), SanitizeName(GetClan(attacker) + attacker.displayName), SanitizeName(GetClan(victim) + victim.displayName)));
if (!_config.EnableLogging) return;
var sfkLog = new StringBuilder($"{DateTime.Now}: ({attacker.UserIDString}){attacker.displayName} burned ({victim.UserIDString}){victim.displayName}");
LogToFile("SimpleKillFeed", sfkLog.ToString(), this);
}
#endregion
#region UI
private class KillEvent : Pool.IPooled
{
public int DisplayUntil;
public string Text;
public KillEvent Init(string text, int displayUntil)
{
Text = text;
DisplayUntil = displayUntil;
return this;
}
public void EnterPool()
{
Text = null;
DisplayUntil = 0;
}
public void LeavePool() { }
}
private class KillQueue : MonoBehaviour
{
private readonly WaitForSeconds _secondDelay = new WaitForSeconds(1f);
private readonly Queue<KillEvent> _queue = new Queue<KillEvent>(_config.MaxFeedMessages);
private readonly CuiOutlineComponent _outlineStatic = new CuiOutlineComponent { Distance = _config.OutlineSize, Color = "0 0 0 1" };
private readonly CuiRectTransformComponent[] _rectTransformStatic = new CuiRectTransformComponent[_config.MaxFeedMessages];
private readonly CuiTextComponent[] _textStatic = new CuiTextComponent[_config.MaxFeedMessages];
private readonly CuiElementContainer _cui = new CuiElementContainer();
private bool _needsRedraw;
private int _currentlyDrawn;
public void OnDeath(BasePlayer victim, BasePlayer attacker, string text)
{
if (_queue.Count == _config.MaxFeedMessages)
DequeueEvent(_queue.Dequeue());
PushEvent(Pool.Get<KillEvent>().Init(text, Epoch.Current + _config.FeedMessageTtlSec));
}
private void PushEvent(KillEvent evt)
{
_queue.Enqueue(evt);
_needsRedraw = true;
DoProccessQueue();
}
private void Start()
{
for (var i = 0; i < _config.MaxFeedMessages; i++)
{
_rectTransformStatic[i] = new CuiRectTransformComponent
{
AnchorMax =
$"{_config.AnchorMax.Split(Convert.ToChar(' '))[0]} {float.Parse(_config.AnchorMax.Split(Convert.ToChar(' '))[1]) - (_config.HeightIdent * i)}",
AnchorMin =
$"{_config.AnchorMin.Split(Convert.ToChar(' '))[0]} {float.Parse(_config.AnchorMin.Split(Convert.ToChar(' '))[1]) - (_config.HeightIdent * i)}"
};
_textStatic[i] = new CuiTextComponent { Align = TextAnchor.MiddleRight, FontSize = _config.FontSize, Text = string.Empty };
}
StartCoroutine(ProccessQueue());
}
private void DequeueEvent(KillEvent evt)
{
Pool.Free(ref evt);
_needsRedraw = true;
}
private void DoProccessQueue()
{
while (_queue.Count > 0 && _queue.Peek().DisplayUntil < Epoch.Current)
DequeueEvent(_queue.Dequeue());
if (!_needsRedraw)
return;
var toBeRemoved = _currentlyDrawn;
_currentlyDrawn = 0;
foreach (var killEvent in _queue)
{
var cuiText = _textStatic[_currentlyDrawn];
cuiText.Text = killEvent.Text;
_cui.Add(new CuiElement
{
Name = $"kf-{_currentlyDrawn}",
Parent = "Hud",
Components =
{
cuiText,
_rectTransformStatic[_currentlyDrawn],
_outlineStatic
}
});
if (++_currentlyDrawn == _config.MaxFeedMessages)
break;
}
_needsRedraw = false;
SendKillCui(_cui, toBeRemoved);
_cui.Clear();
}
private IEnumerator ProccessQueue()
{
while (!Interface.Oxide.IsShuttingDown)
{
DoProccessQueue();
yield return _secondDelay;
}
}
private static void SendKillCui(CuiElementContainer cui, int toBeRemoved)
{
var json = cui.ToJson();
foreach (var plr in BasePlayer.activePlayerList)
{
for (var i = toBeRemoved; i > 0; i--)
CuiHelper.DestroyUi(plr, $"kf-{i - 1}");
if (!_data.DisabledUsers.Contains(plr.userID))
CuiHelper.AddUi(plr, json);
}
}
public static void RemoveKillCui(string name)
{
foreach (var plr in BasePlayer.activePlayerList)
CuiHelper.DestroyUi(plr, name);
}
}
#endregion
#region Utils
private string _(string msg, BasePlayer player = null) => lang.GetMessage(msg, this, player?.UserIDString);
private string GetCustomWeaponName(HitInfo hitInfo)
{
var name = GetWeaponName(hitInfo);
if (string.IsNullOrEmpty(name))
return null;
string translatedName;
if (_config.Weapons.TryGetValue(name, out translatedName))
return translatedName;
_config.Weapons.Add(name, name);
Config.WriteObject(_config);
return name;
}
private string CustomNpcName(BasePlayer npc)
{
var name = npc.ShortPrefabName;
if (string.IsNullOrEmpty(name))
return null;
string translatedName;
if (_config.Npcs.TryGetValue(name, out translatedName))
return translatedName;
_config.Npcs.Add(name, name);
Config.WriteObject(_config);
return name;
}
private string CustomEntName(BaseEntity attacker)
{
var name = attacker.ShortPrefabName;
if (string.IsNullOrEmpty(name))
return null;
string translatedName;
if (_config.Ents.TryGetValue(name, out translatedName))
return translatedName;
_config.Npcs.Add(name, name);
Config.WriteObject(_config);
return name;
}
private string CustomAnimalName(BaseEntity attacker)
{
var name = attacker.ShortPrefabName;
if (string.IsNullOrEmpty(name))
return null;
string translatedName;
if (_config.Animal.TryGetValue(name, out translatedName))
return translatedName;
_config.Npcs.Add(name, name);
Config.WriteObject(_config);
return name;
}
private string GetWeaponName(HitInfo hitInfo)
{
var wpnName = "[N/A]";
if (hitInfo.Weapon != null)
{
var wpn = hitInfo.Weapon;
var item = wpn.GetItem();
if (item != null)
wpnName = item.info.displayName.english;
}
else if (hitInfo.WeaponPrefab != null)
{
if (!_itemNameMapping.TryGetValue(hitInfo.WeaponPrefab.prefabID, out wpnName))
PrintWarning($"GetWeaponName failed. Unresolved prefab {hitInfo.WeaponPrefab.prefabID} ({hitInfo.damageTypes.GetMajorityDamageType()}/{hitInfo.damageTypes.Get(hitInfo.damageTypes.GetMajorityDamageType())})");
}
else
PrintWarning($"GetWeaponName failed. Weapon and WeaponPrefab is null? ({hitInfo.damageTypes.GetMajorityDamageType()})");
return wpnName;
}
private static bool IsExplosion(HitInfo hit) => (hit.WeaponPrefab != null && (hit.WeaponPrefab.ShortPrefabName.Contains("grenade") || hit.WeaponPrefab.ShortPrefabName.Contains("explosive")))
|| hit.damageTypes.GetMajorityDamageType() == DamageType.Explosion || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Explosion));
private static bool IsFlame(HitInfo hit) => hit.damageTypes.GetMajorityDamageType() == DamageType.Heat || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Heat));
private static bool IsRadiation(HitInfo hit) => hit.damageTypes.GetMajorityDamageType() == DamageType.Radiation || (!hit.damageTypes.IsBleedCausing() && hit.damageTypes.Has(DamageType.Radiation));
private static bool IsTrap(BaseEntity ent) => ent != null && _config.Ents.ContainsKey(ent.ShortPrefabName);
private static bool IsAnimal(BaseEntity animal) => animal?.ShortPrefabName != null && _config.Animal.ContainsKey(animal.ShortPrefabName);
private static string GetDistanceColor(int dist)
{
foreach (var distanceColor in _config.DistanceColors)
{
if (distanceColor.TestDistance(dist))
return distanceColor.Color;
}
return _config.DefaultDistanceColor ?? "white";
}
private string GetClan(BasePlayer player)
{
if (_isClansReborn || _isClansIo || Clans == null) return null;
var clan = (string)Clans.Call("GetClanOf", player.UserIDString);
if (clan == null) return null;
var format = string.Format("[" + clan + "] ");
return format;
}
private static string SanitizeName(string name)
{
if (name.Length > _config.MaxPlayerNameLength)
name = name.Substring(0, _config.MaxPlayerNameLength).Trim();
return name.Replace("\"", "''");
}
#endregion
}
}