From d58d84096b935f7cc64f85824c7977a2c7f2d870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Aguilera=20Puerto?= <6766154+Zumorica@users.noreply.github.com> Date: Sun, 16 Aug 2020 16:30:52 +0200 Subject: [PATCH] Adds mobCheck to a bunch of inventory/hands methods (#1704) * Adds mobCheck to a bunch of inventory/hands methods, fix not being able to strip dead bodies * Address review --- .../Components/GUI/HandsComponent.cs | 46 +++++++++++-------- .../Components/GUI/InventoryComponent.cs | 35 +++++++------- .../Components/GUI/StrippableComponent.cs | 18 ++++---- .../Items/Clothing/ClothingComponent.cs | 2 +- .../Components/Items/IHandsComponent.cs | 23 ++++++---- 5 files changed, 70 insertions(+), 54 deletions(-) diff --git a/Content.Server/GameObjects/Components/GUI/HandsComponent.cs b/Content.Server/GameObjects/Components/GUI/HandsComponent.cs index ea75c7f382..f5403a6928 100644 --- a/Content.Server/GameObjects/Components/GUI/HandsComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/HandsComponent.cs @@ -11,6 +11,7 @@ using Content.Server.Interfaces.GameObjects.Components.Interaction; using Content.Server.Interfaces.GameObjects.Components.Items; using Content.Shared.GameObjects.Components.Items; using Content.Shared.GameObjects.Components.Mobs; +using Content.Shared.GameObjects.EntitySystems; using Content.Shared.Health.BodySystem; using Content.Shared.Physics; using Robust.Server.GameObjects; @@ -140,11 +141,11 @@ namespace Content.Server.GameObjects.Components.GUI } } - public bool PutInHand(ItemComponent item) + public bool PutInHand(ItemComponent item, bool mobCheck = true) { foreach (var hand in ActivePriorityEnumerable()) { - if (PutInHand(item, hand, false)) + if (PutInHand(item, hand, false, mobCheck)) { OnItemChanged?.Invoke(); @@ -155,10 +156,10 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - public bool PutInHand(ItemComponent item, string index, bool fallback = true) + public bool PutInHand(ItemComponent item, string index, bool fallback = true, bool mobChecks = true) { var hand = GetHand(index); - if (!CanPutInHand(item, index) || hand == null) + if (!CanPutInHand(item, index, mobChecks) || hand == null) { return fallback && PutInHand(item); } @@ -176,19 +177,23 @@ namespace Content.Server.GameObjects.Components.GUI return success; } - public void PutInHandOrDrop(ItemComponent item) + public void PutInHandOrDrop(ItemComponent item, bool mobCheck = true) { - if (!PutInHand(item)) + if (!PutInHand(item, mobCheck)) { item.Owner.Transform.GridPosition = Owner.Transform.GridPosition; } } - public bool CanPutInHand(ItemComponent item) + public bool CanPutInHand(ItemComponent item, bool mobCheck = true) { + if (mobCheck && !ActionBlockerSystem.CanPickup(Owner)) + return false; + foreach (var handName in ActivePriorityEnumerable()) { - if (CanPutInHand(item, handName)) + // We already did a mobCheck, so let's not waste cycles. + if (CanPutInHand(item, handName, false)) { return true; } @@ -197,8 +202,11 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - public bool CanPutInHand(ItemComponent item, string index) + public bool CanPutInHand(ItemComponent item, string index, bool mobCheck = true) { + if (mobCheck && !ActionBlockerSystem.CanPickup(Owner)) + return false; + return GetHand(index)?.Container.CanInsert(item.Owner) == true; } @@ -284,17 +292,17 @@ namespace Content.Server.GameObjects.Components.GUI return Drop(slot, coords, doMobChecks); } - public bool Drop(string slot, bool doMobChecks = true) + public bool Drop(string slot, bool mobChecks = true) { var hand = GetHand(slot); - if (!CanDrop(slot) || hand?.Entity == null) + if (!CanDrop(slot, mobChecks) || hand?.Entity == null) { return false; } var item = hand.Entity.GetComponent(); - if (!DroppedInteraction(item, doMobChecks)) + if (!DroppedInteraction(item, mobChecks)) return false; if (!hand.Container.Remove(hand.Entity)) @@ -321,7 +329,7 @@ namespace Content.Server.GameObjects.Components.GUI return true; } - public bool Drop(IEntity entity, bool doMobChecks = true) + public bool Drop(IEntity entity, bool mobChecks = true) { if (entity == null) { @@ -333,7 +341,7 @@ namespace Content.Server.GameObjects.Components.GUI throw new ArgumentException("Entity must be held in one of our hands.", nameof(entity)); } - return Drop(slot, doMobChecks); + return Drop(slot, mobChecks); } public bool Drop(string slot, BaseContainer targetContainer, bool doMobChecks = true) @@ -409,13 +417,15 @@ namespace Content.Server.GameObjects.Components.GUI /// /// True if there is an item in the slot and it can be dropped, false otherwise. /// - public bool CanDrop(string name) + public bool CanDrop(string name, bool mobCheck = true) { var hand = GetHand(name); - if (hand?.Entity == null) - { + + if (mobCheck && !ActionBlockerSystem.CanDrop(Owner)) + return false; + + if (hand?.Entity == null) return false; - } return hand.Container.CanRemove(hand.Entity); } diff --git a/Content.Server/GameObjects/Components/GUI/InventoryComponent.cs b/Content.Server/GameObjects/Components/GUI/InventoryComponent.cs index 1d1e52b6a2..57ab715eb5 100644 --- a/Content.Server/GameObjects/Components/GUI/InventoryComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/InventoryComponent.cs @@ -173,9 +173,10 @@ namespace Content.Server.GameObjects.Components.GUI /// /// The slot to put the item in. /// The item to insert into the slot. + /// Whether to perform an ActionBlocker check to the entity. /// The translated reason why the item cannot be equipped, if this function returns false. Can be null. /// True if the item was successfully inserted, false otherwise. - public bool Equip(Slots slot, ItemComponent item, out string reason) + public bool Equip(Slots slot, ItemComponent item, bool mobCheck, out string reason) { if (item == null) { @@ -183,7 +184,7 @@ namespace Content.Server.GameObjects.Components.GUI "Clothing must be passed here. To remove some clothing from a slot, use Unequip()"); } - if (!CanEquip(slot, item, out reason)) + if (!CanEquip(slot, item, mobCheck, out reason)) { return false; } @@ -203,9 +204,9 @@ namespace Content.Server.GameObjects.Components.GUI return true; } - public bool Equip(Slots slot, ItemComponent item) => Equip(slot, item, out var _); + public bool Equip(Slots slot, ItemComponent item, bool mobCheck = true) => Equip(slot, item, mobCheck, out var _); - public bool Equip(Slots slot, IEntity entity) => Equip(slot, entity.GetComponent()); + public bool Equip(Slots slot, IEntity entity, bool mobCheck = true) => Equip(slot, entity.GetComponent(), mobCheck); /// /// Checks whether an item can be put in the specified slot. @@ -214,12 +215,12 @@ namespace Content.Server.GameObjects.Components.GUI /// The item to check for. /// The translated reason why the item cannot be equiped, if this function returns false. Can be null. /// True if the item can be inserted into the specified slot. - public bool CanEquip(Slots slot, ItemComponent item, out string reason) + public bool CanEquip(Slots slot, ItemComponent item, bool mobCheck, out string reason) { var pass = false; reason = null; - if (!ActionBlockerSystem.CanEquip(Owner)) + if (mobCheck && !ActionBlockerSystem.CanEquip(Owner)) return false; if (item is ClothingComponent clothing) @@ -248,18 +249,19 @@ namespace Content.Server.GameObjects.Components.GUI return pass && _slotContainers[slot].CanInsert(item.Owner); } - public bool CanEquip(Slots slot, ItemComponent item) => CanEquip(slot, item, out var _); + public bool CanEquip(Slots slot, ItemComponent item, bool mobCheck = true) => CanEquip(slot, item, mobCheck, out var _); - public bool CanEquip(Slots slot, IEntity entity) => CanEquip(slot, entity.GetComponent()); + public bool CanEquip(Slots slot, IEntity entity, bool mobCheck = true) => CanEquip(slot, entity.GetComponent(), mobCheck); /// /// Drops the item in a slot. /// /// The slot to drop the item from. /// True if an item was dropped, false otherwise. - public bool Unequip(Slots slot) + /// Whether to perform an ActionBlocker check to the entity. + public bool Unequip(Slots slot, bool mobCheck = true) { - if (!CanUnequip(slot)) + if (!CanUnequip(slot, mobCheck)) { return false; } @@ -288,16 +290,17 @@ namespace Content.Server.GameObjects.Components.GUI /// Checks whether an item can be dropped from the specified slot. /// /// The slot to check for. + /// Whether to perform an ActionBlocker check to the entity. /// /// True if there is an item in the slot and it can be dropped, false otherwise. /// - public bool CanUnequip(Slots slot) + public bool CanUnequip(Slots slot, bool mobCheck = true) { - if (!ActionBlockerSystem.CanUnequip(Owner)) + if (mobCheck && !ActionBlockerSystem.CanUnequip(Owner)) return false; - var InventorySlot = _slotContainers[slot]; - return InventorySlot.ContainedEntity != null && InventorySlot.CanRemove(InventorySlot.ContainedEntity); + var inventorySlot = _slotContainers[slot]; + return inventorySlot.ContainedEntity != null && inventorySlot.CanRemove(inventorySlot.ContainedEntity); } /// @@ -398,7 +401,7 @@ namespace Content.Server.GameObjects.Components.GUI if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing)) { hands.Drop(hands.ActiveHand); - if (!Equip(msg.Inventoryslot, clothing, out var reason)) + if (!Equip(msg.Inventoryslot, clothing, true, out var reason)) { hands.PutInHand(clothing); @@ -434,7 +437,7 @@ namespace Content.Server.GameObjects.Components.GUI var activeHand = hands.GetActiveHand; if (activeHand != null && GetSlotItem(msg.Inventoryslot) == null) { - var canEquip = CanEquip(msg.Inventoryslot, activeHand, out var reason); + var canEquip = CanEquip(msg.Inventoryslot, activeHand, true, out var reason); _hoverEntity = new KeyValuePair(msg.Inventoryslot, (activeHand.Owner.Uid, canEquip)); Dirty(); diff --git a/Content.Server/GameObjects/Components/GUI/StrippableComponent.cs b/Content.Server/GameObjects/Components/GUI/StrippableComponent.cs index e045fd2b5a..0363fee424 100644 --- a/Content.Server/GameObjects/Components/GUI/StrippableComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/StrippableComponent.cs @@ -137,7 +137,7 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - if (!inventory.CanEquip(slot, item)) + if (!inventory.CanEquip(slot, item, false)) { _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot equip that there!", Owner)); return false; @@ -162,7 +162,7 @@ namespace Content.Server.GameObjects.Components.GUI if (result != DoAfterStatus.Finished) return; userHands.Drop(item!.Owner, false); - inventory.Equip(slot, item!.Owner); + inventory.Equip(slot, item!.Owner, false); UpdateSubscribed(); } @@ -202,7 +202,7 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - if (!hands.CanPutInHand(item, hand)) + if (!hands.CanPutInHand(item, hand, false)) { _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot put that there!", Owner)); return false; @@ -227,7 +227,7 @@ namespace Content.Server.GameObjects.Components.GUI if (result != DoAfterStatus.Finished) return; userHands.Drop(hand, false); - hands.PutInHand(item, hand, false); + hands.PutInHand(item!, hand, false, false); UpdateSubscribed(); } @@ -253,7 +253,7 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - if (!inventory.CanUnequip(slot)) + if (!inventory.CanUnequip(slot, false)) { _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot unequip that!", Owner)); return false; @@ -277,7 +277,7 @@ namespace Content.Server.GameObjects.Components.GUI if (result != DoAfterStatus.Finished) return; var item = inventory.GetSlotItem(slot); - inventory.Unequip(slot); + inventory.Unequip(slot, false); userHands.PutInHandOrDrop(item); UpdateSubscribed(); } @@ -304,7 +304,7 @@ namespace Content.Server.GameObjects.Components.GUI return false; } - if (!hands.CanDrop(hand)) + if (!hands.CanDrop(hand, false)) { _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot drop that!", Owner)); return false; @@ -329,7 +329,7 @@ namespace Content.Server.GameObjects.Components.GUI var item = hands.GetItem(hand); hands.Drop(hand, false); - userHands.PutInHandOrDrop(item); + userHands.PutInHandOrDrop(item!); UpdateSubscribed(); } @@ -364,8 +364,6 @@ namespace Content.Server.GameObjects.Components.GUI else TakeItemFromHands(user, handMessage.Hand); break; - default: - break; } } } diff --git a/Content.Server/GameObjects/Components/Items/Clothing/ClothingComponent.cs b/Content.Server/GameObjects/Components/Items/Clothing/ClothingComponent.cs index a94c5982bc..7de3236ec9 100644 --- a/Content.Server/GameObjects/Components/Items/Clothing/ClothingComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Clothing/ClothingComponent.cs @@ -112,7 +112,7 @@ namespace Content.Server.GameObjects.Components.Items.Clothing private bool TryEquip(InventoryComponent inv, Slots slot, IEntity user) { - if (!inv.Equip(slot, this, out var reason)) + if (!inv.Equip(slot, this, true, out var reason)) { if (reason != null) _serverNotifyManager.PopupMessage(Owner, user, reason); diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs index e716533285..5765098531 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs @@ -61,8 +61,9 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items /// Puts an item into any empty hand, preferring the active hand. /// /// The item to put in a hand. + /// Whether to perform an ActionBlocker check to the entity. /// True if the item was inserted, false otherwise. - bool PutInHand(ItemComponent item); + bool PutInHand(ItemComponent item, bool mobCheck = true); /// /// Puts an item into a specific hand. @@ -71,24 +72,27 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items /// The name of the hand to put the item into. /// /// If true and the provided hand is full, the method will fall back to + /// Whether to perform an ActionBlocker check to the entity. /// /// True if the item was inserted into a hand, false otherwise. - bool PutInHand(ItemComponent item, string index, bool fallback=true); + bool PutInHand(ItemComponent item, string index, bool fallback=true, bool mobCheck = true); /// /// Checks to see if an item can be put in any hand. /// /// The item to check for. + /// Whether to perform an ActionBlocker check to the entity. /// True if the item can be inserted, false otherwise. - bool CanPutInHand(ItemComponent item); + bool CanPutInHand(ItemComponent item, bool mobCheck = true); /// /// Checks to see if an item can be put in the specified hand. /// /// The item to check for. /// The name for the hand to check for. + /// Whether to perform an ActionBlocker check to the entity. /// True if the item can be inserted, false otherwise. - bool CanPutInHand(ItemComponent item, string index); + bool CanPutInHand(ItemComponent item, string index, bool mobCheck = true); /// /// Finds the hand slot holding the specified entity, if any. @@ -107,15 +111,15 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items /// Drops the item contained in the slot to the same position as our entity. /// /// The slot of which to drop to drop the item. - /// Whether to check the for the mob or not. + /// Whether to check the for the mob or not. /// True on success, false if something blocked the drop. - bool Drop(string slot, bool doMobChecks = true); + bool Drop(string slot, bool mobChecks = true); /// /// Drops an item held by one of our hand slots to the same position as our owning entity. /// /// The item to drop. - /// Whether to check the for the mob or not. + /// Whether to check the for the mob or not. /// True on success, false if something blocked the drop. /// /// Thrown if is null. @@ -123,7 +127,7 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items /// /// Thrown if is not actually held in any hand. /// - bool Drop(IEntity entity, bool doMobChecks = true); + bool Drop(IEntity entity, bool mobChecks = true); /// /// Drops the item in a slot. @@ -194,10 +198,11 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items /// Checks whether the item in the specified hand can be dropped. /// /// The hand to check for. + /// Whether to perform an ActionBlocker check to the entity. /// /// True if the item can be dropped, false if the hand is empty or the item in the hand cannot be dropped. /// - bool CanDrop(string name); + bool CanDrop(string name, bool mobCheck = true); /// /// Adds a new hand to this hands component.