diff --git a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs index 2374532b95..db4f994636 100644 --- a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs +++ b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs @@ -45,10 +45,15 @@ namespace Content.Server.Interfaces.GameObjects return slots[slot]; } - public bool PutInSlot(string slot, IItemComponent item) + public bool Insert(string slot, IItemComponent item) { + if (item == null) + { + throw new ArgumentNullException(nameof(item), "An item must be passed. To remove an item from a slot, use Drop()"); + } + var inventorySlot = _GetSlot(slot); - if (inventorySlot.Item != null) + if (!CanInsert(slot, item)) { return false; } @@ -58,11 +63,22 @@ namespace Content.Server.Interfaces.GameObjects return true; } - public bool DropSlot(string slot) + public bool CanInsert(string slot, IItemComponent item) { + var inventorySlot = _GetSlot(slot); + return inventorySlot.Item == null; + } + + public bool Drop(string slot) + { + if (!CanDrop(slot)) + { + return false; + } + var inventorySlot = _GetSlot(slot); var item = inventorySlot.Item; - if (item == null || !container.Remove(item.Owner)) + if (!container.Remove(item.Owner)) { return false; } @@ -76,17 +92,42 @@ namespace Content.Server.Interfaces.GameObjects return true; } + public bool CanDrop(string slot) + { + var inventorySlot = _GetSlot(slot); + var item = inventorySlot.Item; + return item != null && container.CanRemove(item.Owner); + } + public void AddSlot(string slot) { - if (slots.ContainsKey(slot)) + if (HasSlot(slot)) { - + throw new InvalidOperationException($"Slot '{slot}' already exists."); } + + slots[slot] = new InventorySlot(slot, this); } public void RemoveSlot(string slot) { - throw new NotImplementedException(); + if (!HasSlot(slot)) + { + throw new InvalidOperationException($"Slow '{slot}' does not exist."); + } + + if (Get(slot) != null && !Drop(slot)) + { + // TODO: Handle this potential failiure better. + throw new InvalidOperationException("Unable to remove slot as the contained item could not be dropped"); + } + + slots.Remove(slot); + } + + public bool HasSlot(string slot) + { + return slots.ContainsKey(slot); } private class InventorySlot : IInventorySlot diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs index 881f30328b..d5ff2c1bec 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs @@ -40,11 +40,35 @@ namespace Content.Server.Interfaces.GameObjects /// True if the item was inserted into a hand, false otherwise. bool PutInHand(IItemComponent item, string index, bool fallback=true); + /// + /// Checks to see if an item can be put in any hand. + /// + /// The item to check for. + /// True if the item can be inserted, false otherwise. + bool CanPutInHand(IItemComponent item); + + /// + /// Checks to see if an item can be put in the specified hand. + /// + /// The item to check for. + /// The index for the hand to check for. + /// True if the item can be inserted, false otherwise. + bool CanPutInHand(IItemComponent item, string index); + /// /// Drops an item on the ground, removing it from the hand. /// /// The hand to drop from. /// True if an item was successfully dropped, false otherwise. bool Drop(string index); + + /// + /// Checks whether the item in the specified hand can be dropped. + /// + /// The hand to check for. + /// + /// 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 index); } } diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs index 4f3247ecdf..62fab52d9d 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs @@ -29,14 +29,31 @@ namespace Content.Server.Interfaces.GameObjects /// The slot to put the item in. /// The item to insert into the slot. /// True if the item was successfully inserted, false otherwise. - bool PutInSlot(string slot, IItemComponent item); + bool Insert(string slot, IItemComponent item); + + /// + /// Checks whether an item can be put in the specified slot. + /// + /// The slot to check for. + /// The item to check for. + /// True if the item can be inserted into the specified slot. + bool CanInsert(string slot, IItemComponent item); /// /// Drops the item in a slot. /// /// The slot to drop the item from. /// True if an item was dropped, false otherwise. - bool DropSlot(string slot); + bool Drop(string slot); + + /// + /// Checks whether an item can be dropped from the specified slot. + /// + /// The slot to check for. + /// + /// True if there is an item in the slot and it can be dropped, false otherwise. + /// + bool CanDrop(string slot); /// /// Adds a new slot to this inventory component. @@ -57,10 +74,10 @@ namespace Content.Server.Interfaces.GameObjects void RemoveSlot(string slot); /// - /// + /// Checks whether a slot with the specified name exists. /// - /// - /// + /// The slot name to check. + /// True if the slot exists, false otherwise. bool HasSlot(string slot); }