From: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com> Date: Thu, 23 May 2024 04:16:14 +0000 (-0700) Subject: Prioritize empty item slots when inserting (#28203) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=0e0f50f3d1d76c0ba1283d5fa2a92678e3de0466;p=space-station-14.git Prioritize empty item slots when inserting (#28203) * Prioritize empty item slots when inserting * Revert "Prioritize empty item slots when inserting" This reverts commit 4272a65cba5fc18df801812b0af20123aec08409. * Prioritize empty item slots when inserting * Try drop * Check for any can insert before dropping --- diff --git a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs index 914b34d3c1..2e3f9ed461 100644 --- a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs +++ b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs @@ -197,6 +197,7 @@ namespace Content.Shared.Containers.ItemSlots if (!EntityManager.TryGetComponent(args.User, out HandsComponent? hands)) return; + var slots = new List(); foreach (var slot in itemSlots.Slots.Values) { if (!slot.InsertOnInteract) @@ -205,10 +206,20 @@ namespace Content.Shared.Containers.ItemSlots if (!CanInsert(uid, args.Used, args.User, slot, swap: slot.Swap, popup: args.User)) continue; - // Drop the held item onto the floor. Return if the user cannot drop. - if (!_handsSystem.TryDrop(args.User, args.Used, handsComp: hands)) - return; + slots.Add(slot); + } + if (slots.Count == 0) + return; + + // Drop the held item onto the floor. Return if the user cannot drop. + if (!_handsSystem.TryDrop(args.User, args.Used, handsComp: hands)) + return; + + slots.Sort(SortEmpty); + + foreach (var slot in slots) + { if (slot.Item != null) _handsSystem.TryPickupAnyHand(args.User, slot.Item.Value, handsComp: hands); @@ -333,6 +344,65 @@ namespace Content.Shared.Containers.ItemSlots Insert(uid, slot, held, user, excludeUserAudio: excludeUserAudio); return true; } + + /// + /// Tries to insert an item into any empty slot. + /// + /// The entity that has the item slots. + /// The item to be inserted. + /// The entity performing the interaction. + /// + /// If true, will exclude the user when playing sound. Does nothing client-side. + /// Useful for predicted interactions + /// + /// False if failed to insert item + public bool TryInsertEmpty(Entity ent, EntityUid item, EntityUid? user, bool excludeUserAudio = false) + { + if (!Resolve(ent, ref ent.Comp, false)) + return false; + + var slots = new List(); + foreach (var slot in ent.Comp.Slots.Values) + { + if (slot.ContainerSlot?.ContainedEntity != null) + continue; + + if (CanInsert(ent, item, user, slot)) + slots.Add(slot); + } + + if (slots.Count == 0) + return false; + + if (user != null && _handsSystem.IsHolding(user.Value, item)) + { + if (!_handsSystem.TryDrop(user.Value, item)) + return false; + } + + slots.Sort(SortEmpty); + + foreach (var slot in slots) + { + if (TryInsert(ent, slot, item, user, excludeUserAudio: excludeUserAudio)) + return true; + } + + return false; + } + + private static int SortEmpty(ItemSlot a, ItemSlot b) + { + var aEnt = a.ContainerSlot?.ContainedEntity; + var bEnt = b.ContainerSlot?.ContainedEntity; + if (aEnt == null && bEnt == null) + return a.Priority.CompareTo(b.Priority); + + if (aEnt == null) + return -1; + + return 1; + } #endregion #region Eject