Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/StorageSystem.cs
metalgearsloth 619386a04a Server EntitySystem cleanup (#1617)
* Server EntitySystem cleanup

I went after low-hanging fruit systems.

* Add / change to internal access modifiers to systems
* Use EntityQuery to get components instead
* Add sealed modifier to systems
* Remove unused imports
* Add jetbrains annotation for unused classes
* Removed some pragmas for dependencies

This should also fix a decent chunk of the server build warnings, at least the ones that matter.

* Also disposals

* Update Content.Server/GameObjects/EntitySystems/GravitySystem.cs

* Fix build

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
2020-08-13 14:17:12 +02: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.Interfaces.GameObjects.Components.Interaction
{
[UsedImplicitly]
internal sealed class StorageSystem : EntitySystem
{
private readonly List<IPlayerSession> _sessionCache = new List<IPlayerSession>();
/// <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);
}
}
}
}
}