InventoryComponent type handler for entities in inv slots (#12026)

This commit is contained in:
Vera Aguilera Puerto
2022-11-28 04:01:50 +01:00
committed by GitHub
parent d4c092a373
commit d05b2a8e71
2 changed files with 31 additions and 0 deletions

View File

@@ -7,10 +7,20 @@ namespace Content.Shared.Inventory;
public partial class InventorySystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IViewVariablesManager _vvm = default!;
private void InitializeSlots()
{
SubscribeLocalEvent<InventoryComponent, ComponentInit>(OnInit);
_vvm.GetTypeHandler<InventoryComponent>()
.AddHandler(HandleViewVariablesSlots, ListViewVariablesSlots);
}
private void ShutdownSlots()
{
_vvm.GetTypeHandler<InventoryComponent>()
.RemoveHandler(HandleViewVariablesSlots, ListViewVariablesSlots);
}
protected virtual void OnInit(EntityUid uid, InventoryComponent component, ComponentInit args)
@@ -99,6 +109,21 @@ public partial class InventorySystem : EntitySystem
return _prototypeManager.Index<InventoryTemplatePrototype>(inventoryComponent.TemplateId).Slots;
}
private ViewVariablesPath? HandleViewVariablesSlots(EntityUid uid, InventoryComponent comp, string relativePath)
{
return TryGetSlotEntity(uid, relativePath, out var entity, comp)
? ViewVariablesPath.FromObject(entity)
: null;
}
private IEnumerable<string> ListViewVariablesSlots(EntityUid uid, InventoryComponent comp)
{
foreach (var slotDef in GetSlots(uid, comp))
{
yield return slotDef.Name;
}
}
public struct ContainerSlotEnumerator
{
private readonly InventorySystem _inventorySystem;