Files
tbd-station-14/Content.Server/Inventory/ServerInventorySystem.cs
2023-12-08 08:20:51 +11:00

70 lines
2.5 KiB
C#

using Content.Server.Storage.EntitySystems;
using Content.Shared.Clothing.Components;
using Content.Shared.Explosion;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Storage;
namespace Content.Server.Inventory
{
public sealed class ServerInventorySystem : InventorySystem
{
[Dependency] private readonly StorageSystem _storageSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<InventoryComponent, BeforeExplodeEvent>(OnExploded);
SubscribeLocalEvent<ClothingComponent, UseInHandEvent>(OnUseInHand);
SubscribeNetworkEvent<OpenSlotStorageNetworkMessage>(OnOpenSlotStorage);
}
private void OnExploded(Entity<InventoryComponent> ent, ref BeforeExplodeEvent args)
{
// explode each item in their inventory too
var slots = new InventorySlotEnumerator(ent);
while (slots.MoveNext(out var slot))
{
if (slot.ContainedEntity != null)
args.Contents.Add(slot.ContainedEntity.Value);
}
}
private void OnUseInHand(EntityUid uid, ClothingComponent component, UseInHandEvent args)
{
if (args.Handled || !component.QuickEquip)
return;
QuickEquip(uid, component, args);
}
private void OnOpenSlotStorage(OpenSlotStorageNetworkMessage ev, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not { Valid: true } uid)
return;
if (TryGetSlotEntity(uid, ev.Slot, out var entityUid) && TryComp<StorageComponent>(entityUid, out var storageComponent))
{
_storageSystem.OpenStorageUI(entityUid.Value, uid, storageComponent);
}
}
public void TransferEntityInventories(Entity<InventoryComponent?> source, Entity<InventoryComponent?> target)
{
if (!Resolve(source.Owner, ref source.Comp) || !Resolve(target.Owner, ref target.Comp))
return;
var enumerator = new InventorySlotEnumerator(source.Comp);
while (enumerator.NextItem(out var item, out var slot))
{
if (TryUnequip(source, slot.Name, true, true, inventory: source.Comp))
TryEquip(target, item, slot.Name , true, true, inventory: target.Comp);
}
}
}
}