252 lines
9.9 KiB
C#
252 lines
9.9 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Shared.Hands;
|
|
using Content.Shared.Hands.EntitySystems;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Inventory.Events;
|
|
using Content.Shared.Item;
|
|
using Content.Shared.Popups;
|
|
using Robust.Shared.Containers;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Shared.Inventory.VirtualItem;
|
|
|
|
/// <summary>
|
|
/// In charge of managing virtual items.
|
|
/// Virtual items are used to block a <see cref="SlotButton"/>
|
|
/// or a <see cref="HandButton"/> with a non-existent item that
|
|
/// is a visual copy of another for whatever use
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The slot visuals are managed by <see cref="HandsUiController"/>
|
|
/// and <see cref="InventoryUiController"/>, see the <see cref="VirtualItemComponent"/>
|
|
/// references there for more information
|
|
/// </remarks>
|
|
public abstract class SharedVirtualItemSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly INetManager _netManager = default!;
|
|
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
|
|
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
|
|
[Dependency] private readonly SharedItemSystem _itemSystem = default!;
|
|
[Dependency] private readonly InventorySystem _inventorySystem = default!;
|
|
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
|
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
|
|
|
[ValidatePrototypeId<EntityPrototype>]
|
|
private const string VirtualItem = "VirtualItem";
|
|
|
|
public override void Initialize()
|
|
{
|
|
SubscribeLocalEvent<VirtualItemComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
|
|
|
|
SubscribeLocalEvent<VirtualItemComponent, BeingEquippedAttemptEvent>(OnBeingEquippedAttempt);
|
|
SubscribeLocalEvent<VirtualItemComponent, BeingUnequippedAttemptEvent>(OnBeingUnequippedAttempt);
|
|
|
|
SubscribeLocalEvent<VirtualItemComponent, BeforeRangedInteractEvent>(OnBeforeRangedInteract);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the GUI buttons with the new entity.
|
|
/// </summary>
|
|
private void OnAfterAutoHandleState(Entity<VirtualItemComponent> ent, ref AfterAutoHandleStateEvent args)
|
|
{
|
|
if (_containerSystem.IsEntityInContainer(ent))
|
|
_itemSystem.VisualsChanged(ent);
|
|
}
|
|
|
|
private void OnBeingEquippedAttempt(Entity<VirtualItemComponent> ent, ref BeingEquippedAttemptEvent args)
|
|
{
|
|
// No interactions with a virtual item, please.
|
|
args.Cancel();
|
|
}
|
|
|
|
private void OnBeingUnequippedAttempt(Entity<VirtualItemComponent> ent, ref BeingUnequippedAttemptEvent args)
|
|
{
|
|
// No interactions with a virtual item, please.
|
|
args.Cancel();
|
|
}
|
|
|
|
private void OnBeforeRangedInteract(Entity<VirtualItemComponent> ent, ref BeforeRangedInteractEvent args)
|
|
{
|
|
// No interactions with a virtual item, please.
|
|
args.Handled = true;
|
|
}
|
|
|
|
#region Hands
|
|
|
|
/// <summary>
|
|
/// Spawns a virtual item in a empty hand
|
|
/// </summary>
|
|
/// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
|
|
/// <param name="user">The entity that we want to insert the virtual entity</param>
|
|
/// <param name="dropOthers">Whether or not to try and drop other items to make space</param>
|
|
public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user, bool dropOthers = false)
|
|
{
|
|
return TrySpawnVirtualItemInHand(blockingEnt, user, out _, dropOthers);
|
|
}
|
|
|
|
/// <inheritdoc cref="TrySpawnVirtualItemInHand(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,bool)"/>
|
|
public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user, [NotNullWhen(true)] out EntityUid? virtualItem, bool dropOthers = false)
|
|
{
|
|
virtualItem = null;
|
|
if (!_handsSystem.TryGetEmptyHand(user, out var empty))
|
|
{
|
|
if (!dropOthers)
|
|
return false;
|
|
|
|
foreach (var hand in _handsSystem.EnumerateHands(user))
|
|
{
|
|
if (hand.HeldEntity is not { } held)
|
|
continue;
|
|
|
|
if (held == blockingEnt)
|
|
continue;
|
|
|
|
if (!_handsSystem.TryDrop(user, hand))
|
|
continue;
|
|
|
|
if (!TerminatingOrDeleted(held))
|
|
_popup.PopupClient(Loc.GetString("virtual-item-dropped-other", ("dropped", held)), user, user);
|
|
|
|
empty = hand;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (empty == null)
|
|
return false;
|
|
|
|
if (!TrySpawnVirtualItem(blockingEnt, user, out virtualItem))
|
|
return false;
|
|
|
|
_handsSystem.DoPickup(user, empty, virtualItem.Value);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scan the user's hands until we find the virtual entity, if the
|
|
/// virtual entity is a copy of the matching entity, delete it
|
|
/// </summary>
|
|
public void DeleteInHandsMatching(EntityUid user, EntityUid matching)
|
|
{
|
|
// Client can't currently predict deleting networked entities so we use this workaround, another
|
|
// problem can popup when the hands leave PVS for example and this avoids that too
|
|
if (_netManager.IsClient)
|
|
return;
|
|
|
|
foreach (var hand in _handsSystem.EnumerateHands(user))
|
|
{
|
|
if (TryComp(hand.HeldEntity, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
|
|
{
|
|
DeleteVirtualItem((hand.HeldEntity.Value, virt), user);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Inventory
|
|
|
|
/// <summary>
|
|
/// Spawns a virtual item inside a inventory slot
|
|
/// </summary>
|
|
/// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
|
|
/// <param name="user">The entity that we want to insert the virtual entity</param>
|
|
/// <param name="slot">The slot to which we will insert the virtual entity (could be the "shoes" slot, for example)</param>
|
|
/// <param name="force">Whether or not to force an equip</param>
|
|
public bool TrySpawnVirtualItemInInventory(EntityUid blockingEnt, EntityUid user, string slot, bool force = false)
|
|
{
|
|
return TrySpawnVirtualItemInInventory(blockingEnt, user, slot, force, out _);
|
|
}
|
|
|
|
/// <inheritdoc cref="TrySpawnVirtualItemInInventory(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,string,bool)"/>
|
|
public bool TrySpawnVirtualItemInInventory(EntityUid blockingEnt, EntityUid user, string slot, bool force, [NotNullWhen(true)] out EntityUid? virtualItem)
|
|
{
|
|
if (!TrySpawnVirtualItem(blockingEnt, user, out virtualItem))
|
|
return false;
|
|
|
|
_inventorySystem.TryEquip(user, virtualItem.Value, slot, force: force);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scan the user's inventory slots until we find a virtual entity, when
|
|
/// that's done check if the found virtual entity is a copy of our matching entity,
|
|
/// if it is, delete it
|
|
/// </summary>
|
|
/// <param name="user">The entity that we want to delete the virtual entity from</param>
|
|
/// <param name="matching">The entity that made the virtual entity</param>
|
|
/// <param name="slotName">Set this param if you have the name of the slot, it avoids unnecessary queries</param>
|
|
public void DeleteInSlotMatching(EntityUid user, EntityUid matching, string? slotName = null)
|
|
{
|
|
// Client can't currently predict deleting networked entities so we use this workaround, another
|
|
// problem can popup when the hands leave PVS for example and this avoids that too
|
|
if (_netManager.IsClient)
|
|
return;
|
|
|
|
if (slotName != null)
|
|
{
|
|
if (!_inventorySystem.TryGetSlotEntity(user, slotName, out var slotEnt))
|
|
return;
|
|
|
|
if (TryComp(slotEnt, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
|
|
DeleteVirtualItem((slotEnt.Value, virt), user);
|
|
|
|
return;
|
|
}
|
|
|
|
if (!_inventorySystem.TryGetSlots(user, out var slotDefinitions))
|
|
return;
|
|
|
|
foreach (var slot in slotDefinitions)
|
|
{
|
|
if (!_inventorySystem.TryGetSlotEntity(user, slot.Name, out var slotEnt))
|
|
continue;
|
|
|
|
if (TryComp(slotEnt, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
|
|
DeleteVirtualItem((slotEnt.Value, virt), user);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Spawns a virtual item and setups the component without any special handling
|
|
/// </summary>
|
|
/// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
|
|
/// <param name="user">The entity that we want to insert the virtual entity</param>
|
|
/// <param name="virtualItem">The virtual item, if spawned</param>
|
|
public bool TrySpawnVirtualItem(EntityUid blockingEnt, EntityUid user, [NotNullWhen(true)] out EntityUid? virtualItem)
|
|
{
|
|
if (_netManager.IsClient)
|
|
{
|
|
virtualItem = null;
|
|
return false;
|
|
}
|
|
|
|
var pos = Transform(user).Coordinates;
|
|
virtualItem = Spawn(VirtualItem, pos);
|
|
var virtualItemComp = Comp<VirtualItemComponent>(virtualItem.Value);
|
|
virtualItemComp.BlockingEntity = blockingEnt;
|
|
Dirty(virtualItem.Value, virtualItemComp);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Queues a deletion for a virtual item and notifies the blocking entity and user.
|
|
/// </summary>
|
|
public void DeleteVirtualItem(Entity<VirtualItemComponent> 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;
|
|
|
|
_transformSystem.DetachParentToNull(item, Transform(item));
|
|
if (_netManager.IsServer)
|
|
QueueDel(item);
|
|
}
|
|
}
|