Try fix KeyNotFoundException in KillTrackingSystem (#28553)

This commit is contained in:
Leon Friedrich
2024-06-04 01:36:07 +12:00
committed by GitHub
parent def04cdaf5
commit 0b4388d5d4

View File

@@ -2,6 +2,7 @@ using Content.Server.NPC.HTN;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.FixedPoint; using Content.Shared.FixedPoint;
using Content.Shared.Mobs; using Content.Shared.Mobs;
using Content.Shared.Mobs.Systems;
using Robust.Shared.Player; using Robust.Shared.Player;
namespace Content.Server.KillTracking; namespace Content.Server.KillTracking;
@@ -14,7 +15,8 @@ public sealed class KillTrackingSystem : EntitySystem
/// <inheritdoc/> /// <inheritdoc/>
public override void Initialize() public override void Initialize()
{ {
SubscribeLocalEvent<KillTrackerComponent, DamageChangedEvent>(OnDamageChanged); // Add damage to LifetimeDamage before MobStateChangedEvent gets raised
SubscribeLocalEvent<KillTrackerComponent, DamageChangedEvent>(OnDamageChanged, before: [ typeof(MobThresholdSystem) ]);
SubscribeLocalEvent<KillTrackerComponent, MobStateChangedEvent>(OnMobStateChanged); SubscribeLocalEvent<KillTrackerComponent, MobStateChangedEvent>(OnMobStateChanged);
} }
@@ -50,7 +52,7 @@ public sealed class KillTrackingSystem : EntitySystem
var largestSource = GetLargestSource(component.LifetimeDamage); var largestSource = GetLargestSource(component.LifetimeDamage);
largestSource ??= killImpulse; largestSource ??= killImpulse;
KillSource? killSource; KillSource killSource;
KillSource? assistSource = null; KillSource? assistSource = null;
if (killImpulse is KillEnvironmentSource) if (killImpulse is KillEnvironmentSource)
@@ -69,15 +71,15 @@ public sealed class KillTrackingSystem : EntitySystem
killSource = killImpulse; killSource = killImpulse;
// no assist is given to environmental kills // no assist is given to environmental kills
if (largestSource is not KillEnvironmentSource) if (largestSource is not KillEnvironmentSource
{ && component.LifetimeDamage.TryGetValue(largestSource, out var largestDamage))
// you have to do at least 50% of largest source's damage to get the assist.
if (component.LifetimeDamage[largestSource] >= component.LifetimeDamage[killSource] / 2)
{ {
var killDamage = component.LifetimeDamage.GetValueOrDefault(killSource);
// you have to do at least twice as much damage as the killing source to get the assist.
if (largestDamage >= killDamage / 2)
assistSource = largestSource; assistSource = largestSource;
} }
} }
}
// it's a suicide if: // it's a suicide if:
// - you caused your own death // - you caused your own death