From 1f031ae84229a529f459ae24f5390d315dc8bdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Aguilera=20Puerto?= Date: Wed, 28 Oct 2020 11:12:37 +0100 Subject: [PATCH] Add extensions to EntityPrototype --- .../Utility/EntityPrototypeHelpers.cs | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Content.Shared/Utility/EntityPrototypeHelpers.cs b/Content.Shared/Utility/EntityPrototypeHelpers.cs index 3c7115f20f..704313d329 100644 --- a/Content.Shared/Utility/EntityPrototypeHelpers.cs +++ b/Content.Shared/Utility/EntityPrototypeHelpers.cs @@ -1,5 +1,6 @@ #nullable enable using System; +using JetBrains.Annotations; using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.IoC; @@ -7,8 +8,23 @@ using Robust.Shared.Prototypes; namespace Content.Shared.Utility { + [UsedImplicitly] public static class EntityPrototypeHelpers { + public static bool HasComponent(this EntityPrototype prototype, IComponentFactory? componentFactory = null) where T : IComponent + { + return prototype.HasComponent(typeof(T), componentFactory); + } + + public static bool HasComponent(this EntityPrototype prototype, Type component, IComponentFactory? componentFactory = null) + { + componentFactory ??= IoCManager.Resolve(); + + var registration = componentFactory.GetRegistration(component); + + return prototype.Components.ContainsKey(registration.Name); + } + public static bool HasComponent(string prototype, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null) where T : IComponent { return HasComponent(prototype, typeof(T), prototypeManager, componentFactory); @@ -17,16 +33,8 @@ namespace Content.Shared.Utility public static bool HasComponent(string prototype, Type component, IPrototypeManager? prototypeManager = null, IComponentFactory? componentFactory = null) { prototypeManager ??= IoCManager.Resolve(); - componentFactory ??= IoCManager.Resolve(); - var registration = componentFactory.GetRegistration(component); - - if (!prototypeManager.TryIndex(prototype, out EntityPrototype proto)) - { - return proto.Components.ContainsKey(registration.Name); - } - - return false; + return prototypeManager.TryIndex(prototype, out EntityPrototype proto) && proto.HasComponent(component, componentFactory); } } }