From 844e4f6e50b3626c2c1dff58ad1d25960c4d78b8 Mon Sep 17 00:00:00 2001 From: Paul Ritter Date: Tue, 13 Oct 2020 21:51:54 +0200 Subject: [PATCH] The power of componentdependencies (#2211) * stuff * bucklemeup * powerreceiver * things * Update RobustToolbox * Fix nullability errors Co-authored-by: DrSmugleaf --- .../ActionBlocking/CuffableComponent.cs | 28 ++++++----- .../Components/Atmos/AtmosExposedComponent.cs | 35 ++++++++------ .../Components/Buckle/BuckleComponent.cs | 47 ++++++++----------- .../PowerReceiverComponent.cs | 22 +++++---- .../Components/Strap/StrapComponent.cs | 27 ++++++----- RobustToolbox | 2 +- 6 files changed, 83 insertions(+), 78 deletions(-) diff --git a/Content.Client/GameObjects/Components/ActionBlocking/CuffableComponent.cs b/Content.Client/GameObjects/Components/ActionBlocking/CuffableComponent.cs index 82cb1a6b69..cf84ca56ea 100644 --- a/Content.Client/GameObjects/Components/ActionBlocking/CuffableComponent.cs +++ b/Content.Client/GameObjects/Components/ActionBlocking/CuffableComponent.cs @@ -1,8 +1,11 @@ -using Content.Shared.GameObjects.Components.ActionBlocking; +#nullable enable +using Content.Shared.GameObjects.Components.ActionBlocking; using Content.Shared.Preferences.Appearance; using Robust.Client.GameObjects; using Robust.Client.Graphics; +using Robust.Client.Interfaces.GameObjects.Components; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; @@ -12,9 +15,11 @@ namespace Content.Client.GameObjects.Components.ActionBlocking public class CuffableComponent : SharedCuffableComponent { [ViewVariables] - private string _currentRSI = default; + private string _currentRSI = default!; - public override void HandleComponentState(ComponentState curState, ComponentState nextState) + [ViewVariables] [ComponentDependency] private readonly SpriteComponent? _spriteComponent = null; + + public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) { if (!(curState is CuffableComponentState cuffState)) { @@ -23,23 +28,23 @@ namespace Content.Client.GameObjects.Components.ActionBlocking CanStillInteract = cuffState.CanStillInteract; - if (Owner.TryGetComponent(out var sprite)) + if (_spriteComponent != null) { - sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0); - sprite.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color); + _spriteComponent.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0); + _spriteComponent.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color); if (cuffState.NumHandsCuffed > 0) { if (_currentRSI != cuffState.RSI) // we don't want to keep loading the same RSI { _currentRSI = cuffState.RSI; - sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState), new ResourcePath(cuffState.RSI)); + _spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState), new ResourcePath(cuffState.RSI)); } else { - sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState)); // TODO: safety check to see if RSI contains the state? + _spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState)); // TODO: safety check to see if RSI contains the state? } - } + } } } @@ -47,10 +52,7 @@ namespace Content.Client.GameObjects.Components.ActionBlocking { base.OnRemove(); - if (Owner.TryGetComponent(out var sprite)) - { - sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false); - } + _spriteComponent?.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false); } } } diff --git a/Content.Server/GameObjects/Components/Atmos/AtmosExposedComponent.cs b/Content.Server/GameObjects/Components/Atmos/AtmosExposedComponent.cs index 4dbea6affc..39af196ddd 100644 --- a/Content.Server/GameObjects/Components/Atmos/AtmosExposedComponent.cs +++ b/Content.Server/GameObjects/Components/Atmos/AtmosExposedComponent.cs @@ -1,6 +1,9 @@ -using Content.Server.Atmos; +#nullable enable +using Content.Server.Atmos; using Content.Server.GameObjects.Components.Temperature; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.ComponentDependencies; +using Robust.Shared.ViewVariables; namespace Content.Server.GameObjects.Components.Atmos { @@ -13,29 +16,31 @@ namespace Content.Server.GameObjects.Components.Atmos { public override string Name => "AtmosExposed"; + [ViewVariables] + [ComponentDependency] private readonly TemperatureComponent? _temperatureComponent = null; + + [ViewVariables] + [ComponentDependency] private readonly BarotraumaComponent? _barotraumaComponent = null; + + [ViewVariables] + [ComponentDependency] private readonly FlammableComponent? _flammableComponent = null; + public void Update(TileAtmosphere tile, float frameDelta) { - if (Owner.TryGetComponent(out var temperatureComponent)) + if (_temperatureComponent != null) { if (tile.Air != null) { - var temperatureDelta = tile.Air.Temperature - temperatureComponent.CurrentTemperature; - var heat = temperatureDelta * (tile.Air.HeatCapacity * temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + temperatureComponent.HeatCapacity)); - temperatureComponent.ReceiveHeat(heat); + var temperatureDelta = tile.Air.Temperature - _temperatureComponent.CurrentTemperature; + var heat = temperatureDelta * (tile.Air.HeatCapacity * _temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + _temperatureComponent.HeatCapacity)); + _temperatureComponent.ReceiveHeat(heat); } - temperatureComponent.Update(); + _temperatureComponent.Update(); } - if (Owner.TryGetComponent(out var barotraumaComponent)) - { - barotraumaComponent.Update(tile.Air?.Pressure ?? 0); - } + _barotraumaComponent?.Update(tile.Air?.Pressure ?? 0); - if (Owner.TryGetComponent(out var flammableComponent)) - { - flammableComponent.Update(tile); - } + _flammableComponent?.Update(tile); } - } } diff --git a/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs b/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs index 0bfef0a98e..b64920a386 100644 --- a/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs +++ b/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs @@ -19,6 +19,7 @@ using Robust.Server.GameObjects.EntitySystemMessages; using Robust.Server.GameObjects.EntitySystems; using Robust.Shared.Containers; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Systems; using Robust.Shared.Interfaces.GameObjects; @@ -39,6 +40,11 @@ namespace Content.Server.GameObjects.Components.Buckle [Dependency] private readonly IEntitySystemManager _entitySystem = default!; [Dependency] private readonly IGameTiming _gameTiming = default!; + [ComponentDependency] public readonly AppearanceComponent? AppearanceComponent = null; + [ComponentDependency] private readonly ServerStatusEffectsComponent? _serverStatusEffectsComponent = null; + [ComponentDependency] private readonly StunnableComponent? _stunnableComponent = null; + [ComponentDependency] private readonly MobStateManagerComponent? _mobStateManagerComponent = null; + private int _size; /// @@ -109,15 +115,15 @@ namespace Content.Server.GameObjects.Components.Buckle /// private void BuckleStatus() { - if (Owner.TryGetComponent(out ServerStatusEffectsComponent? status)) + if (_serverStatusEffectsComponent != null) { if (Buckled) { - status.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon); + _serverStatusEffectsComponent.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon); } else { - status.RemoveStatusEffect(StatusEffect.Buckled); + _serverStatusEffectsComponent.RemoveStatusEffect(StatusEffect.Buckled); } } } @@ -280,10 +286,7 @@ namespace Content.Server.GameObjects.Components.Buckle return false; } - if (Owner.TryGetComponent(out AppearanceComponent? appearance)) - { - appearance.SetData(BuckleVisuals.Buckled, true); - } + AppearanceComponent?.SetData(BuckleVisuals.Buckled, true); BuckledTo = strap; @@ -345,12 +348,9 @@ namespace Content.Server.GameObjects.Components.Buckle Owner.Transform.WorldRotation = oldBuckledTo.Owner.Transform.WorldRotation; } - if (Owner.TryGetComponent(out AppearanceComponent? appearance)) - { - appearance.SetData(BuckleVisuals.Buckled, false); - } + AppearanceComponent?.SetData(BuckleVisuals.Buckled, false); - if (Owner.TryGetComponent(out StunnableComponent? stunnable) && stunnable.KnockedDown) + if (_stunnableComponent != null && _stunnableComponent.KnockedDown) { EntitySystem.Get().Down(Owner); } @@ -359,19 +359,16 @@ namespace Content.Server.GameObjects.Components.Buckle EntitySystem.Get().Standing(Owner); } - if (Owner.TryGetComponent(out MobStateManagerComponent? stateManager)) + if (_mobStateManagerComponent != null) { - stateManager.CurrentMobState.EnterState(Owner); + _mobStateManagerComponent.CurrentMobState.EnterState(Owner); } BuckleStatus(); - if (oldBuckledTo.Owner.TryGetComponent(out StrapComponent? strap)) - { - strap.Remove(this); - _entitySystem.GetEntitySystem() - .PlayFromEntity(strap.UnbuckleSound, Owner); - } + oldBuckledTo.Remove(this); + _entitySystem.GetEntitySystem() + .PlayFromEntity(oldBuckledTo.UnbuckleSound, Owner); SendMessage(new UnbuckleMessage(Owner, oldBuckledTo.Owner)); @@ -520,11 +517,7 @@ namespace Content.Server.GameObjects.Components.Buckle _entityManager.EventBus.UnsubscribeEvents(this); - if (BuckledTo != null && - BuckledTo.Owner.TryGetComponent(out StrapComponent? strap)) - { - strap.Remove(this); - } + BuckledTo?.Remove(this); TryUnbuckle(Owner, true); @@ -538,9 +531,9 @@ namespace Content.Server.GameObjects.Components.Buckle if (BuckledTo != null && Owner.Transform.WorldRotation.GetCardinalDir() == Direction.North && - BuckledTo.Owner.TryGetComponent(out SpriteComponent? strapSprite)) + BuckledTo.SpriteComponent != null) { - drawDepth = strapSprite.DrawDepth - 1; + drawDepth = BuckledTo.SpriteComponent.DrawDepth - 1; } return new BuckleComponentState(Buckled, drawDepth); diff --git a/Content.Server/GameObjects/Components/Power/ApcNetComponents/PowerReceiverComponent.cs b/Content.Server/GameObjects/Components/Power/ApcNetComponents/PowerReceiverComponent.cs index c45393e39c..c87979f17c 100644 --- a/Content.Server/GameObjects/Components/Power/ApcNetComponents/PowerReceiverComponent.cs +++ b/Content.Server/GameObjects/Components/Power/ApcNetComponents/PowerReceiverComponent.cs @@ -1,10 +1,12 @@ -using System; +#nullable enable +using System; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Shared.GameObjects.Components.Power; using Content.Shared.GameObjects.EntitySystems; using Robust.Server.GameObjects; using Robust.Server.Interfaces.GameObjects; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.GameObjects.Components; using Robust.Shared.Interfaces.Map; using Robust.Shared.IoC; @@ -23,9 +25,11 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents { [Dependency] private readonly IServerEntityManager _serverEntityManager = default!; + [ViewVariables] [ComponentDependency] private readonly IPhysicsComponent? _collidableComponent = null; + public override string Name => "PowerReceiver"; - public event EventHandler OnPowerStateChanged; + public event EventHandler? OnPowerStateChanged; [ViewVariables] public bool Powered => (HasApcPower || !NeedsPower) && !PowerDisabled; @@ -50,7 +54,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents /// public bool Connectable => Anchored; - private bool Anchored => !Owner.TryGetComponent(out var physics) || physics.Anchored; + private bool Anchored => _collidableComponent == null || _collidableComponent.Anchored; [ViewVariables] public bool NeedsProvider { get; private set; } = true; @@ -92,18 +96,18 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents { TryFindAndSetProvider(); } - if (Owner.TryGetComponent(out var physics)) + if (_collidableComponent != null) { AnchorUpdate(); - physics.AnchoredChanged += AnchorUpdate; + _collidableComponent.AnchoredChanged += AnchorUpdate; } } public override void OnRemove() { - if (Owner.TryGetComponent(out var physics)) + if (_collidableComponent != null) { - physics.AnchoredChanged -= AnchorUpdate; + _collidableComponent.AnchoredChanged -= AnchorUpdate; } _provider.RemoveReceiver(this); base.OnRemove(); @@ -139,7 +143,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents } } } - foundProvider = default; + foundProvider = default!; return false; } @@ -204,7 +208,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents private void OnNewPowerState() { OnPowerStateChanged?.Invoke(this, new PowerStateEventArgs(Powered)); - if (Owner.TryGetComponent(out AppearanceComponent appearance)) + if (Owner.TryGetComponent(out var appearance)) { appearance.SetData(PowerDeviceVisuals.Powered, Powered); } diff --git a/Content.Server/GameObjects/Components/Strap/StrapComponent.cs b/Content.Server/GameObjects/Components/Strap/StrapComponent.cs index aea6e759e2..96e874b78a 100644 --- a/Content.Server/GameObjects/Components/Strap/StrapComponent.cs +++ b/Content.Server/GameObjects/Components/Strap/StrapComponent.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +#nullable enable +using System.Collections.Generic; using Content.Server.GameObjects.Components.Buckle; using Content.Shared.GameObjects.Components.Strap; using Content.Shared.GameObjects.EntitySystems; @@ -7,6 +8,7 @@ using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Utility; using Robust.Server.GameObjects; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Localization; using Robust.Shared.Serialization; @@ -17,11 +19,13 @@ namespace Content.Server.GameObjects.Components.Strap [RegisterComponent] public class StrapComponent : SharedStrapComponent, IInteractHand { - private HashSet _buckledEntities; + [ComponentDependency] public readonly SpriteComponent? SpriteComponent = null; + + private HashSet _buckledEntities = null!; private StrapPosition _position; - private string _buckleSound; - private string _unbuckleSound; - private string _buckledIcon; + private string _buckleSound = null!; + private string _unbuckleSound = null!; + private string _buckledIcon = null!; /// /// The angle in degrees to rotate the player by when they get strapped @@ -103,10 +107,7 @@ namespace Content.Server.GameObjects.Components.Strap _occupiedSize += buckle.Size; - if (buckle.Owner.TryGetComponent(out AppearanceComponent appearance)) - { - appearance.SetData(StrapVisuals.RotationAngle, _rotation); - } + buckle.AppearanceComponent?.SetData(StrapVisuals.RotationAngle, _rotation); SendMessage(new StrapMessage(buckle.Owner, Owner)); @@ -151,7 +152,7 @@ namespace Content.Server.GameObjects.Components.Strap foreach (var entity in _buckledEntities) { - if (entity.TryGetComponent(out BuckleComponent buckle)) + if (entity.TryGetComponent(out var buckle)) { buckle.TryUnbuckle(entity, true); } @@ -168,7 +169,7 @@ namespace Content.Server.GameObjects.Components.Strap bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs) { - if (!eventArgs.User.TryGetComponent(out BuckleComponent buckle)) + if (!eventArgs.User.TryGetComponent(out var buckle)) { return false; } @@ -184,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Strap data.Visibility = VerbVisibility.Invisible; if (!ActionBlockerSystem.CanInteract(component.Owner) || - !user.TryGetComponent(out BuckleComponent buckle) || + !user.TryGetComponent(out var buckle) || buckle.BuckledTo != null && buckle.BuckledTo != component || user == component.Owner) { @@ -215,7 +216,7 @@ namespace Content.Server.GameObjects.Components.Strap protected override void Activate(IEntity user, StrapComponent component) { - if (!user.TryGetComponent(out BuckleComponent buckle)) + if (!user.TryGetComponent(out var buckle)) { return; } diff --git a/RobustToolbox b/RobustToolbox index bc87f2ea9c..f131f367d8 160000 --- a/RobustToolbox +++ b/RobustToolbox @@ -1 +1 @@ -Subproject commit bc87f2ea9cd0b735e8c7bffd02ed9fef037730b6 +Subproject commit f131f367d8d29e464ccdf4b58bfb550e3ff72fc6