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
This commit is contained in:
Víctor Aguilera Puerto
2020-08-16 16:30:52 +02:00
committed by GitHub
parent 1068269db0
commit d58d84096b
5 changed files with 70 additions and 54 deletions

View File

@@ -11,6 +11,7 @@ using Content.Server.Interfaces.GameObjects.Components.Interaction;
using Content.Server.Interfaces.GameObjects.Components.Items; using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.GameObjects.Components.Items; using Content.Shared.GameObjects.Components.Items;
using Content.Shared.GameObjects.Components.Mobs; using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Health.BodySystem; using Content.Shared.Health.BodySystem;
using Content.Shared.Physics; using Content.Shared.Physics;
using Robust.Server.GameObjects; 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()) foreach (var hand in ActivePriorityEnumerable())
{ {
if (PutInHand(item, hand, false)) if (PutInHand(item, hand, false, mobCheck))
{ {
OnItemChanged?.Invoke(); OnItemChanged?.Invoke();
@@ -155,10 +156,10 @@ namespace Content.Server.GameObjects.Components.GUI
return false; 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); var hand = GetHand(index);
if (!CanPutInHand(item, index) || hand == null) if (!CanPutInHand(item, index, mobChecks) || hand == null)
{ {
return fallback && PutInHand(item); return fallback && PutInHand(item);
} }
@@ -176,19 +177,23 @@ namespace Content.Server.GameObjects.Components.GUI
return success; 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; 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()) 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; return true;
} }
@@ -197,8 +202,11 @@ namespace Content.Server.GameObjects.Components.GUI
return false; 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; return GetHand(index)?.Container.CanInsert(item.Owner) == true;
} }
@@ -284,17 +292,17 @@ namespace Content.Server.GameObjects.Components.GUI
return Drop(slot, coords, doMobChecks); 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); var hand = GetHand(slot);
if (!CanDrop(slot) || hand?.Entity == null) if (!CanDrop(slot, mobChecks) || hand?.Entity == null)
{ {
return false; return false;
} }
var item = hand.Entity.GetComponent<ItemComponent>(); var item = hand.Entity.GetComponent<ItemComponent>();
if (!DroppedInteraction(item, doMobChecks)) if (!DroppedInteraction(item, mobChecks))
return false; return false;
if (!hand.Container.Remove(hand.Entity)) if (!hand.Container.Remove(hand.Entity))
@@ -321,7 +329,7 @@ namespace Content.Server.GameObjects.Components.GUI
return true; return true;
} }
public bool Drop(IEntity entity, bool doMobChecks = true) public bool Drop(IEntity entity, bool mobChecks = true)
{ {
if (entity == null) 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)); 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) public bool Drop(string slot, BaseContainer targetContainer, bool doMobChecks = true)
@@ -409,13 +417,15 @@ namespace Content.Server.GameObjects.Components.GUI
/// <returns> /// <returns>
/// True if there is an item in the slot and it can be dropped, false otherwise. /// True if there is an item in the slot and it can be dropped, false otherwise.
/// </returns> /// </returns>
public bool CanDrop(string name) public bool CanDrop(string name, bool mobCheck = true)
{ {
var hand = GetHand(name); var hand = GetHand(name);
if (hand?.Entity == null)
{ if (mobCheck && !ActionBlockerSystem.CanDrop(Owner))
return false;
if (hand?.Entity == null)
return false; return false;
}
return hand.Container.CanRemove(hand.Entity); return hand.Container.CanRemove(hand.Entity);
} }

View File

@@ -173,9 +173,10 @@ namespace Content.Server.GameObjects.Components.GUI
/// </remarks> /// </remarks>
/// <param name="slot">The slot to put the item in.</param> /// <param name="slot">The slot to put the item in.</param>
/// <param name="item">The item to insert into the slot.</param> /// <param name="item">The item to insert into the slot.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <param name="reason">The translated reason why the item cannot be equipped, if this function returns false. Can be null.</param> /// <param name="reason">The translated reason why the item cannot be equipped, if this function returns false. Can be null.</param>
/// <returns>True if the item was successfully inserted, false otherwise.</returns> /// <returns>True if the item was successfully inserted, false otherwise.</returns>
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) 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()"); "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; return false;
} }
@@ -203,9 +204,9 @@ namespace Content.Server.GameObjects.Components.GUI
return true; 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<ItemComponent>()); public bool Equip(Slots slot, IEntity entity, bool mobCheck = true) => Equip(slot, entity.GetComponent<ItemComponent>(), mobCheck);
/// <summary> /// <summary>
/// Checks whether an item can be put in the specified slot. /// Checks whether an item can be put in the specified slot.
@@ -214,12 +215,12 @@ namespace Content.Server.GameObjects.Components.GUI
/// <param name="item">The item to check for.</param> /// <param name="item">The item to check for.</param>
/// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param> /// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param>
/// <returns>True if the item can be inserted into the specified slot.</returns> /// <returns>True if the item can be inserted into the specified slot.</returns>
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; var pass = false;
reason = null; reason = null;
if (!ActionBlockerSystem.CanEquip(Owner)) if (mobCheck && !ActionBlockerSystem.CanEquip(Owner))
return false; return false;
if (item is ClothingComponent clothing) if (item is ClothingComponent clothing)
@@ -248,18 +249,19 @@ namespace Content.Server.GameObjects.Components.GUI
return pass && _slotContainers[slot].CanInsert(item.Owner); 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<ItemComponent>()); public bool CanEquip(Slots slot, IEntity entity, bool mobCheck = true) => CanEquip(slot, entity.GetComponent<ItemComponent>(), mobCheck);
/// <summary> /// <summary>
/// Drops the item in a slot. /// Drops the item in a slot.
/// </summary> /// </summary>
/// <param name="slot">The slot to drop the item from.</param> /// <param name="slot">The slot to drop the item from.</param>
/// <returns>True if an item was dropped, false otherwise.</returns> /// <returns>True if an item was dropped, false otherwise.</returns>
public bool Unequip(Slots slot) /// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
public bool Unequip(Slots slot, bool mobCheck = true)
{ {
if (!CanUnequip(slot)) if (!CanUnequip(slot, mobCheck))
{ {
return false; return false;
} }
@@ -288,16 +290,17 @@ namespace Content.Server.GameObjects.Components.GUI
/// Checks whether an item can be dropped from the specified slot. /// Checks whether an item can be dropped from the specified slot.
/// </summary> /// </summary>
/// <param name="slot">The slot to check for.</param> /// <param name="slot">The slot to check for.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns> /// <returns>
/// True if there is an item in the slot and it can be dropped, false otherwise. /// True if there is an item in the slot and it can be dropped, false otherwise.
/// </returns> /// </returns>
public bool CanUnequip(Slots slot) public bool CanUnequip(Slots slot, bool mobCheck = true)
{ {
if (!ActionBlockerSystem.CanUnequip(Owner)) if (mobCheck && !ActionBlockerSystem.CanUnequip(Owner))
return false; return false;
var InventorySlot = _slotContainers[slot]; var inventorySlot = _slotContainers[slot];
return InventorySlot.ContainedEntity != null && InventorySlot.CanRemove(InventorySlot.ContainedEntity); return inventorySlot.ContainedEntity != null && inventorySlot.CanRemove(inventorySlot.ContainedEntity);
} }
/// <summary> /// <summary>
@@ -398,7 +401,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing)) if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing))
{ {
hands.Drop(hands.ActiveHand); hands.Drop(hands.ActiveHand);
if (!Equip(msg.Inventoryslot, clothing, out var reason)) if (!Equip(msg.Inventoryslot, clothing, true, out var reason))
{ {
hands.PutInHand(clothing); hands.PutInHand(clothing);
@@ -434,7 +437,7 @@ namespace Content.Server.GameObjects.Components.GUI
var activeHand = hands.GetActiveHand; var activeHand = hands.GetActiveHand;
if (activeHand != null && GetSlotItem(msg.Inventoryslot) == null) 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<Slots, (EntityUid entity, bool fits)>(msg.Inventoryslot, (activeHand.Owner.Uid, canEquip)); _hoverEntity = new KeyValuePair<Slots, (EntityUid entity, bool fits)>(msg.Inventoryslot, (activeHand.Owner.Uid, canEquip));
Dirty(); Dirty();

View File

@@ -137,7 +137,7 @@ namespace Content.Server.GameObjects.Components.GUI
return false; 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)); _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot equip that there!", Owner));
return false; return false;
@@ -162,7 +162,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (result != DoAfterStatus.Finished) return; if (result != DoAfterStatus.Finished) return;
userHands.Drop(item!.Owner, false); userHands.Drop(item!.Owner, false);
inventory.Equip(slot, item!.Owner); inventory.Equip(slot, item!.Owner, false);
UpdateSubscribed(); UpdateSubscribed();
} }
@@ -202,7 +202,7 @@ namespace Content.Server.GameObjects.Components.GUI
return false; 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)); _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot put that there!", Owner));
return false; return false;
@@ -227,7 +227,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (result != DoAfterStatus.Finished) return; if (result != DoAfterStatus.Finished) return;
userHands.Drop(hand, false); userHands.Drop(hand, false);
hands.PutInHand(item, hand, false); hands.PutInHand(item!, hand, false, false);
UpdateSubscribed(); UpdateSubscribed();
} }
@@ -253,7 +253,7 @@ namespace Content.Server.GameObjects.Components.GUI
return false; return false;
} }
if (!inventory.CanUnequip(slot)) if (!inventory.CanUnequip(slot, false))
{ {
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot unequip that!", Owner)); _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot unequip that!", Owner));
return false; return false;
@@ -277,7 +277,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (result != DoAfterStatus.Finished) return; if (result != DoAfterStatus.Finished) return;
var item = inventory.GetSlotItem(slot); var item = inventory.GetSlotItem(slot);
inventory.Unequip(slot); inventory.Unequip(slot, false);
userHands.PutInHandOrDrop(item); userHands.PutInHandOrDrop(item);
UpdateSubscribed(); UpdateSubscribed();
} }
@@ -304,7 +304,7 @@ namespace Content.Server.GameObjects.Components.GUI
return false; return false;
} }
if (!hands.CanDrop(hand)) if (!hands.CanDrop(hand, false))
{ {
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot drop that!", Owner)); _notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot drop that!", Owner));
return false; return false;
@@ -329,7 +329,7 @@ namespace Content.Server.GameObjects.Components.GUI
var item = hands.GetItem(hand); var item = hands.GetItem(hand);
hands.Drop(hand, false); hands.Drop(hand, false);
userHands.PutInHandOrDrop(item); userHands.PutInHandOrDrop(item!);
UpdateSubscribed(); UpdateSubscribed();
} }
@@ -364,8 +364,6 @@ namespace Content.Server.GameObjects.Components.GUI
else else
TakeItemFromHands(user, handMessage.Hand); TakeItemFromHands(user, handMessage.Hand);
break; break;
default:
break;
} }
} }
} }

View File

@@ -112,7 +112,7 @@ namespace Content.Server.GameObjects.Components.Items.Clothing
private bool TryEquip(InventoryComponent inv, Slots slot, IEntity user) 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) if (reason != null)
_serverNotifyManager.PopupMessage(Owner, user, reason); _serverNotifyManager.PopupMessage(Owner, user, reason);

View File

@@ -61,8 +61,9 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items
/// Puts an item into any empty hand, preferring the active hand. /// Puts an item into any empty hand, preferring the active hand.
/// </summary> /// </summary>
/// <param name="item">The item to put in a hand.</param> /// <param name="item">The item to put in a hand.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns>True if the item was inserted, false otherwise.</returns> /// <returns>True if the item was inserted, false otherwise.</returns>
bool PutInHand(ItemComponent item); bool PutInHand(ItemComponent item, bool mobCheck = true);
/// <summary> /// <summary>
/// Puts an item into a specific hand. /// Puts an item into a specific hand.
@@ -71,24 +72,27 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items
/// <param name="index">The name of the hand to put the item into.</param> /// <param name="index">The name of the hand to put the item into.</param>
/// <param name="fallback"> /// <param name="fallback">
/// If true and the provided hand is full, the method will fall back to <see cref="PutInHand(ItemComponent)" /> /// If true and the provided hand is full, the method will fall back to <see cref="PutInHand(ItemComponent)" />
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// </param> /// </param>
/// <returns>True if the item was inserted into a hand, false otherwise.</returns> /// <returns>True if the item was inserted into a hand, false otherwise.</returns>
bool PutInHand(ItemComponent item, string index, bool fallback=true); bool PutInHand(ItemComponent item, string index, bool fallback=true, bool mobCheck = true);
/// <summary> /// <summary>
/// Checks to see if an item can be put in any hand. /// Checks to see if an item can be put in any hand.
/// </summary> /// </summary>
/// <param name="item">The item to check for.</param> /// <param name="item">The item to check for.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns>True if the item can be inserted, false otherwise.</returns> /// <returns>True if the item can be inserted, false otherwise.</returns>
bool CanPutInHand(ItemComponent item); bool CanPutInHand(ItemComponent item, bool mobCheck = true);
/// <summary> /// <summary>
/// Checks to see if an item can be put in the specified hand. /// Checks to see if an item can be put in the specified hand.
/// </summary> /// </summary>
/// <param name="item">The item to check for.</param> /// <param name="item">The item to check for.</param>
/// <param name="index">The name for the hand to check for.</param> /// <param name="index">The name for the hand to check for.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns>True if the item can be inserted, false otherwise.</returns> /// <returns>True if the item can be inserted, false otherwise.</returns>
bool CanPutInHand(ItemComponent item, string index); bool CanPutInHand(ItemComponent item, string index, bool mobCheck = true);
/// <summary> /// <summary>
/// Finds the hand slot holding the specified entity, if any. /// 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. /// Drops the item contained in the slot to the same position as our entity.
/// </summary> /// </summary>
/// <param name="slot">The slot of which to drop to drop the item.</param> /// <param name="slot">The slot of which to drop to drop the item.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param> /// <param name="mobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <returns>True on success, false if something blocked the drop.</returns> /// <returns>True on success, false if something blocked the drop.</returns>
bool Drop(string slot, bool doMobChecks = true); bool Drop(string slot, bool mobChecks = true);
/// <summary> /// <summary>
/// Drops an item held by one of our hand slots to the same position as our owning entity. /// Drops an item held by one of our hand slots to the same position as our owning entity.
/// </summary> /// </summary>
/// <param name="entity">The item to drop.</param> /// <param name="entity">The item to drop.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param> /// <param name="mobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <returns>True on success, false if something blocked the drop.</returns> /// <returns>True on success, false if something blocked the drop.</returns>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// Thrown if <see cref="entity"/> is null. /// Thrown if <see cref="entity"/> is null.
@@ -123,7 +127,7 @@ namespace Content.Server.Interfaces.GameObjects.Components.Items
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// Thrown if <see cref="entity"/> is not actually held in any hand. /// Thrown if <see cref="entity"/> is not actually held in any hand.
/// </exception> /// </exception>
bool Drop(IEntity entity, bool doMobChecks = true); bool Drop(IEntity entity, bool mobChecks = true);
/// <summary> /// <summary>
/// Drops the item in a slot. /// 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. /// Checks whether the item in the specified hand can be dropped.
/// </summary> /// </summary>
/// <param name="name">The hand to check for.</param> /// <param name="name">The hand to check for.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns> /// <returns>
/// True if the item can be dropped, false if the hand is empty or the item in the hand cannot be dropped. /// True if the item can be dropped, false if the hand is empty or the item in the hand cannot be dropped.
/// </returns> /// </returns>
bool CanDrop(string name); bool CanDrop(string name, bool mobCheck = true);
/// <summary> /// <summary>
/// Adds a new hand to this hands component. /// Adds a new hand to this hands component.