Files
tbd-station-14/Content.Shared/Inventory/InventorySystem.Slots.cs
Paul Ritter 512d6a38c3 get that crap outta here (completely rewrites inventorysystem) (#5807)
* some work

* equip: done
unequip: todo

* unequipping done & refactored events

* workin

* movin

* reee namespaces

* stun

* mobstate

* fixes

* some work on events

* removes serverside itemcomp & misc fixes

* work

* smol merge fix

* ports template to prototype & finishes ui

* moves relay & adds containerenumerator

* actions & cuffs

* my god what is actioncode

* more fixes

* im loosing my grasp on reality

* more fixes

* more work

* explosions

* yes

* more work

* more fixes

* merge master & misc fixed because i forgot to commit before merging master

* more fixes

* fixes

* moar

* more work

* moar fixes

* suffixmap

* more work on client

* motivation low

* no. no containers

* mirroring client to server

* fixes

* move serverinvcomp

* serverinventorycomponent is dead

* gaming

* only strippable & ai left...

* only ai and richtext left

* fixes ai

* fixes

* fixes sprite layers

* more fixes

* resolves optional

* yes

* stable™️

* fixes

* moar fixes

* moar

* fix some tests

* lmao

* no comment

* good to merge™️

* fixes build but for real

* adresses some reviews

* adresses some more reviews

* nullables, yo

* fixes lobbyscreen

* timid refactor to differentiate actor & target

* adresses more reviews

* more

* my god what a mess

* removed the rest of duplicates

* removed duplicate slotflags and renamed shoes to feet

* removes another unused one

* yes

* fixes lobby & makes tryunequip return unequipped item

* fixes

* some funny renames

* fixes

* misc improvements to attemptevents

* fixes

* merge fixes

Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00

117 lines
4.3 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Shared.Inventory;
public partial class InventorySystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public bool TryGetSlotContainer(EntityUid uid, string slot, [NotNullWhen(true)] out ContainerSlot? containerSlot, [NotNullWhen(true)] out SlotDefinition? slotDefinition,
InventoryComponent? inventory = null, ContainerManagerComponent? containerComp = null)
{
containerSlot = null;
slotDefinition = null;
if (!Resolve(uid, ref inventory, ref containerComp, false))
return false;
if (!TryGetSlot(uid, slot, out slotDefinition, inventory: inventory))
return false;
if (!containerComp.TryGetContainer(slotDefinition.Name, out var container))
{
containerSlot = containerComp.MakeContainer<ContainerSlot>(slotDefinition.Name);
return true;
}
if (container is not ContainerSlot containerSlotChecked) return false;
containerSlot = containerSlotChecked;
return true;
}
public bool HasSlot(EntityUid uid, string slot, InventoryComponent? component = null) =>
TryGetSlot(uid, slot, out _, component);
public bool TryGetSlot(EntityUid uid, string slot, [NotNullWhen(true)] out SlotDefinition? slotDefinition, InventoryComponent? inventory = null)
{
slotDefinition = null;
if (!Resolve(uid, ref inventory, false))
return false;
if (!_prototypeManager.TryIndex<InventoryTemplatePrototype>(inventory.TemplateId, out var templatePrototype))
return false;
slotDefinition = templatePrototype.Slots.FirstOrDefault(x => x.Name == slot);
return slotDefinition != default;
}
public bool TryGetContainerSlotEnumerator(EntityUid uid, out ContainerSlotEnumerator containerSlotEnumerator, InventoryComponent? component = null)
{
containerSlotEnumerator = default;
if (!Resolve(uid, ref component, false))
return false;
containerSlotEnumerator = new ContainerSlotEnumerator(uid, component.TemplateId, _prototypeManager, this);
return true;
}
public bool TryGetSlots(EntityUid uid, [NotNullWhen(true)] out SlotDefinition[]? slotDefinitions, InventoryComponent? inventoryComponent = null)
{
slotDefinitions = null;
if (!Resolve(uid, ref inventoryComponent, false))
return false;
if (!_prototypeManager.TryIndex<InventoryTemplatePrototype>(inventoryComponent.TemplateId, out var templatePrototype))
return false;
slotDefinitions = templatePrototype.Slots;
return true;
}
public SlotDefinition[] GetSlots(EntityUid uid, InventoryComponent? inventoryComponent = null)
{
if (!Resolve(uid, ref inventoryComponent)) throw new InvalidOperationException();
return _prototypeManager.Index<InventoryTemplatePrototype>(inventoryComponent.TemplateId).Slots;
}
public struct ContainerSlotEnumerator
{
private readonly InventorySystem _inventorySystem;
private readonly EntityUid _uid;
private readonly SlotDefinition[] _slots;
private int _nextIdx = int.MaxValue;
public ContainerSlotEnumerator(EntityUid uid, string prototypeId, IPrototypeManager prototypeManager, InventorySystem inventorySystem)
{
_uid = uid;
_inventorySystem = inventorySystem;
if (prototypeManager.TryIndex<InventoryTemplatePrototype>(prototypeId, out var prototype))
{
_slots = prototype.Slots;
if(_slots.Length > 0)
_nextIdx = 0;
}
else
{
_slots = Array.Empty<SlotDefinition>();
}
}
public bool MoveNext([NotNullWhen(true)] out ContainerSlot? container)
{
container = null;
if (_nextIdx >= _slots.Length) return false;
while (_nextIdx < _slots.Length && !_inventorySystem.TryGetSlotContainer(_uid, _slots[_nextIdx++].Name, out container, out _)) { }
return container != null;
}
}
}