Enable nullability in Content.Server (#3685)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items;
|
||||
@@ -11,7 +12,6 @@ using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Interfaces;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
@@ -57,20 +57,20 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
|
||||
private static void HandleContainerModified(ContainerModifiedMessage args)
|
||||
{
|
||||
if (args.Container.Owner.TryGetComponent(out IHandsComponent handsComponent))
|
||||
if (args.Container.Owner.TryGetComponent(out IHandsComponent? handsComponent))
|
||||
{
|
||||
handsComponent.HandleSlotModifiedMaybe(args);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component)
|
||||
private static bool TryGetAttachedComponent<T>(IPlayerSession? session, [NotNullWhen(true)] out T? component)
|
||||
where T : Component
|
||||
{
|
||||
component = default;
|
||||
|
||||
var ent = session.AttachedEntity;
|
||||
var ent = session?.AttachedEntity;
|
||||
|
||||
if (ent == null || !ent.IsValid() || !ent.TryGetComponent(out T comp))
|
||||
if (ent == null || !ent.IsValid() || !ent.TryGetComponent(out T? comp))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -79,9 +79,9 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void HandleSwapHands(ICommonSession session)
|
||||
private static void HandleSwapHands(ICommonSession? session)
|
||||
{
|
||||
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent handsComp))
|
||||
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent? handsComp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -105,14 +105,14 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
}
|
||||
|
||||
private bool HandleDrop(ICommonSession session, EntityCoordinates coords, EntityUid uid)
|
||||
private bool HandleDrop(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
||||
{
|
||||
var ent = ((IPlayerSession) session).AttachedEntity;
|
||||
var ent = ((IPlayerSession?) session)?.AttachedEntity;
|
||||
|
||||
if (ent == null || !ent.IsValid())
|
||||
return false;
|
||||
|
||||
if (!ent.TryGetComponent(out HandsComponent handsComp))
|
||||
if (!ent.TryGetComponent(out HandsComponent? handsComp))
|
||||
return false;
|
||||
|
||||
if (handsComp.ActiveHand == null || handsComp.GetActiveHand == null)
|
||||
@@ -136,41 +136,44 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void HandleActivateItem(ICommonSession session)
|
||||
private static void HandleActivateItem(ICommonSession? session)
|
||||
{
|
||||
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent handsComp))
|
||||
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent? handsComp))
|
||||
return;
|
||||
|
||||
handsComp.ActivateItem();
|
||||
}
|
||||
|
||||
private bool HandleThrowItem(ICommonSession session, EntityCoordinates coords, EntityUid uid)
|
||||
private bool HandleThrowItem(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
||||
{
|
||||
var playerEnt = ((IPlayerSession)session).AttachedEntity;
|
||||
var playerEnt = ((IPlayerSession?)session)?.AttachedEntity;
|
||||
|
||||
if (playerEnt == null || !playerEnt.IsValid())
|
||||
return false;
|
||||
|
||||
if (!playerEnt.TryGetComponent(out HandsComponent handsComp))
|
||||
if (!playerEnt.TryGetComponent(out HandsComponent? handsComp))
|
||||
return false;
|
||||
|
||||
if (!handsComp.CanDrop(handsComp.ActiveHand))
|
||||
if (handsComp.ActiveHand == null || !handsComp.CanDrop(handsComp.ActiveHand))
|
||||
return false;
|
||||
|
||||
var throwEnt = handsComp.GetItem(handsComp.ActiveHand).Owner;
|
||||
var throwEnt = handsComp.GetItem(handsComp.ActiveHand)?.Owner;
|
||||
|
||||
if (throwEnt == null)
|
||||
return false;
|
||||
|
||||
if (!handsComp.ThrowItem())
|
||||
return false;
|
||||
|
||||
// throw the item, split off from a stack if it's meant to be thrown individually
|
||||
if (!throwEnt.TryGetComponent(out StackComponent stackComp) || stackComp.Count < 2 || !stackComp.ThrowIndividually)
|
||||
if (!throwEnt.TryGetComponent(out StackComponent? stackComp) || stackComp.Count < 2 || !stackComp.ThrowIndividually)
|
||||
{
|
||||
handsComp.Drop(handsComp.ActiveHand);
|
||||
}
|
||||
else
|
||||
{
|
||||
stackComp.Use(1);
|
||||
throwEnt = throwEnt.EntityManager.SpawnEntity(throwEnt.Prototype.ID, playerEnt.Transform.Coordinates);
|
||||
throwEnt = throwEnt.EntityManager.SpawnEntity(throwEnt.Prototype?.ID, playerEnt.Transform.Coordinates);
|
||||
|
||||
// can only throw one item at a time, regardless of what the prototype stack size is.
|
||||
if (throwEnt.TryGetComponent<StackComponent>(out var newStackComp))
|
||||
@@ -196,28 +199,28 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
private void HandleSmartEquipBackpack(ICommonSession session)
|
||||
private void HandleSmartEquipBackpack(ICommonSession? session)
|
||||
{
|
||||
HandleSmartEquip(session, Slots.BACKPACK);
|
||||
}
|
||||
|
||||
private void HandleSmartEquipBelt(ICommonSession session)
|
||||
private void HandleSmartEquipBelt(ICommonSession? session)
|
||||
{
|
||||
HandleSmartEquip(session, Slots.BELT);
|
||||
}
|
||||
|
||||
private void HandleSmartEquip(ICommonSession session, Slots equipmentSlot)
|
||||
private void HandleSmartEquip(ICommonSession? session, Slots equipmentSlot)
|
||||
{
|
||||
var plyEnt = ((IPlayerSession) session).AttachedEntity;
|
||||
var plyEnt = ((IPlayerSession?) session)?.AttachedEntity;
|
||||
|
||||
if (plyEnt == null || !plyEnt.IsValid())
|
||||
return;
|
||||
|
||||
if (!plyEnt.TryGetComponent(out HandsComponent handsComp) ||
|
||||
!plyEnt.TryGetComponent(out InventoryComponent inventoryComp))
|
||||
if (!plyEnt.TryGetComponent(out HandsComponent? handsComp) ||
|
||||
!plyEnt.TryGetComponent(out InventoryComponent? inventoryComp))
|
||||
return;
|
||||
|
||||
if (!inventoryComp.TryGetSlotItem(equipmentSlot, out ItemComponent equipmentItem)
|
||||
if (!inventoryComp.TryGetSlotItem(equipmentSlot, out ItemComponent? equipmentItem)
|
||||
|| !equipmentItem.Owner.TryGetComponent<ServerStorageComponent>(out var storageComponent))
|
||||
{
|
||||
plyEnt.PopupMessage(Loc.GetString("You have no {0} to take something out of!",
|
||||
@@ -231,7 +234,7 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
storageComponent.PlayerInsertHeldEntity(plyEnt);
|
||||
}
|
||||
else
|
||||
else if (storageComponent.StoredEntities != null)
|
||||
{
|
||||
if (storageComponent.StoredEntities.Count == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user