Fix deletion of inventories of dead people.

This commit is contained in:
Pieter-Jan Briers
2020-08-21 14:54:38 +02:00
parent a9d8d8051d
commit 80e175aaa9

View File

@@ -10,6 +10,7 @@ using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects.Components.Inventory; using Content.Shared.GameObjects.Components.Inventory;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.Components.Container;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.GameObjects.Components;
@@ -115,8 +116,14 @@ namespace Content.Server.GameObjects.Components.GUI
public override void OnRemove() public override void OnRemove()
{ {
var slots = _slotContainers.Keys.ToList(); var slots = _slotContainers.Keys.ToList();
foreach (var slot in slots) foreach (var slot in slots)
{ {
if (TryGetSlotItem(slot, out ItemComponent item))
{
item.Owner.Delete();
}
RemoveSlot(slot); RemoveSlot(slot);
} }
@@ -267,17 +274,17 @@ namespace Content.Server.GameObjects.Components.GUI
} }
var inventorySlot = _slotContainers[slot]; var inventorySlot = _slotContainers[slot];
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>(); var entity = inventorySlot.ContainedEntity;
if (!inventorySlot.Remove(inventorySlot.ContainedEntity)) var item = entity.GetComponent<ItemComponent>();
if (!inventorySlot.Remove(entity))
{ {
return false; return false;
} }
// TODO: The item should be dropped to the container our owner is in, if any. // TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<ITransformComponent>(); ContainerHelpers.AttachParentToContainerOrGrid(entity.Transform);
itemTransform.GridPosition = Owner.GetComponent<ITransformComponent>().GridPosition;
_entitySystemManager.GetEntitySystem<InteractionSystem>().UnequippedInteraction(Owner, item.Owner, slot); _entitySystemManager.GetEntitySystem<InteractionSystem>().UnequippedInteraction(Owner, entity, slot);
OnItemChanged?.Invoke(); OnItemChanged?.Invoke();
@@ -286,6 +293,29 @@ namespace Content.Server.GameObjects.Components.GUI
return true; return true;
} }
public void ForceUnequip(Slots slot)
{
var inventorySlot = _slotContainers[slot];
var entity = inventorySlot.ContainedEntity;
if (entity == null)
{
return;
}
var item = entity.GetComponent<ItemComponent>();
inventorySlot.ForceRemove(entity);
var itemTransform = entity.Transform;
ContainerHelpers.AttachParentToContainerOrGrid(itemTransform);
_entitySystemManager.GetEntitySystem<InteractionSystem>().UnequippedInteraction(Owner, item.Owner, slot);
OnItemChanged?.Invoke();
Dirty();
}
/// <summary> /// <summary>
/// Checks whether an item can be dropped from the specified slot. /// Checks whether an item can be dropped from the specified slot.
/// </summary> /// </summary>
@@ -340,13 +370,11 @@ namespace Content.Server.GameObjects.Components.GUI
throw new InvalidOperationException($"Slow '{slot}' does not exist."); throw new InvalidOperationException($"Slow '{slot}' does not exist.");
} }
if (GetSlotItem(slot) != null && !Unequip(slot)) ForceUnequip(slot);
{
// TODO: Handle this potential failiure better.
throw new InvalidOperationException(
"Unable to remove slot as the contained clothing could not be dropped");
}
var container = _slotContainers[slot];
container.Shutdown();
_slotContainers.Remove(slot); _slotContainers.Remove(slot);
OnItemChanged?.Invoke(); OnItemChanged?.Invoke();