Remove FoodContainer, add whitelists, replace with SpawnItemsOnUse (#4358)
This commit is contained in:
41
Content.Server/Storage/EntitySystems/ItemCounterSystem.cs
Normal file
41
Content.Server/Storage/EntitySystems/ItemCounterSystem.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Shared.Storage.ItemCounter;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class ItemCounterSystem : SharedItemCounterSystem
|
||||
{
|
||||
protected override bool TryGetContainer(ContainerModifiedMessage msg,
|
||||
ItemCounterComponent itemCounter,
|
||||
out IReadOnlyList<string> showLayers)
|
||||
{
|
||||
if (msg.Container.Owner.TryGetComponent(out ServerStorageComponent? component))
|
||||
{
|
||||
var containedLayers = component.StoredEntities ?? new List<IEntity>();
|
||||
var list = new List<string>();
|
||||
foreach (var mapLayerData in itemCounter.MapLayers.Values)
|
||||
{
|
||||
foreach (var entity in containedLayers)
|
||||
{
|
||||
if (mapLayerData.Whitelist.IsValid(entity))
|
||||
{
|
||||
list.Add(mapLayerData.Layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
showLayers = list;
|
||||
return true;
|
||||
}
|
||||
|
||||
showLayers = new List<string>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
public class SpawnItemsOnUseSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<SpawnItemsOnUseComponent, UseInHandEvent>(OnUseInHand);
|
||||
}
|
||||
|
||||
private void OnUseInHand(EntityUid uid, SpawnItemsOnUseComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
var owner = EntityManager.GetEntity(uid);
|
||||
var alreadySpawnedGroups = new List<string>();
|
||||
IEntity? entityToPlaceInHands = null;
|
||||
foreach (var storageItem in component.Items)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(storageItem.GroupId) &&
|
||||
alreadySpawnedGroups.Contains(storageItem.GroupId)) continue;
|
||||
|
||||
if (storageItem.SpawnProbability != 1f &&
|
||||
!_random.Prob(storageItem.SpawnProbability))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var i = 0; i < storageItem.Amount; i++)
|
||||
{
|
||||
entityToPlaceInHands = EntityManager.SpawnEntity(storageItem.PrototypeId, args.User.Transform.Coordinates);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId);
|
||||
}
|
||||
|
||||
if (component.Sound != null)
|
||||
SoundSystem.Play(Filter.Pvs(owner), component.Sound.GetSound());
|
||||
|
||||
component.Uses--;
|
||||
if (component.Uses == 0)
|
||||
{
|
||||
args.Handled = true;
|
||||
owner.Delete();
|
||||
}
|
||||
|
||||
if (entityToPlaceInHands != null
|
||||
&& args.User.TryGetComponent<SharedHandsComponent>(out var hands))
|
||||
{
|
||||
hands.TryPutInAnyHand(entityToPlaceInHands);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
96
Content.Server/Storage/EntitySystems/StorageSystem.cs
Normal file
96
Content.Server/Storage/EntitySystems/StorageSystem.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Interaction;
|
||||
using Content.Server.Storage.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Storage.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class StorageSystem : EntitySystem
|
||||
{
|
||||
private readonly List<IPlayerSession> _sessionCache = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleEntityRemovedFromContainer);
|
||||
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleEntityInsertedIntoContainer);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var component in ComponentManager.EntityQuery<ServerStorageComponent>(true))
|
||||
{
|
||||
CheckSubscribedEntities(component);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleEntityRemovedFromContainer(EntRemovedFromContainerMessage message)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
|
||||
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
||||
{
|
||||
storageComp.HandleEntityMaybeRemoved(message);
|
||||
}
|
||||
|
||||
if (oldParentEntity.TryGetComponent<StorageCounterComponent>(out var newStorageComp))
|
||||
{
|
||||
newStorageComp.ContainerUpdateAppearance(message.Container);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
|
||||
{
|
||||
var oldParentEntity = message.Container.Owner;
|
||||
|
||||
if (oldParentEntity.TryGetComponent(out ServerStorageComponent? storageComp))
|
||||
{
|
||||
storageComp.HandleEntityMaybeInserted(message);
|
||||
}
|
||||
|
||||
if (oldParentEntity.TryGetComponent<StorageCounterComponent>(out var newStorageComp))
|
||||
{
|
||||
newStorageComp.ContainerUpdateAppearance(message.Container);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user