Some Power nullable & ComponentDependency (#3050)

* Power nullability

* AME nullability

* re-adds EnsureComponents

* conflict fix

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/RadiationCollectorComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* removes duplicate component assignment

Co-authored-by: py01 <pyronetics01@gmail.com>
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>
This commit is contained in:
collinlunn
2021-02-01 10:19:43 -06:00
committed by GitHub
parent 044040effe
commit b51b8cb182
18 changed files with 94 additions and 62 deletions

View File

@@ -1,4 +1,5 @@
using Robust.Shared.GameObjects;
#nullable enable
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.AME

View File

@@ -1,3 +1,4 @@
#nullable enable
using System.Threading.Tasks;
using System.Linq;
using Content.Server.GameObjects.Components.Interactable;
@@ -28,14 +29,13 @@ namespace Content.Server.GameObjects.Components.Power.AME
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
if (!args.User.TryGetComponent(out IHandsComponent hands))
if (!args.User.TryGetComponent<IHandsComponent>(out var hands))
{
Owner.PopupMessage(args.User, Loc.GetString("You have no hands."));
return true;
}
var activeHandEntity = hands.GetActiveHand.Owner;
if (activeHandEntity.TryGetComponent<ToolComponent>(out var multitool) && multitool.Qualities == ToolQuality.Multitool)
if (args.Using.TryGetComponent<ToolComponent>(out var multitool) && multitool.Qualities == ToolQuality.Multitool)
{
var mapGrid = _mapManager.GetGrid(args.ClickLocation.GetGridId(_serverEntityManager));

View File

@@ -179,6 +179,10 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
return ApcExternalPowerState.None;
}
var consumer = batteryStorage.Consumer;
if (consumer == null)
return ApcExternalPowerState.None;
if (consumer.ReceivedPower == 0 && consumer.DrawRate != 0)
{
return ApcExternalPowerState.None;

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{

View File

@@ -1,6 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
@@ -20,7 +20,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
void UpdateReceiverLoad(int oldLoad, int newLoad);
public IEntity ProviderOwner { get; }
public IEntity? ProviderOwner { get; }
public bool HasApcPower { get; }
}
@@ -172,7 +172,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
public void AddReceiver(PowerReceiverComponent receiver) { }
public void RemoveReceiver(PowerReceiverComponent receiver) { }
public void UpdateReceiverLoad(int oldLoad, int newLoad) { }
public IEntity ProviderOwner => default;
public IEntity? ProviderOwner => default;
}
}
}

View File

@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Content.Server.GameObjects.Components.NodeContainer;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using Content.Server.Explosions;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Shared.GameObjects.Components.Power;
@@ -11,8 +12,6 @@ using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
#nullable enable
namespace Content.Server.GameObjects.Components.Power
{
/// <summary>

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{

View File

@@ -1,6 +1,9 @@
using Robust.Shared.GameObjects;
#nullable enable
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
@@ -13,10 +16,10 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override string Name => "BatteryDischarger";
[ViewVariables]
private BatteryComponent _battery;
[ComponentDependency] private BatteryComponent? _battery = default!;
[ViewVariables]
private PowerSupplierComponent _supplier;
[ComponentDependency] private PowerSupplierComponent? _supplier = default!;
[ViewVariables(VVAccess.ReadWrite)]
public int ActiveSupplyRate { get => _activeSupplyRate; set => SetActiveSupplyRate(value); }
@@ -31,14 +34,16 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
_supplier = Owner.EnsureComponent<PowerSupplierComponent>();
Owner.EnsureComponentWarn<BatteryComponent>();
Owner.EnsureComponentWarn<PowerSupplierComponent>();
UpdateSupplyRate();
}
public void Update(float frameTime)
{
if (_battery == null)
return;
//Simplified implementation - if the battery is empty, and charge is being added to the battery
//at a lower rate that this is using it, the charge is used without creating power supply.
_battery.CurrentCharge -= ActiveSupplyRate * frameTime;
@@ -47,6 +52,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void UpdateSupplyRate()
{
if (_battery == null)
return;
if (_battery.BatteryState == BatteryState.Empty)
{
SetSupplierSupplyRate(0);
@@ -59,6 +67,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void SetSupplierSupplyRate(int newSupplierSupplyRate)
{
if (_supplier == null)
return;
if (_supplier.SupplyRate != newSupplierSupplyRate)
{
_supplier.SupplyRate = newSupplierSupplyRate;

View File

@@ -1,4 +1,6 @@
using Robust.Shared.GameObjects;
#nullable enable
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -17,10 +19,12 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private int _activeDrawRate;
[ViewVariables]
private BatteryComponent _battery;
[ComponentDependency] private BatteryComponent? _battery = default!;
[ViewVariables]
public PowerConsumerComponent Consumer { get; private set; }
public PowerConsumerComponent? Consumer => _consumer;
[ComponentDependency] private PowerConsumerComponent? _consumer = default!;
public override void ExposeData(ObjectSerializer serializer)
{
@@ -31,21 +35,26 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
Consumer = Owner.EnsureComponent<PowerConsumerComponent>();
Owner.EnsureComponentWarn<BatteryComponent>();
Owner.EnsureComponentWarn<PowerConsumerComponent>();
UpdateDrawRate();
}
public void Update(float frameTime)
{
if (_consumer == null || _battery == null)
return;
//Simplified implementation - If a frame adds more power to a partially full battery than it can hold, the power is lost.
_battery.CurrentCharge += Consumer.ReceivedPower * frameTime;
_battery.CurrentCharge += _consumer.ReceivedPower * frameTime;
UpdateDrawRate();
}
private void UpdateDrawRate()
{
if (_battery == null)
return;
if (_battery.BatteryState == BatteryState.Full)
{
SetConsumerDraw(0);
@@ -58,9 +67,12 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void SetConsumerDraw(int newConsumerDrawRate)
{
if (Consumer.DrawRate != newConsumerDrawRate)
if (_consumer == null)
return;
if (_consumer.DrawRate != newConsumerDrawRate)
{
Consumer.DrawRate = newConsumerDrawRate;
_consumer.DrawRate = newConsumerDrawRate;
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
#nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Diagnostics;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
@@ -34,7 +35,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public int ReceivedPower { get => _receivedPower; set => SetReceivedPower(value); }
private int _receivedPower;
public event EventHandler<ReceivedPowerChangedEventArgs> OnReceivedPowerChanged;
public event EventHandler<ReceivedPowerChangedEventArgs>? OnReceivedPowerChanged;
public override void ExposeData(ObjectSerializer serializer)
{

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;

View File

@@ -1,20 +1,18 @@
#nullable enable
using System;
using System.Threading;
using Content.Server.Utility;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Doors;
using Content.Shared.GameObjects.Components.Singularity;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Timer = Robust.Shared.Timers.Timer;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
@@ -27,19 +25,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private bool _enabled;
private TimeSpan _coolDownEnd;
private PhysicsComponent _collidableComponent;
[ComponentDependency] private readonly PhysicsComponent? _collidableComponent = default!;
public override void Initialize()
{
base.Initialize();
if (!Owner.TryGetComponent(out _collidableComponent))
{
Logger.Error("RadiationCollectorComponent created with no CollidableComponent");
return;
}
}
public override void HandleMessage(ComponentMessage message, IComponent component)
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
@@ -52,7 +40,8 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void OnAnchoredChanged()
{
if(_collidableComponent.Anchored) Owner.SnapToGrid();
if(_collidableComponent != null && _collidableComponent.Anchored)
Owner.SnapToGrid();
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
@@ -99,7 +88,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
protected void SetAppearance(RadiationCollectorVisualState state)
{
if (Owner.TryGetComponent(out AppearanceComponent appearance))
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{
appearance.SetData(RadiationCollectorVisuals.VisualState, state);
}

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
@@ -62,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void UpdateSupply()
{
if (Owner.TryGetComponent(out PowerSupplierComponent supplier))
if (Owner.TryGetComponent<PowerSupplierComponent>(out var supplier))
{
supplier.SupplyRate = (int) (_maxSupply * _coverage);
}
@@ -72,7 +73,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
base.Initialize();
Owner.EnsureComponentWarn(out PowerSupplierComponent _);
Owner.EnsureComponentWarn<PowerSupplierComponent>();
UpdateSupply();
}
@@ -86,7 +87,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public void OnBreak(BreakageEventArgs args)
{
var sprite = Owner.GetComponent<SpriteComponent>();
if (!Owner.TryGetComponent<SpriteComponent>(out var sprite))
return;
sprite.LayerSetState(0, "broken");
MaxSupply = 0;
}

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
#nullable enable
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Interactable;
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.GameObjects.Components.Interactable;
@@ -18,7 +19,7 @@ namespace Content.Server.GameObjects.Components.Power
public override string Name => "Wire";
[ViewVariables]
private string _wireDroppedOnCutPrototype;
private string? _wireDroppedOnCutPrototype;
/// <summary>
/// Checked by <see cref="WirePlacerComponent"/> to determine if there is
@@ -37,7 +38,10 @@ namespace Content.Server.GameObjects.Components.Power
public async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
{
if (!eventArgs.Using.TryGetComponent(out ToolComponent tool)) return false;
if (_wireDroppedOnCutPrototype == null)
return false;
if (!eventArgs.Using.TryGetComponent<ToolComponent>(out var tool)) return false;
if (!await tool.UseTool(eventArgs.User, Owner, 0.25f, ToolQuality.Cutting)) return false;
Owner.Delete();

View File

@@ -1,7 +1,7 @@
using Content.Server.GameObjects.Components.Stack;
#nullable enable
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.Map;
@@ -21,7 +21,7 @@ namespace Content.Server.GameObjects.Components.Power
public override string Name => "WirePlacer";
[ViewVariables]
private string _wirePrototypeID;
private string? _wirePrototypeID;
[ViewVariables]
private WireType _blockingWireType;
@@ -36,6 +36,9 @@ namespace Content.Server.GameObjects.Components.Power
/// <inheritdoc />
public async Task AfterInteract(AfterInteractEventArgs eventArgs)
{
if (_wirePrototypeID == null)
return;
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true)) return;
if(!_mapManager.TryGetGrid(eventArgs.ClickLocation.GetGridId(Owner.EntityManager), out var grid))
return;
@@ -50,7 +53,7 @@ namespace Content.Server.GameObjects.Components.Power
return;
}
}
if (Owner.TryGetComponent(out StackComponent stack) && !stack.Use(1))
if (Owner.TryGetComponent<StackComponent>(out var stack) && !stack.Use(1))
return;
Owner.EntityManager.SpawnEntity(_wirePrototypeID, grid.GridTileToLocal(snapPos));
}