Files
Delta-v/Content.Shared/_DV/Traits/Conditions/HasCompCondition.cs
T
Milonandpre-commit-ci[bot] 884cf115c6 more traits improvements (#5285)
* wowie

* whoopsie

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* WHY DID I DO THAT

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-25 09:12:38 -06:00

52 lines
1.6 KiB
C#

using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared._DV.Traits.Conditions;
/// <summary>
/// Condition that checks if the player has a specific component.
/// Use Invert = true to check if the player does NOT have the component.
/// </summary>
public sealed partial class HasCompCondition : BaseTraitCondition
{
/// <summary>
/// The component name to check for (e.g., "Pacifism").
/// </summary>
[DataField(required: true, customTypeSerializer: typeof(ComponentNameSerializer))]
public string Component = string.Empty;
/// <summary>
/// The tooltip text to display, if any.
/// </summary>
[DataField]
public LocId? Tooltip;
protected override bool EvaluateImplementation(TraitConditionContext ctx)
{
if (string.IsNullOrEmpty(Component))
return false;
try
{
var compType = ctx.CompFactory.GetRegistration(Component).Type;
return ctx.EntMan.HasComponent(ctx.Player, compType);
}
catch (Exception)
{
// Log the actual error instead of silently catching
ctx.LogMan.GetSawmill("traits").Error($"Failed to get component registration for '{Component}'");
return false;
}
}
public override string GetTooltip(IPrototypeManager proto, ILocalizationManager loc)
{
// If there's a custom tooltip supplied, use that
if (Tooltip is not null)
return Loc.GetString(Tooltip);
// No tooltip for this condition since we're dealing with comps
return string.Empty;
}
}