CatStation/Content.Shared/Vehicle/Systems/VehicleSystem.Key.cs
AndrewFenriz d2472f82ed
Hand blocker for vehicles (and mechs) (#45045)
* add BlockHands

* and mechs

* move to another place

* small fix

* TrySpawnUnremoveableVirtualItemInHand

* upd

* fix docs

* VehicleHandBlockerComponent
2026-07-31 17:48:40 +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))
{
args = args with { CanRun = false };
return;
}
var hasKey = container.ContainedEntities.Any(contained =>
_entityWhitelist.IsWhitelistPass(ent.Comp.KeyWhitelist, contained));
if (!hasKey)
args = args with { CanRun = false };
}
}