Add a test that puts all components on an entity and checks for no exceptions (#1815)
* Add test that puts all components on an entity and checks for no exceptions Also fix all the exceptions that happened because of this * Add comments to the test * Fix nullable errors * Fix more nullable errors * More nullable error fixes * Unignore basic actor component * Fix more nullable errors * NULLABLE ERROR * Add string interpolation * Merge if checks * Remove redundant pragma warning disable 649 * Address reviews * Remove null wrappers around TryGetComponent * Merge conflict fixes * APC battery component error fix * Fix power test * Fix atmos mapgrid usages
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components.VendingMachines;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
@@ -29,27 +29,28 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class VendingMachineComponent : SharedVendingMachineComponent, IActivate, IExamine, IBreakAct, IWires
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IRobustRandom _random;
|
||||
#pragma warning restore 649
|
||||
private AppearanceComponent _appearance;
|
||||
private BoundUserInterface _userInterface;
|
||||
private PowerReceiverComponent _powerReceiver;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
private bool _ejecting = false;
|
||||
private bool _ejecting;
|
||||
private TimeSpan _animationDuration = TimeSpan.Zero;
|
||||
private string _packPrototypeId;
|
||||
private string _description;
|
||||
private string _spriteName;
|
||||
private string _packPrototypeId = "";
|
||||
private string? _description;
|
||||
private string _spriteName = "";
|
||||
|
||||
private bool Powered => _powerReceiver.Powered;
|
||||
private bool _broken = false;
|
||||
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
private bool _broken;
|
||||
|
||||
private string _soundVend;
|
||||
private string _soundVend = "";
|
||||
|
||||
private BoundUserInterface? UserInterface =>
|
||||
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
|
||||
ui.TryGetBoundUserInterface(VendingMachineUiKey.Key, out var boundUi)
|
||||
? boundUi
|
||||
: null;
|
||||
|
||||
public void Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if(!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
if(!eventArgs.User.TryGetComponent(out IActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -62,7 +63,7 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
wires.OpenInterface(actor.playerSession);
|
||||
} else
|
||||
{
|
||||
_userInterface.Open(actor.playerSession);
|
||||
UserInterface?.Open(actor.playerSession);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,25 +107,32 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
|
||||
.GetBoundUserInterface(VendingMachineUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
||||
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
|
||||
_powerReceiver.OnPowerStateChanged += UpdatePower;
|
||||
TrySetVisualState(_powerReceiver.Powered ? VendingMachineVisualState.Normal : VendingMachineVisualState.Off);
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
receiver.OnPowerStateChanged += UpdatePower;
|
||||
TrySetVisualState(receiver.Powered ? VendingMachineVisualState.Normal : VendingMachineVisualState.Off);
|
||||
}
|
||||
|
||||
InitializeFromPrototype();
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
_appearance = null;
|
||||
_powerReceiver.OnPowerStateChanged -= UpdatePower;
|
||||
_powerReceiver = null;
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
receiver.OnPowerStateChanged -= UpdatePower;
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
private void UpdatePower(object sender, PowerStateEventArgs args)
|
||||
private void UpdatePower(object? sender, PowerStateEventArgs args)
|
||||
{
|
||||
var state = args.Powered ? VendingMachineVisualState.Normal : VendingMachineVisualState.Off;
|
||||
TrySetVisualState(state);
|
||||
@@ -141,8 +149,8 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
case VendingMachineEjectMessage msg:
|
||||
TryEject(msg.ID);
|
||||
break;
|
||||
case InventorySyncRequestMessage msg:
|
||||
_userInterface.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
||||
case InventorySyncRequestMessage _:
|
||||
UserInterface?.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -160,7 +168,7 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
return;
|
||||
}
|
||||
|
||||
VendingMachineInventoryEntry entry = Inventory.Find(x => x.ID == id);
|
||||
var entry = Inventory.Find(x => x.ID == id);
|
||||
if (entry == null)
|
||||
{
|
||||
FlickDenyAnimation();
|
||||
@@ -175,7 +183,7 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
|
||||
_ejecting = true;
|
||||
entry.Amount--;
|
||||
_userInterface.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
||||
UserInterface?.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
||||
TrySetVisualState(VendingMachineVisualState.Eject);
|
||||
|
||||
Timer.Spawn(_animationDuration, () =>
|
||||
@@ -204,14 +212,20 @@ namespace Content.Server.GameObjects.Components.VendingMachines
|
||||
if (_broken)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Broken;
|
||||
} else if (_ejecting)
|
||||
}
|
||||
else if (_ejecting)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Eject;
|
||||
} else if (!Powered)
|
||||
}
|
||||
else if (!Powered)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Off;
|
||||
}
|
||||
_appearance.SetData(VendingMachineVisuals.VisualState, finalState);
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(VendingMachineVisuals.VisualState, finalState);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnBreak(BreakageEventArgs eventArgs)
|
||||
|
||||
Reference in New Issue
Block a user