Paper Bins (#13449)

This commit is contained in:
Nemanja
2023-01-31 17:03:27 -05:00
committed by GitHub
parent eae58211e1
commit e04d81ec3c
8 changed files with 308 additions and 6 deletions

View File

@@ -0,0 +1,71 @@
using Content.Shared.Storage.EntitySystems;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.Storage.Components;
/// <summary>
/// This is used for things like paper bins, in which
/// you can only take off of the top of the bin.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(BinSystem))]
public sealed class BinComponent : Component
{
/// <summary>
/// The containers that contain the items held in the bin
/// </summary>
[ViewVariables]
public Container ItemContainer = default!;
/// <summary>
/// A list representing the order in which
/// all the entities are stored in the bin.
/// </summary>
/// <remarks>
/// The only reason this isn't a stack is so that
/// i can handle entities being deleted and removed
/// out of order by other systems
/// </remarks>
[DataField("items")]
public List<EntityUid> Items = new();
/// <summary>
/// The items that start in the bin. Sorted in order.
/// </summary>
[DataField("initialContents", customTypeSerializer: typeof(PrototypeIdListSerializer<EntityPrototype>))]
public List<string> InitialContents = new();
/// <summary>
/// A whitelist governing what items can be inserted into the bin.
/// </summary>
[DataField("whitelist")]
public EntityWhitelist? Whitelist;
/// <summary>
/// The maximum amount of items
/// that can be stored in the bin.
/// </summary>
[DataField("maxItems")]
public int MaxItems = 20;
}
[Serializable, NetSerializable]
public sealed class BinComponentState : ComponentState
{
public List<EntityUid> Items;
public EntityWhitelist? Whitelist;
public int MaxItems;
public BinComponentState(List<EntityUid> items, EntityWhitelist? whitelist, int maxItems)
{
Items = items;
Whitelist = whitelist;
MaxItems = maxItems;
}
}

View File

@@ -0,0 +1,159 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Storage.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Network;
using Robust.Shared.Timing;
namespace Content.Shared.Storage.EntitySystems;
/// <summary>
/// This handles <see cref="BinComponent"/>
/// </summary>
public sealed class BinSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly ISharedAdminLogManager _admin = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
public const string BinContainerId = "bin-container";
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<BinComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<BinComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<BinComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<BinComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<BinComponent, EntRemovedFromContainerMessage>(OnEntRemoved);
SubscribeLocalEvent<BinComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<BinComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
}
private void OnGetState(EntityUid uid, BinComponent component, ref ComponentGetState args)
{
args.State = new BinComponentState(component.Items, component.Whitelist, component.MaxItems);
}
private void OnHandleState(EntityUid uid, BinComponent component, ref ComponentHandleState args)
{
if (args.Current is not BinComponentState state)
return;
component.Items = new List<EntityUid>(state.Items);
component.Whitelist = state.Whitelist;
component.MaxItems = state.MaxItems;
}
private void OnStartup(EntityUid uid, BinComponent component, ComponentStartup args)
{
component.ItemContainer = _container.EnsureContainer<Container>(uid, BinContainerId);
}
private void OnMapInit(EntityUid uid, BinComponent component, MapInitEvent args)
{
// don't spawn on the client.
if (_net.IsClient)
return;
var xform = Transform(uid);
foreach (var id in component.InitialContents)
{
var ent = Spawn(id, xform.Coordinates);
if (!TryInsertIntoBin(uid, ent, component))
{
Logger.Error($"Entity {ToPrettyString(ent)} was unable to be initialized into bin {ToPrettyString(uid)}");
return;
}
}
}
private void OnEntRemoved(EntityUid uid, BinComponent component, EntRemovedFromContainerMessage args)
{
component.Items.Remove(args.Entity);
}
private void OnInteractHand(EntityUid uid, BinComponent component, InteractHandEvent args)
{
if (args.Handled || !_timing.IsFirstTimePredicted)
return;
EntityUid? toGrab = component.Items.LastOrDefault();
if (!TryRemoveFromBin(uid, toGrab, component))
return;
_hands.TryPickupAnyHand(args.User, toGrab.Value);
_admin.Add(LogType.Pickup, LogImpact.Low,
$"{ToPrettyString(uid):player} removed {ToPrettyString(toGrab.Value)} from bin {ToPrettyString(uid)}.");
args.Handled = true;
}
private void OnAfterInteractUsing(EntityUid uid, BinComponent component, AfterInteractUsingEvent args)
{
if (args.Handled || !args.CanReach)
return;
if (!_timing.IsFirstTimePredicted)
return;
if (!TryInsertIntoBin(uid, args.Used, component))
return;
_admin.Add(LogType.Pickup, LogImpact.Low, $"{ToPrettyString(uid):player} inserted {ToPrettyString(args.User)} into bin {ToPrettyString(uid)}.");
args.Handled = true;
}
/// <summary>
/// Inserts an entity at the top of the bin
/// </summary>
/// <param name="uid"></param>
/// <param name="toInsert"></param>
/// <param name="component"></param>
/// <returns></returns>
public bool TryInsertIntoBin(EntityUid uid, EntityUid toInsert, BinComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (component.Items.Count >= component.MaxItems)
return false;
if (component.Whitelist != null && !component.Whitelist.IsValid(toInsert))
return false;
component.ItemContainer.Insert(toInsert);
component.Items.Add(toInsert);
Dirty(component);
return true;
}
/// <summary>
/// Tries to remove an entity from the top of the bin.
/// </summary>
/// <param name="uid"></param>
/// <param name="toRemove"></param>
/// <param name="component"></param>
/// <returns></returns>
public bool TryRemoveFromBin(EntityUid uid, [NotNullWhen(true)] EntityUid? toRemove, BinComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (toRemove != component.Items.Last())
return false;
if (!component.ItemContainer.Remove(toRemove.Value))
return false;
component.Items.Remove(toRemove.Value);
Dirty(component);
return true;
}
}

View File

@@ -0,0 +1,78 @@
- type: entity
parent: BaseStructureDynamic
id: PaperBin
name: paper bin
description: What secrets lie at the bottom of its endless stack?
suffix: Empty
components:
- type: Sprite
netsync: false
sprite: Objects/Misc/bureaucracy.rsi
state: paper_bin0
drawdepth: SmallObjects
noRot: true
- type: Appearance
- type: ItemMapper
sprite: Objects/Misc/bureaucracy.rsi
mapLayers:
paper_bin1:
whitelist:
tags:
- Document
- Write
- type: Fixtures
fixtures:
- shape:
!type:PhysShapeAabb
bounds: "-0.10,-0.10,0.10,0.10"
density: 500
mask:
- TabletopMachineMask
- type: InteractionOutline
- type: Bin
whitelist:
tags:
- Document
- Write
- type: ContainerContainer
containers:
bin-container: !type:Container
- type: entity
parent: PaperBin
id: PaperBin5
suffix: 5
components:
- type: Bin
initialContents:
- Paper
- Paper
- Paper
- Paper
- Paper
whitelist:
tags:
- Document
- Write
- type: entity
parent: PaperBin
id: PaperBin10
suffix: 10
components:
- type: Bin
initialContents:
- Paper
- Paper
- Paper
- Paper
- Paper
- Paper
- Paper
- Paper
- Paper
- Paper
whitelist:
tags:
- Document
- Write

View File

@@ -96,12 +96,6 @@
{ {
"name": "paper_bin1" "name": "paper_bin1"
}, },
{
"name": "paper_bin2"
},
{
"name": "paper_bin3"
},
{ {
"name": "paper_plane" "name": "paper_plane"
}, },

Binary file not shown.

Before

Width:  |  Height:  |  Size: 278 B

After

Width:  |  Height:  |  Size: 227 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 300 B

After

Width:  |  Height:  |  Size: 204 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 330 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 362 B