Enable nullability in Content.Server (#3685)

This commit is contained in:
DrSmugleaf
2021-03-16 15:50:20 +01:00
committed by GitHub
parent 90fec0ed24
commit a5ade526b7
306 changed files with 1616 additions and 1441 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Administration.Commands;
using Content.Server.GameObjects.Components.Items.Clothing;
@@ -42,7 +43,7 @@ namespace Content.Server.GameObjects.Components.GUI
public IEnumerable<Slots> Slots => _slotContainers.Keys;
public event Action OnItemChanged;
public event Action? OnItemChanged;
public override void Initialize()
{
@@ -145,7 +146,7 @@ namespace Content.Server.GameObjects.Components.GUI
bool IEffectBlocker.CanSlip()
{
return !TryGetSlotItem(EquipmentSlotDefines.Slots.SHOES, out ItemComponent shoes) || EffectBlockerSystem.CanSlip(shoes.Owner);
return !TryGetSlotItem(EquipmentSlotDefines.Slots.SHOES, out ItemComponent? shoes) || EffectBlockerSystem.CanSlip(shoes.Owner);
}
public override void OnRemove()
@@ -154,7 +155,7 @@ namespace Content.Server.GameObjects.Components.GUI
foreach (var slot in slots)
{
if (TryGetSlotItem(slot, out ItemComponent item))
if (TryGetSlotItem(slot, out ItemComponent? item))
{
item.Owner.Delete();
}
@@ -191,18 +192,19 @@ namespace Content.Server.GameObjects.Components.GUI
/// </summary>
/// <param name="slot">The slot to get the item for.</param>
/// <returns>Null if the slot is empty, otherwise the item.</returns>
public ItemComponent GetSlotItem(Slots slot)
public ItemComponent? GetSlotItem(Slots slot)
{
return GetSlotItem<ItemComponent>(slot);
}
public IEnumerable<T> LookupItems<T>() where T : Component
public IEnumerable<T?> LookupItems<T>() where T : Component
{
return _slotContainers.Values.SelectMany(x => x.ContainedEntities.Select(e => e.GetComponentOrNull<T>()))
return _slotContainers.Values
.SelectMany(x => x.ContainedEntities.Select(e => e.GetComponentOrNull<T>()))
.Where(x => x != null);
}
public T GetSlotItem<T>(Slots slot) where T : ItemComponent
public T? GetSlotItem<T>(Slots slot) where T : ItemComponent
{
if (!_slotContainers.ContainsKey(slot))
{
@@ -212,7 +214,7 @@ namespace Content.Server.GameObjects.Components.GUI
var containedEntity = _slotContainers[slot].ContainedEntity;
if (containedEntity?.Deleted == true)
{
_slotContainers[slot] = null;
_slotContainers.Remove(slot);
containedEntity = null;
Dirty();
}
@@ -220,7 +222,7 @@ namespace Content.Server.GameObjects.Components.GUI
return containedEntity?.GetComponent<T>();
}
public bool TryGetSlotItem<T>(Slots slot, out T itemComponent) where T : ItemComponent
public bool TryGetSlotItem<T>(Slots slot, [NotNullWhen(true)] out T? itemComponent) where T : ItemComponent
{
itemComponent = GetSlotItem<T>(slot);
return itemComponent != null;
@@ -237,7 +239,7 @@ namespace Content.Server.GameObjects.Components.GUI
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <param name="reason">The translated reason why the item cannot be equipped, if this function returns false. Can be null.</param>
/// <returns>True if the item was successfully inserted, false otherwise.</returns>
public bool Equip(Slots slot, ItemComponent item, bool mobCheck, out string reason)
public bool Equip(Slots slot, ItemComponent item, bool mobCheck, [NotNullWhen(false)] out string? reason)
{
if (item == null)
{
@@ -253,6 +255,7 @@ namespace Content.Server.GameObjects.Components.GUI
var inventorySlot = _slotContainers[slot];
if (!inventorySlot.Insert(item.Owner))
{
reason = Loc.GetString("You can't equip this!");
return false;
}
@@ -280,13 +283,16 @@ namespace Content.Server.GameObjects.Components.GUI
/// <param name="item">The item to check for.</param>
/// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param>
/// <returns>True if the item can be inserted into the specified slot.</returns>
public bool CanEquip(Slots slot, ItemComponent item, bool mobCheck, out string reason)
public bool CanEquip(Slots slot, ItemComponent item, bool mobCheck, [NotNullWhen(false)] out string? reason)
{
var pass = false;
reason = null;
if (mobCheck && !ActionBlockerSystem.CanEquip(Owner))
{
reason = Loc.GetString("You can't equip this!");
return false;
}
if (item is ClothingComponent clothing)
{
@@ -300,7 +306,7 @@ namespace Content.Server.GameObjects.Components.GUI
}
}
if (Owner.TryGetComponent(out IInventoryController controller))
if (Owner.TryGetComponent(out IInventoryController? controller))
{
pass = controller.CanEquip(slot, item.Owner, pass, out var controllerReason);
reason = controllerReason ?? reason;
@@ -311,7 +317,14 @@ namespace Content.Server.GameObjects.Components.GUI
reason = Loc.GetString("You can't equip this!");
}
return pass && _slotContainers[slot].CanInsert(item.Owner);
var canEquip = pass && _slotContainers[slot].CanInsert(item.Owner);
if (!canEquip)
{
reason = Loc.GetString("You can't equip this!");
}
return canEquip;
}
public bool CanEquip(Slots slot, ItemComponent item, bool mobCheck = true) =>
@@ -335,7 +348,12 @@ namespace Content.Server.GameObjects.Components.GUI
var inventorySlot = _slotContainers[slot];
var entity = inventorySlot.ContainedEntity;
var item = entity.GetComponent<ItemComponent>();
if (entity == null)
{
return false;
}
if (!inventorySlot.Remove(entity))
{
return false;
@@ -357,7 +375,7 @@ namespace Content.Server.GameObjects.Components.GUI
private void UpdateMovementSpeed()
{
if (Owner.TryGetComponent(out MovementSpeedModifierComponent mod))
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? mod))
{
mod.RefreshMovementSpeedModifiers();
}
@@ -476,7 +494,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (container is not ContainerSlot slot || !_slotContainers.ContainsValue(slot))
return;
if (entity.TryGetComponent(out ItemComponent itemComp))
if (entity.TryGetComponent(out ItemComponent? itemComp))
{
itemComp.RemovedFromSlot();
}
@@ -497,16 +515,15 @@ namespace Content.Server.GameObjects.Components.GUI
case ClientInventoryUpdate.Equip:
{
var hands = Owner.GetComponent<HandsComponent>();
var activeHand = hands.GetActiveHand;
if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing))
var activeHand = hands.ActiveHand;
var activeItem = hands.GetActiveHand;
if (activeHand != null && activeItem != null && activeItem.Owner.TryGetComponent(out ItemComponent? clothing))
{
hands.Drop(hands.ActiveHand, doDropInteraction: false);
hands.Drop(activeHand, doDropInteraction: false);
if (!Equip(msg.Inventoryslot, clothing, true, out var reason))
{
hands.PutInHand(clothing);
if (reason != null)
Owner.PopupMessageCursor(reason);
Owner.PopupMessageCursor(reason);
}
}
@@ -553,7 +570,7 @@ namespace Content.Server.GameObjects.Components.GUI
}
/// <inheritdoc />
public override void HandleMessage(ComponentMessage message, IComponent component)
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
@@ -571,7 +588,7 @@ namespace Content.Server.GameObjects.Components.GUI
/// <inheritdoc />
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel,
ICommonSession session = null)
ICommonSession? session = null)
{
base.HandleNetworkMessage(message, netChannel, session);
@@ -593,7 +610,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (!HasSlot(msg.Slot)) // client input sanitization
return;
var item = GetSlotItem(msg.Slot);
if (item != null && item.Owner.TryGetComponent(out ServerStorageComponent storage))
if (item != null && item.Owner.TryGetComponent(out ServerStorageComponent? storage))
storage.OpenStorageUI(Owner);
break;
}