using Content.Shared._Sunrise.Biocode.Components;
using Content.Shared.Containers;
using Content.Shared.Hands.Components;
using Content.Shared.Inventory;
using Content.Shared.Storage.Components;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Storage;
using Content.Shared.Hands;
using Content.Shared.Inventory.Events;
using Robust.Shared.Containers;
namespace Content.Shared._Sunrise.Biocode.Systems;
///
/// System that handles automatic deactivation of biocoded items when they're not in authorized user's possession.
///
public abstract class BiocodeDeactivationSystem : EntitySystem
{
[Dependency] private readonly BiocodeSystem _biocodeSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnActivate);
SubscribeLocalEvent(OnUseInHand);
SubscribeLocalEvent(OnItemDropped);
SubscribeLocalEvent(OnItemPickedUp);
}
private void OnItemDropped(EntityUid uid, BiocodeDeactivationComponent component, DroppedEvent args)
{
if (!component.DeactivateOnRemoval)
return;
// Check if this item has biocode
if (!TryComp(uid, out var biocodeComponent))
return;
// Item was dropped, deactivate it
DeactivateItem(uid);
}
private void OnItemPickedUp(EntityUid uid, BiocodeDeactivationComponent component, GotEquippedEvent args)
{
if (!component.DeactivateOnUnauthorized)
return;
// Check if this item has biocode
if (!TryComp(uid, out var biocodeComponent))
return;
// Check if the picker is authorized
if (_biocodeSystem.CanUse(args.Equipee, biocodeComponent.Factions))
return;
// Picker is not authorized, deactivate the item
DeactivateItem(uid);
}
private void OnActivate(EntityUid uid, BiocodeDeactivationComponent component, ActivateInWorldEvent args)
{
if (args.Handled || !args.Complex)
return;
// Check if this item has biocode
if (!TryComp(uid, out var biocodeComponent))
return;
// Check if user is authorized
if (_biocodeSystem.CanUse(args.User, biocodeComponent.Factions))
return;
// User is not authorized, show alert and prevent activation
var alertText = component.AlertText ?? biocodeComponent.AlertText;
ShowAlert(args.User, alertText);
args.Handled = true;
}
private void OnUseInHand(EntityUid uid, BiocodeDeactivationComponent component, UseInHandEvent args)
{
if (args.Handled)
return;
// Check if this item has biocode
if (!TryComp(uid, out var biocodeComponent))
return;
// Check if user is authorized
if (_biocodeSystem.CanUse(args.User, biocodeComponent.Factions))
return;
// User is not authorized, show alert and prevent use
var alertText = component.AlertText ?? biocodeComponent.AlertText;
ShowAlert(args.User, alertText);
args.Handled = true;
}
///
/// Shows an alert to the user. Override this method to implement specific alert display logic.
///
protected abstract void ShowAlert(EntityUid user, string alertText);
///
/// Deactivates the item. Override this method in the shared system to implement specific deactivation logic.
///
protected abstract void DeactivateItem(EntityUid uid);
private EntityUid? GetContainerOwner(EntityUid container)
{
// Try to find the owner through various container types
if (TryComp(container, out _))
{
return container;
}
if (TryComp(container, out _))
{
return container;
}
if (TryComp(container, out _))
{
return container;
}
// Check if this container is inside another entity
var parent = Transform(container).ParentUid;
if (parent != EntityUid.Invalid)
{
return GetContainerOwner(parent);
}
return null;
}
}