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