diff --git a/Content.Server/GameObjects/Components/Items/HandsComponent.cs b/Content.Server/GameObjects/Components/Items/HandsComponent.cs index 371cab4818..997e57bcba 100644 --- a/Content.Server/GameObjects/Components/Items/HandsComponent.cs +++ b/Content.Server/GameObjects/Components/Items/HandsComponent.cs @@ -1,9 +1,11 @@ using Content.Server.Interfaces.GameObjects; using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; +using SS14.Shared.Utility; using System.Collections.Generic; using System; using System.Linq; +using YamlDotNet.RepresentationModel; namespace Content.Server.GameObjects { @@ -32,10 +34,28 @@ namespace Content.Server.GameObjects public override void Initialize() { inventory = Owner.GetComponent(); + base.Initialize(); } - public IEnumerable GetAllHands() + public override void OnRemove() { + inventory = null; + base.OnRemove(); + } + + public override void LoadParameters(YamlMappingNode mapping) + { + foreach (var node in mapping.GetNode("hands")) + { + AddHand(node.AsString()); + } + base.LoadParameters(mapping); + } + + public IEnumerable GetAllHeldItems() + { + throw new NotImplementedException(); + /* foreach (var slot in hands.Values) { if (slot.Item != null) @@ -43,6 +63,7 @@ namespace Content.Server.GameObjects yield return slot.Item; } } + */ } public IItemComponent GetHand(string index) @@ -56,6 +77,8 @@ namespace Content.Server.GameObjects /// private IEnumerable ActivePriorityEnumerable() { + throw new NotImplementedException(); + /* yield return ActiveIndex; foreach (var hand in hands.Keys) { @@ -66,6 +89,7 @@ namespace Content.Server.GameObjects yield return hand; } + */ } public bool PutInHand(IItemComponent item) @@ -127,5 +151,37 @@ namespace Content.Server.GameObjects var slot = hands[index]; return slot.Item != null && slot.Owner.CanDrop(slot.Name); } + + public void AddHand(string index) + { + if (HasHand(index)) + { + throw new InvalidOperationException($"Hand '{index}' already exists."); + } + + var slot = inventory.AddSlot(HandSlotName(index)); + hands[index] = slot; + } + + public void RemoveHand(string index) + { + if (!HasHand(index)) + { + throw new InvalidOperationException($"Hand '{index}' does not exist."); + } + + inventory.RemoveSlot(HandSlotName(index)); + hands.Remove(index); + } + + public bool HasHand(string index) + { + return hands.ContainsKey(index); + } + + /// + /// Get the name of the slot passed to the inventory component. + /// + private string HandSlotName(string index) => $"_hand_{index}"; } } diff --git a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs index 6e206d311a..585235d948 100644 --- a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs +++ b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs @@ -2,10 +2,12 @@ using Content.Server.Interfaces.GameObjects; using SS14.Server.GameObjects; using SS14.Server.GameObjects.Components.Container; using SS14.Server.Interfaces.GameObjects; +using SS14.Shared.Utility; using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; using System; using System.Collections.Generic; +using YamlDotNet.RepresentationModel; namespace Content.Server.GameObjects { @@ -36,6 +38,18 @@ namespace Content.Server.GameObjects base.OnRemove(); } + public override void LoadParameters(YamlMappingNode mapping) + { + if (mapping.TryGetNode("slots", out var slotsNode)) + { + foreach (var node in slotsNode) + { + AddSlot(node.AsString()); + } + } + base.LoadParameters(mapping); + } + public IItemComponent Get(string slot) { return _GetSlot(slot).Item; @@ -106,14 +120,14 @@ namespace Content.Server.GameObjects return item != null && container.CanRemove(item.Owner); } - public void AddSlot(string slot) + public IInventorySlot AddSlot(string slot) { if (HasSlot(slot)) { throw new InvalidOperationException($"Slot '{slot}' already exists."); } - slots[slot] = new InventorySlot(slot, this); + return slots[slot] = new InventorySlot(slot, this); } public void RemoveSlot(string slot) diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs index 52a63baf05..0d8fc902d4 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs @@ -13,7 +13,7 @@ namespace Content.Server.Interfaces.GameObjects /// /// Enumerates over every held item. /// - IEnumerable GetAllHands(); + IEnumerable GetAllHeldItems(); /// /// Gets the item held by a hand. @@ -70,5 +70,30 @@ namespace Content.Server.Interfaces.GameObjects /// 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); + + /// + /// Adds a new hand to this hands component. + /// + /// The name of the hand to add. + /// + /// Thrown if a hand with specified name already exists. + /// + void AddHand(string index); + + /// + /// Removes a hand from this hands component. + /// + /// + /// If the hand contains an item, the item is dropped. + /// + /// The name of the hand to remove. + void RemoveHand(string index); + + /// + /// Checks whether a hand with the specified name exists. + /// + /// The hand name to check. + /// True if the hand exists, false otherwise. + bool HasHand(string index); } } diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs index 62fab52d9d..6fe70fc1b6 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IInventoryComponent.cs @@ -62,7 +62,7 @@ namespace Content.Server.Interfaces.GameObjects /// /// Thrown if the slot with specified name already exists. /// - void AddSlot(string slot); + IInventorySlot AddSlot(string slot); /// /// Removes a slot from this inventory component.