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:
Ygg01
2021-09-04 19:42:32 +02:00
committed by GitHub
parent fc1ddb00cb
commit 53b53c3e0e
22 changed files with 370 additions and 326 deletions

View File

@@ -177,7 +177,6 @@ namespace Content.Client.Entry
"Firelock",
"AtmosPlaque",
"Spillable",
"StorageCounter",
"SpaceVillainArcade",
"Flammable",
"Smoking",

View File

@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using Content.Client.Animations;
using Content.Client.Items.Components;
using Content.Client.Hands;
using Content.Client.Items.Components;
using Content.Client.UserInterface.Controls;
using Content.Shared.DragDrop;
using Content.Shared.Stacks;
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
@@ -33,7 +35,6 @@ namespace Content.Client.Storage
private int StorageSizeUsed;
private int StorageCapacityMax;
private StorageWindow? _window;
private SharedBagState _bagState;
public override IReadOnlyList<IEntity> StoredEntities => _storedEntities;
@@ -83,15 +84,12 @@ namespace Content.Client.Storage
//Updates what we are storing for the UI
case StorageHeldItemsMessage msg:
HandleStorageMessage(msg);
ChangeStorageVisualization(_bagState);
break;
//Opens the UI
case OpenStorageUIMessage _:
ChangeStorageVisualization(SharedBagState.Open);
ToggleUI();
break;
case CloseStorageUIMessage _:
ChangeStorageVisualization(SharedBagState.Close);
CloseUI();
break;
case AnimateInsertingEntitiesMessage msg:
@@ -138,22 +136,36 @@ namespace Content.Client.Storage
if (_window == null) return;
if (_window.IsOpen)
{
_window.Close();
ChangeStorageVisualization(SharedBagState.Close);
}
else
{
_window.OpenCentered();
ChangeStorageVisualization(SharedBagState.Open);
}
}
private void CloseUI()
{
_window?.Close();
if (_window == null) return;
_window.Close();
ChangeStorageVisualization(SharedBagState.Close);
}
private void ChangeStorageVisualization(SharedBagState state)
{
_bagState = state;
if (Owner.TryGetComponent<AppearanceComponent>(out var appearanceComponent))
{
appearanceComponent.SetData(SharedBagOpenVisuals.BagState, state);
if (Owner.HasComponent<ItemCounterComponent>())
{
appearanceComponent.SetData(StackVisuals.Hide, state == SharedBagState.Close);
}
}
}

View File

@@ -1,6 +1,7 @@

using Content.Shared.Stacks;
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
@@ -57,7 +58,7 @@ namespace Content.Client.Storage.Visualizers
spriteComponent.LayerSetVisible(OpenIcon, false);
break;
}
component.SetData(StackVisuals.Hide, bagState == SharedBagState.Close);
}
}
}

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Storage.ItemCounter;
using Content.Shared.Storage.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;

View File

@@ -1,82 +0,0 @@
using System;
using System.Collections.Generic;
using Content.Shared.Stacks;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.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: StorageCounter
/// amount: 6 # Note: this field can be omitted
/// countTag: Cigarette # Note: field doesn't point to entity Id, but its tag
/// </code>
[Obsolete("Should be deprecated in favor of SharedItemCounterSystem")]
[RegisterComponent]
public class StorageCounterComponent : Component, ISerializationHooks
{
// TODO Convert to EntityWhitelist
[DataField("countTag")]
private string? _countTag;
[DataField("amount")]
private int? _maxAmount;
/// <summary>
/// Single item storage component usually have an attached StackedVisualizer.
/// </summary>
[ComponentDependency] private readonly AppearanceComponent? _appearanceComponent = default;
public override string Name => "StorageCounter";
void ISerializationHooks.AfterDeserialization()
{
if (_countTag == null)
{
Logger.Warning("StorageCounterComponent without a `countTag` is useless");
}
}
public void ContainerUpdateAppearance(IContainer container)
{
if(_appearanceComponent is null)
return;
var actual = Count(container.ContainedEntities);
_appearanceComponent.SetData(StackVisuals.Actual, actual);
if (_maxAmount != null)
{
_appearanceComponent.SetData(StackVisuals.MaxCount, _maxAmount);
}
}
private int Count(IReadOnlyList<IEntity> containerContainedEntities)
{
var count = 0;
if (_countTag != null)
{
foreach (var entity in containerContainedEntities)
{
if (entity.HasTag(_countTag))
{
count++;
}
}
}
return count;
}
}
}

View File

@@ -1,41 +1,29 @@
using System.Collections.Generic;
using Content.Server.Storage.Components;
using Content.Shared.Storage.ItemCounter;
using Content.Server.Storage.Components;
using Content.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems;
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)
protected override int? GetCount(ContainerModifiedMessage msg, ItemCounterComponent itemCounter)
{
if (msg.Container.Owner.TryGetComponent(out ServerStorageComponent? component))
if (!msg.Container.Owner.TryGetComponent(out ServerStorageComponent? component)
|| component.StoredEntities == null)
{
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;
}
}
return null;
}
showLayers = list;
return true;
var count = 0;
foreach (var entity in component.StoredEntities)
{
if (itemCounter.Count.IsValid(entity)) count++;
}
showLayers = new List<string>();
return false;
return count;
}
}
}

View File

@@ -0,0 +1,43 @@
using System.Collections.Generic;
using Content.Server.Storage.Components;
using Content.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems;
using JetBrains.Annotations;
using Robust.Shared.Analyzers;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
namespace Content.Server.Storage.EntitySystems
{
[UsedImplicitly]
public class ItemMapperSystem : SharedItemMapperSystem
{
protected override bool TryGetLayers(ContainerModifiedMessage msg,
ItemMapperComponent itemMapper,
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 itemMapper.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;
}
}
}

View File

@@ -39,11 +39,6 @@ namespace Content.Server.Storage.EntitySystems
{
storageComp.HandleEntityMaybeRemoved(message);
}
if (oldParentEntity.TryGetComponent<StorageCounterComponent>(out var newStorageComp))
{
newStorageComp.ContainerUpdateAppearance(message.Container);
}
}
private static void HandleEntityInsertedIntoContainer(EntInsertedIntoContainerMessage message)
@@ -54,11 +49,6 @@ namespace Content.Server.Storage.EntitySystems
{
storageComp.HandleEntityMaybeInserted(message);
}
if (oldParentEntity.TryGetComponent<StorageCounterComponent>(out var newStorageComp))
{
newStorageComp.ContainerUpdateAppearance(message.Container);
}
}
private void CheckSubscribedEntities(ServerStorageComponent storageComp)

View 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; }
}
}

View File

@@ -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();

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -32,7 +32,7 @@
- Welder
- Radio
- PowerCell
- type: ItemCounter
- type: ItemMapper
mapLayers:
cutters_red:
whitelist:
@@ -99,7 +99,7 @@
- Radio
- Handcuff
- PowerCell
- type: ItemCounter
- type: ItemMapper
mapLayers:
drill:
whitelist:
@@ -161,7 +161,7 @@
- Handcuff
- RangedMagazine
- Ammo
- type: ItemCounter
- type: ItemMapper
mapLayers:
flashbang:
whitelist:
@@ -196,7 +196,7 @@
- Soap
- Flashlight
- CigPack
- type: ItemCounter
- type: ItemMapper
mapLayers:
bottle:
whitelist:
@@ -241,7 +241,7 @@
- Hypospray
- SurgeryTool
- Radio
- type: ItemCounter
- type: ItemMapper
mapLayers:
bottle:
whitelist:
@@ -300,7 +300,7 @@
components:
- Seed
- Smoking
- type: ItemCounter
- type: ItemMapper
mapLayers:
hatchet:
whitelist:
@@ -352,7 +352,7 @@
- FlashOnTrigger
- Flash
- Handcuff
- type: ItemCounter
- type: ItemMapper
mapLayers:
flashbang:
whitelist:
@@ -396,7 +396,7 @@
- type: StorageFill
contents:
- id: CaptainSabre
- type: ItemCounter
- type: ItemMapper
mapLayers:
sheath-bag:
whitelist:

View File

@@ -30,8 +30,9 @@
amount: 3
- id: FoodDonutPlain
amount: 3
- type: StorageCounter
countTag: Donut
- type: ItemCounter
count:
tags: [Donut]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer
@@ -70,8 +71,9 @@
contents:
- id: FoodEgg
amount: 12
- type: StorageCounter
countTag: Egg
- type: ItemCounter
count:
tags: [Egg]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer
@@ -238,8 +240,9 @@
contents:
- id: FoodBakedNugget
amount: 6
- type: StorageCounter
countTag: Nugget
- type: ItemCounter
count:
tags: [Nugget]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer

View File

@@ -15,8 +15,9 @@
contents:
- id: Cigarette
amount: 6
- type: StorageCounter
countTag: Cigarette
- type: ItemCounter
count:
tags: [Cigarette]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer

View File

@@ -18,8 +18,9 @@
contents:
- id: Cigar
amount: 8
- type: StorageCounter
countTag: Cigar
- type: ItemCounter
count:
tags: [Cigar]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer

View File

@@ -193,8 +193,9 @@
contents:
- id: DrinkColaCan
amount: 6
- type: StorageCounter
countTag: Cola
- type: ItemCounter
count:
tags: [Cola]
- type: Appearance
visuals:
- type: StackVisualizer

View File

@@ -203,8 +203,6 @@
sprite: Objects/Fun/crayons.rsi
size: 9999
HeldPrefix: box
- type: StorageCounter
countTag: Crayon
- type: StorageFill
contents:
- id: CrayonRed
@@ -214,7 +212,7 @@
- id: CrayonBlue
- id: CrayonPurple
- id: CrayonBlack
- type: ItemCounter
- type: ItemMapper
mapLayers:
black_box:
whitelist:

View File

@@ -63,8 +63,9 @@
contents:
- id: Matchstick
amount: 6
- type: StorageCounter
countTag: Matchstick
- type: ItemCounter
count:
tags: [Cigar]
- type: Appearance
visuals:
- type: BagOpenCloseVisualizer