From ed8bcb0a664b12f6ac5f4566bf332acc6686527e Mon Sep 17 00:00:00 2001
From: deltanedas <39013340+deltanedas@users.noreply.github.com>
Date: Fri, 9 Aug 2024 07:43:57 +0000
Subject: [PATCH] improve job special (#30753)
* cleanup of AddComponentSpecial
* add RemoveComponentSpecial
* require
---------
Co-authored-by: deltanedas <@deltanedas:kde.org>
---
Content.Server/Jobs/AddComponentSpecial.cs | 42 +++++++------------
Content.Server/Jobs/RemoveComponentSpecial.cs | 16 +++++++
2 files changed, 31 insertions(+), 27 deletions(-)
create mode 100644 Content.Server/Jobs/RemoveComponentSpecial.cs
diff --git a/Content.Server/Jobs/AddComponentSpecial.cs b/Content.Server/Jobs/AddComponentSpecial.cs
index 6489068d1f..3bac923656 100644
--- a/Content.Server/Jobs/AddComponentSpecial.cs
+++ b/Content.Server/Jobs/AddComponentSpecial.cs
@@ -1,34 +1,22 @@
using Content.Shared.Roles;
-using JetBrains.Annotations;
using Robust.Shared.Prototypes;
-using Robust.Shared.Serialization.Manager;
-namespace Content.Server.Jobs
+namespace Content.Server.Jobs;
+
+public sealed partial class AddComponentSpecial : JobSpecial
{
- [UsedImplicitly]
- public sealed partial class AddComponentSpecial : JobSpecial
+ [DataField(required: true)]
+ public ComponentRegistry Components { get; private set; } = new();
+
+ ///
+ /// If this is true then existing components will be removed and replaced with these ones.
+ ///
+ [DataField]
+ public bool RemoveExisting = true;
+
+ public override void AfterEquip(EntityUid mob)
{
- [DataField("components")]
- [AlwaysPushInheritance]
- public ComponentRegistry Components { get; private set; } = new();
-
- public override void AfterEquip(EntityUid mob)
- {
- // now its a registry of components, still throws i bet.
- // TODO: This is hot garbage and probably needs an engine change to not be a POS.
- var factory = IoCManager.Resolve();
- var entityManager = IoCManager.Resolve();
- var serializationManager = IoCManager.Resolve();
-
- foreach (var (name, data) in Components)
- {
- var component = (Component) factory.GetComponent(name);
-
- var temp = (object)component;
- serializationManager.CopyTo(data.Component, ref temp);
- entityManager.RemoveComponent(mob, temp!.GetType());
- entityManager.AddComponent(mob, (Component)temp);
- }
- }
+ var entMan = IoCManager.Resolve();
+ entMan.AddComponents(mob, Components, removeExisting: RemoveExisting);
}
}
diff --git a/Content.Server/Jobs/RemoveComponentSpecial.cs b/Content.Server/Jobs/RemoveComponentSpecial.cs
new file mode 100644
index 0000000000..7a9b03b3ba
--- /dev/null
+++ b/Content.Server/Jobs/RemoveComponentSpecial.cs
@@ -0,0 +1,16 @@
+using Content.Shared.Roles;
+using Robust.Shared.Prototypes;
+
+namespace Content.Server.Jobs;
+
+public sealed partial class RemoveComponentSpecial : JobSpecial
+{
+ [DataField(required: true)]
+ public ComponentRegistry Components { get; private set; } = new();
+
+ public override void AfterEquip(EntityUid mob)
+ {
+ var entMan = IoCManager.Resolve();
+ entMan.RemoveComponents(mob, Components);
+ }
+}