| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using System.Diagnostics.CodeAnalysis;
- using Content.Shared.Hands;
- using Content.Shared.Hands.Components;
- using Content.Shared.Hands.EntitySystems;
- using Content.Shared.Interaction;
- using Content.Shared.Interaction.Events;
- 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);
- SubscribeLocalEvent<VirtualItemComponent, GettingInteractedWithAttemptEvent>(OnGettingInteractedWithAttemptEvent);
- SubscribeLocalEvent<VirtualItemComponent, GetUsedEntityEvent>(OnGetUsedEntity);
- }
- /// <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;
- }
- private void OnGettingInteractedWithAttemptEvent(Entity<VirtualItemComponent> ent, ref GettingInteractedWithAttemptEvent args)
- {
- // No interactions with a virtual item, please.
- args.Cancelled = true;
- }
- private void OnGetUsedEntity(Entity<VirtualItemComponent> ent, ref GetUsedEntityEvent args)
- {
- if (args.Handled)
- return;
- // if the user is holding the real item the virtual item points to,
- // we allow them to use it in the interaction
- foreach (var hand in _handsSystem.EnumerateHands(args.User))
- {
- if (hand.HeldEntity == ent.Comp.BlockingEntity)
- {
- args.Used = ent.Comp.BlockingEntity;
- return;
- }
- }
- }
- #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, Hand? empty = null)
- {
- virtualItem = null;
- if (empty == null && !_handsSystem.TryGetEmptyHand(user, out 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.DetachEntity(item, Transform(item));
- if (_netManager.IsServer)
- QueueDel(item);
- }
- }
|