Storage Component ECS (#7530)
Co-authored-by: fishfish458 <fishfish458> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -1,39 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Interaction;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Coordinates;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Placeable;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Sound;
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Shared.Storage;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using System.Threading;
|
||||
|
||||
namespace Content.Server.Storage.Components
|
||||
{
|
||||
@@ -41,36 +10,36 @@ namespace Content.Server.Storage.Components
|
||||
/// Storage component for containing entities within this one, matches a UI on the client which shows stored entities
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IStorageComponent))]
|
||||
[ComponentReference(typeof(SharedStorageComponent))]
|
||||
public sealed class ServerStorageComponent : SharedStorageComponent, IInteractUsing, IActivate, IStorageComponent, IDestroyAct, IAfterInteract
|
||||
public sealed class ServerStorageComponent : SharedStorageComponent
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
|
||||
|
||||
private const string LoggerName = "Storage";
|
||||
public string LoggerName = "Storage";
|
||||
|
||||
public Container? Storage;
|
||||
|
||||
private readonly Dictionary<EntityUid, int> _sizeCache = new();
|
||||
public readonly Dictionary<EntityUid, int> SizeCache = new();
|
||||
|
||||
[DataField("occludesLight")]
|
||||
private bool _occludesLight = true;
|
||||
|
||||
[DataField("quickInsert")]
|
||||
private bool _quickInsert = false; // Can insert storables by "attacking" them with the storage entity
|
||||
public bool QuickInsert = false; // Can insert storables by "attacking" them with the storage entity
|
||||
|
||||
[DataField("clickInsert")]
|
||||
private bool _clickInsert = true; // Can insert stuff by clicking the storage entity with it
|
||||
public bool ClickInsert = true; // Can insert stuff by clicking the storage entity with it
|
||||
|
||||
[DataField("areaInsert")]
|
||||
private bool _areaInsert = false; // "Attacking" with the storage entity causes it to insert all nearby storables after a delay
|
||||
public bool AreaInsert = false; // "Attacking" with the storage entity causes it to insert all nearby storables after a delay
|
||||
|
||||
/// <summary>
|
||||
/// Token for interrupting area insert do after.
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CancelToken;
|
||||
|
||||
[DataField("areaInsertRadius")]
|
||||
private int _areaInsertRadius = 1;
|
||||
public int AreaInsertRadius = 1;
|
||||
|
||||
[DataField("whitelist")]
|
||||
private EntityWhitelist? _whitelist = null;
|
||||
public EntityWhitelist? Whitelist = null;
|
||||
[DataField("blacklist")]
|
||||
public EntityWhitelist? Blacklist = null;
|
||||
|
||||
@@ -81,19 +50,30 @@ namespace Content.Server.Storage.Components
|
||||
[DataField("popup")]
|
||||
public bool ShowPopup = true;
|
||||
|
||||
private bool _storageInitialCalculated;
|
||||
/// <summary>
|
||||
/// This storage has an open UI
|
||||
/// </summary>
|
||||
public bool IsOpen = false;
|
||||
public int StorageUsed;
|
||||
[DataField("capacity")]
|
||||
public int StorageCapacityMax = 10000;
|
||||
public readonly HashSet<IPlayerSession> SubscribedSessions = new();
|
||||
|
||||
[DataField("storageSoundCollection")]
|
||||
public SoundSpecifier StorageSoundCollection { get; set; } = new SoundCollectionSpecifier("storageRustle");
|
||||
[DataField("storageOpenSound")]
|
||||
public SoundSpecifier? StorageOpenSound { get; set; } = new SoundCollectionSpecifier("storageRustle");
|
||||
|
||||
[DataField("storageInsertSound")]
|
||||
public SoundSpecifier? StorageInsertSound { get; set; } = new SoundCollectionSpecifier("storageRustle");
|
||||
|
||||
[DataField("storageRemoveSound")]
|
||||
public SoundSpecifier? StorageRemoveSound { get; set; }
|
||||
[DataField("storageCloseSound")]
|
||||
public SoundSpecifier? StorageCloseSound { get; set; }
|
||||
|
||||
[ViewVariables]
|
||||
public override IReadOnlyList<EntityUid>? StoredEntities => Storage?.ContainedEntities;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("occludesLight")]
|
||||
public bool OccludesLight
|
||||
{
|
||||
get => _occludesLight;
|
||||
@@ -104,567 +84,10 @@ namespace Content.Server.Storage.Components
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStorageVisualization()
|
||||
{
|
||||
if (!_entityManager.TryGetComponent(Owner, out AppearanceComponent appearance))
|
||||
return;
|
||||
|
||||
bool open = SubscribedSessions.Count != 0;
|
||||
|
||||
appearance.SetData(StorageVisuals.Open, open);
|
||||
appearance.SetData(SharedBagOpenVisuals.BagState, open ? SharedBagState.Open : SharedBagState.Closed);
|
||||
|
||||
if (_entityManager.HasComponent<ItemCounterComponent>(Owner))
|
||||
appearance.SetData(StackVisuals.Hide, !open);
|
||||
}
|
||||
|
||||
private void EnsureInitialCalculated()
|
||||
{
|
||||
if (_storageInitialCalculated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RecalculateStorageUsed();
|
||||
|
||||
_storageInitialCalculated = true;
|
||||
}
|
||||
|
||||
private void RecalculateStorageUsed()
|
||||
{
|
||||
StorageUsed = 0;
|
||||
_sizeCache.Clear();
|
||||
|
||||
if (Storage == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in Storage.ContainedEntities)
|
||||
{
|
||||
var item = _entityManager.GetComponent<SharedItemComponent>(entity);
|
||||
StorageUsed += item.Size;
|
||||
_sizeCache.Add(entity, item.Size);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies if an entity can be stored and if it fits
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to check</param>
|
||||
/// <returns>true if it can be inserted, false otherwise</returns>
|
||||
public bool CanInsert(EntityUid entity)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (_entityManager.TryGetComponent(entity, out ServerStorageComponent? storage) &&
|
||||
storage.StorageCapacityMax >= StorageCapacityMax)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_entityManager.TryGetComponent(entity, out SharedItemComponent? store) &&
|
||||
store.Size > StorageCapacityMax - StorageUsed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_whitelist != null && !_whitelist.IsValid(entity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Blacklist != null && Blacklist.IsValid(entity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_entityManager.GetComponent<TransformComponent>(entity).Anchored)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts into the storage container
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to insert</param>
|
||||
/// <returns>true if the entity was inserted, false otherwise</returns>
|
||||
public bool Insert(EntityUid entity)
|
||||
{
|
||||
return CanInsert(entity) && Storage?.Insert(entity) == true;
|
||||
}
|
||||
|
||||
// neccesary for abstraction, should be deleted on complete storage ECS
|
||||
public override bool Remove(EntityUid entity)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
return Storage?.Remove(entity) == true;
|
||||
}
|
||||
|
||||
public void HandleEntityMaybeInserted(EntInsertedIntoContainerMessage message)
|
||||
{
|
||||
if (message.Container != Storage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PlaySoundCollection();
|
||||
EnsureInitialCalculated();
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) had entity (UID {message.Entity}) inserted into it.");
|
||||
|
||||
var size = 0;
|
||||
if (_entityManager.TryGetComponent(message.Entity, out SharedItemComponent? storable))
|
||||
size = storable.Size;
|
||||
|
||||
StorageUsed += size;
|
||||
_sizeCache[message.Entity] = size;
|
||||
|
||||
UpdateClientInventories();
|
||||
}
|
||||
|
||||
public void HandleEntityMaybeRemoved(EntRemovedFromContainerMessage message)
|
||||
{
|
||||
if (message.Container != Storage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnsureInitialCalculated();
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) had entity (UID {message.Entity}) removed from it.");
|
||||
|
||||
if (!_sizeCache.TryGetValue(message.Entity, out var size))
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"Removed entity {_entityManager.ToPrettyString(message.Entity)} without a cached size from storage {_entityManager.ToPrettyString(Owner)} at {_entityManager.GetComponent<TransformComponent>(Owner).MapPosition}");
|
||||
|
||||
RecalculateStorageUsed();
|
||||
return;
|
||||
}
|
||||
|
||||
StorageUsed -= size;
|
||||
|
||||
UpdateClientInventories();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an entity into storage from the player's active hand
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity from</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertHeldEntity(EntityUid player)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!_entityManager.TryGetComponent(player, out HandsComponent? hands) ||
|
||||
hands.ActiveHandEntity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var toInsert = hands.ActiveHandEntity;
|
||||
|
||||
var handSys = _sysMan.GetEntitySystem<SharedHandsSystem>();
|
||||
|
||||
if (!handSys.TryDrop(player, toInsert.Value, handsComp: hands))
|
||||
{
|
||||
Popup(player, "comp-storage-cant-insert");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Insert(toInsert.Value))
|
||||
{
|
||||
handSys.PickupOrDrop(player, toInsert.Value, handsComp: hands);
|
||||
Popup(player, "comp-storage-cant-insert");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an Entity (<paramref name="toInsert"/>) in the world into storage, informing <paramref name="player"/> if it fails.
|
||||
/// <paramref name="toInsert"/> is *NOT* held, see <see cref="PlayerInsertHeldEntity(Robust.Shared.GameObjects.EntityUid)"/>.
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity with</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertEntityInWorld(EntityUid player, EntityUid toInsert)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!Insert(toInsert))
|
||||
{
|
||||
Popup(player, "comp-storage-cant-insert");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the storage UI for an entity
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to open the UI for</param>
|
||||
public void OpenStorageUI(EntityUid entity)
|
||||
{
|
||||
PlaySoundCollection();
|
||||
EnsureInitialCalculated();
|
||||
|
||||
var userSession = _entityManager.GetComponent<ActorComponent>(entity).PlayerSession;
|
||||
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) \"used\" by player session (UID {userSession.AttachedEntity}).");
|
||||
|
||||
SubscribeSession(userSession);
|
||||
_entityManager.EntityNetManager?.SendSystemNetworkMessage(new OpenStorageUIEvent(Owner), userSession.ConnectedClient);
|
||||
UpdateClientInventory(userSession);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the storage UI on all subscribed actors, informing them of the state of the container.
|
||||
/// </summary>
|
||||
private void UpdateClientInventories()
|
||||
{
|
||||
foreach (var session in SubscribedSessions)
|
||||
{
|
||||
UpdateClientInventory(session);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates storage UI on a client, informing them of the state of the container.
|
||||
/// </summary>
|
||||
/// <param name="session">The client to be updated</param>
|
||||
private void UpdateClientInventory(IPlayerSession session)
|
||||
{
|
||||
if (session.AttachedEntity == null)
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) detected no attached entity in player session (UID {session.AttachedEntity}).");
|
||||
|
||||
UnsubscribeSession(session);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Storage == null)
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"{nameof(UpdateClientInventory)} called with null {nameof(Storage)}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (StoredEntities == null)
|
||||
{
|
||||
Logger.WarningS(LoggerName, $"{nameof(UpdateClientInventory)} called with null {nameof(StoredEntities)}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var stored = StoredEntities.Select(e => e).ToArray();
|
||||
|
||||
_entityManager.EntityNetManager?.SendSystemNetworkMessage(new StorageHeldItemsEvent(Owner, StorageCapacityMax,StorageUsed, stored), session.ConnectedClient);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a session to the update list.
|
||||
/// </summary>
|
||||
/// <param name="session">The session to add</param>
|
||||
private void SubscribeSession(IPlayerSession session)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (!SubscribedSessions.Contains(session))
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) subscribed player session (UID {session.AttachedEntity}).");
|
||||
|
||||
session.PlayerStatusChanged += HandlePlayerSessionChangeEvent;
|
||||
SubscribedSessions.Add(session);
|
||||
}
|
||||
|
||||
_entityManager.EnsureComponent<ActiveStorageComponent>(Owner);
|
||||
if (SubscribedSessions.Count == 1)
|
||||
UpdateStorageVisualization();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a session from the update list.
|
||||
/// </summary>
|
||||
/// <param name="session">The session to remove</param>
|
||||
public void UnsubscribeSession(IPlayerSession session)
|
||||
{
|
||||
if (SubscribedSessions.Contains(session))
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) unsubscribed player session (UID {session.AttachedEntity}).");
|
||||
|
||||
SubscribedSessions.Remove(session);
|
||||
_entityManager.EntityNetManager?.SendSystemNetworkMessage(new CloseStorageUIEvent(Owner),
|
||||
session.ConnectedClient);
|
||||
}
|
||||
|
||||
CloseNestedInterfaces(session);
|
||||
|
||||
if (SubscribedSessions.Count == 0)
|
||||
{
|
||||
UpdateStorageVisualization();
|
||||
|
||||
if (_entityManager.HasComponent<ActiveStorageComponent>(Owner))
|
||||
{
|
||||
_entityManager.RemoveComponent<ActiveStorageComponent>(Owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the user has nested-UIs open (e.g., PDA UI open when pda is in a backpack), close them.
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
public void CloseNestedInterfaces(IPlayerSession session)
|
||||
{
|
||||
if (StoredEntities == null)
|
||||
return;
|
||||
|
||||
foreach (var entity in StoredEntities)
|
||||
{
|
||||
if (_entityManager.TryGetComponent(entity, out ServerStorageComponent storageComponent))
|
||||
{
|
||||
DebugTools.Assert(storageComponent != this, $"Storage component contains itself!? Entity: {Owner}");
|
||||
storageComponent.UnsubscribeSession(session);
|
||||
}
|
||||
|
||||
if (_entityManager.TryGetComponent(entity, out ServerUserInterfaceComponent uiComponent))
|
||||
{
|
||||
foreach (var ui in uiComponent.Interfaces)
|
||||
{
|
||||
ui.Close(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePlayerSessionChangeEvent(object? obj, SessionStatusEventArgs sessionStatus)
|
||||
{
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) handled a status change in player session (UID {sessionStatus.Session.AttachedEntity}).");
|
||||
|
||||
if (sessionStatus.NewStatus != SessionStatus.InGame)
|
||||
{
|
||||
UnsubscribeSession(sessionStatus.Session);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
Storage = Owner.EnsureContainer<Container>("storagebase");
|
||||
Storage.OccludesLight = _occludesLight;
|
||||
UpdateStorageVisualization();
|
||||
EnsureInitialCalculated();
|
||||
}
|
||||
|
||||
public void HandleRemoveEntity(RemoveEntityEvent remove, ICommonSession session)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (session.AttachedEntity is not {Valid: true} player)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var ownerTransform = _entityManager.GetComponent<TransformComponent>(Owner);
|
||||
var playerTransform = _entityManager.GetComponent<TransformComponent>(player);
|
||||
|
||||
if (!playerTransform.Coordinates.InRange(_entityManager, ownerTransform.Coordinates, 2) ||
|
||||
Owner.IsInContainer() && !playerTransform.ContainsEntity(ownerTransform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!remove.EntityUid.Valid || Storage?.Contains(remove.EntityUid) == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_sysMan.GetEntitySystem<SharedHandsSystem>().TryPickupAnyHand(player, remove.EntityUid);
|
||||
}
|
||||
|
||||
public void HandleInsertEntity(ICommonSession session)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
|
||||
if (session.AttachedEntity is not {Valid: true} player)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EntitySystem.Get<SharedInteractionSystem>().InRangeUnobstructed(player, Owner, popup: ShowPopup))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerInsertHeldEntity(player);
|
||||
}
|
||||
|
||||
public void HandleCloseUI(ICommonSession session)
|
||||
{
|
||||
if (session is not IPlayerSession playerSession)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UnsubscribeSession(playerSession);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts storable entities into this storage container if possible, otherwise return to the hand of the user
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!_clickInsert)
|
||||
return false;
|
||||
Logger.DebugS(LoggerName, $"Storage (UID {Owner}) attacked by user (UID {eventArgs.User}) with entity (UID {eventArgs.Using}).");
|
||||
|
||||
if (_entityManager.HasComponent<PlaceableSurfaceComponent>(Owner))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return PlayerInsertHeldEntity(eventArgs.User);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to open the storage UI
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
EnsureInitialCalculated();
|
||||
OpenStorageUI(eventArgs.User);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows a user to pick up entities by clicking them, or pick up all entities in a certain radius
|
||||
/// arround a click.
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.CanReach) return false;
|
||||
|
||||
// Pick up all entities in a radius around the clicked location.
|
||||
// The last half of the if is because carpets exist and this is terrible
|
||||
if (_areaInsert && (eventArgs.Target == null || !_entityManager.HasComponent<SharedItemComponent>(eventArgs.Target.Value)))
|
||||
{
|
||||
var validStorables = new List<EntityUid>();
|
||||
foreach (var entity in EntitySystem.Get<EntityLookupSystem>().GetEntitiesInRange(eventArgs.ClickLocation, _areaInsertRadius, LookupFlags.None))
|
||||
{
|
||||
if (entity.IsInContainer()
|
||||
|| entity == eventArgs.User
|
||||
|| !_entityManager.HasComponent<SharedItemComponent>(entity)
|
||||
|| !EntitySystem.Get<InteractionSystem>().InRangeUnobstructed(eventArgs.User, entity))
|
||||
continue;
|
||||
validStorables.Add(entity);
|
||||
}
|
||||
|
||||
//If there's only one then let's be generous
|
||||
if (validStorables.Count > 1)
|
||||
{
|
||||
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
|
||||
var doAfterArgs = new DoAfterEventArgs(eventArgs.User, 0.2f * validStorables.Count, CancellationToken.None, Owner)
|
||||
{
|
||||
BreakOnStun = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnUserMove = true,
|
||||
NeedHand = true,
|
||||
};
|
||||
var result = await doAfterSystem.WaitDoAfter(doAfterArgs);
|
||||
if (result != DoAfterStatus.Finished) return true;
|
||||
}
|
||||
|
||||
var successfullyInserted = new List<EntityUid>();
|
||||
var successfullyInsertedPositions = new List<EntityCoordinates>();
|
||||
foreach (var entity in validStorables)
|
||||
{
|
||||
// Check again, situation may have changed for some entities, but we'll still pick up any that are valid
|
||||
if (entity.IsInContainer()
|
||||
|| entity == eventArgs.User
|
||||
|| !_entityManager.HasComponent<SharedItemComponent>(entity))
|
||||
continue;
|
||||
var position = EntityCoordinates.FromMap(_entityManager.GetComponent<TransformComponent>(Owner).Parent?.Owner ?? Owner, _entityManager.GetComponent<TransformComponent>(entity).MapPosition);
|
||||
if (PlayerInsertEntityInWorld(eventArgs.User, entity))
|
||||
{
|
||||
successfullyInserted.Add(entity);
|
||||
successfullyInsertedPositions.Add(position);
|
||||
}
|
||||
}
|
||||
|
||||
// If we picked up atleast one thing, play a sound and do a cool animation!
|
||||
if (successfullyInserted.Count > 0)
|
||||
{
|
||||
PlaySoundCollection();
|
||||
_entityManager.EntityNetManager?.SendSystemNetworkMessage(
|
||||
new AnimateInsertingEntitiesEvent(Owner, successfullyInserted, successfullyInsertedPositions));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Pick up the clicked entity
|
||||
else if (_quickInsert)
|
||||
{
|
||||
if (eventArgs.Target is not {Valid: true} target)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (target.IsInContainer()
|
||||
|| target == eventArgs.User
|
||||
|| !_entityManager.HasComponent<SharedItemComponent>(target))
|
||||
return false;
|
||||
var position = EntityCoordinates.FromMap(
|
||||
_entityManager.GetComponent<TransformComponent>(Owner).Parent?.Owner ?? Owner,
|
||||
_entityManager.GetComponent<TransformComponent>(target).MapPosition);
|
||||
if (PlayerInsertEntityInWorld(eventArgs.User, target))
|
||||
{
|
||||
_entityManager.EntityNetManager?.SendSystemNetworkMessage(new AnimateInsertingEntitiesEvent(Owner,
|
||||
new List<EntityUid> { target },
|
||||
new List<EntityCoordinates> { position }));
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
var storedEntities = StoredEntities?.ToList();
|
||||
|
||||
if (storedEntities == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entity in storedEntities)
|
||||
{
|
||||
Remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private void Popup(EntityUid player, string message)
|
||||
{
|
||||
if (!ShowPopup) return;
|
||||
|
||||
Owner.PopupMessage(player, Loc.GetString(message));
|
||||
}
|
||||
|
||||
private void PlaySoundCollection()
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), StorageSoundCollection.GetSound(), Owner, AudioParams.Default);
|
||||
}
|
||||
}
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class ActiveStorageComponent : Component {}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Content.Server.Storage.Components;
|
||||
using Robust.Shared.Random;
|
||||
using System.Linq;
|
||||
using Content.Shared.Storage;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems;
|
||||
@@ -10,8 +8,10 @@ public sealed partial class StorageSystem
|
||||
private void OnStorageFillMapInit(EntityUid uid, StorageFillComponent component, MapInitEvent args)
|
||||
{
|
||||
if (component.Contents.Count == 0) return;
|
||||
|
||||
if (!TryComp<IStorageComponent>(uid, out var storage))
|
||||
// ServerStorageComponent needs to rejoin IStorageComponent when other storage components are ECS'd
|
||||
TryComp<IStorageComponent>(uid, out var storage);
|
||||
TryComp<ServerStorageComponent>(uid, out var serverStorageComp);
|
||||
if (storage == null && serverStorageComp == null)
|
||||
{
|
||||
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({uid})");
|
||||
return;
|
||||
@@ -24,7 +24,12 @@ public sealed partial class StorageSystem
|
||||
{
|
||||
var ent = EntityManager.SpawnEntity(item, coordinates);
|
||||
|
||||
if (storage.Insert(ent)) continue;
|
||||
// handle depending on storage component, again this should be unified after ECS
|
||||
if (storage != null && storage.Insert(ent))
|
||||
continue;
|
||||
|
||||
if (serverStorageComp != null && Insert(uid, ent, serverStorageComp))
|
||||
continue;
|
||||
|
||||
Logger.ErrorS("storage", $"Tried to StorageFill {item} inside {uid} but can't.");
|
||||
EntityManager.DeleteEntity(ent);
|
||||
|
||||
@@ -14,6 +14,21 @@ using Robust.Shared.Containers;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
using System.Threading;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Interaction;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Placeable;
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Server.Containers;
|
||||
using Content.Server.Popups;
|
||||
using static Content.Shared.Storage.SharedStorageComponent;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
@@ -22,51 +37,50 @@ namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly ContainerSystem _containerSystem = default!;
|
||||
[Dependency] private readonly DisposalUnitSystem _disposalSystem = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookupSystem = default!;
|
||||
[Dependency] private readonly InteractionSystem _interactionSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _sharedHandsSystem = default!;
|
||||
[Dependency] private readonly SharedInteractionSystem _sharedInteractionSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
||||
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
||||
SubscribeLocalEvent<ServerStorageComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<UtilityVerb>>(AddTransferVerbs);
|
||||
SubscribeLocalEvent<ServerStorageComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<ServerStorageComponent, ActivateInWorldEvent>(OnActivate);
|
||||
SubscribeLocalEvent<ServerStorageComponent, AfterInteractEvent>(AfterInteract);
|
||||
SubscribeLocalEvent<ServerStorageComponent, DestructionEventArgs>(OnDestroy);
|
||||
SubscribeLocalEvent<ServerStorageComponent, StorageRemoveItemMessage>(OnRemoveItemMessage);
|
||||
SubscribeLocalEvent<ServerStorageComponent, StorageInsertItemMessage>(OnInsertItemMessage);
|
||||
SubscribeLocalEvent<ServerStorageComponent, BoundUIOpenedEvent>(OnBoundUIOpen);
|
||||
SubscribeLocalEvent<ServerStorageComponent, BoundUIClosedEvent>(OnBoundUIClosed);
|
||||
SubscribeLocalEvent<ServerStorageComponent, EntRemovedFromContainerMessage>(OnStorageItemRemoved);
|
||||
|
||||
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
|
||||
SubscribeLocalEvent<EntityStorageComponent, RelayMovementEntityEvent>(OnRelayMovement);
|
||||
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
|
||||
SubscribeLocalEvent<ServerStorageComponent, GetVerbsEvent<UtilityVerb>>(AddTransferVerbs);
|
||||
|
||||
SubscribeLocalEvent<StorageFillComponent, MapInitEvent>(OnStorageFillMapInit);
|
||||
|
||||
SubscribeNetworkEvent<RemoveEntityEvent>(OnRemoveEntity);
|
||||
SubscribeNetworkEvent<InsertEntityEvent>(OnInsertEntity);
|
||||
SubscribeNetworkEvent<CloseStorageUIEvent>(OnCloseStorageUI);
|
||||
}
|
||||
|
||||
private void OnRemoveEntity(RemoveEntityEvent ev, EntitySessionEventArgs args)
|
||||
private void OnComponentInit(EntityUid uid, ServerStorageComponent storageComp, ComponentInit args)
|
||||
{
|
||||
if (TryComp<ServerStorageComponent>(ev.Storage, out var storage))
|
||||
{
|
||||
storage.HandleRemoveEntity(ev, args.SenderSession);
|
||||
}
|
||||
}
|
||||
base.Initialize();
|
||||
|
||||
private void OnInsertEntity(InsertEntityEvent ev, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp<ServerStorageComponent>(ev.Storage, out var storage))
|
||||
{
|
||||
storage.HandleInsertEntity(args.SenderSession);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCloseStorageUI(CloseStorageUIEvent ev, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp<ServerStorageComponent>(ev.Storage, out var storage))
|
||||
{
|
||||
storage.HandleCloseUI(args.SenderSession);
|
||||
}
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
storageComp.Storage = _containerSystem.EnsureContainer<Container>(uid, "storagebase");
|
||||
storageComp.Storage.OccludesLight = storageComp.OccludesLight;
|
||||
UpdateStorageVisualization(uid, storageComp);
|
||||
RecalculateStorageUsed(storageComp);
|
||||
UpdateStorageUI(uid, storageComp);
|
||||
}
|
||||
|
||||
private void OnRelayMovement(EntityUid uid, EntityStorageComponent component, RelayMovementEntityEvent args)
|
||||
@@ -76,22 +90,12 @@ namespace Content.Server.Storage.EntitySystems
|
||||
|
||||
if (_gameTiming.CurTime <
|
||||
component.LastInternalOpenAttempt + EntityStorageComponent.InternalOpenAttemptDelay)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
component.LastInternalOpenAttempt = _gameTiming.CurTime;
|
||||
component.TryOpenStorage(args.Entity);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var (_, component) in EntityManager.EntityQuery<ActiveStorageComponent, ServerStorageComponent>())
|
||||
{
|
||||
CheckSubscribedEntities(component);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddToggleOpenVerb(EntityUid uid, EntityStorageComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
{
|
||||
@@ -121,19 +125,18 @@ namespace Content.Server.Storage.EntitySystems
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
return;
|
||||
|
||||
if (EntityManager.TryGetComponent(uid, out LockComponent? lockComponent) && lockComponent.Locked)
|
||||
if (TryComp<LockComponent>(uid, out var lockComponent) && lockComponent.Locked)
|
||||
return;
|
||||
|
||||
// Get the session for the user
|
||||
var session = EntityManager.GetComponentOrNull<ActorComponent>(args.User)?.PlayerSession;
|
||||
if (session == null)
|
||||
if (!TryComp<ActorComponent>(args.User, out var actor) || actor?.PlayerSession == null)
|
||||
return;
|
||||
|
||||
// Does this player currently have the storage UI open?
|
||||
var uiOpen = component.SubscribedSessions.Contains(session);
|
||||
bool uiOpen = _uiSystem.SessionHasOpenUi(uid, StorageUiKey.Key, actor.PlayerSession);
|
||||
|
||||
ActivationVerb verb = new();
|
||||
verb.Act = () => component.OpenStorageUI(args.User);
|
||||
verb.Act = () => OpenStorageUI(uid, args.User, component);
|
||||
if (uiOpen)
|
||||
{
|
||||
verb.Text = Loc.GetString("verb-common-close-ui");
|
||||
@@ -187,6 +190,236 @@ namespace Content.Server.Storage.EntitySystems
|
||||
args.Verbs.Add(dispose);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Inserts storable entities into this storage container if possible, otherwise return to the hand of the user
|
||||
/// </summary>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
private void OnInteractUsing(EntityUid uid, ServerStorageComponent storageComp, InteractUsingEvent args)
|
||||
{
|
||||
if (!storageComp.ClickInsert)
|
||||
return;
|
||||
|
||||
Logger.DebugS(storageComp.LoggerName, $"Storage (UID {uid}) attacked by user (UID {args.User}) with entity (UID {args.Used}).");
|
||||
|
||||
if (HasComp<PlaceableSurfaceComponent>(uid))
|
||||
return;
|
||||
|
||||
PlayerInsertHeldEntity(uid, args.User, storageComp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to open the storage UI
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void OnActivate(EntityUid uid, ServerStorageComponent storageComp, ActivateInWorldEvent args)
|
||||
{
|
||||
if (!TryComp<ActorComponent>(args.User, out var actor))
|
||||
return;
|
||||
|
||||
OpenStorageUI(uid, args.User, storageComp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows a user to pick up entities by clicking them, or pick up all entities in a certain radius
|
||||
/// around a click.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async void AfterInteract(EntityUid uid, ServerStorageComponent storageComp, AfterInteractEvent eventArgs)
|
||||
{
|
||||
if (!eventArgs.CanReach) return;
|
||||
|
||||
if (storageComp.CancelToken != null)
|
||||
{
|
||||
storageComp.CancelToken.Cancel();
|
||||
storageComp.CancelToken = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Pick up all entities in a radius around the clicked location.
|
||||
// The last half of the if is because carpets exist and this is terrible
|
||||
if (storageComp.AreaInsert && (eventArgs.Target == null || !HasComp<SharedItemComponent>(eventArgs.Target.Value)))
|
||||
{
|
||||
var validStorables = new List<EntityUid>();
|
||||
foreach (var entity in _entityLookupSystem.GetEntitiesInRange(eventArgs.ClickLocation, storageComp.AreaInsertRadius, LookupFlags.None))
|
||||
{
|
||||
if (entity == eventArgs.User
|
||||
|| !HasComp<SharedItemComponent>(entity)
|
||||
|| !_interactionSystem.InRangeUnobstructed(eventArgs.User, entity))
|
||||
continue;
|
||||
|
||||
validStorables.Add(entity);
|
||||
}
|
||||
|
||||
//If there's only one then let's be generous
|
||||
if (validStorables.Count > 1)
|
||||
{
|
||||
storageComp.CancelToken = new CancellationTokenSource();
|
||||
var doAfterArgs = new DoAfterEventArgs(eventArgs.User, 0.2f * validStorables.Count, storageComp.CancelToken.Token, uid)
|
||||
{
|
||||
BreakOnStun = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnUserMove = true,
|
||||
NeedHand = true,
|
||||
};
|
||||
|
||||
await _doAfterSystem.WaitDoAfter(doAfterArgs);
|
||||
}
|
||||
|
||||
// TODO: Make it use the event DoAfter
|
||||
var successfullyInserted = new List<EntityUid>();
|
||||
var successfullyInsertedPositions = new List<EntityCoordinates>();
|
||||
foreach (var entity in validStorables)
|
||||
{
|
||||
// Check again, situation may have changed for some entities, but we'll still pick up any that are valid
|
||||
if (_containerSystem.IsEntityInContainer(entity)
|
||||
|| entity == eventArgs.User
|
||||
|| !HasComp<SharedItemComponent>(entity))
|
||||
continue;
|
||||
|
||||
if (TryComp<TransformComponent>(uid, out var transformOwner) && TryComp<TransformComponent>(entity, out var transformEnt))
|
||||
{
|
||||
var position = EntityCoordinates.FromMap(transformOwner.Parent?.Owner ?? uid, transformEnt.MapPosition);
|
||||
|
||||
if (PlayerInsertEntityInWorld(uid, eventArgs.User, entity, storageComp))
|
||||
{
|
||||
successfullyInserted.Add(entity);
|
||||
successfullyInsertedPositions.Add(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we picked up atleast one thing, play a sound and do a cool animation!
|
||||
if (successfullyInserted.Count > 0)
|
||||
{
|
||||
if (storageComp.StorageInsertSound is not null)
|
||||
SoundSystem.Play(Filter.Pvs(uid, entityManager: EntityManager), storageComp.StorageInsertSound.GetSound(), uid, AudioParams.Default);
|
||||
RaiseNetworkEvent(new AnimateInsertingEntitiesEvent(uid, successfullyInserted, successfullyInsertedPositions));
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Pick up the clicked entity
|
||||
else if (storageComp.QuickInsert)
|
||||
{
|
||||
if (eventArgs.Target is not {Valid: true} target)
|
||||
return;
|
||||
|
||||
if (_containerSystem.IsEntityInContainer(target)
|
||||
|| target == eventArgs.User
|
||||
|| !HasComp<SharedItemComponent>(target))
|
||||
return;
|
||||
|
||||
if (TryComp<TransformComponent>(uid, out var transformOwner) && TryComp<TransformComponent>(target, out var transformEnt))
|
||||
{
|
||||
var parent = transformOwner.ParentUid;
|
||||
|
||||
var position = EntityCoordinates.FromMap(
|
||||
parent.IsValid() ? parent : uid,
|
||||
transformEnt.MapPosition);
|
||||
if (PlayerInsertEntityInWorld(uid, eventArgs.User, target, storageComp))
|
||||
{
|
||||
RaiseNetworkEvent(new AnimateInsertingEntitiesEvent(uid,
|
||||
new List<EntityUid> { target },
|
||||
new List<EntityCoordinates> { position }));
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private void OnDestroy(EntityUid uid, ServerStorageComponent storageComp, DestructionEventArgs args)
|
||||
{
|
||||
var storedEntities = storageComp.StoredEntities?.ToList();
|
||||
|
||||
if (storedEntities == null)
|
||||
return;
|
||||
|
||||
foreach (var entity in storedEntities)
|
||||
{
|
||||
RemoveAndDrop(uid, entity, storageComp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRemoveItemMessage(EntityUid uid, ServerStorageComponent storageComp, StorageRemoveItemMessage args)
|
||||
{
|
||||
if (args.Session.AttachedEntity == null)
|
||||
return;
|
||||
|
||||
HandleRemoveEntity(uid, args.Session.AttachedEntity.Value, args.InteractedItemUID, storageComp);
|
||||
}
|
||||
|
||||
private void OnInsertItemMessage(EntityUid uid, ServerStorageComponent storageComp, StorageInsertItemMessage args)
|
||||
{
|
||||
if (args.Session.AttachedEntity == null)
|
||||
return;
|
||||
|
||||
PlayerInsertHeldEntity(uid, args.Session.AttachedEntity.Value, storageComp);
|
||||
}
|
||||
|
||||
private void OnBoundUIOpen(EntityUid uid, ServerStorageComponent storageComp, BoundUIOpenedEvent args)
|
||||
{
|
||||
if (!storageComp.IsOpen)
|
||||
{
|
||||
storageComp.IsOpen = true;
|
||||
UpdateStorageVisualization(uid, storageComp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBoundUIClosed(EntityUid uid, ServerStorageComponent storageComp, BoundUIClosedEvent args)
|
||||
{
|
||||
if (TryComp<ActorComponent>(args.Session.AttachedEntity, out var actor) && actor?.PlayerSession != null)
|
||||
CloseNestedInterfaces(uid, actor.PlayerSession, storageComp);
|
||||
|
||||
// If UI is closed for everyone
|
||||
if (!_uiSystem.IsUiOpen(uid, args.UiKey))
|
||||
{
|
||||
storageComp.IsOpen = false;
|
||||
UpdateStorageVisualization(uid, storageComp);
|
||||
|
||||
if (storageComp.StorageCloseSound is not null)
|
||||
SoundSystem.Play(Filter.Pvs(uid, entityManager: EntityManager), storageComp.StorageCloseSound.GetSound(), uid, AudioParams.Default);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStorageItemRemoved(EntityUid uid, ServerStorageComponent storageComp, EntRemovedFromContainerMessage args)
|
||||
{
|
||||
RecalculateStorageUsed(storageComp);
|
||||
UpdateStorageUI(uid, storageComp);
|
||||
}
|
||||
|
||||
private void UpdateStorageVisualization(EntityUid uid, ServerStorageComponent storageComp)
|
||||
{
|
||||
if (!TryComp<AppearanceComponent>(uid, out var appearance))
|
||||
return;
|
||||
|
||||
appearance.SetData(StorageVisuals.Open, storageComp.IsOpen);
|
||||
appearance.SetData(SharedBagOpenVisuals.BagState, storageComp.IsOpen ? SharedBagState.Open : SharedBagState.Closed);
|
||||
|
||||
if (HasComp<ItemCounterComponent>(uid))
|
||||
appearance.SetData(StackVisuals.Hide, !storageComp.IsOpen);
|
||||
}
|
||||
|
||||
private void RecalculateStorageUsed(ServerStorageComponent storageComp)
|
||||
{
|
||||
storageComp.StorageUsed = 0;
|
||||
storageComp.SizeCache.Clear();
|
||||
|
||||
if (storageComp.Storage == null)
|
||||
return;
|
||||
|
||||
var itemQuery = GetEntityQuery<SharedItemComponent>();
|
||||
|
||||
foreach (var entity in storageComp.Storage.ContainedEntities)
|
||||
{
|
||||
if (!itemQuery.TryGetComponent(entity, out var itemComp))
|
||||
continue;
|
||||
|
||||
storageComp.StorageUsed += itemComp.Size;
|
||||
storageComp.SizeCache.Add(entity, itemComp.Size);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move entities from one storage to another.
|
||||
/// </summary>
|
||||
@@ -207,8 +440,10 @@ namespace Content.Server.Storage.EntitySystems
|
||||
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
targetComp.Insert(entity);
|
||||
Insert(target, entity, targetComp);
|
||||
}
|
||||
RecalculateStorageUsed(sourceComp);
|
||||
UpdateStorageUI(source, sourceComp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -236,57 +471,207 @@ namespace Content.Server.Storage.EntitySystems
|
||||
_disposalSystem.AfterInsert(disposalComp, entity);
|
||||
}
|
||||
}
|
||||
RecalculateStorageUsed(sourceComp);
|
||||
UpdateStorageUI(source, sourceComp);
|
||||
}
|
||||
|
||||
private void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
||||
public void HandleRemoveEntity(EntityUid uid, EntityUid player, EntityUid itemToRemove, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return;
|
||||
|
||||
if (EntityManager.TryGetComponent(oldParentEntity, out ServerStorageComponent? storageComp))
|
||||
if (!_containerSystem.ContainsEntity(uid, itemToRemove))
|
||||
return;
|
||||
|
||||
// succeeded, remove entity and update UI
|
||||
_containerSystem.RemoveEntity(uid, itemToRemove, false);
|
||||
|
||||
if (storageComp.StorageRemoveSound is not null)
|
||||
SoundSystem.Play(Filter.Pvs(uid, entityManager: EntityManager), storageComp.StorageRemoveSound.GetSound(), uid, AudioParams.Default);
|
||||
|
||||
_sharedHandsSystem.TryPickupAnyHand(player, itemToRemove);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies if an entity can be stored and if it fits
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to check</param>
|
||||
/// <returns>true if it can be inserted, false otherwise</returns>
|
||||
public bool CanInsert(EntityUid uid, EntityUid insertEnt, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return false;
|
||||
|
||||
if (TryComp(insertEnt, out ServerStorageComponent? storage) &&
|
||||
storage.StorageCapacityMax >= storageComp.StorageCapacityMax)
|
||||
return false;
|
||||
|
||||
if (TryComp(insertEnt, out SharedItemComponent? itemComp) &&
|
||||
itemComp.Size > storageComp.StorageCapacityMax - storageComp.StorageUsed)
|
||||
return false;
|
||||
|
||||
if (storageComp.Whitelist?.IsValid(insertEnt, EntityManager) == false)
|
||||
return false;
|
||||
|
||||
if (storageComp.Blacklist?.IsValid(insertEnt, EntityManager) == true)
|
||||
return false;
|
||||
|
||||
if (TryComp(insertEnt, out TransformComponent? transformComp) && transformComp.Anchored)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts into the storage container
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to insert</param>
|
||||
/// <returns>true if the entity was inserted, false otherwise</returns>
|
||||
public bool Insert(EntityUid uid, EntityUid insertEnt, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return false;
|
||||
|
||||
if (!CanInsert(uid, insertEnt, storageComp) || storageComp.Storage?.Insert(insertEnt) == false)
|
||||
return false;
|
||||
|
||||
if (storageComp.StorageInsertSound is not null)
|
||||
SoundSystem.Play(Filter.Pvs(uid, entityManager: EntityManager), storageComp.StorageInsertSound.GetSound(), uid, AudioParams.Default);
|
||||
|
||||
RecalculateStorageUsed(storageComp);
|
||||
UpdateStorageUI(uid, storageComp);
|
||||
return true;
|
||||
}
|
||||
|
||||
// REMOVE: remove and drop on the ground
|
||||
public bool RemoveAndDrop(EntityUid uid, EntityUid removeEnt, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return false;
|
||||
|
||||
var itemRemoved = storageComp.Storage?.Remove(removeEnt) == true;
|
||||
if (itemRemoved)
|
||||
RecalculateStorageUsed(storageComp);
|
||||
|
||||
return itemRemoved;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an entity into storage from the player's active hand
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity from</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertHeldEntity(EntityUid uid, EntityUid player, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return false;
|
||||
|
||||
if (!TryComp(player, out HandsComponent? hands) ||
|
||||
hands.ActiveHandEntity == null)
|
||||
return false;
|
||||
|
||||
var toInsert = hands.ActiveHandEntity;
|
||||
|
||||
if (!_sharedHandsSystem.TryDrop(player, toInsert.Value, handsComp: hands))
|
||||
{
|
||||
storageComp.HandleEntityMaybeRemoved(message);
|
||||
Popup(uid, player, "comp-storage-cant-insert", storageComp);
|
||||
return false;
|
||||
}
|
||||
|
||||
return PlayerInsertEntityInWorld(uid, player, toInsert.Value, storageComp);
|
||||
}
|
||||
|
||||
private void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
|
||||
/// <summary>
|
||||
/// Inserts an Entity (<paramref name="toInsert"/>) in the world into storage, informing <paramref name="player"/> if it fails.
|
||||
/// <paramref name="toInsert"/> is *NOT* held, see <see cref="PlayerInsertHeldEntity(Robust.Shared.GameObjects.EntityUid)"/>.
|
||||
/// </summary>
|
||||
/// <param name="player">The player to insert an entity with</param>
|
||||
/// <returns>true if inserted, false otherwise</returns>
|
||||
public bool PlayerInsertEntityInWorld(EntityUid uid, EntityUid player, EntityUid toInsert, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return false;
|
||||
|
||||
if (EntityManager.TryGetComponent(oldParentEntity, out ServerStorageComponent? storageComp))
|
||||
if (!_sharedInteractionSystem.InRangeUnobstructed(player, uid, popup: storageComp.ShowPopup))
|
||||
return false;
|
||||
|
||||
if (!Insert(uid, toInsert, storageComp))
|
||||
{
|
||||
storageComp.HandleEntityMaybeInserted(message);
|
||||
Popup(uid, player, "comp-storage-cant-insert", storageComp);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CheckSubscribedEntities(ServerStorageComponent storageComp)
|
||||
/// <summary>
|
||||
/// Opens the storage UI for an entity
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to open the UI for</param>
|
||||
public void OpenStorageUI(EntityUid uid, EntityUid entity, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
var xform = Transform(storageComp.Owner);
|
||||
var storagePos = xform.WorldPosition;
|
||||
var storageMap = xform.MapID;
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return;
|
||||
|
||||
var remove = new RemQueue<IPlayerSession>();
|
||||
if (!TryComp(entity, out ActorComponent? player))
|
||||
return;
|
||||
|
||||
foreach (var session in storageComp.SubscribedSessions)
|
||||
if (storageComp.StorageOpenSound is not null)
|
||||
SoundSystem.Play(Filter.Pvs(uid, entityManager: EntityManager), storageComp.StorageOpenSound.GetSound(), uid, AudioParams.Default);
|
||||
|
||||
Logger.DebugS(storageComp.LoggerName, $"Storage (UID {uid}) \"used\" by player session (UID {player.PlayerSession.AttachedEntity}).");
|
||||
|
||||
_uiSystem.GetUiOrNull(uid, StorageUiKey.Key)?.Open(player.PlayerSession);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the user has nested-UIs open (e.g., PDA UI open when pda is in a backpack), close them.
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
public void CloseNestedInterfaces(EntityUid uid, IPlayerSession session, ServerStorageComponent? storageComp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref storageComp))
|
||||
return;
|
||||
|
||||
if (storageComp.StoredEntities == null)
|
||||
return;
|
||||
|
||||
// for each containing thing
|
||||
// if it has a storage comp
|
||||
// ensure unsubscribe from session
|
||||
// if it has a ui component
|
||||
// close ui
|
||||
foreach (var entity in storageComp.StoredEntities)
|
||||
{
|
||||
// The component manages the set of sessions, so this invalid session should be removed soon.
|
||||
if (session.AttachedEntity is not {} attachedEntity || !EntityManager.EntityExists(attachedEntity))
|
||||
continue;
|
||||
|
||||
var attachedXform = Transform(attachedEntity);
|
||||
if (storageMap != attachedXform.MapID)
|
||||
continue;
|
||||
|
||||
var distanceSquared = (storagePos - attachedXform.WorldPosition).LengthSquared;
|
||||
if (distanceSquared > SharedInteractionSystem.InteractionRangeSquared)
|
||||
if (TryComp(entity, out ServerStorageComponent? storedStorageComp))
|
||||
{
|
||||
remove.Add(session);
|
||||
DebugTools.Assert(storedStorageComp != storageComp, $"Storage component contains itself!? Entity: {uid}");
|
||||
}
|
||||
|
||||
if (TryComp(entity, out ServerUserInterfaceComponent? uiComponent))
|
||||
{
|
||||
foreach (var ui in uiComponent.Interfaces)
|
||||
{
|
||||
ui.Close(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var session in remove)
|
||||
{
|
||||
storageComp.UnsubscribeSession(session);
|
||||
}
|
||||
private void UpdateStorageUI(EntityUid uid, ServerStorageComponent storageComp)
|
||||
{
|
||||
if (storageComp.Storage == null)
|
||||
return;
|
||||
|
||||
var state = new StorageBoundUserInterfaceState((List<EntityUid>) storageComp.Storage.ContainedEntities, storageComp.StorageUsed, storageComp.StorageCapacityMax);
|
||||
|
||||
_uiSystem.GetUiOrNull(uid, StorageUiKey.Key)?.SetState(state);
|
||||
}
|
||||
|
||||
private void Popup(EntityUid uid, EntityUid player, string message, ServerStorageComponent storageComp)
|
||||
{
|
||||
if (!storageComp.ShowPopup) return;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString(message), player, Filter.Entities(player));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user