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

@@ -25,11 +25,9 @@ using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Players;
using Robust.Shared.ViewVariables;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
namespace Content.Server.GameObjects.Components.GUI
{
@@ -101,12 +99,12 @@ namespace Content.Server.GameObjects.Components.GUI
return false;
}
private Hand? GetHand(string name)
private Hand? GetHand(string? name)
{
return _hands.FirstOrDefault(hand => hand.Name == name);
}
public ItemComponent? GetItem(string handName)
public ItemComponent? GetItem(string? handName)
{
return GetHand(handName)?.Entity?.GetComponent<ItemComponent>();
}
@@ -758,19 +756,26 @@ namespace Content.Server.GameObjects.Components.GUI
return false;
var source = eventArgs.Source;
var target = eventArgs.Target;
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Effects/thudswoosh.ogg", source,
AudioHelpers.WithVariation(0.025f));
if (source != null)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Effects/thudswoosh.ogg", source,
AudioHelpers.WithVariation(0.025f));
if (ActiveHand != null && Drop(ActiveHand, false))
{
source.PopupMessageOtherClients(Loc.GetString("{0} disarms {1}!", source.Name, eventArgs.Target.Name));
source.PopupMessageCursor(Loc.GetString("You disarm {0}!", eventArgs.Target.Name));
}
else
{
source.PopupMessageOtherClients(Loc.GetString("{0} shoves {1}!", source.Name, eventArgs.Target.Name));
source.PopupMessageCursor(Loc.GetString("You shove {0}!", eventArgs.Target.Name));
if (target != null)
{
if (ActiveHand != null && Drop(ActiveHand, false))
{
source.PopupMessageOtherClients(Loc.GetString("{0} disarms {1}!", source.Name, target.Name));
source.PopupMessageCursor(Loc.GetString("You disarm {0}!", target.Name));
}
else
{
source.PopupMessageOtherClients(Loc.GetString("{0} shoves {1}!", source.Name, target.Name));
source.PopupMessageCursor(Loc.GetString("You shove {0}!", target.Name));
}
}
}
return true;

View File

@@ -1,5 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Server.GameObjects.Components.Items.Storage;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
@@ -14,7 +14,7 @@ namespace Content.Server.GameObjects.Components.GUI
{
public override string Name => "HumanInventoryController";
private InventoryComponent _inventory;
private InventoryComponent _inventory = default!;
public override void Initialize()
{
@@ -23,7 +23,7 @@ namespace Content.Server.GameObjects.Components.GUI
_inventory = Owner.EnsureComponent<InventoryComponent>();
}
bool IInventoryController.CanEquip(Slots slot, IEntity entity, bool flagsCheck, out string reason)
bool IInventoryController.CanEquip(Slots slot, IEntity entity, bool flagsCheck, [NotNullWhen(false)] out string? reason)
{
var slotMask = SlotMasks[slot];
reason = null;
@@ -59,7 +59,7 @@ namespace Content.Server.GameObjects.Components.GUI
return flagsCheck;
}
public override void HandleMessage(ComponentMessage message, IComponent component)
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);

View File

@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameObjects;
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
@@ -16,12 +17,12 @@ namespace Content.Server.GameObjects.Components.GUI
/// <param name="flagsCheck">Whether the entity passes default slot masks & flags checks.</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 entity can be equipped, false otherwise</returns>
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck, out string reason)
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck, [NotNullWhen(false)] out string? reason)
{
reason = null;
return flagsCheck;
}
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck) => CanEquip(slot, entity, flagsCheck, out var _);
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck) => CanEquip(slot, entity, flagsCheck, out _);
}
}

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;
}

View File

@@ -76,11 +76,6 @@ namespace Content.Server.GameObjects.Components.GUI
public override bool Drop(DragDropEventArgs args)
{
if (args.User == null)
{
return false;
}
if (!args.User.TryGetComponent(out IActorComponent? actor)) return false;
OpenUserInterface(actor.playerSession);
@@ -288,7 +283,7 @@ namespace Content.Server.GameObjects.Components.GUI
if (!inventory.HasSlot(slot))
return false;
if (!inventory.TryGetSlotItem(slot, out ItemComponent itemToTake))
if (!inventory.TryGetSlotItem(slot, out ItemComponent? itemToTake))
{
user.PopupMessageCursor(Loc.GetString("{0:They} {0:have} nothing there!", Owner));
return false;
@@ -319,7 +314,12 @@ namespace Content.Server.GameObjects.Components.GUI
var item = inventory.GetSlotItem(slot);
inventory.Unequip(slot, false);
userHands.PutInHandOrDrop(item);
if (item != null)
{
userHands.PutInHandOrDrop(item);
}
UpdateSubscribed();
}