2022-04-28 06:11:15 -06:00
|
|
|
using Content.Server.Storage.EntitySystems;
|
2023-11-19 17:44:42 +00:00
|
|
|
using Content.Shared.Explosion;
|
2021-12-30 22:56:10 +01:00
|
|
|
using Content.Shared.Inventory;
|
|
|
|
|
using Content.Shared.Inventory.Events;
|
2023-09-11 21:20:46 +10:00
|
|
|
using Content.Shared.Storage;
|
2021-12-30 22:56:10 +01:00
|
|
|
|
|
|
|
|
namespace Content.Server.Inventory
|
|
|
|
|
{
|
2022-04-28 06:11:15 -06:00
|
|
|
public sealed class ServerInventorySystem : InventorySystem
|
2021-12-30 22:56:10 +01:00
|
|
|
{
|
2022-04-28 06:11:15 -06:00
|
|
|
[Dependency] private readonly StorageSystem _storageSystem = default!;
|
|
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2023-11-19 17:44:42 +00:00
|
|
|
SubscribeLocalEvent<InventoryComponent, BeforeExplodeEvent>(OnExploded);
|
2021-12-30 22:56:10 +01:00
|
|
|
SubscribeNetworkEvent<OpenSlotStorageNetworkMessage>(OnOpenSlotStorage);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 17:44:42 +00:00
|
|
|
private void OnExploded(Entity<InventoryComponent> ent, ref BeforeExplodeEvent args)
|
|
|
|
|
{
|
|
|
|
|
// explode each item in their inventory too
|
2023-12-07 16:20:51 -05:00
|
|
|
var slots = new InventorySlotEnumerator(ent);
|
2023-11-19 17:44:42 +00:00
|
|
|
while (slots.MoveNext(out var slot))
|
|
|
|
|
{
|
|
|
|
|
if (slot.ContainedEntity != null)
|
|
|
|
|
args.Contents.Add(slot.ContainedEntity.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-13 11:18:24 +13:00
|
|
|
private void OnOpenSlotStorage(OpenSlotStorageNetworkMessage ev, EntitySessionEventArgs args)
|
2021-12-30 22:56:10 +01:00
|
|
|
{
|
2023-01-19 03:56:45 +01:00
|
|
|
if (args.SenderSession.AttachedEntity is not { Valid: true } uid)
|
2022-02-13 11:18:24 +13:00
|
|
|
return;
|
|
|
|
|
|
2023-09-11 21:20:46 +10:00
|
|
|
if (TryGetSlotEntity(uid, ev.Slot, out var entityUid) && TryComp<StorageComponent>(entityUid, out var storageComponent))
|
2021-12-30 22:56:10 +01:00
|
|
|
{
|
2022-04-28 06:11:15 -06:00
|
|
|
_storageSystem.OpenStorageUI(entityUid.Value, uid, storageComponent);
|
2021-12-30 22:56:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-12 01:53:13 -04:00
|
|
|
|
2023-12-07 16:20:51 -05:00
|
|
|
public void TransferEntityInventories(Entity<InventoryComponent?> source, Entity<InventoryComponent?> target)
|
2022-06-12 01:53:13 -04:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
if (!Resolve(source.Owner, ref source.Comp) || !Resolve(target.Owner, ref target.Comp))
|
2023-03-06 12:37:18 -05:00
|
|
|
return;
|
|
|
|
|
|
2023-12-07 16:20:51 -05:00
|
|
|
var enumerator = new InventorySlotEnumerator(source.Comp);
|
|
|
|
|
while (enumerator.NextItem(out var item, out var slot))
|
2023-03-06 12:37:18 -05:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
if (TryUnequip(source, slot.Name, true, true, inventory: source.Comp))
|
|
|
|
|
TryEquip(target, item, slot.Name , true, true, inventory: target.Comp);
|
2022-06-12 01:53:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-30 22:56:10 +01:00
|
|
|
}
|
|
|
|
|
}
|