Fix eject sounds sometimes not playing. (#6013)

This commit is contained in:
Leon Friedrich
2022-01-04 10:32:22 +13:00
committed by GitHub
parent c5a98129ff
commit 2791215bcd

View File

@@ -177,7 +177,7 @@ namespace Content.Shared.Containers.ItemSlots
if (slot.Item != null) if (slot.Item != null)
hands.TryPutInAnyHand(slot.Item.Value); hands.TryPutInAnyHand(slot.Item.Value);
Insert(uid, slot, args.Used, args.User, true); Insert(uid, slot, args.Used, args.User);
args.Handled = true; args.Handled = true;
return; return;
} }
@@ -189,12 +189,14 @@ namespace Content.Shared.Containers.ItemSlots
/// Insert an item into a slot. This does not perform checks, so make sure to also use <see /// Insert an item into a slot. This does not perform checks, so make sure to also use <see
/// cref="CanInsert"/> or just use <see cref="TryInsert"/> instead. /// cref="CanInsert"/> or just use <see cref="TryInsert"/> instead.
/// </summary> /// </summary>
private void Insert(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool userAudio = false) /// <param name="excludeUserAudio">If true, will exclude the user when playing sound. Does nothing client-side.
/// Useful for predicted interactions</param>
private void Insert(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false)
{ {
slot.ContainerSlot.Insert(item); slot.ContainerSlot.Insert(item);
// ContainerSlot automatically raises a directed EntInsertedIntoContainerMessage // ContainerSlot automatically raises a directed EntInsertedIntoContainerMessage
PlaySound(uid, slot.InsertSound, slot.SoundOptions, userAudio ? null : user); PlaySound(uid, slot.InsertSound, slot.SoundOptions, excludeUserAudio ? user : null);
} }
/// <summary> /// <summary>
@@ -203,7 +205,7 @@ namespace Content.Shared.Containers.ItemSlots
/// <param name="uid">Source of the sound</param> /// <param name="uid">Source of the sound</param>
/// <param name="sound">The sound</param> /// <param name="sound">The sound</param>
/// <param name="excluded">Optional (server-side) argument used to prevent sending the audio to a specific /// <param name="excluded">Optional (server-side) argument used to prevent sending the audio to a specific
/// user.</param> /// user. When run client-side, exclusion does nothing.</param>
private void PlaySound(EntityUid uid, SoundSpecifier? sound, AudioParams audioParams, EntityUid? excluded) private void PlaySound(EntityUid uid, SoundSpecifier? sound, AudioParams audioParams, EntityUid? excluded)
{ {
if (sound == null || !_gameTiming.IsFirstTimePredicted) if (sound == null || !_gameTiming.IsFirstTimePredicted)
@@ -304,19 +306,21 @@ namespace Content.Shared.Containers.ItemSlots
/// Eject an item into a slot. This does not perform checks (e.g., is the slot locked?), so you should /// Eject an item into a slot. This does not perform checks (e.g., is the slot locked?), so you should
/// probably just use <see cref="TryEject"/> instead. /// probably just use <see cref="TryEject"/> instead.
/// </summary> /// </summary>
private void Eject(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user) /// <param name="excludeUserAudio">If true, will exclude the user when playing sound. Does nothing client-side.
/// Useful for predicted interactions</param>
private void Eject(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false)
{ {
slot.ContainerSlot.Remove(item); slot.ContainerSlot.Remove(item);
// ContainerSlot automatically raises a directed EntRemovedFromContainerMessage // ContainerSlot automatically raises a directed EntRemovedFromContainerMessage
PlaySound(uid, slot.EjectSound, slot.SoundOptions, user); PlaySound(uid, slot.EjectSound, slot.SoundOptions, excludeUserAudio ? user : null);
} }
/// <summary> /// <summary>
/// Try to eject an item from a slot. /// Try to eject an item from a slot.
/// </summary> /// </summary>
/// <returns>False if item slot is locked or has no item inserted</returns> /// <returns>False if item slot is locked or has no item inserted</returns>
public bool TryEject(EntityUid uid, ItemSlot slot, EntityUid? user, [NotNullWhen(true)] out EntityUid? item) public bool TryEject(EntityUid uid, ItemSlot slot, EntityUid? user, [NotNullWhen(true)] out EntityUid? item, bool excludeUserAudio = false)
{ {
item = null; item = null;
@@ -324,7 +328,7 @@ namespace Content.Shared.Containers.ItemSlots
return false; return false;
item = slot.Item; item = slot.Item;
Eject(uid, slot, item.Value, user); Eject(uid, slot, item.Value, user, excludeUserAudio);
return true; return true;
} }
@@ -333,7 +337,7 @@ namespace Content.Shared.Containers.ItemSlots
/// </summary> /// </summary>
/// <returns>False if the id is not valid, the item slot is locked, or it has no item inserted</returns> /// <returns>False if the id is not valid, the item slot is locked, or it has no item inserted</returns>
public bool TryEject(EntityUid uid, string id, EntityUid user, public bool TryEject(EntityUid uid, string id, EntityUid user,
[NotNullWhen(true)] out EntityUid? item, ItemSlotsComponent? itemSlots = null) [NotNullWhen(true)] out EntityUid? item, ItemSlotsComponent? itemSlots = null, bool excludeUserAudio = false)
{ {
item = null; item = null;
@@ -343,7 +347,7 @@ namespace Content.Shared.Containers.ItemSlots
if (!itemSlots.Slots.TryGetValue(id, out var slot)) if (!itemSlots.Slots.TryGetValue(id, out var slot))
return false; return false;
return TryEject(uid, slot, user, out item); return TryEject(uid, slot, user, out item, excludeUserAudio);
} }
/// <summary> /// <summary>
@@ -354,9 +358,9 @@ namespace Content.Shared.Containers.ItemSlots
/// False if the id is not valid, the item slot is locked, or it has no item inserted. True otherwise, even /// False if the id is not valid, the item slot is locked, or it has no item inserted. True otherwise, even
/// if the user has no hands. /// if the user has no hands.
/// </returns> /// </returns>
public bool TryEjectToHands(EntityUid uid, ItemSlot slot, EntityUid? user) public bool TryEjectToHands(EntityUid uid, ItemSlot slot, EntityUid? user, bool excludeUserAudio = false)
{ {
if (!TryEject(uid, slot, user, out var item)) if (!TryEject(uid, slot, user, out var item, excludeUserAudio))
return false; return false;
if (user != null && EntityManager.TryGetComponent(user.Value, out SharedHandsComponent? hands)) if (user != null && EntityManager.TryGetComponent(user.Value, out SharedHandsComponent? hands))
@@ -390,7 +394,7 @@ namespace Content.Shared.Containers.ItemSlots
: EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty; : EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty;
Verb verb = new(); Verb verb = new();
verb.Act = () => TryEjectToHands(uid, slot, args.User); verb.Act = () => TryEjectToHands(uid, slot, args.User, excludeUserAudio: true);
if (slot.EjectVerbText == null) if (slot.EjectVerbText == null)
{ {
@@ -425,7 +429,7 @@ namespace Content.Shared.Containers.ItemSlots
: EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty; : EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty;
Verb takeVerb = new(); Verb takeVerb = new();
takeVerb.Act = () => TryEjectToHands(uid, slot, args.User); takeVerb.Act = () => TryEjectToHands(uid, slot, args.User, excludeUserAudio: true);
takeVerb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png"; takeVerb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";
if (slot.EjectVerbText == null) if (slot.EjectVerbText == null)
@@ -451,7 +455,7 @@ namespace Content.Shared.Containers.ItemSlots
: EntityManager.GetComponent<MetaDataComponent>(args.Using.Value).EntityName ?? string.Empty; : EntityManager.GetComponent<MetaDataComponent>(args.Using.Value).EntityName ?? string.Empty;
Verb insertVerb = new(); Verb insertVerb = new();
insertVerb.Act = () => Insert(uid, slot, args.Using.Value, args.User); insertVerb.Act = () => Insert(uid, slot, args.Using.Value, args.User, excludeUserAudio: true);
if (slot.InsertVerbText != null) if (slot.InsertVerbText != null)
{ {