Cigarette ecs (#4495)
* Reorganized Shared.Storage folder * Replace StorageCounter with Item Counter * Change stack visuals setting data * Fix mirrorcult suggestions * Fix items from upstream * Fix type formatting
This commit is contained in:
33
Content.Shared/Storage/Components/ItemCounterComponent.cs
Normal file
33
Content.Shared/Storage/Components/ItemCounterComponent.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Content.Shared.Storage.EntitySystems;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.Storage.Components
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Storage that spawns and counts a single item.
|
||||
/// Usually used for things like matchboxes, cigarette packs,
|
||||
/// cigar cases etc.
|
||||
/// </summary>
|
||||
/// <code>
|
||||
/// - type: ItemCounter
|
||||
/// amount: 6 # Note: this field can be omitted.
|
||||
/// count:
|
||||
/// tags: [Cigarette]
|
||||
/// </code>
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(SharedItemCounterSystem))]
|
||||
public class ItemCounterComponent : Component
|
||||
{
|
||||
public override string Name => "ItemCounter";
|
||||
|
||||
[DataField("count", required: true)]
|
||||
public EntityWhitelist Count { get; set; } = default!;
|
||||
|
||||
[DataField("amount")]
|
||||
public int? MaxAmount { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Storage.EntitySystems;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.Storage.ItemCounter
|
||||
namespace Content.Shared.Storage.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ItemCounterComponent : Component, ISerializationHooks
|
||||
[Friend(typeof(SharedItemMapperSystem))]
|
||||
public class ItemMapperComponent : Component, ISerializationHooks
|
||||
{
|
||||
public override string Name => "ItemCounter";
|
||||
public override string Name => "ItemMapper";
|
||||
|
||||
[DataField("mapLayers")] public readonly Dictionary<string, SharedMapLayerData> MapLayers = new();
|
||||
|
||||
@@ -21,4 +24,4 @@ namespace Content.Shared.Storage.ItemCounter
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Storage
|
||||
namespace Content.Shared.Storage.Components
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
public enum SharedBagOpenVisuals : byte
|
||||
@@ -4,7 +4,7 @@ using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.Storage.ItemCounter
|
||||
namespace Content.Shared.Storage.Components
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
public enum StorageMapVisuals : sbyte
|
||||
@@ -43,4 +43,4 @@ namespace Content.Shared.Storage.ItemCounter
|
||||
QueuedEntities = other.QueuedEntities;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Content.Shared.Stacks;
|
||||
using Content.Shared.Storage.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Storage.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedItemCounterSystem : EntitySystem
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ItemCounterComponent, EntInsertedIntoContainerMessage>(CounterEntityInserted);
|
||||
SubscribeLocalEvent<ItemCounterComponent, EntRemovedFromContainerMessage>(CounterEntityRemoved);
|
||||
}
|
||||
|
||||
private void CounterEntityInserted(EntityUid uid, ItemCounterComponent itemCounter,
|
||||
EntInsertedIntoContainerMessage args)
|
||||
{
|
||||
if (!itemCounter.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)) return;
|
||||
|
||||
var count = GetCount(args, itemCounter);
|
||||
if (count == null)
|
||||
return;
|
||||
|
||||
appearanceComponent.SetData(StackVisuals.Actual, count);
|
||||
if (itemCounter.MaxAmount != null)
|
||||
appearanceComponent.SetData(StackVisuals.MaxCount, itemCounter.MaxAmount);
|
||||
|
||||
}
|
||||
|
||||
private void CounterEntityRemoved(EntityUid uid, ItemCounterComponent itemCounter,
|
||||
EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (!itemCounter.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)) return;
|
||||
|
||||
var count = GetCount(args, itemCounter);
|
||||
if (count == null)
|
||||
return;
|
||||
|
||||
appearanceComponent.SetData(StackVisuals.Actual, count);
|
||||
if (itemCounter.MaxAmount != null)
|
||||
appearanceComponent.SetData(StackVisuals.MaxCount, itemCounter.MaxAmount);
|
||||
}
|
||||
|
||||
protected abstract int? GetCount(ContainerModifiedMessage msg, ItemCounterComponent itemCounter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Storage.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Storage.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedItemMapperSystem : EntitySystem
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ItemMapperComponent, ComponentInit>(InitLayers);
|
||||
SubscribeLocalEvent<ItemMapperComponent, EntInsertedIntoContainerMessage>(MapperEntityInserted);
|
||||
SubscribeLocalEvent<ItemMapperComponent, EntRemovedFromContainerMessage>(MapperEntityRemoved);
|
||||
}
|
||||
|
||||
private void InitLayers(EntityUid uid, ItemMapperComponent component, ComponentInit args)
|
||||
{
|
||||
if (component.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent))
|
||||
{
|
||||
var list = new List<string>(component.MapLayers.Keys);
|
||||
appearanceComponent.SetData(StorageMapVisuals.InitLayers, new ShowLayerData(list));
|
||||
}
|
||||
}
|
||||
|
||||
private void MapperEntityRemoved(EntityUid uid, ItemMapperComponent itemMapper,
|
||||
EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (itemMapper.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)
|
||||
&& TryGetLayers(args, itemMapper, out var containedLayers))
|
||||
{
|
||||
appearanceComponent.SetData(StorageMapVisuals.LayerChanged, new ShowLayerData(containedLayers));
|
||||
}
|
||||
}
|
||||
|
||||
private void MapperEntityInserted(EntityUid uid, ItemMapperComponent itemMapper,
|
||||
EntInsertedIntoContainerMessage args)
|
||||
{
|
||||
if (itemMapper.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)
|
||||
&& TryGetLayers(args, itemMapper, out var containedLayers))
|
||||
{
|
||||
appearanceComponent.SetData(StorageMapVisuals.LayerChanged, new ShowLayerData(containedLayers));
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract bool TryGetLayers(ContainerModifiedMessage msg,
|
||||
ItemMapperComponent itemMapper,
|
||||
out IReadOnlyList<string> containedLayers);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Storage.ItemCounter
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedItemCounterSystem : EntitySystem
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ItemCounterComponent, ComponentInit>(InitLayers);
|
||||
SubscribeLocalEvent<ItemCounterComponent, EntInsertedIntoContainerMessage>(HandleEntityInsert);
|
||||
SubscribeLocalEvent<ItemCounterComponent, EntRemovedFromContainerMessage>(HandleEntityRemoved);
|
||||
}
|
||||
|
||||
private void InitLayers(EntityUid uid, ItemCounterComponent component, ComponentInit args)
|
||||
{
|
||||
if (component.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent))
|
||||
{
|
||||
var list = new List<string>(component.MapLayers.Keys);
|
||||
appearanceComponent.SetData(StorageMapVisuals.InitLayers, new ShowLayerData(list));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEntityRemoved(EntityUid uid, ItemCounterComponent itemCounter,
|
||||
EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (itemCounter.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)
|
||||
&& TryGetContainer(args, itemCounter, out var containedLayers))
|
||||
{
|
||||
appearanceComponent.SetData(StorageMapVisuals.LayerChanged, new ShowLayerData(containedLayers));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEntityInsert(EntityUid uid, ItemCounterComponent itemCounter,
|
||||
EntInsertedIntoContainerMessage args)
|
||||
{
|
||||
if (itemCounter.Owner.TryGetComponent(out SharedAppearanceComponent? appearanceComponent)
|
||||
&& TryGetContainer(args, itemCounter, out var containedLayers))
|
||||
{
|
||||
appearanceComponent.SetData(StorageMapVisuals.LayerChanged, new ShowLayerData(containedLayers));
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract bool TryGetContainer(ContainerModifiedMessage msg,
|
||||
ItemCounterComponent itemCounter,
|
||||
out IReadOnlyList<string> containedLayers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user