Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/StorageSystem.cs
DrSmugleaf 5c0cf1b1a0 Use 'new' expression in places where the type is evident for content (#2590)
* Content.Client

* Content.Benchmarks

* Content.IntegrationTests

* Content.Server

* Content.Server.Database

* Content.Shared

* Content.Tests

* Merge fixes

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2020-11-27 21:00:49 +11:00

85 lines
3.0 KiB
C#

using System.Collections.Generic;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.EntitySystems.Click;
using JetBrains.Annotations;
using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects.Systems;
namespace Content.Server.GameObjects.EntitySystems
{
[UsedImplicitly]
internal sealed class StorageSystem : EntitySystem
{
private readonly List<IPlayerSession> _sessionCache = new();
/// <inheritdoc />
public override void Initialize()
{
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
}
/// <inheritdoc />
public override void Update(float frameTime)
{
foreach (var component in ComponentManager.EntityQuery<ServerStorageComponent>())
{
CheckSubscribedEntities(component);
}
}
private static void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
{
var oldParentEntity = message.Container.Owner;
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
{
storageComp.HandleEntityMaybeRemoved(message);
}
}
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
{
var oldParentEntity = message.Container.Owner;
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
{
storageComp.HandleEntityMaybeInserted(message);
}
}
private void CheckSubscribedEntities(ServerStorageComponent storageComp)
{
// We have to cache the set of sessions because Unsubscribe modifies the original.
_sessionCache.Clear();
_sessionCache.AddRange(storageComp.SubscribedSessions);
if (_sessionCache.Count == 0)
return;
var storagePos = storageComp.Owner.Transform.WorldPosition;
var storageMap = storageComp.Owner.Transform.MapID;
foreach (var session in _sessionCache)
{
var attachedEntity = session.AttachedEntity;
// The component manages the set of sessions, so this invalid session should be removed soon.
if (attachedEntity == null || !attachedEntity.IsValid())
continue;
if (storageMap != attachedEntity.Transform.MapID)
continue;
var distanceSquared = (storagePos - attachedEntity.Transform.WorldPosition).LengthSquared;
if (distanceSquared > InteractionSystem.InteractionRangeSquared)
{
storageComp.UnsubscribeSession(session);
}
}
}
}
}