2023-02-26 06:14:35 +13:00
|
|
|
using Content.Shared.Containers.ItemSlots;
|
2022-01-05 17:20:25 +13:00
|
|
|
using Content.Shared.PowerCell.Components;
|
2023-02-26 06:14:35 +13:00
|
|
|
using Content.Shared.Rejuvenate;
|
2022-01-05 17:20:25 +13:00
|
|
|
using Robust.Shared.Containers;
|
|
|
|
|
|
|
|
|
|
namespace Content.Shared.PowerCell;
|
|
|
|
|
|
|
|
|
|
public abstract class SharedPowerCellSystem : EntitySystem
|
|
|
|
|
{
|
2023-02-26 06:14:35 +13:00
|
|
|
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
|
2023-05-13 18:28:11 +02:00
|
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
2023-02-26 06:14:35 +13:00
|
|
|
|
2022-01-05 17:20:25 +13:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2023-02-26 06:14:35 +13:00
|
|
|
SubscribeLocalEvent<PowerCellSlotComponent, RejuvenateEvent>(OnRejuventate);
|
2022-01-05 17:20:25 +13:00
|
|
|
SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
|
|
|
|
|
SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
|
|
|
|
|
SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 06:14:35 +13:00
|
|
|
private void OnRejuventate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args)
|
|
|
|
|
{
|
2023-05-14 13:15:18 +10:00
|
|
|
if (!_itemSlots.TryGetSlot(uid, component.CellSlotId, out var itemSlot) || !itemSlot.Item.HasValue)
|
2023-02-26 06:14:35 +13:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// charge entity batteries and remove booby traps.
|
|
|
|
|
RaiseLocalEvent(itemSlot.Item.Value, args);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:20:25 +13:00
|
|
|
private void OnCellInsertAttempt(EntityUid uid, PowerCellSlotComponent component, ContainerIsInsertingAttemptEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (!component.Initialized)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-10-15 13:15:39 -07:00
|
|
|
if (args.Container.ID != component.CellSlotId)
|
2022-01-05 17:20:25 +13:00
|
|
|
return;
|
|
|
|
|
|
2022-06-16 18:37:07 +10:00
|
|
|
if (!HasComp<PowerCellComponent>(args.EntityUid))
|
2022-01-05 17:20:25 +13:00
|
|
|
{
|
|
|
|
|
args.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnCellInserted(EntityUid uid, PowerCellSlotComponent component, EntInsertedIntoContainerMessage args)
|
|
|
|
|
{
|
|
|
|
|
if (!component.Initialized)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-10-15 13:15:39 -07:00
|
|
|
if (args.Container.ID != component.CellSlotId)
|
2022-01-05 17:20:25 +13:00
|
|
|
return;
|
2023-05-13 18:28:11 +02:00
|
|
|
_appearance.SetData(uid, PowerCellSlotVisuals.Enabled, true);
|
2022-01-05 17:20:25 +13:00
|
|
|
RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 01:27:56 -04:00
|
|
|
protected virtual void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
|
2022-01-05 17:20:25 +13:00
|
|
|
{
|
2022-10-15 13:15:39 -07:00
|
|
|
if (args.Container.ID != component.CellSlotId)
|
2022-01-05 17:20:25 +13:00
|
|
|
return;
|
2023-05-13 18:28:11 +02:00
|
|
|
_appearance.SetData(uid, PowerCellSlotVisuals.Enabled, false);
|
2022-01-05 17:20:25 +13:00
|
|
|
RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
|
|
|
|
|
}
|
|
|
|
|
}
|