Add utility verbs (#6473)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Disposal.Unit.Components;
|
||||
using Content.Server.Disposal.Unit.EntitySystems;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Shared.Interaction;
|
||||
@@ -8,9 +11,6 @@ using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly DisposalUnitSystem _disposalSystem = default!;
|
||||
|
||||
private readonly List<IPlayerSession> _sessionCache = new();
|
||||
|
||||
@@ -33,9 +34,11 @@ namespace Content.Server.Storage.EntitySystems
|
||||
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
||||
|
||||
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
|
||||
SubscribeLocalEvent<EntityStorageComponent, RelayMovementEntityEvent>(OnRelayMovement);
|
||||
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<UtilityVerb>>(AddTransferVerbs);
|
||||
|
||||
SubscribeLocalEvent<StorageFillComponent, MapInitEvent>(OnStorageFillMapInit);
|
||||
}
|
||||
|
||||
@@ -117,6 +120,97 @@ namespace Content.Server.Storage.EntitySystems
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
private void AddTransferVerbs(EntityUid uid, ServerStorageComponent component, GetVerbsEvent<UtilityVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
return;
|
||||
|
||||
var entities = component.Storage?.ContainedEntities;
|
||||
if (entities == null || entities.Count == 0)
|
||||
return;
|
||||
|
||||
if (TryComp(uid, out LockComponent? lockComponent) && lockComponent.Locked)
|
||||
return;
|
||||
|
||||
// if the target is storage, add a verb to transfer storage.
|
||||
if (TryComp(args.Target, out ServerStorageComponent? targetStorage)
|
||||
&& (!TryComp(uid, out LockComponent? targetLock) || !targetLock.Locked))
|
||||
{
|
||||
UtilityVerb verb = new()
|
||||
{
|
||||
Text = Loc.GetString("storage-component-transfer-verb"),
|
||||
IconEntity = args.Using,
|
||||
Act = () => TransferEntities(uid, args.Target, component, lockComponent, targetStorage, targetLock)
|
||||
};
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
// if the target is a disposal unit, add a verb to transfer storage into the unit (e.g., empty a trash bag).
|
||||
if (!TryComp(args.Target, out DisposalUnitComponent? disposal))
|
||||
return;
|
||||
|
||||
UtilityVerb dispose = new()
|
||||
{
|
||||
Text = Loc.GetString("storage-component-dispose-verb"),
|
||||
IconEntity = args.Using,
|
||||
Act = () => DisposeEntities(args.User, uid, args.Target, component, lockComponent, disposal)
|
||||
};
|
||||
|
||||
args.Verbs.Add(dispose);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move entities from one storage to another.
|
||||
/// </summary>
|
||||
public void TransferEntities(EntityUid source, EntityUid target,
|
||||
ServerStorageComponent? sourceComp = null, LockComponent? sourceLock = null,
|
||||
ServerStorageComponent? targetComp = null, LockComponent? targetLock = null)
|
||||
{
|
||||
if (!Resolve(source, ref sourceComp) || !Resolve(target, ref targetComp))
|
||||
return;
|
||||
|
||||
var entities = sourceComp.Storage?.ContainedEntities;
|
||||
if (entities == null || entities.Count == 0)
|
||||
return;
|
||||
|
||||
if (Resolve(source, ref sourceLock, false) && sourceLock.Locked
|
||||
|| Resolve(target, ref targetLock, false) && targetLock.Locked)
|
||||
return;
|
||||
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
targetComp.Insert(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move entities from storage into a disposal unit.
|
||||
/// </summary>
|
||||
public void DisposeEntities(EntityUid user, EntityUid source, EntityUid target,
|
||||
ServerStorageComponent? sourceComp = null, LockComponent? sourceLock = null,
|
||||
DisposalUnitComponent? disposalComp = null)
|
||||
{
|
||||
if (!Resolve(source, ref sourceComp) || !Resolve(target, ref disposalComp))
|
||||
return;
|
||||
|
||||
var entities = sourceComp.Storage?.ContainedEntities;
|
||||
if (entities == null || entities.Count == 0)
|
||||
return;
|
||||
|
||||
if (Resolve(source, ref sourceLock, false) && sourceLock.Locked)
|
||||
return;
|
||||
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
if (_disposalSystem.CanInsert(disposalComp, entity)
|
||||
&& disposalComp.Container.Insert(entity))
|
||||
{
|
||||
_disposalSystem.AfterInsert(disposalComp, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
|
||||
Reference in New Issue
Block a user