From dc03f1f5450fd087ea38b1d624831e066f725673 Mon Sep 17 00:00:00 2001 From: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Date: Sun, 16 May 2021 22:33:21 +0200 Subject: [PATCH] AnchorableComponent light cleanup. (#4009) * Remove awful commands * Unanchoring is a word. * Fix disposal tests.... * Slight anchorable cleanup --- .../Tests/Disposal/DisposalUnitTest.cs | 13 +- .../Commands/Interactable/AnchorCommand.cs | 58 --------- .../Commands/Interactable/UnAnchorCommand.cs | 58 --------- Content.Server/Commands/SetAnchorCommand.cs | 60 --------- .../Components/AnchorableComponent.cs | 116 ++++++++++-------- Content.Server/GlobalVerbs/SetAnchorVerb.cs | 55 --------- SpaceStation14.sln.DotSettings | 1 + 7 files changed, 73 insertions(+), 288 deletions(-) delete mode 100644 Content.Server/Commands/Interactable/AnchorCommand.cs delete mode 100644 Content.Server/Commands/Interactable/UnAnchorCommand.cs delete mode 100644 Content.Server/Commands/SetAnchorCommand.cs delete mode 100644 Content.Server/GlobalVerbs/SetAnchorVerb.cs diff --git a/Content.IntegrationTests/Tests/Disposal/DisposalUnitTest.cs b/Content.IntegrationTests/Tests/Disposal/DisposalUnitTest.cs index 74d194abad..527eb24aff 100644 --- a/Content.IntegrationTests/Tests/Disposal/DisposalUnitTest.cs +++ b/Content.IntegrationTests/Tests/Disposal/DisposalUnitTest.cs @@ -8,6 +8,7 @@ using NUnit.Framework; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; +using Robust.Shared.Physics; namespace Content.IntegrationTests.Tests.Disposal { @@ -63,6 +64,8 @@ namespace Content.IntegrationTests.Tests.Disposal name: HumanDummy id: HumanDummy components: + - type: Body + - type: MobState - type: Damageable damagePrototype: biologicalDamageContainer @@ -70,6 +73,7 @@ namespace Content.IntegrationTests.Tests.Disposal name: WrenchDummy id: WrenchDummy components: + - type: Item - type: Tool qualities: - Anchoring @@ -120,16 +124,13 @@ namespace Content.IntegrationTests.Tests.Disposal Assert.True(disposalTrunk.HasComponent()); // Can't insert, unanchored and unpowered - var disposalUnitAnchorable = disposalUnit.GetComponent(); - await disposalUnitAnchorable.TryUnAnchor(human, null, true); + var physics = disposalUnit.GetComponent(); + physics.BodyType = BodyType.Dynamic; Assert.False(unit.Anchored); UnitInsertContains(unit, false, human, wrench, disposalUnit, disposalTrunk); // Anchor the disposal unit - await disposalUnitAnchorable.TryAnchor(human, null, true); - Assert.True(disposalUnit.TryGetComponent(out AnchorableComponent? anchorableUnit)); - Assert.True(await anchorableUnit!.TryAnchor(human, wrench)); - Assert.True(unit.Anchored); + physics.BodyType = BodyType.Static; // No power Assert.False(unit.Powered); diff --git a/Content.Server/Commands/Interactable/AnchorCommand.cs b/Content.Server/Commands/Interactable/AnchorCommand.cs deleted file mode 100644 index e67a85201e..0000000000 --- a/Content.Server/Commands/Interactable/AnchorCommand.cs +++ /dev/null @@ -1,58 +0,0 @@ -#nullable enable -using System.Linq; -using Content.Server.Administration; -using Content.Server.GameObjects.Components; -using Content.Shared.Administration; -using Robust.Server.GameObjects; -using Robust.Server.Player; -using Robust.Shared.Console; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; - -namespace Content.Server.Commands.Interactable -{ - [AdminCommand(AdminFlags.Debug)] - class AnchorCommand : IConsoleCommand - { - public string Command => "anchor"; - public string Description => "Anchors all entities in a radius around the user"; - public string Help => $"Usage: {Command} "; - - public void Execute(IConsoleShell shell, string argStr, string[] args) - { - var player = shell.Player as IPlayerSession; - if (player?.AttachedEntity == null) - { - return; - } - - if (args.Length != 1) - { - shell.WriteLine(Help); - return; - } - - if (!int.TryParse(args[0], out var radius)) - { - shell.WriteLine($"{args[0]} isn't a valid integer."); - return; - } - - if (radius < 0) - { - shell.WriteLine("Radius must be positive."); - return; - } - - var entities = IoCManager.Resolve().GetEntitiesInRange(player.AttachedEntity, radius).ToList(); - - foreach (var entity in entities) - { - if (entity.TryGetComponent(out AnchorableComponent? anchorable)) - { - _ = anchorable.TryAnchor(player.AttachedEntity, force: true); - } - } - } - } -} diff --git a/Content.Server/Commands/Interactable/UnAnchorCommand.cs b/Content.Server/Commands/Interactable/UnAnchorCommand.cs deleted file mode 100644 index b7d0f4a40e..0000000000 --- a/Content.Server/Commands/Interactable/UnAnchorCommand.cs +++ /dev/null @@ -1,58 +0,0 @@ -#nullable enable -using System.Linq; -using Content.Server.Administration; -using Content.Server.GameObjects.Components; -using Content.Shared.Administration; -using Robust.Server.GameObjects; -using Robust.Server.Player; -using Robust.Shared.Console; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; - -namespace Content.Server.Commands.Interactable -{ - [AdminCommand(AdminFlags.Debug)] - class UnAnchorCommand : IConsoleCommand - { - public string Command => "unanchor"; - public string Description => "Unanchors all anchorable entities in a radius around the user"; - public string Help => $"Usage: {Command} "; - - public void Execute(IConsoleShell shell, string argStr, string[] args) - { - var player = shell.Player as IPlayerSession; - if (player?.AttachedEntity == null) - { - return; - } - - if (args.Length != 1) - { - shell.WriteLine(Help); - return; - } - - if (!int.TryParse(args[0], out var radius)) - { - shell.WriteLine($"{args[0]} isn't a valid integer."); - return; - } - - if (radius < 0) - { - shell.WriteLine("Radius must be positive."); - return; - } - - var entities = IoCManager.Resolve().GetEntitiesInRange(player.AttachedEntity, radius).ToList(); - - foreach (var entity in entities) - { - if (entity.TryGetComponent(out AnchorableComponent? anchorable)) - { - _ = anchorable.TryUnAnchor(player.AttachedEntity, force: true); - } - } - } - } -} diff --git a/Content.Server/Commands/SetAnchorCommand.cs b/Content.Server/Commands/SetAnchorCommand.cs deleted file mode 100644 index 94b7215503..0000000000 --- a/Content.Server/Commands/SetAnchorCommand.cs +++ /dev/null @@ -1,60 +0,0 @@ -#nullable enable -using Content.Server.Administration; -using Content.Server.GameObjects.Components; -using Content.Shared.Administration; -using Robust.Shared.Console; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Physics; - -namespace Content.Server.Commands -{ - [AdminCommand(AdminFlags.Debug)] - public class SetAnchorCommand : IConsoleCommand - { - public string Command => "setanchor"; - public string Description => "Sets the anchoring state of an entity."; - public string Help => "setanchor "; - public async void Execute(IConsoleShell shell, string argStr, string[] args) - { - if (args.Length == 0 || args.Length > 2) - { - shell.WriteLine("Invalid number of argument!"); - return; - } - - if (!int.TryParse(args[0], out var id)) - { - shell.WriteLine("Invalid argument specified!"); - return; - } - - var entId = new EntityUid(id); - - var entityManager = IoCManager.Resolve(); - - if (!entityManager.TryGetEntity(entId, out var entity) || entity.Deleted || !entity.TryGetComponent(out AnchorableComponent? anchorable)) - { - shell.WriteLine("Invalid entity specified!"); - return; - } - - if (args.Length == 2) - { - if (!bool.TryParse(args[1], out var value)) - { - shell.WriteLine("Invalid argument specified!"); - return; - } - - if (value) - await anchorable.TryAnchor(default, force: true); - else - await anchorable.TryUnAnchor(default, force: true); - return; - } - - await anchorable.TryToggleAnchor(default, force: true); - } - } -} diff --git a/Content.Server/GameObjects/Components/AnchorableComponent.cs b/Content.Server/GameObjects/Components/AnchorableComponent.cs index 07a4fc1835..8d6a7ce2ed 100644 --- a/Content.Server/GameObjects/Components/AnchorableComponent.cs +++ b/Content.Server/GameObjects/Components/AnchorableComponent.cs @@ -1,6 +1,5 @@ #nullable enable using System; -using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Content.Server.GameObjects.Components.Interactable; using Content.Server.GameObjects.Components.Pulling; @@ -14,6 +13,7 @@ using Robust.Shared.ViewVariables; namespace Content.Server.GameObjects.Components { + // TODO: Move this component's logic to an EntitySystem. [RegisterComponent] public class AnchorableComponent : Component, IInteractUsing { @@ -32,49 +32,40 @@ namespace Content.Server.GameObjects.Components [DataField("snap")] public bool Snap { get; private set; } = true; - public override void Initialize() - { - base.Initialize(); - Owner.EnsureComponent(out _physicsComponent); - } - /// /// Checks if a tool can change the anchored status. /// /// The user doing the action - /// The tool being used, can be null if forcing it - /// Whether or not to check if the tool is valid + /// The tool being used + /// True if we're anchoring, and false if we're unanchoring. /// true if it is valid, false otherwise - private async Task Valid(IEntity? user, IEntity? utilizing, [NotNullWhen(true)] bool force = false) + private async Task Valid(IEntity user, IEntity utilizing, bool anchoring) { if (!Owner.HasComponent()) { return false; } - if (user != null && !force) - { - if (utilizing == null || - !utilizing.TryGetComponent(out ToolComponent? tool) || - !(await tool.UseTool(user, Owner, 0.5f, Tool))) - { - return false; - } - } + BaseAnchoredAttemptEvent attempt = + anchoring ? new AnchorAttemptEvent(user, utilizing) : new UnanchorAttemptEvent(user, utilizing); - return true; + Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false); + + if (attempt.Cancelled) + return false; + + return utilizing.TryGetComponent(out ToolComponent? tool) && await tool.UseTool(user, Owner, 0.5f, Tool); } /// /// Tries to anchor the owner of this component. /// /// The entity doing the anchoring - /// The tool being used, if any - /// Whether or not to ignore valid tool checks + /// The tool being used /// true if anchored, false otherwise - public async Task TryAnchor(IEntity? user, IEntity? utilizing = null, bool force = false) + public async Task TryAnchor(IEntity user, IEntity utilizing) { - if (!(await Valid(user, utilizing, force))) + if (!(await Valid(user, utilizing, true))) { return false; } @@ -82,13 +73,6 @@ namespace Content.Server.GameObjects.Components if (_physicsComponent == null) return false; - var attempt = new AnchorAttemptMessage(); - - Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false); - - if (attempt.Cancelled) - return false; - // Snap rotation to cardinal (multiple of 90) var rot = Owner.Transform.LocalRotation; Owner.Transform.LocalRotation = Math.Round(rot / (Math.PI / 2)) * (Math.PI / 2); @@ -106,7 +90,7 @@ namespace Content.Server.GameObjects.Components _physicsComponent.BodyType = BodyType.Static; - Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new AnchoredMessage(), false); + Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new AnchoredEvent(user, utilizing), false); return true; } @@ -118,9 +102,9 @@ namespace Content.Server.GameObjects.Components /// The tool being used, if any /// Whether or not to ignore valid tool checks /// true if unanchored, false otherwise - public async Task TryUnAnchor(IEntity? user, IEntity? utilizing = null, bool force = false) + public async Task TryUnAnchor(IEntity user, IEntity utilizing) { - if (!(await Valid(user, utilizing, force))) + if (!(await Valid(user, utilizing, false))) { return false; } @@ -128,16 +112,9 @@ namespace Content.Server.GameObjects.Components if (_physicsComponent == null) return false; - var attempt = new UnanchorAttemptMessage(); - - Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false); - - if (attempt.Cancelled) - return false; - _physicsComponent.BodyType = BodyType.Dynamic; - Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new UnanchoredMessage(), false); + Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new UnanchoredEvent(user, utilizing), false); return true; } @@ -146,17 +123,16 @@ namespace Content.Server.GameObjects.Components /// Tries to toggle the anchored status of this component's owner. /// /// The entity doing the unanchoring - /// The tool being used, if any - /// Whether or not to ignore valid tool checks + /// The tool being used /// true if toggled, false otherwise - public async Task TryToggleAnchor(IEntity? user, IEntity? utilizing = null, bool force = false) + public async Task TryToggleAnchor(IEntity user, IEntity utilizing) { if (_physicsComponent == null) return false; return _physicsComponent.BodyType == BodyType.Static ? - await TryUnAnchor(user, utilizing, force) : - await TryAnchor(user, utilizing, force); + await TryUnAnchor(user, utilizing) : + await TryAnchor(user, utilizing); } async Task IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs) @@ -165,9 +141,47 @@ namespace Content.Server.GameObjects.Components } } - public class AnchorAttemptMessage : CancellableEntityEventArgs { } - public class UnanchorAttemptMessage : CancellableEntityEventArgs { } + public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs + { + public IEntity User { get; } + public IEntity Tool { get; } - public class AnchoredMessage : EntityEventArgs {} - public class UnanchoredMessage : EntityEventArgs {} + protected BaseAnchoredAttemptEvent(IEntity user, IEntity tool) + { + User = user; + Tool = tool; + } + } + + public class AnchorAttemptEvent : BaseAnchoredAttemptEvent + { + public AnchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { } + } + + public class UnanchorAttemptEvent : BaseAnchoredAttemptEvent + { + public UnanchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { } + } + + public abstract class BaseAnchoredEvent : EntityEventArgs + { + public IEntity User { get; } + public IEntity Tool { get; } + + protected BaseAnchoredEvent(IEntity user, IEntity tool) + { + User = user; + Tool = tool; + } + } + + public class AnchoredEvent : BaseAnchoredEvent + { + public AnchoredEvent(IEntity user, IEntity tool) : base(user, tool) { } + } + + public class UnanchoredEvent : BaseAnchoredEvent + { + public UnanchoredEvent(IEntity user, IEntity tool) : base(user, tool) { } + } } diff --git a/Content.Server/GlobalVerbs/SetAnchorVerb.cs b/Content.Server/GlobalVerbs/SetAnchorVerb.cs deleted file mode 100644 index 520f4d9a0e..0000000000 --- a/Content.Server/GlobalVerbs/SetAnchorVerb.cs +++ /dev/null @@ -1,55 +0,0 @@ -#nullable enable -using Content.Shared.GameObjects.Verbs; -using Robust.Server.Console; -using Robust.Server.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Physics; - -namespace Content.Server.GlobalVerbs -{ - [GlobalVerb] - public class SetAnchorVerb : GlobalVerb - { - public override bool RequireInteractionRange => false; - public override bool BlockedByContainers => false; - - public override void GetData(IEntity user, IEntity target, VerbData data) - { - data.CategoryData = VerbCategories.Debug; - data.Visibility = VerbVisibility.Invisible; - data.IconTexture = "/Textures/Interface/VerbIcons/anchor.svg.192dpi.png"; - - var groupController = IoCManager.Resolve(); - - if (user.TryGetComponent(out var player)) - { - if (!target.TryGetComponent(out PhysicsComponent? physics)) - { - return; - } - - if (groupController.CanCommand(player.PlayerSession, "setanchor")) - { - data.Text = physics.BodyType == BodyType.Static ? "Unanchor" : "Anchor"; - data.Visibility = VerbVisibility.Visible; - } - } - } - - public override void Activate(IEntity user, IEntity target) - { - if (user.TryGetComponent(out var player)) - { - var groupController = IoCManager.Resolve(); - if (!groupController.CanCommand(player.PlayerSession, "setanchor")) - return; - - if (target.TryGetComponent(out PhysicsComponent? physics)) - { - physics.BodyType = physics.BodyType == BodyType.Static ? BodyType.Dynamic : BodyType.Static; - } - } - } - } -} diff --git a/SpaceStation14.sln.DotSettings b/SpaceStation14.sln.DotSettings index 7ad5f23278..fe00eccf41 100644 --- a/SpaceStation14.sln.DotSettings +++ b/SpaceStation14.sln.DotSettings @@ -169,6 +169,7 @@ True True True + True True True True