Files
crystall-punk-14/Content.Server/PDA/PDAExtensions.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2021-12-16 23:42:02 +13:00
using System.Diagnostics.CodeAnalysis;
2021-06-09 22:19:39 +02:00
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
2021-12-16 23:42:02 +13:00
using Content.Shared.Access.Components;
using Content.Shared.PDA;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
2021-06-09 22:19:39 +02:00
namespace Content.Server.PDA
{
public static class PdaExtensions
{
/// <summary>
/// Gets the id that a player is holding in their hands or inventory.
/// Order: Hands > ID slot > PDA in ID slot
/// </summary>
/// <param name="player">The player to check in.</param>
/// <returns>The id card component.</returns>
2021-12-04 14:14:22 +01:00
public static IdCardComponent? GetHeldId(this EntityUid player)
{
IdCardComponent? firstIdInPda = null;
2021-12-08 17:32:32 +01:00
var entMan = IoCManager.Resolve<IEntityManager>();
if (entMan.TryGetComponent(player, out HandsComponent? hands))
{
foreach (var item in hands.GetAllHeldItems())
{
if (firstIdInPda == null &&
2021-12-08 17:32:32 +01:00
entMan.TryGetComponent(item.Owner, out PDAComponent? pda) &&
pda.ContainedID != null)
{
firstIdInPda = pda.ContainedID;
}
2021-12-08 17:32:32 +01:00
if (entMan.TryGetComponent(item.Owner, out IdCardComponent? card))
{
return card;
}
}
}
if (firstIdInPda != null)
{
return firstIdInPda;
}
IdCardComponent? firstIdInInventory = null;
2021-12-08 17:32:32 +01:00
if (entMan.TryGetComponent(player, out InventoryComponent? inventory))
{
foreach (var item in inventory.GetAllHeldItems())
{
if (firstIdInInventory == null &&
2021-12-08 17:32:32 +01:00
entMan.TryGetComponent(item, out PDAComponent? pda) &&
pda.ContainedID != null)
{
firstIdInInventory = pda.ContainedID;
}
2021-12-08 17:32:32 +01:00
if (entMan.TryGetComponent(item, out IdCardComponent? card))
{
return card;
}
}
}
return firstIdInInventory;
}
/// <summary>
/// Gets the id that a player is holding in their hands or inventory.
/// Order: Hands > ID slot > PDA in ID slot
/// </summary>
/// <param name="player">The player to check in.</param>
/// <param name="id">The id card component.</param>
/// <returns>true if found, false otherwise.</returns>
2021-12-04 14:14:22 +01:00
public static bool TryGetHeldId(this EntityUid player, [NotNullWhen(true)] out IdCardComponent? id)
{
return (id = player.GetHeldId()) != null;
}
}
}