Add inventory helpers and an integration test for it (#1841)
This commit is contained in:
committed by
GitHub
parent
94e85adedb
commit
fd81e05d5b
49
Content.Server/Utility/InventoryHelpers.cs
Normal file
49
Content.Server/Utility/InventoryHelpers.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Shared.GameObjects.Components.Inventory;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
|
||||
|
||||
namespace Content.Server.Utility
|
||||
{
|
||||
public static class InventoryHelpers
|
||||
{
|
||||
public static bool SpawnItemInSlot(this InventoryComponent inventory, Slots slot, string prototype, bool mobCheck = false)
|
||||
{
|
||||
var entityManager = inventory.Owner.EntityManager;
|
||||
var protoManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
// Let's do nothing if the owner of the inventory has been deleted.
|
||||
if (inventory.Owner.Deleted)
|
||||
return false;
|
||||
|
||||
// If we don't have that slot or there's already an item there, we do nothing.
|
||||
if (!inventory.HasSlot(slot) || inventory.TryGetSlotItem(slot, out ItemComponent _))
|
||||
return false;
|
||||
|
||||
// If the prototype in question doesn't exist, we do nothing.
|
||||
if (!protoManager.HasIndex<EntityPrototype>(prototype))
|
||||
return false;
|
||||
|
||||
// Let's spawn this in nullspace first...
|
||||
var item = entityManager.SpawnEntity(prototype, MapCoordinates.Nullspace);
|
||||
|
||||
// Helper method that deletes the item and returns false.
|
||||
bool DeleteItem()
|
||||
{
|
||||
item.Delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
// If this doesn't have an item component, then we can't do anything with it.
|
||||
if (!item.TryGetComponent(out ItemComponent itemComp))
|
||||
return DeleteItem();
|
||||
|
||||
// We finally try to equip the item, otherwise we delete it.
|
||||
return inventory.Equip(slot, itemComp, mobCheck) || DeleteItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user