diff --git a/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/ActionsComponentTests.cs b/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/ActionsComponentTests.cs index f429db3f52..fe7614d904 100644 --- a/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/ActionsComponentTests.cs +++ b/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/ActionsComponentTests.cs @@ -74,7 +74,8 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs await server.WaitAssertion(() => { - var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity; + var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); + Assert.That(playerEnt, Is.Not.EqualTo(default)); var actionsComponent = sEntities.GetComponent(playerEnt); // player should begin with their innate actions granted @@ -102,7 +103,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs { var local = clientPlayerMgr.LocalPlayer; var controlled = local!.ControlledEntity; - var actionsComponent = cEntities.GetComponent(controlled!); + var actionsComponent = cEntities.GetComponent(controlled!.Value); // we should have our innate actions and debug1. foreach (var innateAction in innateActions) @@ -156,7 +157,8 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs // now revoke the action and check that the client sees it as revoked await server.WaitAssertion(() => { - var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity; + var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); + Assert.That(playerEnt, Is.Not.EqualTo(default)); var actionsComponent = IoCManager.Resolve().GetComponent(playerEnt); actionsComponent.Revoke(ActionType.DebugInstant); }); @@ -168,7 +170,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs { var local = clientPlayerMgr.LocalPlayer; var controlled = local!.ControlledEntity; - var actionsComponent = IoCManager.Resolve().GetComponent(controlled!); + var actionsComponent = IoCManager.Resolve().GetComponent(controlled!.Value); // we should have our innate actions, but debug1 should be revoked foreach (var innateAction in innateActions) @@ -250,7 +252,8 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs await server.WaitAssertion(() => { - serverPlayerEnt = serverPlayerManager.Sessions.Single().AttachedEntity; + serverPlayerEnt = serverPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); + Assert.That(serverPlayerEnt, Is.Not.EqualTo(default)); serverActionsComponent = serverEntManager.GetComponent(serverPlayerEnt); // spawn and give them an item that has actions @@ -291,7 +294,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs { var local = clientPlayerMgr.LocalPlayer; var controlled = local!.ControlledEntity; - clientActionsComponent = clientEntities.GetComponent(controlled!); + clientActionsComponent = clientEntities.GetComponent(controlled!.Value); var lightEntry = clientActionsComponent.ItemActionStates() .Where(entry => entry.Value.ContainsKey(ItemActionType.ToggleLight)) diff --git a/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/AlertsComponentTests.cs b/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/AlertsComponentTests.cs index 9c4d8a8d1c..e454fb179b 100644 --- a/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/AlertsComponentTests.cs +++ b/Content.IntegrationTests/Tests/GameObjects/Components/Mobs/AlertsComponentTests.cs @@ -29,7 +29,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs await server.WaitAssertion(() => { - var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity; + var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); Assert.That(playerEnt != default); var alertsComponent = IoCManager.Resolve().GetComponent(playerEnt); Assert.NotNull(alertsComponent); @@ -69,8 +69,8 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs await server.WaitAssertion(() => { - var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity; - Assert.That(playerEnt != default); + var playerEnt = serverPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); + Assert.That(playerEnt, Is.Not.EqualTo(default)); var alertsComponent = IoCManager.Resolve().GetComponent(playerEnt); Assert.NotNull(alertsComponent); diff --git a/Content.IntegrationTests/Tests/PDA/PDAExtensionsTests.cs b/Content.IntegrationTests/Tests/PDA/PDAExtensionsTests.cs index 81275a9f2e..fa3c862397 100644 --- a/Content.IntegrationTests/Tests/PDA/PDAExtensionsTests.cs +++ b/Content.IntegrationTests/Tests/PDA/PDAExtensionsTests.cs @@ -59,7 +59,7 @@ namespace Content.IntegrationTests.Tests.PDA await server.WaitAssertion(() => { - var player = sPlayerManager.Sessions.Single().AttachedEntity; + var player = sPlayerManager.Sessions.Single().AttachedEntity.GetValueOrDefault(); Assert.That(player != default); diff --git a/Content.Server/AI/Utility/Considerations/Containers/TargetAccessibleCon.cs b/Content.Server/AI/Utility/Considerations/Containers/TargetAccessibleCon.cs index 6b0f394754..ebed8d3c34 100644 --- a/Content.Server/AI/Utility/Considerations/Containers/TargetAccessibleCon.cs +++ b/Content.Server/AI/Utility/Considerations/Containers/TargetAccessibleCon.cs @@ -17,15 +17,16 @@ namespace Content.Server.AI.Utility.Considerations.Containers { protected override float GetScore(Blackboard context) { + var entMan = IoCManager.Resolve(); var target = context.GetState().GetValue(); - if (target == null) + if (!entMan.EntityExists(target)) { return 0.0f; } if (target.TryGetContainer(out var container)) { - if (IoCManager.Resolve().TryGetComponent(container.Owner, out EntityStorageComponent? storageComponent)) + if (entMan.TryGetComponent(container.Owner, out EntityStorageComponent? storageComponent)) { if (storageComponent.IsWeldedShut && !storageComponent.Open) { diff --git a/Content.Server/Actions/Commands/CooldownAction.cs b/Content.Server/Actions/Commands/CooldownAction.cs index 67e45ffaa4..dfd9cb4f41 100644 --- a/Content.Server/Actions/Commands/CooldownAction.cs +++ b/Content.Server/Actions/Commands/CooldownAction.cs @@ -21,15 +21,13 @@ namespace Content.Server.Actions.Commands public void Execute(IConsoleShell shell, string argStr, string[] args) { var player = shell.Player as IPlayerSession; - if (player == null) return; - var attachedEntity = player.AttachedEntity; + if (player?.AttachedEntity is not {} attachedEntity) return; if (args.Length > 2) { var target = args[2]; if (!CommandUtils.TryGetAttachedEntityByUsernameOrId(shell, target, player, out attachedEntity)) return; } - if (attachedEntity == default) return; if (!IoCManager.Resolve().TryGetComponent(attachedEntity, out ServerActionsComponent? actionsComponent)) { shell.WriteError("user has no actions component"); diff --git a/Content.Server/Actions/Commands/GrantAction.cs b/Content.Server/Actions/Commands/GrantAction.cs index 7ed94e89da..09d8fe4b7d 100644 --- a/Content.Server/Actions/Commands/GrantAction.cs +++ b/Content.Server/Actions/Commands/GrantAction.cs @@ -19,8 +19,8 @@ namespace Content.Server.Actions.Commands public void Execute(IConsoleShell shell, string argStr, string[] args) { var player = shell.Player as IPlayerSession; - if (player == null) return; - var attachedEntity = player.AttachedEntity; + if (player?.AttachedEntity == null) return; + var attachedEntity = player.AttachedEntity.Value; if (args.Length > 1) { var target = args[1]; diff --git a/Content.Server/Actions/Commands/RevokeAction.cs b/Content.Server/Actions/Commands/RevokeAction.cs index 0cf89c4d94..39db78d88d 100644 --- a/Content.Server/Actions/Commands/RevokeAction.cs +++ b/Content.Server/Actions/Commands/RevokeAction.cs @@ -20,8 +20,8 @@ namespace Content.Server.Actions.Commands public void Execute(IConsoleShell shell, string argStr, string[] args) { var player = shell.Player as IPlayerSession; - if (player == null) return; - var attachedEntity = player.AttachedEntity; + if (player?.AttachedEntity == null) return; + var attachedEntity = player.AttachedEntity.Value; if (args.Length > 1) { var target = args[1]; diff --git a/Content.Server/Administration/AdminSystem.cs b/Content.Server/Administration/AdminSystem.cs index 99f035c404..d2d234e389 100644 --- a/Content.Server/Administration/AdminSystem.cs +++ b/Content.Server/Administration/AdminSystem.cs @@ -125,12 +125,12 @@ namespace Content.Server.Administration var name = session.Name; var username = string.Empty; - if (session.AttachedEntity != default) - username = EntityManager.GetComponent(session.AttachedEntity).EntityName; + if (session.AttachedEntity != null) + username = EntityManager.GetComponent(session.AttachedEntity.Value).EntityName; var antag = session.ContentData()?.Mind?.AllRoles.Any(r => r.Antagonist) ?? false; - return new PlayerInfo(name, username, antag, session.AttachedEntity, session.UserId); + return new PlayerInfo(name, username, antag, session.AttachedEntity.GetValueOrDefault(), session.UserId); } } } diff --git a/Content.Server/Administration/Commands/AGhost.cs b/Content.Server/Administration/Commands/AGhost.cs index c94b609719..d7252145c7 100644 --- a/Content.Server/Administration/Commands/AGhost.cs +++ b/Content.Server/Administration/Commands/AGhost.cs @@ -42,9 +42,9 @@ namespace Content.Server.Administration.Commands return; } - var canReturn = mind.CurrentEntity != default; - var coordinates = player.AttachedEntity != default - ? _entities.GetComponent(player.AttachedEntity).Coordinates + var canReturn = mind.CurrentEntity != null; + var coordinates = player.AttachedEntity != null + ? _entities.GetComponent(player.AttachedEntity.Value).Coordinates : EntitySystem.Get().GetObserverSpawnPoint(); var ghost = _entities.SpawnEntity("AdminObserver", coordinates); diff --git a/Content.Server/Administration/Commands/RejuvenateCommand.cs b/Content.Server/Administration/Commands/RejuvenateCommand.cs index 73d38b0f0c..186dc13a5b 100644 --- a/Content.Server/Administration/Commands/RejuvenateCommand.cs +++ b/Content.Server/Administration/Commands/RejuvenateCommand.cs @@ -30,12 +30,12 @@ namespace Content.Server.Administration.Commands if (args.Length < 1 && shell.Player is IPlayerSession player) //Try to heal the users mob if applicable { shell.WriteLine(Loc.GetString("rejuvenate-command-self-heal-message")); - if (player.AttachedEntity == default) + if (player.AttachedEntity == null) { shell.WriteLine(Loc.GetString("rejuvenate-command-no-entity-attached-message")); return; } - PerformRejuvenate(player.AttachedEntity); + PerformRejuvenate(player.AttachedEntity.Value); } var entityManager = IoCManager.Resolve(); diff --git a/Content.Server/Alert/Commands/ClearAlert.cs b/Content.Server/Alert/Commands/ClearAlert.cs index 850a0d8740..3348d3bf3b 100644 --- a/Content.Server/Alert/Commands/ClearAlert.cs +++ b/Content.Server/Alert/Commands/ClearAlert.cs @@ -26,7 +26,7 @@ namespace Content.Server.Alert.Commands return; } - var attachedEntity = player.AttachedEntity; + var attachedEntity = player.AttachedEntity.Value; if (args.Length > 1) { diff --git a/Content.Server/Alert/Commands/ShowAlert.cs b/Content.Server/Alert/Commands/ShowAlert.cs index b55b5d79dc..04769bc3d2 100644 --- a/Content.Server/Alert/Commands/ShowAlert.cs +++ b/Content.Server/Alert/Commands/ShowAlert.cs @@ -20,19 +20,13 @@ namespace Content.Server.Alert.Commands public void Execute(IConsoleShell shell, string argStr, string[] args) { var player = shell.Player as IPlayerSession; - if (player == null) + if (player?.AttachedEntity == null) { - shell.WriteLine("You cannot run this command from the server."); + shell.WriteLine("You cannot run this from the server or without an attached entity."); return; } - var attachedEntity = player.AttachedEntity; - - if (attachedEntity == null) - { - shell.WriteLine("You don't have an entity."); - return; - } + var attachedEntity = player.AttachedEntity.Value; if (args.Length > 2) { diff --git a/Content.Server/Alert/ServerAlertsComponent.cs b/Content.Server/Alert/ServerAlertsComponent.cs index 32db5fd927..954838a1ba 100644 --- a/Content.Server/Alert/ServerAlertsComponent.cs +++ b/Content.Server/Alert/ServerAlertsComponent.cs @@ -56,7 +56,7 @@ namespace Content.Server.Alert { case ClickAlertMessage msg: { - var player = session.AttachedEntity; + var player = session.AttachedEntity.GetValueOrDefault(); if (player != Owner) { diff --git a/Content.Server/Atmos/Components/GasAnalyzerComponent.cs b/Content.Server/Atmos/Components/GasAnalyzerComponent.cs index 4bec111fd0..03230438e3 100644 --- a/Content.Server/Atmos/Components/GasAnalyzerComponent.cs +++ b/Content.Server/Atmos/Components/GasAnalyzerComponent.cs @@ -235,7 +235,7 @@ namespace Content.Server.Atmos.Components if (handsComponent.GetActiveHand?.Owner is not {Valid: true} activeHandEntity || !_entities.TryGetComponent(activeHandEntity, out GasAnalyzerComponent? gasAnalyzer)) { - serverMsg.Session.AttachedEntity.PopupMessage(Loc.GetString("gas-analyzer-component-need-gas-analyzer-in-hand-message")); + serverMsg.Session.AttachedEntity.Value.PopupMessage(Loc.GetString("gas-analyzer-component-need-gas-analyzer-in-hand-message")); return; } diff --git a/Content.Server/Body/Commands/AddHandCommand.cs b/Content.Server/Body/Commands/AddHandCommand.cs index 9d0e9da6dc..d709c79246 100644 --- a/Content.Server/Body/Commands/AddHandCommand.cs +++ b/Content.Server/Body/Commands/AddHandCommand.cs @@ -44,13 +44,13 @@ namespace Content.Server.Body.Commands return; } - if (player.AttachedEntity == default) + if (player.AttachedEntity == null) { shell.WriteLine("You don't have an entity to add a hand to."); return; } - entity = player.AttachedEntity; + entity = player.AttachedEntity.Value; hand = entityManager.SpawnEntity(DefaultHandPrototype, entityManager.GetComponent(entity).Coordinates); break; } @@ -75,13 +75,13 @@ namespace Content.Server.Body.Commands return; } - if (player.AttachedEntity == default) + if (player.AttachedEntity == null) { shell.WriteLine("You don't have an entity to add a hand to."); return; } - entity = player.AttachedEntity; + entity = player.AttachedEntity.Value; hand = entityManager.SpawnEntity(args[0], entityManager.GetComponent(entity).Coordinates); } diff --git a/Content.Server/Body/Commands/AttachBodyPartCommand.cs b/Content.Server/Body/Commands/AttachBodyPartCommand.cs index eacfe32d6e..c46e366400 100644 --- a/Content.Server/Body/Commands/AttachBodyPartCommand.cs +++ b/Content.Server/Body/Commands/AttachBodyPartCommand.cs @@ -44,7 +44,7 @@ namespace Content.Server.Body.Commands return; } - entity = player.AttachedEntity; + entity = player.AttachedEntity.Value; break; case 2: diff --git a/Content.Server/Body/Commands/DestroyMechanismCommand.cs b/Content.Server/Body/Commands/DestroyMechanismCommand.cs index 47f7684c14..a9a3add4a5 100644 --- a/Content.Server/Body/Commands/DestroyMechanismCommand.cs +++ b/Content.Server/Body/Commands/DestroyMechanismCommand.cs @@ -31,13 +31,13 @@ namespace Content.Server.Body.Commands return; } - if (player.AttachedEntity == default) + if (player.AttachedEntity is not {} attached) { shell.WriteLine("You have no entity."); return; } - if (!IoCManager.Resolve().TryGetComponent(player.AttachedEntity, out SharedBodyComponent? body)) + if (!IoCManager.Resolve().TryGetComponent(attached, out SharedBodyComponent? body)) { var random = IoCManager.Resolve(); var text = $"You have no body{(random.Prob(0.2f) ? " and you must scream." : ".")}"; diff --git a/Content.Server/Chat/Commands/MeCommand.cs b/Content.Server/Chat/Commands/MeCommand.cs index 6b75dc13dd..94951e1dea 100644 --- a/Content.Server/Chat/Commands/MeCommand.cs +++ b/Content.Server/Chat/Commands/MeCommand.cs @@ -24,7 +24,7 @@ namespace Content.Server.Chat.Commands return; } - if (player.Status != SessionStatus.InGame || player.AttachedEntity == default) + if (player.Status != SessionStatus.InGame || player.AttachedEntity == null) return; if (args.Length < 1) diff --git a/Content.Server/Chat/Commands/SayCommand.cs b/Content.Server/Chat/Commands/SayCommand.cs index 5f658f25fb..a35e51c3c0 100644 --- a/Content.Server/Chat/Commands/SayCommand.cs +++ b/Content.Server/Chat/Commands/SayCommand.cs @@ -25,9 +25,15 @@ namespace Content.Server.Chat.Commands return; } - if (player.Status != SessionStatus.InGame || player.AttachedEntity == default) + if (player.Status != SessionStatus.InGame) return; + if (player.AttachedEntity is not {} playerEntity) + { + shell.WriteLine("You don't have an entity!"); + return; + } + if (args.Length < 1) return; @@ -38,12 +44,6 @@ namespace Content.Server.Chat.Commands var chat = IoCManager.Resolve(); var chatSanitizer = IoCManager.Resolve(); - if (player.AttachedEntity is not {Valid: true} playerEntity) - { - shell.WriteLine("You don't have an entity!"); - return; - } - if (IoCManager.Resolve().HasComponent(playerEntity)) chat.SendDeadChat(player, message); else diff --git a/Content.Server/Chat/Managers/ChatManager.cs b/Content.Server/Chat/Managers/ChatManager.cs index 437abe9f99..02cb35f2f4 100644 --- a/Content.Server/Chat/Managers/ChatManager.cs +++ b/Content.Server/Chat/Managers/ChatManager.cs @@ -306,7 +306,7 @@ namespace Content.Server.Chat.Managers msg.MessageWrap = Loc.GetString("chat-manager-send-dead-chat-wrap-message", ("deadChannelName", Loc.GetString("chat-manager-dead-channel-name")), ("playerName", (playerName))); - msg.SenderEntity = player.AttachedEntity; + msg.SenderEntity = player.AttachedEntity.GetValueOrDefault(); _netManager.ServerSendToMany(msg, clients.ToList()); } diff --git a/Content.Server/Chemistry/Components/SolutionTransferComponent.cs b/Content.Server/Chemistry/Components/SolutionTransferComponent.cs index e64041c0ac..0bb22eb62e 100644 --- a/Content.Server/Chemistry/Components/SolutionTransferComponent.cs +++ b/Content.Server/Chemistry/Components/SolutionTransferComponent.cs @@ -89,6 +89,9 @@ namespace Content.Server.Chemistry.Components public void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg) { + if (serverMsg.Session.AttachedEntity == null) + return; + switch (serverMsg.Message) { case TransferAmountSetValueMessage svm: @@ -96,7 +99,7 @@ namespace Content.Server.Chemistry.Components var amount = Math.Clamp(sval, MinimumTransferAmount.Float(), MaximumTransferAmount.Float()); - serverMsg.Session.AttachedEntity.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", + serverMsg.Session.AttachedEntity.Value.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", ("amount", amount))); SetTransferAmount(FixedPoint2.New(amount)); break; diff --git a/Content.Server/Cloning/Components/CloningPodComponent.cs b/Content.Server/Cloning/Components/CloningPodComponent.cs index 4326bbcd33..6a00e39095 100644 --- a/Content.Server/Cloning/Components/CloningPodComponent.cs +++ b/Content.Server/Cloning/Components/CloningPodComponent.cs @@ -80,20 +80,21 @@ namespace Content.Server.Cloning.Components private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj) { - if (obj.Message is not CloningPodUiButtonPressedMessage message) return; + if (obj.Message is not CloningPodUiButtonPressedMessage message || obj.Session.AttachedEntity == null) + return; switch (message.Button) { case UiButton.Clone: if (BodyContainer.ContainedEntity != null) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-occupied")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-occupied")); return; } if (message.ScanId == null) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-no-selection")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-no-selection")); return; } @@ -101,7 +102,7 @@ namespace Content.Server.Cloning.Components if (!cloningSystem.IdToDNA.TryGetValue(message.ScanId.Value, out var dna)) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-bad-selection")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-bad-selection")); return; // ScanId is not in database } @@ -115,7 +116,7 @@ namespace Content.Server.Cloning.Components _entities.TryGetComponent(clone, out MindComponent? cloneMindComp) && (cloneMindComp.Mind == null || cloneMindComp.Mind == mind)) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-already-cloning")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-already-cloning")); return; // Mind already has clone } @@ -126,14 +127,14 @@ namespace Content.Server.Cloning.Components _entities.TryGetComponent(mind.OwnedEntity.Value, out var state) && !state.IsDead()) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-already-alive")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-already-alive")); return; // Body controlled by mind is not dead } // Yes, we still need to track down the client because we need to open the Eui if (mind.UserId == null || !_playerManager.TryGetSessionById(mind.UserId.Value, out var client)) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-user-offline")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("cloning-pod-component-msg-user-offline")); return; // If we can't track down the client, we can't offer transfer. That'd be quite bad. } diff --git a/Content.Server/Commands/CommandUtils.cs b/Content.Server/Commands/CommandUtils.cs index 0c4fd86d9c..eb2e934edf 100644 --- a/Content.Server/Commands/CommandUtils.cs +++ b/Content.Server/Commands/CommandUtils.cs @@ -43,13 +43,13 @@ namespace Content.Server.Commands { attachedEntity = default; if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false; - if (session.AttachedEntity == default) + if (session.AttachedEntity == null) { shell.WriteLine("User has no attached entity."); return false; } - attachedEntity = session.AttachedEntity; + attachedEntity = session.AttachedEntity.Value; return true; } diff --git a/Content.Server/Damage/Commands/GodModeCommand.cs b/Content.Server/Damage/Commands/GodModeCommand.cs index a94ad5ca37..1d79d7c20c 100644 --- a/Content.Server/Damage/Commands/GodModeCommand.cs +++ b/Content.Server/Damage/Commands/GodModeCommand.cs @@ -31,13 +31,13 @@ namespace Content.Server.Damage.Commands return; } - if (player.AttachedEntity == default) + if (player.AttachedEntity == null) { shell.WriteLine("An entity needs to be specified when you aren't attached to an entity."); return; } - entity = player.AttachedEntity; + entity = player.AttachedEntity.Value; break; case 1: if (!EntityUid.TryParse(args[0], out var id)) diff --git a/Content.Server/Disposal/Tube/Components/DisposalRouterComponent.cs b/Content.Server/Disposal/Tube/Components/DisposalRouterComponent.cs index 760e9814b9..0e7a59072b 100644 --- a/Content.Server/Disposal/Tube/Components/DisposalRouterComponent.cs +++ b/Content.Server/Disposal/Tube/Components/DisposalRouterComponent.cs @@ -104,7 +104,7 @@ namespace Content.Server.Disposal.Tube.Components private bool PlayerCanUseDisposalTagger(IPlayerSession session) { //Need player entity to check if they are still able to use the configuration interface - if (session.AttachedEntity == null) + if (session.AttachedEntity is not {} attached) return false; if (!Anchored) return false; @@ -112,7 +112,7 @@ namespace Content.Server.Disposal.Tube.Components var actionBlocker = EntitySystem.Get(); var groupController = IoCManager.Resolve(); //Check if player can interact in their current state - if (!groupController.CanAdminMenu(session) && (!actionBlocker.CanInteract(session.AttachedEntity) || !actionBlocker.CanUse(session.AttachedEntity))) + if (!groupController.CanAdminMenu(session) && (!actionBlocker.CanInteract(attached) || !actionBlocker.CanUse(attached))) return false; return true; diff --git a/Content.Server/Disposal/Tube/Components/DisposalTaggerComponent.cs b/Content.Server/Disposal/Tube/Components/DisposalTaggerComponent.cs index ab73137b62..9c9dc10c0b 100644 --- a/Content.Server/Disposal/Tube/Components/DisposalTaggerComponent.cs +++ b/Content.Server/Disposal/Tube/Components/DisposalTaggerComponent.cs @@ -86,7 +86,7 @@ namespace Content.Server.Disposal.Tube.Components private bool PlayerCanUseDisposalTagger(IPlayerSession session) { //Need player entity to check if they are still able to use the configuration interface - if (session.AttachedEntity == null) + if (session.AttachedEntity is not {} attached) return false; if (!Anchored) return false; @@ -94,7 +94,7 @@ namespace Content.Server.Disposal.Tube.Components var actionBlocker = EntitySystem.Get(); var groupController = IoCManager.Resolve(); //Check if player can interact in their current state - if (!groupController.CanAdminMenu(session) && (!actionBlocker.CanInteract(session.AttachedEntity) || !actionBlocker.CanUse(session.AttachedEntity))) + if (!groupController.CanAdminMenu(session) && (!actionBlocker.CanInteract(attached) || !actionBlocker.CanUse(attached))) return false; return true; diff --git a/Content.Server/Disposal/TubeConnectionsCommand.cs b/Content.Server/Disposal/TubeConnectionsCommand.cs index b89eadd307..0a7d023c36 100644 --- a/Content.Server/Disposal/TubeConnectionsCommand.cs +++ b/Content.Server/Disposal/TubeConnectionsCommand.cs @@ -52,7 +52,7 @@ namespace Content.Server.Disposal return; } - tube.PopupDirections(player.AttachedEntity); + tube.PopupDirections(player.AttachedEntity.Value); } } } diff --git a/Content.Server/EntityList/SpawnEntityListCommand.cs b/Content.Server/EntityList/SpawnEntityListCommand.cs index 9615df7f77..966b206a13 100644 --- a/Content.Server/EntityList/SpawnEntityListCommand.cs +++ b/Content.Server/EntityList/SpawnEntityListCommand.cs @@ -30,7 +30,7 @@ namespace Content.Server.EntityList return; } - if (player.AttachedEntity == null) + if (player.AttachedEntity is not {} attached) { shell.WriteError("You must have an entity to run this command."); return; @@ -49,7 +49,7 @@ namespace Content.Server.EntityList foreach (var entity in prototype.Entities(prototypeManager)) { - entityManager.SpawnEntity(entity.ID, entityManager.GetComponent(player.AttachedEntity).Coordinates); + entityManager.SpawnEntity(entity.ID, entityManager.GetComponent(attached).Coordinates); i++; } diff --git a/Content.Server/GameTicking/Presets/PresetSuspicion.cs b/Content.Server/GameTicking/Presets/PresetSuspicion.cs index 7b2f041efb..d68aa75611 100644 --- a/Content.Server/GameTicking/Presets/PresetSuspicion.cs +++ b/Content.Server/GameTicking/Presets/PresetSuspicion.cs @@ -66,13 +66,13 @@ namespace Content.Server.GameTicking.Presets foreach (var player in list) { - if (!ReadyProfiles.ContainsKey(player.UserId)) + if (!ReadyProfiles.ContainsKey(player.UserId) || player.AttachedEntity is not {} attached) { continue; } prefList.Add(player); - player.AttachedEntity.EnsureComponent(); + attached.EnsureComponent(); } var numTraitors = MathHelper.Clamp(readyPlayers.Count / PlayersPerTraitor, diff --git a/Content.Server/Instruments/InstrumentSystem.cs b/Content.Server/Instruments/InstrumentSystem.cs index c0c48c9bc6..580c945dbf 100644 --- a/Content.Server/Instruments/InstrumentSystem.cs +++ b/Content.Server/Instruments/InstrumentSystem.cs @@ -94,7 +94,10 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem if (!EntityManager.TryGetComponent(uid, out InstrumentComponent? instrument)) return; - if (!instrument.Playing || args.SenderSession != instrument.InstrumentPlayer || instrument.InstrumentPlayer == null) + if (!instrument.Playing + || args.SenderSession != instrument.InstrumentPlayer + || instrument.InstrumentPlayer == null + || args.SenderSession.AttachedEntity is not {} attached) return; var send = true; @@ -108,11 +111,11 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem { if (instrument.LaggedBatches == (int) (MaxMidiLaggedBatches * (1 / 3d) + 1)) { - instrument.InstrumentPlayer.AttachedEntity.PopupMessage( + attached.PopupMessage( Loc.GetString("instrument-component-finger-cramps-light-message")); } else if (instrument.LaggedBatches == (int) (MaxMidiLaggedBatches * (2 / 3d) + 1)) { - instrument.InstrumentPlayer.AttachedEntity.PopupMessage( + attached.PopupMessage( Loc.GetString("instrument-component-finger-cramps-serious-message")); } } diff --git a/Content.Server/Interaction/TilePryCommand.cs b/Content.Server/Interaction/TilePryCommand.cs index 47766677e4..fd5845b669 100644 --- a/Content.Server/Interaction/TilePryCommand.cs +++ b/Content.Server/Interaction/TilePryCommand.cs @@ -25,7 +25,7 @@ namespace Content.Server.Interaction public void Execute(IConsoleShell shell, string argStr, string[] args) { var player = shell.Player as IPlayerSession; - if (player?.AttachedEntity == null) + if (player?.AttachedEntity is not {} attached) { return; } @@ -49,9 +49,9 @@ namespace Content.Server.Interaction } var mapManager = IoCManager.Resolve(); - var playerGrid = _entities.GetComponent(player.AttachedEntity).GridID; + var playerGrid = _entities.GetComponent(attached).GridID; var mapGrid = mapManager.GetGrid(playerGrid); - var playerPosition = _entities.GetComponent(player.AttachedEntity).Coordinates; + var playerPosition = _entities.GetComponent(attached).Coordinates; var tileDefinitionManager = IoCManager.Resolve(); for (var i = -radius; i <= radius; i++) diff --git a/Content.Server/Kitchen/EntitySystems/ReagentGrinderSystem.cs b/Content.Server/Kitchen/EntitySystems/ReagentGrinderSystem.cs index b0df3b056f..6062145c5f 100644 --- a/Content.Server/Kitchen/EntitySystems/ReagentGrinderSystem.cs +++ b/Content.Server/Kitchen/EntitySystems/ReagentGrinderSystem.cs @@ -152,7 +152,7 @@ namespace Content.Server.Kitchen.EntitySystems private void OnUIMessageReceived(EntityUid uid, ReagentGrinderComponent component, ServerBoundUserInterfaceMessage message) { - if (component.Busy) + if (component.Busy || message.Session.AttachedEntity is not {} attached) { return; } @@ -163,7 +163,7 @@ namespace Content.Server.Kitchen.EntitySystems if (!EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver) || !receiver.Powered) break; ClickSound(component); - DoWork(component, message.Session.AttachedEntity, + DoWork(component, attached, SharedReagentGrinderComponent.GrinderProgram.Grind); break; @@ -171,7 +171,7 @@ namespace Content.Server.Kitchen.EntitySystems if (!EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver2) || !receiver2.Powered) break; ClickSound(component); - DoWork(component, message.Session.AttachedEntity, + DoWork(component, attached, SharedReagentGrinderComponent.GrinderProgram.Juice); break; diff --git a/Content.Server/MachineLinking/System/SignalLinkerSystem.cs b/Content.Server/MachineLinking/System/SignalLinkerSystem.cs index ed75573a6f..549731ab9a 100644 --- a/Content.Server/MachineLinking/System/SignalLinkerSystem.cs +++ b/Content.Server/MachineLinking/System/SignalLinkerSystem.cs @@ -116,6 +116,8 @@ namespace Content.Server.MachineLinking.System private void OnReceiverUIMessage(EntityUid uid, SignalReceiverComponent component, ServerBoundUserInterfaceMessage msg) { + if (msg.Session.AttachedEntity is not { } attached) return; + switch (msg.Message) { case SignalPortSelected portSelected: @@ -123,12 +125,12 @@ namespace Content.Server.MachineLinking.System !EntityManager.TryGetComponent(msg.Session.AttachedEntity, out HandsComponent? hands) || !hands.TryGetActiveHeldEntity(out var heldEntity) || !EntityManager.TryGetComponent(heldEntity, out SignalLinkerComponent? signalLinkerComponent) || - !_interaction.InRangeUnobstructed(msg.Session.AttachedEntity, component.Owner, ignoreInsideBlocker: true) || + !_interaction.InRangeUnobstructed(attached, component.Owner, ignoreInsideBlocker: true) || !signalLinkerComponent.Port.HasValue || !signalLinkerComponent.Port.Value.transmitter.Outputs.ContainsPort(signalLinkerComponent.Port .Value.port) || !component.Inputs.ContainsPort(portSelected.Port)) return; - LinkerInteraction(msg.Session.AttachedEntity, signalLinkerComponent.Port.Value.transmitter, + LinkerInteraction(attached, signalLinkerComponent.Port.Value.transmitter, signalLinkerComponent.Port.Value.port, component, portSelected.Port); break; } @@ -156,6 +158,9 @@ namespace Content.Server.MachineLinking.System private void OnTransmitterUIMessage(EntityUid uid, SignalTransmitterComponent component, ServerBoundUserInterfaceMessage msg) { + if (msg.Session.AttachedEntity is not { } attached) + return; + switch (msg.Message) { case SignalPortSelected portSelected: @@ -163,9 +168,9 @@ namespace Content.Server.MachineLinking.System !EntityManager.TryGetComponent(msg.Session.AttachedEntity, out HandsComponent? hands) || !hands.TryGetActiveHeldEntity(out var heldEntity) || !EntityManager.TryGetComponent(heldEntity, out SignalLinkerComponent? signalLinkerComponent) || - !_interaction.InRangeUnobstructed(msg.Session.AttachedEntity, component.Owner, ignoreInsideBlocker: true)) + !_interaction.InRangeUnobstructed(attached, component.Owner, ignoreInsideBlocker: true)) return; - LinkerSaveInteraction(msg.Session.AttachedEntity, signalLinkerComponent, component, + LinkerSaveInteraction(attached, signalLinkerComponent, component, portSelected.Port); break; } diff --git a/Content.Server/Medical/Components/MedicalScannerComponent.cs b/Content.Server/Medical/Components/MedicalScannerComponent.cs index 8eb8bf2a0b..80c8b9a176 100644 --- a/Content.Server/Medical/Components/MedicalScannerComponent.cs +++ b/Content.Server/Medical/Components/MedicalScannerComponent.cs @@ -189,7 +189,7 @@ namespace Content.Server.Medical.Components private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj) { - if (obj.Message is not UiButtonPressedMessage message) return; + if (obj.Message is not UiButtonPressedMessage message || obj.Session.AttachedEntity == null) return; switch (message.Button) { @@ -200,7 +200,7 @@ namespace Content.Server.Medical.Components if (!IoCManager.Resolve().TryGetComponent(_bodyContainer.ContainedEntity.Value, out MindComponent? mindComp) || mindComp.Mind == null) { - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-no-soul")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-no-soul")); break; } @@ -215,7 +215,7 @@ namespace Content.Server.Medical.Components if (mindUser == null) { // For now assume this means soul departed - obj.Session.AttachedEntity.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-soul-broken")); + obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-soul-broken")); break; } diff --git a/Content.Server/Power/Components/ApcComponent.cs b/Content.Server/Power/Components/ApcComponent.cs index 145a4957db..d035104d6c 100644 --- a/Content.Server/Power/Components/ApcComponent.cs +++ b/Content.Server/Power/Components/ApcComponent.cs @@ -87,25 +87,21 @@ namespace Content.Server.Power.Components private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg) { - if (serverMsg.Message is ApcToggleMainBreakerMessage) + if (serverMsg.Message is not ApcToggleMainBreakerMessage || serverMsg.Session.AttachedEntity is not {} attached) + return; + + var accessSystem = EntitySystem.Get(); + if (_accessReader == null || accessSystem.IsAllowed(_accessReader, attached)) { - var user = serverMsg.Session.AttachedEntity; - if (user == null) return; - - var accessSystem = EntitySystem.Get(); - if (_accessReader == null || accessSystem.IsAllowed(_accessReader, user)) - { - MainBreakerEnabled = !MainBreakerEnabled; - IoCManager.Resolve().GetComponent(Owner).CanDischarge = MainBreakerEnabled; - - _uiDirty = true; - SoundSystem.Play(Filter.Pvs(Owner), _onReceiveMessageSound.GetSound(), Owner, AudioParams.Default.WithVolume(-2f)); - } - else - { - user.PopupMessageCursor(Loc.GetString("apc-component-insufficient-access")); - } + MainBreakerEnabled = !MainBreakerEnabled; + IoCManager.Resolve().GetComponent(Owner).CanDischarge = MainBreakerEnabled; + _uiDirty = true; + SoundSystem.Play(Filter.Pvs(Owner), _onReceiveMessageSound.GetSound(), Owner, AudioParams.Default.WithVolume(-2f)); + } + else + { + attached.PopupMessageCursor(Loc.GetString("apc-component-insufficient-access")); } } diff --git a/Content.Server/Sandbox/SandboxManager.cs b/Content.Server/Sandbox/SandboxManager.cs index 46ed8590f3..572dc5f4cb 100644 --- a/Content.Server/Sandbox/SandboxManager.cs +++ b/Content.Server/Sandbox/SandboxManager.cs @@ -115,7 +115,7 @@ namespace Content.Server.Sandbox } var player = _playerManager.GetSessionByChannel(message.MsgChannel); - if (player.AttachedEntity == default) + if (player.AttachedEntity is not {} attached) { return; } @@ -124,7 +124,7 @@ namespace Content.Server.Sandbox .EnumeratePrototypes() .Select(p => p.ID).ToArray(); - if (_entityManager.TryGetComponent(player.AttachedEntity, out InventoryComponent? inv) + if (_entityManager.TryGetComponent(attached, out InventoryComponent? inv) && inv.TryGetSlotItem(Slots.IDCARD, out ItemComponent? wornItem)) { if (_entityManager.HasComponent(wornItem.Owner)) @@ -148,10 +148,10 @@ namespace Content.Server.Sandbox } } } - else if (_entityManager.TryGetComponent(player.AttachedEntity, out var hands)) + else if (_entityManager.TryGetComponent(attached, out var hands)) { var card = CreateFreshId(); - if (!_entityManager.TryGetComponent(player.AttachedEntity, out inv) || !inv.Equip(Slots.IDCARD, card)) + if (!_entityManager.TryGetComponent(attached, out inv) || !inv.Equip(Slots.IDCARD, card)) { hands.PutInHandOrDrop(_entityManager.GetComponent(card)); } @@ -170,10 +170,10 @@ namespace Content.Server.Sandbox EntityUid CreateFreshId() { - var card = _entityManager.SpawnEntity("CaptainIDCard", _entityManager.GetComponent(player.AttachedEntity).Coordinates); + var card = _entityManager.SpawnEntity("CaptainIDCard", _entityManager.GetComponent(attached).Coordinates); UpgradeId(card); - _entityManager.GetComponent(card).FullName = _entityManager.GetComponent(player.AttachedEntity).EntityName; + _entityManager.GetComponent(card).FullName = _entityManager.GetComponent(attached).EntityName; return card; } } diff --git a/Content.Server/Storage/EntitySystems/StorageSystem.cs b/Content.Server/Storage/EntitySystems/StorageSystem.cs index 83f0453649..72a8aa4022 100644 --- a/Content.Server/Storage/EntitySystems/StorageSystem.cs +++ b/Content.Server/Storage/EntitySystems/StorageSystem.cs @@ -149,10 +149,8 @@ namespace Content.Server.Storage.EntitySystems foreach (var session in _sessionCache) { - var attachedEntity = session.AttachedEntity; - // The component manages the set of sessions, so this invalid session should be removed soon. - if (attachedEntity == default || !IoCManager.Resolve().EntityExists(attachedEntity)) + if (session.AttachedEntity is not {} attachedEntity || !IoCManager.Resolve().EntityExists(attachedEntity)) continue; if (storageMap != IoCManager.Resolve().GetComponent(attachedEntity).MapID) diff --git a/Content.Server/Traitor/Uplink/Commands/AddUplinkCommand.cs b/Content.Server/Traitor/Uplink/Commands/AddUplinkCommand.cs index 7f23fed43d..7f5346c7cc 100644 --- a/Content.Server/Traitor/Uplink/Commands/AddUplinkCommand.cs +++ b/Content.Server/Traitor/Uplink/Commands/AddUplinkCommand.cs @@ -35,12 +35,11 @@ namespace Content.Server.Traitor.Uplink.Commands shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist")); return; } - if (session.AttachedEntity == null) + if (session.AttachedEntity is not {} user) { shell.WriteLine(Loc.GetString("Selected player doesn't controll any entity")); return; } - var user = session.AttachedEntity; // Get target item EntityUid? uplinkEntity = null; diff --git a/Content.Server/UserInterface/ActivatableUISystem.cs b/Content.Server/UserInterface/ActivatableUISystem.cs index bc83ec1fb3..3d50e6c414 100644 --- a/Content.Server/UserInterface/ActivatableUISystem.cs +++ b/Content.Server/UserInterface/ActivatableUISystem.cs @@ -116,7 +116,7 @@ namespace Content.Server.UserInterface // Must ToList in order to close things safely. foreach (var session in ui.SubscribedSessions.ToArray()) { - if (session.AttachedEntity == null || !_actionBlockerSystem.CanInteract(session.AttachedEntity)) + if (session.AttachedEntity == null || !_actionBlockerSystem.CanInteract(session.AttachedEntity.Value)) { ui.Close(session); } diff --git a/Content.Server/Verbs/Commands/InvokeVerbCommand.cs b/Content.Server/Verbs/Commands/InvokeVerbCommand.cs index 6ca9c2e1bd..cc98d71d22 100644 --- a/Content.Server/Verbs/Commands/InvokeVerbCommand.cs +++ b/Content.Server/Verbs/Commands/InvokeVerbCommand.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using Content.Server.Administration; using Content.Shared.Administration; @@ -34,7 +34,7 @@ namespace Content.Server.Verbs.Commands { if (args[0] == "self" && shell.Player?.AttachedEntity != null) { - playerEntity = shell.Player.AttachedEntity; + playerEntity = shell.Player.AttachedEntity.Value; } else { diff --git a/Content.Server/Verbs/VerbSystem.cs b/Content.Server/Verbs/VerbSystem.cs index 3ff58540d7..369ae31ae8 100644 --- a/Content.Server/Verbs/VerbSystem.cs +++ b/Content.Server/Verbs/VerbSystem.cs @@ -21,9 +21,8 @@ namespace Content.Server.Verbs public void HandleTryExecuteVerb(ExecuteVerbEvent args, EntitySessionEventArgs eventArgs) { var session = eventArgs.SenderSession; - var userEntity = session.AttachedEntity; - if (userEntity == null) + if (session.AttachedEntity is not {} userEntity) { Logger.Warning($"{nameof(HandleTryExecuteVerb)} called by player {session} with no attached entity."); return; @@ -61,7 +60,7 @@ namespace Content.Server.Verbs return; } - if (player.AttachedEntity == default) + if (player.AttachedEntity is not {} attached) { Logger.Warning($"{nameof(HandleVerbRequest)} called by player {player} with no attached entity."); return; @@ -71,7 +70,7 @@ namespace Content.Server.Verbs // this, and some verbs (e.g. view variables) won't even care about whether an entity is accessible through // the entity menu or not. - var response = new VerbsResponseEvent(args.EntityUid, GetLocalVerbs(args.EntityUid, player.AttachedEntity, args.Type)); + var response = new VerbsResponseEvent(args.EntityUid, GetLocalVerbs(args.EntityUid, attached, args.Type)); RaiseNetworkEvent(response, player.ConnectedClient); } } diff --git a/Content.Server/WireHacking/WiresComponent.cs b/Content.Server/WireHacking/WiresComponent.cs index a7ae2f314d..d9acca8305 100644 --- a/Content.Server/WireHacking/WiresComponent.cs +++ b/Content.Server/WireHacking/WiresComponent.cs @@ -407,8 +407,7 @@ namespace Content.Server.WireHacking { case WiresActionMessage msg: var wire = WiresList.Find(x => x.Id == msg.Id); - var player = serverMsg.Session.AttachedEntity; - if (wire == null || player == default) + if (wire == null || serverMsg.Session.AttachedEntity is not {} player) { return; } diff --git a/Content.Shared/Pulling/Systems/SharedPullableSystem.cs b/Content.Shared/Pulling/Systems/SharedPullableSystem.cs index f05240bfca..7d928c0a7c 100644 --- a/Content.Shared/Pulling/Systems/SharedPullableSystem.cs +++ b/Content.Shared/Pulling/Systems/SharedPullableSystem.cs @@ -20,7 +20,7 @@ namespace Content.Shared.Pulling.Systems private void OnRelayMoveInput(EntityUid uid, SharedPullableComponent component, RelayMoveInputEvent args) { var entity = args.Session.AttachedEntity; - if (entity == default || !_blocker.CanMove(entity)) return; + if (entity == null || !_blocker.CanMove(entity.Value)) return; _pullSystem.TryStopPull(component); } }