From 749a46de20269640522f627df526bbf7d8aefb54 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Thu, 9 Jul 2020 12:35:18 +0200 Subject: [PATCH] Fix dropping an item inside a locker placing it outside --- .../Components/GUI/ServerHandsComponent.cs | 110 +++++++++--------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs b/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs index 9082ef3b43..0c5d7a21ef 100644 --- a/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs @@ -11,6 +11,7 @@ using Content.Shared.GameObjects; using Robust.Server.GameObjects; using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.EntitySystemMessages; +using Robust.Shared.Containers; using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Components; using Robust.Shared.Interfaces.GameObjects; @@ -33,50 +34,50 @@ namespace Content.Server.GameObjects [Dependency] private readonly IEntitySystemManager _entitySystemManager; #pragma warning restore 649 - private string activeIndex; + private string _activeIndex; [ViewVariables(VVAccess.ReadWrite)] public string ActiveIndex { - get => activeIndex; + get => _activeIndex; set { - if (!hands.ContainsKey(value)) + if (!_hands.ContainsKey(value)) { throw new ArgumentException($"No hand '{value}'"); } - activeIndex = value; + _activeIndex = value; Dirty(); } } - [ViewVariables] private Dictionary hands = new Dictionary(); - [ViewVariables] private List orderedHands = new List(); + [ViewVariables] private readonly Dictionary _hands = new Dictionary(); + [ViewVariables] private List _orderedHands = new List(); // Mostly arbitrary. - public const float PICKUP_RANGE = 2; + public const float PickupRange = 2; public override void ExposeData(ObjectSerializer serializer) { base.ExposeData(serializer); // TODO: This does not serialize what objects are held. - serializer.DataField(ref orderedHands, "hands", new List(0)); + serializer.DataField(ref _orderedHands, "hands", new List(0)); if (serializer.Reading) { - foreach (var handsname in orderedHands) + foreach (var handsname in _orderedHands) { AddHand(handsname); } } - serializer.DataField(ref activeIndex, "defaultHand", orderedHands.LastOrDefault()); + serializer.DataField(ref _activeIndex, "defaultHand", _orderedHands.LastOrDefault()); } public IEnumerable GetAllHeldItems() { - foreach (var slot in hands.Values) + foreach (var slot in _hands.Values) { if (slot.ContainedEntity != null) { @@ -87,7 +88,7 @@ namespace Content.Server.GameObjects public bool IsHolding(IEntity entity) { - foreach (var slot in hands.Values) + foreach (var slot in _hands.Values) { if (slot.ContainedEntity == entity) { @@ -99,7 +100,7 @@ namespace Content.Server.GameObjects public ItemComponent GetHand(string index) { - var slot = hands[index]; + var slot = _hands[index]; return slot.ContainedEntity?.GetComponent(); } @@ -111,7 +112,7 @@ namespace Content.Server.GameObjects public IEnumerable ActivePriorityEnumerable() { yield return ActiveIndex; - foreach (var hand in hands.Keys) + foreach (var hand in _hands.Keys) { if (hand == ActiveIndex) { @@ -142,7 +143,7 @@ namespace Content.Server.GameObjects return fallback && PutInHand(item); } - var slot = hands[index]; + var slot = _hands[index]; Dirty(); var success = slot.Insert(item.Owner); if (success) @@ -176,13 +177,13 @@ namespace Content.Server.GameObjects public bool CanPutInHand(ItemComponent item, string index) { - var slot = hands[index]; + var slot = _hands[index]; return slot.CanInsert(item.Owner); } public string FindHand(IEntity entity) { - foreach (var (index, slot) in hands) + foreach (var (index, slot) in _hands) { if (slot.ContainedEntity == entity) { @@ -200,7 +201,7 @@ namespace Content.Server.GameObjects return false; } - var inventorySlot = hands[slot]; + var inventorySlot = _hands[slot]; var item = inventorySlot.ContainedEntity.GetComponent(); if (!inventorySlot.Remove(inventorySlot.ContainedEntity)) @@ -211,9 +212,13 @@ namespace Content.Server.GameObjects if (doMobChecks && !_entitySystemManager.GetEntitySystem().TryDroppedInteraction(Owner, item.Owner)) return false; - item.RemovedFromSlot(); + if (ContainerHelpers.TryGetContainer(Owner, out var container) && + !container.Insert(item.Owner)) + { + return false; + } - // TODO: The item should be dropped to the container our owner is in, if any. + item.RemovedFromSlot(); item.Owner.Transform.GridPosition = coords; Dirty(); @@ -243,7 +248,7 @@ namespace Content.Server.GameObjects return false; } - var inventorySlot = hands[slot]; + var inventorySlot = _hands[slot]; var item = inventorySlot.ContainedEntity.GetComponent(); if (doMobChecks && !_entitySystemManager.GetEntitySystem().TryDroppedInteraction(Owner, item.Owner)) @@ -254,10 +259,15 @@ namespace Content.Server.GameObjects return false; } - item.RemovedFromSlot(); + if (ContainerHelpers.TryGetContainer(Owner, out var container) && + !container.Insert(item.Owner)) + { + return false; + } - // TODO: The item should be dropped to the container our owner is in, if any. + item.RemovedFromSlot(); item.Owner.Transform.GridPosition = Owner.Transform.GridPosition; + if (item.Owner.TryGetComponent(out var spriteComponent)) { spriteComponent.RenderOrder = item.Owner.EntityManager.CurrentTick.Value; @@ -301,7 +311,7 @@ namespace Content.Server.GameObjects } - var inventorySlot = hands[slot]; + var inventorySlot = _hands[slot]; var item = inventorySlot.ContainedEntity.GetComponent(); if (doMobChecks && !_entitySystemManager.GetEntitySystem().TryDroppedInteraction(Owner, item.Owner)) @@ -360,7 +370,14 @@ namespace Content.Server.GameObjects /// public bool CanDrop(string slot) { - var inventorySlot = hands[slot]; + var inventorySlot = _hands[slot]; + + if (ContainerHelpers.TryGetContainer(Owner, out var container) && + !container.CanInsert(inventorySlot.ContainedEntity)) + { + return false; + } + return inventorySlot.CanRemove(inventorySlot.ContainedEntity); } @@ -372,17 +389,13 @@ namespace Content.Server.GameObjects } var slot = ContainerManagerComponent.Create(Name + "_" + index, Owner); - hands[index] = slot; - if (!orderedHands.Contains(index)) + _hands[index] = slot; + if (!_orderedHands.Contains(index)) { - orderedHands.Add(index); - } - - if (ActiveIndex == null) - { - ActiveIndex = index; + _orderedHands.Add(index); } + ActiveIndex ??= index; Dirty(); } @@ -393,20 +406,13 @@ namespace Content.Server.GameObjects throw new InvalidOperationException($"Hand '{index}' does not exist."); } - hands[index].Shutdown(); //TODO verify this - hands.Remove(index); - orderedHands.Remove(index); + _hands[index].Shutdown(); //TODO verify this + _hands.Remove(index); + _orderedHands.Remove(index); if (index == ActiveIndex) { - if (orderedHands.Count == 0) - { - activeIndex = null; - } - else - { - activeIndex = orderedHands[0]; - } + _activeIndex = _orderedHands.Count == 0 ? null : _orderedHands[0]; } Dirty(); @@ -414,7 +420,7 @@ namespace Content.Server.GameObjects public bool HasHand(string index) { - return hands.ContainsKey(index); + return _hands.ContainsKey(index); } /// @@ -424,8 +430,8 @@ namespace Content.Server.GameObjects public override ComponentState GetComponentState() { - var dict = new Dictionary(hands.Count); - foreach (var hand in hands) + var dict = new Dictionary(_hands.Count); + foreach (var hand in _hands) { if (hand.Value.ContainedEntity != null) { @@ -438,14 +444,14 @@ namespace Content.Server.GameObjects public void SwapHands() { - var index = orderedHands.FindIndex(x => x == ActiveIndex); + var index = _orderedHands.FindIndex(x => x == ActiveIndex); index++; - if (index >= orderedHands.Count) + if (index >= _orderedHands.Count) { index = 0; } - ActiveIndex = orderedHands[index]; + ActiveIndex = _orderedHands[index]; } public void ActivateItem() @@ -492,7 +498,7 @@ namespace Content.Server.GameObjects case ClientAttackByInHandMsg msg: { - if (!hands.TryGetValue(msg.Index, out var slot)) + if (!_hands.TryGetValue(msg.Index, out var slot)) { Logger.WarningS("go.comp.hands", "Got a ClientAttackByInHandMsg with invalid hand index '{0}'", msg.Index); @@ -553,7 +559,7 @@ namespace Content.Server.GameObjects public void HandleSlotModifiedMaybe(ContainerModifiedMessage message) { - foreach (var container in hands.Values) + foreach (var container in _hands.Values) { if (container != message.Container) {