Entity storage whitelist (#9694)

This commit is contained in:
Leon Friedrich
2022-07-14 23:38:39 +12:00
committed by GitHub
parent 3bf8c27888
commit 6bd6937572
5 changed files with 28 additions and 41 deletions

View File

@@ -1,7 +0,0 @@
namespace Content.Server.Storage.Components;
[RegisterComponent]
public sealed class ArtifactStorageComponent : Component
{
}

View File

@@ -1,5 +1,6 @@
using Content.Shared.Physics;
using Content.Shared.Sound;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
namespace Content.Server.Storage.Components;
@@ -64,6 +65,13 @@ public sealed class EntityStorageComponent : Component
[DataField("openSound")]
public SoundSpecifier OpenSound = new SoundPathSpecifier("/Audio/Effects/closetopen.ogg");
/// <summary>
/// Whitelist for what entities are allowed to be inserted into this container. If this is not null, the
/// standard requirement that the entity must be an item or mob is waived.
/// </summary>
[DataField("whitelist")]
public EntityWhitelist? Whitelist;
[ViewVariables]
public Container Contents = default!;
@@ -93,7 +101,10 @@ public sealed class StorageBeforeCloseEvent : EventArgs
public HashSet<EntityUid> Contents;
public HashSet<EntityUid> ContentsWhitelist = new();
/// <summary>
/// Entities that will get inserted, regardless of any insertion or whitelist checks.
/// </summary>
public HashSet<EntityUid> BypassChecks = new();
public StorageBeforeCloseEvent(EntityUid container, HashSet<EntityUid> contents)
{

View File

@@ -1,23 +0,0 @@
using Content.Server.Storage.Components;
using Content.Server.Xenoarchaeology.XenoArtifacts;
namespace Content.Server.Storage.EntitySystems;
public sealed class ArtifactStorageSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ArtifactStorageComponent, StorageBeforeCloseEvent>(OnBeforeClose);
}
private void OnBeforeClose(EntityUid uid, ArtifactStorageComponent component, StorageBeforeCloseEvent args)
{
foreach (var ent in args.Contents)
{
if (HasComp<ArtifactComponent>(ent))
args.ContentsWhitelist.Add(ent);
}
}
}

View File

@@ -10,6 +10,7 @@ using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Placeable;
using Content.Shared.Storage;
using Content.Shared.Whitelist;
using Robust.Server.Containers;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
@@ -143,16 +144,20 @@ public sealed class EntityStorageSystem : EntitySystem
var targetCoordinates = new EntityCoordinates(uid, component.EnteringOffset);
var ev = new StorageBeforeCloseEvent(uid, _lookup.GetEntitiesInRange(targetCoordinates, component.EnteringRange, LookupFlags.Approximate));
var entities = _lookup.GetEntitiesInRange(targetCoordinates, component.EnteringRange, LookupFlags.Approximate);
var ev = new StorageBeforeCloseEvent(uid, entities);
RaiseLocalEvent(uid, ev, true);
var count = 0;
foreach (var entity in ev.Contents)
{
if (!ev.ContentsWhitelist.Contains(entity))
if (!CanFit(entity, uid))
if (!ev.BypassChecks.Contains(entity))
{
if (!CanFit(entity, uid, component.Whitelist))
continue;
}
if (!AddToContents(entity, uid, component))
continue;
@@ -279,7 +284,7 @@ public sealed class EntityStorageSystem : EntitySystem
return Insert(toAdd, container, component);
}
public bool CanFit(EntityUid toInsert, EntityUid container)
public bool CanFit(EntityUid toInsert, EntityUid container, EntityWhitelist? whitelist)
{
// conditions are complicated because of pizzabox-related issues, so follow this guide
// 0. Accomplish your goals at all costs.
@@ -296,13 +301,12 @@ public sealed class EntityStorageSystem : EntitySystem
if (attemptEvent.Cancelled)
return false;
// checks
// TODO: Make the others sub to it.
var targetIsItem = HasComp<SharedItemComponent>(toInsert);
var targetIsMob = HasComp<SharedBodyComponent>(toInsert);
var storageIsItem = HasComp<SharedItemComponent>(container);
var allowedToEat = targetIsItem;
var allowedToEat = whitelist == null
? HasComp<SharedItemComponent>(toInsert)
: whitelist.IsValid(toInsert);
// BEFORE REPLACING THIS WITH, I.E. A PROPERTY:
// Make absolutely 100% sure you have worked out how to stop people ending up in backpacks.

View File

@@ -40,7 +40,9 @@
state: artifact_container_icon
- type: EntityStorage
Capacity: 1
- type: ArtifactStorage
whitelist:
components:
- Artifact
- type: Weldable
- type: SuppressArtifactContainer
- type: PlaceableSurface