SharedVirtualItemSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Hands;
  3. using Content.Shared.Hands.Components;
  4. using Content.Shared.Hands.EntitySystems;
  5. using Content.Shared.Interaction;
  6. using Content.Shared.Interaction.Events;
  7. using Content.Shared.Inventory.Events;
  8. using Content.Shared.Item;
  9. using Content.Shared.Popups;
  10. using Robust.Shared.Containers;
  11. using Robust.Shared.Network;
  12. using Robust.Shared.Prototypes;
  13. namespace Content.Shared.Inventory.VirtualItem;
  14. /// <summary>
  15. /// In charge of managing virtual items.
  16. /// Virtual items are used to block a <see cref="SlotButton"/>
  17. /// or a <see cref="HandButton"/> with a non-existent item that
  18. /// is a visual copy of another for whatever use
  19. /// </summary>
  20. /// <remarks>
  21. /// The slot visuals are managed by <see cref="HandsUiController"/>
  22. /// and <see cref="InventoryUiController"/>, see the <see cref="VirtualItemComponent"/>
  23. /// references there for more information
  24. /// </remarks>
  25. public abstract class SharedVirtualItemSystem : EntitySystem
  26. {
  27. [Dependency] private readonly INetManager _netManager = default!;
  28. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  29. [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
  30. [Dependency] private readonly SharedItemSystem _itemSystem = default!;
  31. [Dependency] private readonly InventorySystem _inventorySystem = default!;
  32. [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
  33. [Dependency] private readonly SharedPopupSystem _popup = default!;
  34. [ValidatePrototypeId<EntityPrototype>]
  35. private const string VirtualItem = "VirtualItem";
  36. public override void Initialize()
  37. {
  38. SubscribeLocalEvent<VirtualItemComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
  39. SubscribeLocalEvent<VirtualItemComponent, BeingEquippedAttemptEvent>(OnBeingEquippedAttempt);
  40. SubscribeLocalEvent<VirtualItemComponent, BeingUnequippedAttemptEvent>(OnBeingUnequippedAttempt);
  41. SubscribeLocalEvent<VirtualItemComponent, BeforeRangedInteractEvent>(OnBeforeRangedInteract);
  42. SubscribeLocalEvent<VirtualItemComponent, GettingInteractedWithAttemptEvent>(OnGettingInteractedWithAttemptEvent);
  43. SubscribeLocalEvent<VirtualItemComponent, GetUsedEntityEvent>(OnGetUsedEntity);
  44. }
  45. /// <summary>
  46. /// Updates the GUI buttons with the new entity.
  47. /// </summary>
  48. private void OnAfterAutoHandleState(Entity<VirtualItemComponent> ent, ref AfterAutoHandleStateEvent args)
  49. {
  50. if (_containerSystem.IsEntityInContainer(ent))
  51. _itemSystem.VisualsChanged(ent);
  52. }
  53. private void OnBeingEquippedAttempt(Entity<VirtualItemComponent> ent, ref BeingEquippedAttemptEvent args)
  54. {
  55. // No interactions with a virtual item, please.
  56. args.Cancel();
  57. }
  58. private void OnBeingUnequippedAttempt(Entity<VirtualItemComponent> ent, ref BeingUnequippedAttemptEvent args)
  59. {
  60. // No interactions with a virtual item, please.
  61. args.Cancel();
  62. }
  63. private void OnBeforeRangedInteract(Entity<VirtualItemComponent> ent, ref BeforeRangedInteractEvent args)
  64. {
  65. // No interactions with a virtual item, please.
  66. args.Handled = true;
  67. }
  68. private void OnGettingInteractedWithAttemptEvent(Entity<VirtualItemComponent> ent, ref GettingInteractedWithAttemptEvent args)
  69. {
  70. // No interactions with a virtual item, please.
  71. args.Cancelled = true;
  72. }
  73. private void OnGetUsedEntity(Entity<VirtualItemComponent> ent, ref GetUsedEntityEvent args)
  74. {
  75. if (args.Handled)
  76. return;
  77. // if the user is holding the real item the virtual item points to,
  78. // we allow them to use it in the interaction
  79. foreach (var hand in _handsSystem.EnumerateHands(args.User))
  80. {
  81. if (hand.HeldEntity == ent.Comp.BlockingEntity)
  82. {
  83. args.Used = ent.Comp.BlockingEntity;
  84. return;
  85. }
  86. }
  87. }
  88. #region Hands
  89. /// <summary>
  90. /// Spawns a virtual item in a empty hand
  91. /// </summary>
  92. /// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
  93. /// <param name="user">The entity that we want to insert the virtual entity</param>
  94. /// <param name="dropOthers">Whether or not to try and drop other items to make space</param>
  95. public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user, bool dropOthers = false)
  96. {
  97. return TrySpawnVirtualItemInHand(blockingEnt, user, out _, dropOthers);
  98. }
  99. /// <inheritdoc cref="TrySpawnVirtualItemInHand(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,bool)"/>
  100. public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user, [NotNullWhen(true)] out EntityUid? virtualItem, bool dropOthers = false, Hand? empty = null)
  101. {
  102. virtualItem = null;
  103. if (empty == null && !_handsSystem.TryGetEmptyHand(user, out empty))
  104. {
  105. if (!dropOthers)
  106. return false;
  107. foreach (var hand in _handsSystem.EnumerateHands(user))
  108. {
  109. if (hand.HeldEntity is not { } held)
  110. continue;
  111. if (held == blockingEnt)
  112. continue;
  113. if (!_handsSystem.TryDrop(user, hand))
  114. continue;
  115. if (!TerminatingOrDeleted(held))
  116. _popup.PopupClient(Loc.GetString("virtual-item-dropped-other", ("dropped", held)), user, user);
  117. empty = hand;
  118. break;
  119. }
  120. }
  121. if (empty == null)
  122. return false;
  123. if (!TrySpawnVirtualItem(blockingEnt, user, out virtualItem))
  124. return false;
  125. _handsSystem.DoPickup(user, empty, virtualItem.Value);
  126. return true;
  127. }
  128. /// <summary>
  129. /// Scan the user's hands until we find the virtual entity, if the
  130. /// virtual entity is a copy of the matching entity, delete it
  131. /// </summary>
  132. public void DeleteInHandsMatching(EntityUid user, EntityUid matching)
  133. {
  134. // Client can't currently predict deleting networked entities so we use this workaround, another
  135. // problem can popup when the hands leave PVS for example and this avoids that too
  136. if (_netManager.IsClient)
  137. return;
  138. foreach (var hand in _handsSystem.EnumerateHands(user))
  139. {
  140. if (TryComp(hand.HeldEntity, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
  141. {
  142. DeleteVirtualItem((hand.HeldEntity.Value, virt), user);
  143. }
  144. }
  145. }
  146. #endregion
  147. #region Inventory
  148. /// <summary>
  149. /// Spawns a virtual item inside a inventory slot
  150. /// </summary>
  151. /// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
  152. /// <param name="user">The entity that we want to insert the virtual entity</param>
  153. /// <param name="slot">The slot to which we will insert the virtual entity (could be the "shoes" slot, for example)</param>
  154. /// <param name="force">Whether or not to force an equip</param>
  155. public bool TrySpawnVirtualItemInInventory(EntityUid blockingEnt, EntityUid user, string slot, bool force = false)
  156. {
  157. return TrySpawnVirtualItemInInventory(blockingEnt, user, slot, force, out _);
  158. }
  159. /// <inheritdoc cref="TrySpawnVirtualItemInInventory(Robust.Shared.GameObjects.EntityUid,Robust.Shared.GameObjects.EntityUid,string,bool)"/>
  160. public bool TrySpawnVirtualItemInInventory(EntityUid blockingEnt, EntityUid user, string slot, bool force, [NotNullWhen(true)] out EntityUid? virtualItem)
  161. {
  162. if (!TrySpawnVirtualItem(blockingEnt, user, out virtualItem))
  163. return false;
  164. _inventorySystem.TryEquip(user, virtualItem.Value, slot, force: force);
  165. return true;
  166. }
  167. /// <summary>
  168. /// Scan the user's inventory slots until we find a virtual entity, when
  169. /// that's done check if the found virtual entity is a copy of our matching entity,
  170. /// if it is, delete it
  171. /// </summary>
  172. /// <param name="user">The entity that we want to delete the virtual entity from</param>
  173. /// <param name="matching">The entity that made the virtual entity</param>
  174. /// <param name="slotName">Set this param if you have the name of the slot, it avoids unnecessary queries</param>
  175. public void DeleteInSlotMatching(EntityUid user, EntityUid matching, string? slotName = null)
  176. {
  177. // Client can't currently predict deleting networked entities so we use this workaround, another
  178. // problem can popup when the hands leave PVS for example and this avoids that too
  179. if (_netManager.IsClient)
  180. return;
  181. if (slotName != null)
  182. {
  183. if (!_inventorySystem.TryGetSlotEntity(user, slotName, out var slotEnt))
  184. return;
  185. if (TryComp(slotEnt, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
  186. DeleteVirtualItem((slotEnt.Value, virt), user);
  187. return;
  188. }
  189. if (!_inventorySystem.TryGetSlots(user, out var slotDefinitions))
  190. return;
  191. foreach (var slot in slotDefinitions)
  192. {
  193. if (!_inventorySystem.TryGetSlotEntity(user, slot.Name, out var slotEnt))
  194. continue;
  195. if (TryComp(slotEnt, out VirtualItemComponent? virt) && virt.BlockingEntity == matching)
  196. DeleteVirtualItem((slotEnt.Value, virt), user);
  197. }
  198. }
  199. #endregion
  200. /// <summary>
  201. /// Spawns a virtual item and setups the component without any special handling
  202. /// </summary>
  203. /// <param name="blockingEnt">The entity we will make a virtual entity copy of</param>
  204. /// <param name="user">The entity that we want to insert the virtual entity</param>
  205. /// <param name="virtualItem">The virtual item, if spawned</param>
  206. public bool TrySpawnVirtualItem(EntityUid blockingEnt, EntityUid user, [NotNullWhen(true)] out EntityUid? virtualItem)
  207. {
  208. if (_netManager.IsClient)
  209. {
  210. virtualItem = null;
  211. return false;
  212. }
  213. var pos = Transform(user).Coordinates;
  214. virtualItem = Spawn(VirtualItem, pos);
  215. var virtualItemComp = Comp<VirtualItemComponent>(virtualItem.Value);
  216. virtualItemComp.BlockingEntity = blockingEnt;
  217. Dirty(virtualItem.Value, virtualItemComp);
  218. return true;
  219. }
  220. /// <summary>
  221. /// Queues a deletion for a virtual item and notifies the blocking entity and user.
  222. /// </summary>
  223. public void DeleteVirtualItem(Entity<VirtualItemComponent> item, EntityUid user)
  224. {
  225. var userEv = new VirtualItemDeletedEvent(item.Comp.BlockingEntity, user);
  226. RaiseLocalEvent(user, userEv);
  227. var targEv = new VirtualItemDeletedEvent(item.Comp.BlockingEntity, user);
  228. RaiseLocalEvent(item.Comp.BlockingEntity, targEv);
  229. if (TerminatingOrDeleted(item))
  230. return;
  231. _transformSystem.DetachEntity(item, Transform(item));
  232. if (_netManager.IsServer)
  233. QueueDel(item);
  234. }
  235. }