Fix dropping an item inside a locker placing it outside

This commit is contained in:
DrSmugleaf
2020-07-09 12:35:18 +02:00
parent 71f9e7aad2
commit 749a46de20

View File

@@ -11,6 +11,7 @@ using Content.Shared.GameObjects;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystemMessages; using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -33,50 +34,50 @@ namespace Content.Server.GameObjects
[Dependency] private readonly IEntitySystemManager _entitySystemManager; [Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649 #pragma warning restore 649
private string activeIndex; private string _activeIndex;
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public string ActiveIndex public string ActiveIndex
{ {
get => activeIndex; get => _activeIndex;
set set
{ {
if (!hands.ContainsKey(value)) if (!_hands.ContainsKey(value))
{ {
throw new ArgumentException($"No hand '{value}'"); throw new ArgumentException($"No hand '{value}'");
} }
activeIndex = value; _activeIndex = value;
Dirty(); Dirty();
} }
} }
[ViewVariables] private Dictionary<string, ContainerSlot> hands = new Dictionary<string, ContainerSlot>(); [ViewVariables] private readonly Dictionary<string, ContainerSlot> _hands = new Dictionary<string, ContainerSlot>();
[ViewVariables] private List<string> orderedHands = new List<string>(); [ViewVariables] private List<string> _orderedHands = new List<string>();
// Mostly arbitrary. // Mostly arbitrary.
public const float PICKUP_RANGE = 2; public const float PickupRange = 2;
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
// TODO: This does not serialize what objects are held. // TODO: This does not serialize what objects are held.
serializer.DataField(ref orderedHands, "hands", new List<string>(0)); serializer.DataField(ref _orderedHands, "hands", new List<string>(0));
if (serializer.Reading) if (serializer.Reading)
{ {
foreach (var handsname in orderedHands) foreach (var handsname in _orderedHands)
{ {
AddHand(handsname); AddHand(handsname);
} }
} }
serializer.DataField(ref activeIndex, "defaultHand", orderedHands.LastOrDefault()); serializer.DataField(ref _activeIndex, "defaultHand", _orderedHands.LastOrDefault());
} }
public IEnumerable<ItemComponent> GetAllHeldItems() public IEnumerable<ItemComponent> GetAllHeldItems()
{ {
foreach (var slot in hands.Values) foreach (var slot in _hands.Values)
{ {
if (slot.ContainedEntity != null) if (slot.ContainedEntity != null)
{ {
@@ -87,7 +88,7 @@ namespace Content.Server.GameObjects
public bool IsHolding(IEntity entity) public bool IsHolding(IEntity entity)
{ {
foreach (var slot in hands.Values) foreach (var slot in _hands.Values)
{ {
if (slot.ContainedEntity == entity) if (slot.ContainedEntity == entity)
{ {
@@ -99,7 +100,7 @@ namespace Content.Server.GameObjects
public ItemComponent GetHand(string index) public ItemComponent GetHand(string index)
{ {
var slot = hands[index]; var slot = _hands[index];
return slot.ContainedEntity?.GetComponent<ItemComponent>(); return slot.ContainedEntity?.GetComponent<ItemComponent>();
} }
@@ -111,7 +112,7 @@ namespace Content.Server.GameObjects
public IEnumerable<string> ActivePriorityEnumerable() public IEnumerable<string> ActivePriorityEnumerable()
{ {
yield return ActiveIndex; yield return ActiveIndex;
foreach (var hand in hands.Keys) foreach (var hand in _hands.Keys)
{ {
if (hand == ActiveIndex) if (hand == ActiveIndex)
{ {
@@ -142,7 +143,7 @@ namespace Content.Server.GameObjects
return fallback && PutInHand(item); return fallback && PutInHand(item);
} }
var slot = hands[index]; var slot = _hands[index];
Dirty(); Dirty();
var success = slot.Insert(item.Owner); var success = slot.Insert(item.Owner);
if (success) if (success)
@@ -176,13 +177,13 @@ namespace Content.Server.GameObjects
public bool CanPutInHand(ItemComponent item, string index) public bool CanPutInHand(ItemComponent item, string index)
{ {
var slot = hands[index]; var slot = _hands[index];
return slot.CanInsert(item.Owner); return slot.CanInsert(item.Owner);
} }
public string FindHand(IEntity entity) public string FindHand(IEntity entity)
{ {
foreach (var (index, slot) in hands) foreach (var (index, slot) in _hands)
{ {
if (slot.ContainedEntity == entity) if (slot.ContainedEntity == entity)
{ {
@@ -200,7 +201,7 @@ namespace Content.Server.GameObjects
return false; return false;
} }
var inventorySlot = hands[slot]; var inventorySlot = _hands[slot];
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>(); var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
if (!inventorySlot.Remove(inventorySlot.ContainedEntity)) if (!inventorySlot.Remove(inventorySlot.ContainedEntity))
@@ -211,9 +212,13 @@ namespace Content.Server.GameObjects
if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner)) if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner))
return false; 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; item.Owner.Transform.GridPosition = coords;
Dirty(); Dirty();
@@ -243,7 +248,7 @@ namespace Content.Server.GameObjects
return false; return false;
} }
var inventorySlot = hands[slot]; var inventorySlot = _hands[slot];
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>(); var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner)) if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner))
@@ -254,10 +259,15 @@ namespace Content.Server.GameObjects
return false; 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; item.Owner.Transform.GridPosition = Owner.Transform.GridPosition;
if (item.Owner.TryGetComponent<SpriteComponent>(out var spriteComponent)) if (item.Owner.TryGetComponent<SpriteComponent>(out var spriteComponent))
{ {
spriteComponent.RenderOrder = item.Owner.EntityManager.CurrentTick.Value; 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<ItemComponent>(); var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner)) if (doMobChecks && !_entitySystemManager.GetEntitySystem<InteractionSystem>().TryDroppedInteraction(Owner, item.Owner))
@@ -360,7 +370,14 @@ namespace Content.Server.GameObjects
/// </returns> /// </returns>
public bool CanDrop(string slot) 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); return inventorySlot.CanRemove(inventorySlot.ContainedEntity);
} }
@@ -372,17 +389,13 @@ namespace Content.Server.GameObjects
} }
var slot = ContainerManagerComponent.Create<ContainerSlot>(Name + "_" + index, Owner); var slot = ContainerManagerComponent.Create<ContainerSlot>(Name + "_" + index, Owner);
hands[index] = slot; _hands[index] = slot;
if (!orderedHands.Contains(index)) if (!_orderedHands.Contains(index))
{ {
orderedHands.Add(index); _orderedHands.Add(index);
}
if (ActiveIndex == null)
{
ActiveIndex = index;
} }
ActiveIndex ??= index;
Dirty(); Dirty();
} }
@@ -393,20 +406,13 @@ namespace Content.Server.GameObjects
throw new InvalidOperationException($"Hand '{index}' does not exist."); throw new InvalidOperationException($"Hand '{index}' does not exist.");
} }
hands[index].Shutdown(); //TODO verify this _hands[index].Shutdown(); //TODO verify this
hands.Remove(index); _hands.Remove(index);
orderedHands.Remove(index); _orderedHands.Remove(index);
if (index == ActiveIndex) if (index == ActiveIndex)
{ {
if (orderedHands.Count == 0) _activeIndex = _orderedHands.Count == 0 ? null : _orderedHands[0];
{
activeIndex = null;
}
else
{
activeIndex = orderedHands[0];
}
} }
Dirty(); Dirty();
@@ -414,7 +420,7 @@ namespace Content.Server.GameObjects
public bool HasHand(string index) public bool HasHand(string index)
{ {
return hands.ContainsKey(index); return _hands.ContainsKey(index);
} }
/// <summary> /// <summary>
@@ -424,8 +430,8 @@ namespace Content.Server.GameObjects
public override ComponentState GetComponentState() public override ComponentState GetComponentState()
{ {
var dict = new Dictionary<string, EntityUid>(hands.Count); var dict = new Dictionary<string, EntityUid>(_hands.Count);
foreach (var hand in hands) foreach (var hand in _hands)
{ {
if (hand.Value.ContainedEntity != null) if (hand.Value.ContainedEntity != null)
{ {
@@ -438,14 +444,14 @@ namespace Content.Server.GameObjects
public void SwapHands() public void SwapHands()
{ {
var index = orderedHands.FindIndex(x => x == ActiveIndex); var index = _orderedHands.FindIndex(x => x == ActiveIndex);
index++; index++;
if (index >= orderedHands.Count) if (index >= _orderedHands.Count)
{ {
index = 0; index = 0;
} }
ActiveIndex = orderedHands[index]; ActiveIndex = _orderedHands[index];
} }
public void ActivateItem() public void ActivateItem()
@@ -492,7 +498,7 @@ namespace Content.Server.GameObjects
case ClientAttackByInHandMsg msg: 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}'", Logger.WarningS("go.comp.hands", "Got a ClientAttackByInHandMsg with invalid hand index '{0}'",
msg.Index); msg.Index);
@@ -553,7 +559,7 @@ namespace Content.Server.GameObjects
public void HandleSlotModifiedMaybe(ContainerModifiedMessage message) public void HandleSlotModifiedMaybe(ContainerModifiedMessage message)
{ {
foreach (var container in hands.Values) foreach (var container in _hands.Values)
{ {
if (container != message.Container) if (container != message.Container)
{ {