Virtual items cleanup (#23912)
* Virtual items cleanup * Detail * Review --------- Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Hands.Components;
|
||||
|
||||
[RegisterComponent]
|
||||
[NetworkedComponent]
|
||||
[AutoGenerateComponentState(true)]
|
||||
public sealed partial class HandVirtualItemComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The entity blocking this hand.
|
||||
/// </summary>
|
||||
[DataField("blockingEntity"), AutoNetworkedField]
|
||||
public EntityUid BlockingEntity;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Inventory.VirtualItem;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
@@ -28,8 +29,8 @@ public abstract partial class SharedHandsSystem
|
||||
var didUnequip = new DidUnequipHandEvent(uid, args.Entity, hand);
|
||||
RaiseLocalEvent(uid, didUnequip);
|
||||
|
||||
if (TryComp(args.Entity, out HandVirtualItemComponent? @virtual))
|
||||
_virtualSystem.Delete((args.Entity, @virtual), uid);
|
||||
if (TryComp(args.Entity, out VirtualItemComponent? @virtual))
|
||||
_virtualSystem.DeleteVirtualItem((args.Entity, @virtual), uid);
|
||||
}
|
||||
|
||||
private bool ShouldIgnoreRestrictions(EntityUid user)
|
||||
|
||||
@@ -3,6 +3,7 @@ using Content.Shared.Examine;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Inventory.VirtualItem;
|
||||
using Content.Shared.Localizations;
|
||||
using Robust.Shared.Input.Binding;
|
||||
using Robust.Shared.Map;
|
||||
@@ -183,7 +184,7 @@ public abstract partial class SharedHandsSystem : EntitySystem
|
||||
private void HandleExamined(EntityUid uid, HandsComponent handsComp, ExaminedEvent args)
|
||||
{
|
||||
var held = EnumerateHeld(uid, handsComp)
|
||||
.Where(x => !HasComp<HandVirtualItemComponent>(x)).ToList();
|
||||
.Where(x => !HasComp<VirtualItemComponent>(x)).ToList();
|
||||
|
||||
using (args.PushGroup(nameof(HandsComponent)))
|
||||
{
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
using Content.Shared.Hands.Components;
|
||||
|
||||
namespace Content.Shared.Hands.EntitySystems;
|
||||
|
||||
public abstract partial class SharedHandsSystem
|
||||
{
|
||||
private void InitializeVirtual()
|
||||
{
|
||||
SubscribeLocalEvent<HandVirtualItemComponent, AfterAutoHandleStateEvent>(OnVirtualAfter);
|
||||
}
|
||||
|
||||
private void OnVirtualAfter(EntityUid uid, HandVirtualItemComponent component, ref AfterAutoHandleStateEvent args)
|
||||
{
|
||||
// update hands GUI with new entity.
|
||||
if (ContainerSystem.IsEntityInContainer(uid))
|
||||
_items.VisualsChanged(uid);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Inventory.VirtualItem;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Storage.EntitySystems;
|
||||
using Robust.Shared.Containers;
|
||||
@@ -20,7 +21,7 @@ public abstract partial class SharedHandsSystem
|
||||
[Dependency] private readonly SharedItemSystem _items = default!;
|
||||
[Dependency] private readonly SharedStorageSystem _storage = default!;
|
||||
[Dependency] protected readonly SharedTransformSystem TransformSystem = default!;
|
||||
[Dependency] private readonly SharedHandVirtualItemSystem _virtualSystem = default!;
|
||||
[Dependency] private readonly SharedVirtualItemSystem _virtualSystem = default!;
|
||||
|
||||
protected event Action<Entity<HandsComponent>?>? OnHandSetActive;
|
||||
|
||||
@@ -31,7 +32,6 @@ public abstract partial class SharedHandsSystem
|
||||
InitializeInteractions();
|
||||
InitializeDrop();
|
||||
InitializePickup();
|
||||
InitializeVirtual();
|
||||
InitializeRelay();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Robust.Shared.Network;
|
||||
|
||||
namespace Content.Shared.Hands;
|
||||
|
||||
public abstract class SharedHandVirtualItemSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly INetManager _net = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _hands = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<HandVirtualItemComponent, BeingEquippedAttemptEvent>(OnBeingEquippedAttempt);
|
||||
SubscribeLocalEvent<HandVirtualItemComponent, BeforeRangedInteractEvent>(HandleBeforeInteract);
|
||||
}
|
||||
|
||||
public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user)
|
||||
{
|
||||
return TrySpawnVirtualItemInHand(blockingEnt, user, out _);
|
||||
}
|
||||
|
||||
public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user, [NotNullWhen(true)] out EntityUid? virtualItem)
|
||||
{
|
||||
if (_net.IsClient || !_hands.TryGetEmptyHand(user, out var hand))
|
||||
{
|
||||
virtualItem = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var pos = Transform(user).Coordinates;
|
||||
virtualItem = Spawn("HandVirtualItem", pos);
|
||||
var virtualItemComp = EntityManager.GetComponent<HandVirtualItemComponent>(virtualItem.Value);
|
||||
virtualItemComp.BlockingEntity = blockingEnt;
|
||||
Dirty(virtualItemComp);
|
||||
_hands.DoPickup(user, hand, virtualItem.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all virtual items in a user's hands with
|
||||
/// the specified blocked entity.
|
||||
/// </summary>
|
||||
public void DeleteInHandsMatching(EntityUid user, EntityUid matching)
|
||||
{
|
||||
// Client can't currently predict deleting network entities atm and this might happen due to the
|
||||
// hands leaving PVS for example, in which case we wish to ignore it.
|
||||
if (_net.IsClient)
|
||||
return;
|
||||
|
||||
foreach (var hand in _hands.EnumerateHands(user))
|
||||
{
|
||||
if (TryComp(hand.HeldEntity, out HandVirtualItemComponent? virt) && virt.BlockingEntity == matching)
|
||||
{
|
||||
Delete((hand.HeldEntity.Value, virt), user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBeingEquippedAttempt(EntityUid uid, HandVirtualItemComponent component, BeingEquippedAttemptEvent args)
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private static void HandleBeforeInteract(
|
||||
EntityUid uid,
|
||||
HandVirtualItemComponent component,
|
||||
BeforeRangedInteractEvent args)
|
||||
{
|
||||
// No interactions with a virtual item, please.
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a deletion for a virtual item and notifies the blocking entity and user.
|
||||
/// </summary>
|
||||
public void Delete(Entity<HandVirtualItemComponent> item, EntityUid user)
|
||||
{
|
||||
var userEv = new VirtualItemDeletedEvent(item.Comp.BlockingEntity, user);
|
||||
RaiseLocalEvent(user, userEv);
|
||||
var targEv = new VirtualItemDeletedEvent(item.Comp.BlockingEntity, user);
|
||||
RaiseLocalEvent(item.Comp.BlockingEntity, targEv);
|
||||
|
||||
if (TerminatingOrDeleted(item))
|
||||
return;
|
||||
|
||||
_transform.DetachParentToNull(item, Transform(item));
|
||||
if (_net.IsServer)
|
||||
QueueDel(item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user