Remove manual container ejection checking code.

This fixes map load when something is in a storage component.
This commit is contained in:
Pieter-Jan Briers
2019-05-05 13:09:21 +02:00
parent 23d8b92f94
commit b7d30f0870
5 changed files with 82 additions and 71 deletions

View File

@@ -7,6 +7,7 @@ using Content.Shared.Input;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
@@ -91,22 +92,6 @@ namespace Content.Server.GameObjects
return false;
}
/// <inheritdoc />
public void RemoveHandEntity(IEntity entity)
{
if (entity == null)
return;
foreach (var slot in hands.Values)
{
if (slot.ContainedEntity == entity)
{
slot.Remove(entity);
}
}
Dirty();
}
public ItemComponent GetHand(string index)
{
var slot = hands[index];
@@ -504,5 +489,26 @@ namespace Content.Server.GameObjects
}
}
}
public void HandleSlotModifiedMaybe(ContainerModifiedMessage message)
{
foreach (var container in hands.Values)
{
if (container != message.Container)
{
continue;
}
Dirty();
if (!message.Entity.TryGetComponent(out PhysicsComponent physics))
{
return;
}
// set velocity to zero
physics.LinearVelocity = Vector2.Zero;
return;
}
}
}
}

View File

@@ -18,6 +18,7 @@ using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.ViewVariables;
using Content.Server.GameObjects.Components;
using Robust.Server.GameObjects.EntitySystemMessages;
namespace Content.Server.GameObjects
{
@@ -82,14 +83,21 @@ namespace Content.Server.GameObjects
public bool Remove(IEntity toremove)
{
_ensureInitialCalculated();
if (storage.Remove(toremove))
{
Logger.InfoS("Storage", "Storage (UID {0}) had entity (UID {1}) removed from it.", Owner.Uid, toremove.Uid);
StorageUsed -= toremove.GetComponent<StoreableComponent>().ObjectSize;
UpdateClientInventories();
return true;
return storage.Remove(toremove);
}
return false;
internal void HandleEntityMaybeRemoved(EntRemovedFromContainerMessage message)
{
if (message.Container != storage)
{
return;
}
_ensureInitialCalculated();
Logger.DebugS("Storage", "Storage (UID {0}) had entity (UID {1}) removed from it.", Owner.Uid,
message.Entity.Uid);
StorageUsed -= message.Entity.GetComponent<StoreableComponent>().ObjectSize;
UpdateClientInventories();
}
/// <summary>
@@ -99,14 +107,21 @@ namespace Content.Server.GameObjects
/// <returns></returns>
public bool Insert(IEntity toinsert)
{
if (CanInsert(toinsert) && storage.Insert(toinsert))
{
Logger.InfoS("Storage", "Storage (UID {0}) had entity (UID {1}) inserted into it.", Owner.Uid, toinsert.Uid);
StorageUsed += toinsert.GetComponent<StoreableComponent>().ObjectSize;
UpdateClientInventories();
return true;
return CanInsert(toinsert) && storage.Insert(toinsert);
}
return false;
internal void HandleEntityMaybeInserted(EntInsertedIntoContainerMessage message)
{
if (message.Container != storage)
{
return;
}
_ensureInitialCalculated();
Logger.DebugS("Storage", "Storage (UID {0}) had entity (UID {1}) inserted into it.", Owner.Uid,
message.Entity.Uid);
StorageUsed += message.Entity.GetComponent<StoreableComponent>().ObjectSize;
UpdateClientInventories();
}
/// <summary>
@@ -328,6 +343,11 @@ namespace Content.Server.GameObjects
StorageUsed = 0;
if (storage == null)
{
return;
}
foreach (var entity in storage.ContainedEntities)
{
var item = entity.GetComponent<ItemComponent>();

View File

@@ -4,14 +4,14 @@ using Content.Server.GameObjects.Components.Stack;
using Content.Server.Interfaces.GameObjects;
using Content.Shared.Input;
using Content.Shared.Physics;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
@@ -21,7 +21,8 @@ using Robust.Shared.Players;
namespace Content.Server.GameObjects.EntitySystems
{
internal class HandsSystem : EntitySystem
[UsedImplicitly]
internal sealed class HandsSystem : EntitySystem
{
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
@@ -58,31 +59,16 @@ namespace Content.Server.GameObjects.EntitySystems
/// <inheritdoc />
public override void SubscribeEvents()
{
SubscribeEvent<EntParentChangedMessage>(HandleParented);
SubscribeEvent<EntRemovedFromContainerMessage>(HandleContainerModified);
SubscribeEvent<EntInsertedIntoContainerMessage>(HandleContainerModified);
}
private static void HandleParented(object sender, EntitySystemMessage args)
private static void HandleContainerModified(object sender, ContainerModifiedMessage args)
{
var msg = (EntParentChangedMessage) args;
// entity is no longer a child of OldParent, therefore it cannot be in the hand of the parent
if (msg.OldParent != null && msg.OldParent.IsValid() && msg.OldParent.Transform != msg.Entity.Transform.Parent && msg.OldParent.TryGetComponent(out IHandsComponent handsComp))
if (args.Container.Owner.TryGetComponent(out IHandsComponent handsComponent))
{
handsComp.RemoveHandEntity(msg.Entity);
handsComponent.HandleSlotModifiedMaybe(args);
}
if (msg.Entity.Deleted)
return;
// if item is in a container
if (msg.Entity.Transform.IsMapTransform)
return;
if (!msg.Entity.TryGetComponent(out PhysicsComponent physics))
return;
// set velocity to zero
physics.LinearVelocity = Vector2.Zero;
}
private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component)

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
@@ -22,7 +22,8 @@ namespace Content.Server.GameObjects.EntitySystems
{
base.SubscribeEvents();
SubscribeEvent<EntParentChangedMessage>(HandleParentChanged);
SubscribeEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
SubscribeEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
}
/// <inheritdoc />
@@ -34,22 +35,23 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
private static void HandleParentChanged(object sender, EntitySystemMessage message)
private static void HandleEntityRemovedFromContainer(object sender, EntRemovedFromContainerMessage message)
{
if(!(sender is IEntity childEntity))
return;
if(!(message is EntParentChangedMessage msg))
return;
var oldParentEntity = msg.OldParent;
if(oldParentEntity == null || !oldParentEntity.IsValid())
return;
var oldParentEntity = message.Container.Owner;
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
{
storageComp.Remove(childEntity);
storageComp.HandleEntityMaybeRemoved(message);
}
}
private static void HandleEntityInsertedIntoContainer(object sender, EntInsertedIntoContainerMessage message)
{
var oldParentEntity = message.Container.Owner;
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
{
storageComp.HandleEntityMaybeInserted(message);
}
}

View File

@@ -3,6 +3,7 @@ using Content.Server.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using System.Collections.Generic;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Shared.Map;
namespace Content.Server.Interfaces.GameObjects
@@ -26,12 +27,6 @@ namespace Content.Server.Interfaces.GameObjects
/// <returns>The item in the held, null if no item is held</returns>
ItemComponent GetHand(string index);
/// <summary>
/// If any hands are holding this entity, immediately remove the entity without dropping it.
/// </summary>
/// <param name="entity">Entity to be removed.</param>
void RemoveHandEntity(IEntity entity);
/// <summary>
/// Gets item held by the current active hand
/// </summary>
@@ -195,5 +190,7 @@ namespace Content.Server.Interfaces.GameObjects
/// <param name="index">The hand name to check.</param>
/// <returns>True if the hand exists, false otherwise.</returns>
bool HasHand(string index);
void HandleSlotModifiedMaybe(ContainerModifiedMessage message);
}
}