2022-04-28 06:11:15 -06:00
|
|
|
using Content.Server.Storage.EntitySystems;
|
2022-10-23 11:30:37 +13:00
|
|
|
using Content.Shared.Clothing.Components;
|
2023-11-19 17:44:42 +00:00
|
|
|
using Content.Shared.Explosion;
|
2022-03-13 04:05:11 +13:00
|
|
|
using Content.Shared.Interaction.Events;
|
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);
|
|
|
|
|
|
2022-03-13 04:05:11 +13:00
|
|
|
SubscribeLocalEvent<ClothingComponent, UseInHandEvent>(OnUseInHand);
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (!TryGetContainerSlotEnumerator(ent, out var slots, ent.Comp))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// explode each item in their inventory too
|
|
|
|
|
while (slots.MoveNext(out var slot))
|
|
|
|
|
{
|
|
|
|
|
if (slot.ContainedEntity != null)
|
|
|
|
|
args.Contents.Add(slot.ContainedEntity.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 04:05:11 +13:00
|
|
|
private void OnUseInHand(EntityUid uid, ClothingComponent component, UseInHandEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Handled || !component.QuickEquip)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QuickEquip(uid, component, args);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
public void TransferEntityInventories(EntityUid uid, EntityUid target)
|
|
|
|
|
{
|
2023-03-06 12:37:18 -05:00
|
|
|
if (!TryGetContainerSlotEnumerator(uid, out var enumerator))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Dictionary<string, EntityUid> inventoryEntities = new();
|
|
|
|
|
var slots = GetSlots(uid);
|
|
|
|
|
while (enumerator.MoveNext(out var containerSlot))
|
2022-06-12 01:53:13 -04:00
|
|
|
{
|
2023-03-06 12:37:18 -05:00
|
|
|
//records all the entities stored in each of the target's slots
|
|
|
|
|
foreach (var slot in slots)
|
2022-06-12 01:53:13 -04:00
|
|
|
{
|
2023-03-06 12:37:18 -05:00
|
|
|
if (TryGetSlotContainer(target, slot.Name, out var conslot, out _) &&
|
|
|
|
|
conslot.ID == containerSlot.ID &&
|
|
|
|
|
containerSlot.ContainedEntity is { } containedEntity)
|
2022-06-12 01:53:13 -04:00
|
|
|
{
|
2023-03-06 12:37:18 -05:00
|
|
|
inventoryEntities.Add(slot.Name, containedEntity);
|
2022-06-12 01:53:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 12:37:18 -05:00
|
|
|
//drops everything in the target's inventory on the ground
|
|
|
|
|
TryUnequip(uid, containerSlot.ID, true, true);
|
|
|
|
|
}
|
|
|
|
|
// This takes the objects we removed and stored earlier
|
|
|
|
|
// and actually equips all of it to the new entity
|
|
|
|
|
foreach (var (slot, item) in inventoryEntities)
|
|
|
|
|
{
|
|
|
|
|
TryEquip(target, item, slot , true, true);
|
2022-06-12 01:53:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-30 22:56:10 +01:00
|
|
|
}
|
|
|
|
|
}
|