CatStation/Content.Shared/Vehicle/Systems/VehicleSystem.Key.cs
deltanedas 243ffe5c24
Improve VehicleCanRunEvent (#44947)
* improve VehicleCanRunEvent

* !

---------

Co-authored-by: deltanedas <@deltanedas:goida.zip>
2026-08-01 03:32:21 +00:00

63 lines
2 KiB
C#

using System.Linq;
using Content.Shared.Vehicle.Components;
using Robust.Shared.Containers;
namespace Content.Shared.Vehicle.Systems;
public sealed partial class VehicleSystem
{
[SubscribeLocalEvent]
private void OnGenericKeyedInsertAttempt(Entity<GenericKeyedVehicleComponent> ent, ref ContainerIsInsertingAttemptEvent args)
{
if (args.Cancelled || _timing.ApplyingState || !ent.Comp.PreventInvalidInsertion || args.Container.ID != ent.Comp.ContainerId)
return;
if (_entityWhitelist.IsWhitelistPass(ent.Comp.KeyWhitelist, args.EntityUid))
return;
args.Cancel();
}
[SubscribeLocalEvent]
private void OnGenericKeyedEntInserted(Entity<GenericKeyedVehicleComponent> ent, ref EntInsertedIntoContainerMessage args)
{
if (_timing.ApplyingState || args.Container.ID != ent.Comp.ContainerId)
return;
if (!_vehicleQuery.TryComp(ent, out var vehicle))
return;
RefreshCanRun((ent.Owner, vehicle));
}
[SubscribeLocalEvent]
private void OnGenericKeyedEntRemoved(Entity<GenericKeyedVehicleComponent> ent, ref EntRemovedFromContainerMessage args)
{
if (_timing.ApplyingState || args.Container.ID != ent.Comp.ContainerId)
return;
if (!_vehicleQuery.TryComp(ent, out var vehicle))
return;
RefreshCanRun((ent.Owner, vehicle));
}
[SubscribeLocalEvent]
private void OnGenericKeyedCanRun(Entity<GenericKeyedVehicleComponent> ent, ref VehicleCanRunEvent args)
{
if (!args.CanRun)
return;
if (!_container.TryGetContainer(ent.Owner, ent.Comp.ContainerId, out var container))
{
Log.Error($"Vehicle {ToPrettyString(ent)} has no key container {ent.Comp.ContainerId}!");
args.CanRun = false;
return;
}
var hasKey = container.ContainedEntities.Any(contained =>
_entityWhitelist.IsWhitelistPass(ent.Comp.KeyWhitelist, contained));
args.CanRun = hasKey;
}
}