2018-10-25 17:35:34 -07:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-17 11:03:07 +02:00
|
|
|
|
using Content.Server.GameObjects.Components.Items.Storage;
|
2020-07-06 14:27:03 -07:00
|
|
|
|
using Content.Server.GameObjects.EntitySystems.Click;
|
2020-08-13 22:17:12 +10:00
|
|
|
|
using JetBrains.Annotations;
|
2019-05-05 13:09:21 +02:00
|
|
|
|
using Robust.Server.GameObjects.EntitySystemMessages;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Server.Interfaces.Player;
|
|
|
|
|
|
using Robust.Shared.GameObjects.Systems;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-08-13 14:40:27 +02:00
|
|
|
|
namespace Content.Server.GameObjects.EntitySystems
|
2018-10-25 17:35:34 -07:00
|
|
|
|
{
|
2020-08-13 22:17:12 +10:00
|
|
|
|
[UsedImplicitly]
|
|
|
|
|
|
internal sealed class StorageSystem : EntitySystem
|
2018-10-25 17:35:34 -07:00
|
|
|
|
{
|
2020-11-27 11:00:49 +01:00
|
|
|
|
private readonly List<IPlayerSession> _sessionCache = new();
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
2020-02-19 17:08:59 -08:00
|
|
|
|
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
|
|
|
|
|
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
2018-11-11 11:32:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-25 17:35:34 -07:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
|
{
|
2020-08-13 22:17:12 +10:00
|
|
|
|
foreach (var component in ComponentManager.EntityQuery<ServerStorageComponent>())
|
2018-10-25 17:35:34 -07:00
|
|
|
|
{
|
2020-08-13 22:17:12 +10:00
|
|
|
|
CheckSubscribedEntities(component);
|
2018-11-11 11:32:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-02-19 14:39:00 -08:00
|
|
|
|
private static void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
2018-11-11 11:32:05 -08:00
|
|
|
|
{
|
2019-05-05 13:09:21 +02:00
|
|
|
|
var oldParentEntity = message.Container.Owner;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2019-05-05 13:09:21 +02:00
|
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
|
|
|
|
|
|
{
|
|
|
|
|
|
storageComp.HandleEntityMaybeRemoved(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-02-19 14:39:00 -08:00
|
|
|
|
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
|
2019-05-05 13:09:21 +02:00
|
|
|
|
{
|
|
|
|
|
|
var oldParentEntity = message.Container.Owner;
|
2018-11-11 11:32:05 -08:00
|
|
|
|
|
|
|
|
|
|
if (oldParentEntity.TryGetComponent(out ServerStorageComponent storageComp))
|
|
|
|
|
|
{
|
2019-05-05 13:09:21 +02:00
|
|
|
|
storageComp.HandleEntityMaybeInserted(message);
|
2018-11-11 11:32:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-13 22:17:12 +10:00
|
|
|
|
private void CheckSubscribedEntities(ServerStorageComponent storageComp)
|
2018-11-11 11:32:05 -08:00
|
|
|
|
{
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
// We have to cache the set of sessions because Unsubscribe modifies the original.
|
|
|
|
|
|
_sessionCache.Clear();
|
|
|
|
|
|
_sessionCache.AddRange(storageComp.SubscribedSessions);
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
if (_sessionCache.Count == 0)
|
|
|
|
|
|
return;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2020-08-13 22:17:12 +10:00
|
|
|
|
var storagePos = storageComp.Owner.Transform.WorldPosition;
|
|
|
|
|
|
var storageMap = storageComp.Owner.Transform.MapID;
|
2018-11-11 11:32:05 -08:00
|
|
|
|
|
|
|
|
|
|
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;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
if (storageMap != attachedEntity.Transform.MapID)
|
|
|
|
|
|
continue;
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
2018-11-11 11:32:05 -08:00
|
|
|
|
var distanceSquared = (storagePos - attachedEntity.Transform.WorldPosition).LengthSquared;
|
2019-05-16 15:51:26 +02:00
|
|
|
|
if (distanceSquared > InteractionSystem.InteractionRangeSquared)
|
2018-11-11 11:32:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
storageComp.UnsubscribeSession(session);
|
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|