Fix nullable errors with AfterInteract in UtensilComponent.

This commit is contained in:
Pieter-Jan Briers
2021-01-11 09:36:21 +01:00
parent 99727e8bc3
commit 96b21ffd1d
4 changed files with 23 additions and 20 deletions

View File

@@ -112,9 +112,9 @@ namespace Content.Server.GameObjects.Components.Culinary
TryUseUtensil(eventArgs.User, eventArgs.Target); TryUseUtensil(eventArgs.User, eventArgs.Target);
} }
private void TryUseUtensil(IEntity user, IEntity target) private void TryUseUtensil(IEntity user, IEntity? target)
{ {
if (!target.TryGetComponent(out FoodComponent? food)) if (target == null || !target.TryGetComponent(out FoodComponent? food))
{ {
return; return;
} }

View File

@@ -411,7 +411,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
} }
var afterInteracts = weapon.GetAllComponents<IAfterInteract>().ToList(); var afterInteracts = weapon.GetAllComponents<IAfterInteract>().ToList();
var afterInteractEventArgs = new AfterInteractEventArgs { User = user, ClickLocation = clickLocation, CanReach = canReach }; var afterInteractEventArgs = new AfterInteractEventArgs(user, clickLocation, null, canReach);
foreach (var afterInteract in afterInteracts) foreach (var afterInteract in afterInteracts)
{ {
@@ -460,10 +460,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
// If we aren't directly attacking the nearby object, lets see if our item has an after attack we can do // If we aren't directly attacking the nearby object, lets see if our item has an after attack we can do
var afterAttacks = weapon.GetAllComponents<IAfterInteract>().ToList(); var afterAttacks = weapon.GetAllComponents<IAfterInteract>().ToList();
var afterAttackEventArgs = new AfterInteractEventArgs var afterAttackEventArgs = new AfterInteractEventArgs(user, clickLocation, attacked, canReach: true);
{
User = user, ClickLocation = clickLocation, Target = attacked, CanReach = true
};
foreach (var afterAttack in afterAttacks) foreach (var afterAttack in afterAttacks)
{ {
@@ -833,10 +830,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
return; return;
var afterAttacks = weapon.GetAllComponents<IAfterInteract>().ToList(); var afterAttacks = weapon.GetAllComponents<IAfterInteract>().ToList();
var afterAttackEventArgs = new AfterInteractEventArgs var afterAttackEventArgs = new AfterInteractEventArgs(user, clickLocation, attacked, canReach: false);
{
User = user, ClickLocation = clickLocation, Target = attacked, CanReach = false
};
//See if we have a ranged attack interaction //See if we have a ranged attack interaction
foreach (var afterAttack in afterAttacks) foreach (var afterAttack in afterAttacks)

View File

@@ -6,7 +6,6 @@ using Content.Shared.Utility;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map; using Robust.Shared.Map;
@@ -161,7 +160,7 @@ namespace Content.Shared.GameObjects.EntitySystems
public static bool InRangeUnOccluded(AfterInteractEventArgs args, float range, Ignored? predicate, bool ignoreInsideBlocker = true) public static bool InRangeUnOccluded(AfterInteractEventArgs args, float range, Ignored? predicate, bool ignoreInsideBlocker = true)
{ {
var originPos = args.User.Transform.MapPosition; var originPos = args.User.Transform.MapPosition;
var otherPos = args.Target.Transform.MapPosition; var otherPos = args.Target?.Transform.MapPosition ?? args.ClickLocation.ToMap(args.User.EntityManager);
return InRangeUnOccluded(originPos, otherPos, range, predicate, ignoreInsideBlocker); return InRangeUnOccluded(originPos, otherPos, range, predicate, ignoreInsideBlocker);
} }

View File

@@ -5,6 +5,8 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map; using Robust.Shared.Map;
#nullable enable
namespace Content.Shared.Interfaces.GameObjects.Components namespace Content.Shared.Interfaces.GameObjects.Components
{ {
/// <summary> /// <summary>
@@ -22,10 +24,18 @@ namespace Content.Shared.Interfaces.GameObjects.Components
public class AfterInteractEventArgs : EventArgs public class AfterInteractEventArgs : EventArgs
{ {
public IEntity User { get; set; } public IEntity User { get; }
public EntityCoordinates ClickLocation { get; set; } public EntityCoordinates ClickLocation { get; }
public IEntity Target { get; set; } public IEntity? Target { get; }
public bool CanReach { get; set; } public bool CanReach { get; }
public AfterInteractEventArgs(IEntity user, EntityCoordinates clickLocation, IEntity? target, bool canReach)
{
User = user;
ClickLocation = clickLocation;
Target = target;
CanReach = canReach;
}
} }
/// <summary> /// <summary>
@@ -52,7 +62,7 @@ namespace Content.Shared.Interfaces.GameObjects.Components
/// <summary> /// <summary>
/// Entity that was attacked. This can be null if the attack did not click on an entity. /// Entity that was attacked. This can be null if the attack did not click on an entity.
/// </summary> /// </summary>
public IEntity Attacked { get; } public IEntity? Attacked { get; }
/// <summary> /// <summary>
/// Location that the user clicked outside of their interaction range. /// Location that the user clicked outside of their interaction range.
@@ -65,7 +75,8 @@ namespace Content.Shared.Interfaces.GameObjects.Components
/// </summary> /// </summary>
public bool CanReach { get; } public bool CanReach { get; }
public AfterInteractMessage(IEntity user, IEntity itemInHand, IEntity attacked, EntityCoordinates clickLocation, bool canReach) public AfterInteractMessage(IEntity user, IEntity itemInHand, IEntity? attacked,
EntityCoordinates clickLocation, bool canReach)
{ {
User = user; User = user;
Attacked = attacked; Attacked = attacked;
@@ -74,5 +85,4 @@ namespace Content.Shared.Interfaces.GameObjects.Components
CanReach = canReach; CanReach = canReach;
} }
} }
} }