Make held entity nullable (#5923)
This commit is contained in:
@@ -71,7 +71,7 @@ namespace Content.Shared.Access.Systems
|
||||
if (EntityManager.TryGetComponent(uid, out SharedHandsComponent? hands))
|
||||
{
|
||||
if (hands.TryGetActiveHeldEntity(out var heldItem) &&
|
||||
FindAccessTagsItem(heldItem, out tags))
|
||||
FindAccessTagsItem(heldItem.Value, out tags))
|
||||
{
|
||||
return tags;
|
||||
}
|
||||
|
||||
@@ -285,15 +285,16 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
|
||||
if (!hands.TryGetActiveHeldEntity(out var item))
|
||||
return false;
|
||||
var heldItem = item.Value;
|
||||
|
||||
if (!CanInsert(uid, item, slot))
|
||||
if (!CanInsert(uid, item.Value, slot))
|
||||
return false;
|
||||
|
||||
// hands.Drop(item) checks CanDrop action blocker
|
||||
if (!_actionBlockerSystem.CanInteract(user) && hands.Drop(item))
|
||||
if (!_actionBlockerSystem.CanInteract(user) && hands.Drop(heldItem))
|
||||
return false;
|
||||
|
||||
Insert(uid, slot, item, user);
|
||||
Insert(uid, slot, heldItem, user);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -217,23 +217,23 @@ namespace Content.Shared.Hands.Components
|
||||
if (!TryGetActiveHand(out var hand))
|
||||
return false;
|
||||
|
||||
return hand.HeldEntity != default;
|
||||
return hand.HeldEntity != null;
|
||||
}
|
||||
|
||||
public bool TryGetHeldEntity(string handName, out EntityUid heldEntity)
|
||||
public bool TryGetHeldEntity(string handName,[NotNullWhen(true)] out EntityUid? heldEntity)
|
||||
{
|
||||
heldEntity = default;
|
||||
heldEntity = null;
|
||||
|
||||
if (!TryGetHand(handName, out var hand))
|
||||
return false;
|
||||
|
||||
heldEntity = hand.HeldEntity;
|
||||
return heldEntity != default;
|
||||
return hand.HeldEntity != null;
|
||||
}
|
||||
|
||||
public bool TryGetActiveHeldEntity(out EntityUid heldEntity)
|
||||
public bool TryGetActiveHeldEntity([NotNullWhen(true)] out EntityUid? heldEntity)
|
||||
{
|
||||
heldEntity = GetActiveHand()?.HeldEntity ?? default;
|
||||
heldEntity = GetActiveHand()?.HeldEntity;
|
||||
return heldEntity != null;
|
||||
}
|
||||
|
||||
@@ -251,8 +251,8 @@ namespace Content.Shared.Hands.Components
|
||||
{
|
||||
foreach (var hand in Hands)
|
||||
{
|
||||
if (hand.HeldEntity != default)
|
||||
yield return hand.HeldEntity;
|
||||
if (hand.HeldEntity.HasValue)
|
||||
yield return hand.HeldEntity.Value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,7 +265,7 @@ namespace Content.Shared.Hands.Components
|
||||
int acc = 0;
|
||||
foreach (var hand in Hands)
|
||||
{
|
||||
if (hand.HeldEntity == default)
|
||||
if (hand.HeldEntity == null)
|
||||
acc += 1;
|
||||
}
|
||||
|
||||
@@ -418,16 +418,8 @@ namespace Content.Shared.Hands.Components
|
||||
if (hand.HeldEntity == null)
|
||||
return false;
|
||||
|
||||
var heldEntity = hand.HeldEntity;
|
||||
|
||||
var handContainer = hand.Container;
|
||||
if (handContainer == null)
|
||||
return false;
|
||||
|
||||
if (!handContainer.CanRemove(heldEntity))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return hand.Container?.CanRemove(hand.HeldEntity.Value) ?? false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -446,11 +438,9 @@ namespace Content.Shared.Hands.Components
|
||||
/// </summary>
|
||||
private void RemoveHeldEntityFromHand(Hand hand)
|
||||
{
|
||||
if (hand.HeldEntity == null)
|
||||
if (hand.HeldEntity is not { } heldEntity)
|
||||
return;
|
||||
|
||||
var heldEntity = hand.HeldEntity;
|
||||
|
||||
var handContainer = hand.Container;
|
||||
if (handContainer == null)
|
||||
return;
|
||||
@@ -474,11 +464,9 @@ namespace Content.Shared.Hands.Components
|
||||
/// </summary>
|
||||
public void DropHeldEntity(Hand hand, EntityCoordinates targetDropLocation)
|
||||
{
|
||||
if (hand.IsEmpty)
|
||||
if (hand.HeldEntity is not { } heldEntity)
|
||||
return;
|
||||
|
||||
var heldEntity = hand.HeldEntity;
|
||||
|
||||
RemoveHeldEntityFromHand(hand);
|
||||
|
||||
EntitySystem.Get<SharedInteractionSystem>().DroppedInteraction(Owner, heldEntity);
|
||||
@@ -537,11 +525,9 @@ namespace Content.Shared.Hands.Components
|
||||
|
||||
private bool CanPutHeldEntityIntoContainer(Hand hand, IContainer targetContainer, bool checkActionBlocker)
|
||||
{
|
||||
if (hand.HeldEntity == null)
|
||||
if (hand.HeldEntity is not { } heldEntity)
|
||||
return false;
|
||||
|
||||
var heldEntity = hand.HeldEntity;
|
||||
|
||||
if (checkActionBlocker && !PlayerCanDrop())
|
||||
return false;
|
||||
|
||||
@@ -556,11 +542,9 @@ namespace Content.Shared.Hands.Components
|
||||
/// </summary>
|
||||
private void PutHeldEntityIntoContainer(Hand hand, IContainer targetContainer)
|
||||
{
|
||||
if (hand.HeldEntity == null)
|
||||
if (hand.HeldEntity is not { } heldEntity)
|
||||
return;
|
||||
|
||||
var heldEntity = hand.HeldEntity;
|
||||
|
||||
RemoveHeldEntityFromHand(hand);
|
||||
|
||||
if (!targetContainer.Insert(heldEntity))
|
||||
@@ -718,7 +702,7 @@ namespace Content.Shared.Hands.Components
|
||||
return;
|
||||
|
||||
await EntitySystem.Get<SharedInteractionSystem>()
|
||||
.InteractUsing(Owner, activeHeldEntity, heldEntity, EntityCoordinates.Invalid);
|
||||
.InteractUsing(Owner, activeHeldEntity.Value, heldEntity.Value, EntityCoordinates.Invalid);
|
||||
}
|
||||
|
||||
public void ActivateItem(bool altInteract = false)
|
||||
@@ -727,7 +711,7 @@ namespace Content.Shared.Hands.Components
|
||||
return;
|
||||
|
||||
EntitySystem.Get<SharedInteractionSystem>()
|
||||
.TryUseInteraction(Owner, heldEntity, altInteract);
|
||||
.TryUseInteraction(Owner, heldEntity.Value, altInteract);
|
||||
}
|
||||
|
||||
public void ActivateHeldEntity(string handName)
|
||||
@@ -750,14 +734,14 @@ namespace Content.Shared.Hands.Components
|
||||
if (!TryGetHeldEntity(handName, out var heldEntity))
|
||||
return false;
|
||||
|
||||
if (!CanInsertEntityIntoHand(activeHand, heldEntity) || !CanRemoveHeldEntityFromHand(hand))
|
||||
if (!CanInsertEntityIntoHand(activeHand, heldEntity.Value) || !CanRemoveHeldEntityFromHand(hand))
|
||||
return false;
|
||||
|
||||
if (checkActionBlocker && (!PlayerCanDrop() || !PlayerCanPickup()))
|
||||
return false;
|
||||
|
||||
RemoveHeldEntityFromHand(hand);
|
||||
PutEntityIntoHand(activeHand, heldEntity);
|
||||
PutEntityIntoHand(activeHand, heldEntity.Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -766,13 +750,13 @@ namespace Content.Shared.Hands.Components
|
||||
private void DeselectActiveHeldEntity()
|
||||
{
|
||||
if (TryGetActiveHeldEntity(out var entity))
|
||||
EntitySystem.Get<SharedInteractionSystem>().HandDeselectedInteraction(Owner, entity);
|
||||
EntitySystem.Get<SharedInteractionSystem>().HandDeselectedInteraction(Owner, entity.Value);
|
||||
}
|
||||
|
||||
private void SelectActiveHeldEntity()
|
||||
{
|
||||
if (TryGetActiveHeldEntity(out var entity))
|
||||
EntitySystem.Get<SharedInteractionSystem>().HandSelectedInteraction(Owner, entity);
|
||||
EntitySystem.Get<SharedInteractionSystem>().HandSelectedInteraction(Owner, entity.Value);
|
||||
}
|
||||
|
||||
private void HandCountChanged()
|
||||
@@ -905,11 +889,9 @@ namespace Content.Shared.Hands.Components
|
||||
[ViewVariables]
|
||||
public IContainer? Container { get; set; }
|
||||
|
||||
// TODO: Make this a nullable EntityUid...
|
||||
[ViewVariables]
|
||||
public EntityUid HeldEntity => Container?.ContainedEntities.FirstOrDefault() ?? EntityUid.Invalid;
|
||||
public EntityUid? HeldEntity => Container?.ContainedEntities?.Count > 0 ? Container.ContainedEntities[0] : null;
|
||||
|
||||
public bool IsEmpty => HeldEntity == default;
|
||||
|
||||
public Hand(string name, HandLocation location, IContainer? container = null)
|
||||
{
|
||||
|
||||
@@ -139,18 +139,15 @@ namespace Content.Shared.Interaction
|
||||
if (!TryComp(user, out SharedHandsComponent? hands))
|
||||
return;
|
||||
|
||||
// TODO remove invalid/default uid and use nullable.
|
||||
hands.TryGetActiveHeldEntity(out var heldEntity);
|
||||
EntityUid? held = heldEntity.Valid ? heldEntity : null;
|
||||
|
||||
// TODO: Replace with body interaction range when we get something like arm length or telekinesis or something.
|
||||
var inRangeUnobstructed = user.InRangeUnobstructed(coordinates, ignoreInsideBlocker: true);
|
||||
if (target == null || !inRangeUnobstructed)
|
||||
{
|
||||
if (held == null)
|
||||
if (!hands.TryGetActiveHeldEntity(out var heldEntity))
|
||||
return;
|
||||
|
||||
if (!await InteractUsingRanged(user, held.Value, target, coordinates, inRangeUnobstructed) &&
|
||||
if (!await InteractUsingRanged(user, heldEntity.Value, target, coordinates, inRangeUnobstructed) &&
|
||||
!inRangeUnobstructed)
|
||||
{
|
||||
var message = Loc.GetString("interaction-system-user-interaction-cannot-reach");
|
||||
@@ -165,13 +162,17 @@ namespace Content.Shared.Interaction
|
||||
if (altInteract)
|
||||
// Perform alternative interactions, using context menu verbs.
|
||||
AltInteract(user, target.Value);
|
||||
else if (held != null && held != target)
|
||||
// We are performing a standard interaction with an item, and the target isn't the same as the item
|
||||
// currently in our hand. We will use the item in our hand on the nearby object via InteractUsing
|
||||
await InteractUsing(user, held.Value, target.Value, coordinates);
|
||||
else if (held == null)
|
||||
|
||||
if (!hands.TryGetActiveHeldEntity(out var heldEntity))
|
||||
{
|
||||
// Since our hand is empty we will use InteractHand/Activate
|
||||
InteractHand(user, target.Value);
|
||||
}
|
||||
else if (heldEntity != target)
|
||||
{
|
||||
await InteractUsing(user, heldEntity.Value, target.Value, coordinates);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user