From 2791215bcd684260584e80f3dee485dc1706587a Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Tue, 4 Jan 2022 10:32:22 +1300 Subject: [PATCH] Fix eject sounds sometimes not playing. (#6013) --- .../Containers/ItemSlot/ItemSlotsSystem.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs index 7935ba7d3d..7097355925 100644 --- a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs +++ b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs @@ -177,7 +177,7 @@ namespace Content.Shared.Containers.ItemSlots if (slot.Item != null) hands.TryPutInAnyHand(slot.Item.Value); - Insert(uid, slot, args.Used, args.User, true); + Insert(uid, slot, args.Used, args.User); args.Handled = true; 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 or just use instead. /// - private void Insert(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool userAudio = false) + /// If true, will exclude the user when playing sound. Does nothing client-side. + /// Useful for predicted interactions + private void Insert(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false) { slot.ContainerSlot.Insert(item); // ContainerSlot automatically raises a directed EntInsertedIntoContainerMessage - PlaySound(uid, slot.InsertSound, slot.SoundOptions, userAudio ? null : user); + PlaySound(uid, slot.InsertSound, slot.SoundOptions, excludeUserAudio ? user : null); } /// @@ -203,7 +205,7 @@ namespace Content.Shared.Containers.ItemSlots /// Source of the sound /// The sound /// Optional (server-side) argument used to prevent sending the audio to a specific - /// user. + /// user. When run client-side, exclusion does nothing. private void PlaySound(EntityUid uid, SoundSpecifier? sound, AudioParams audioParams, EntityUid? excluded) { 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 /// probably just use instead. /// - private void Eject(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user) + /// If true, will exclude the user when playing sound. Does nothing client-side. + /// Useful for predicted interactions + private void Eject(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false) { slot.ContainerSlot.Remove(item); // ContainerSlot automatically raises a directed EntRemovedFromContainerMessage - PlaySound(uid, slot.EjectSound, slot.SoundOptions, user); + PlaySound(uid, slot.EjectSound, slot.SoundOptions, excludeUserAudio ? user : null); } /// /// Try to eject an item from a slot. /// /// False if item slot is locked or has no item inserted - 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; @@ -324,7 +328,7 @@ namespace Content.Shared.Containers.ItemSlots return false; item = slot.Item; - Eject(uid, slot, item.Value, user); + Eject(uid, slot, item.Value, user, excludeUserAudio); return true; } @@ -333,7 +337,7 @@ namespace Content.Shared.Containers.ItemSlots /// /// False if the id is not valid, the item slot is locked, or it has no item inserted 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; @@ -343,7 +347,7 @@ namespace Content.Shared.Containers.ItemSlots if (!itemSlots.Slots.TryGetValue(id, out var slot)) return false; - return TryEject(uid, slot, user, out item); + return TryEject(uid, slot, user, out item, excludeUserAudio); } /// @@ -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 /// if the user has no hands. /// - 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; if (user != null && EntityManager.TryGetComponent(user.Value, out SharedHandsComponent? hands)) @@ -390,7 +394,7 @@ namespace Content.Shared.Containers.ItemSlots : EntityManager.GetComponent(slot.Item!.Value).EntityName ?? string.Empty; Verb verb = new(); - verb.Act = () => TryEjectToHands(uid, slot, args.User); + verb.Act = () => TryEjectToHands(uid, slot, args.User, excludeUserAudio: true); if (slot.EjectVerbText == null) { @@ -425,7 +429,7 @@ namespace Content.Shared.Containers.ItemSlots : EntityManager.GetComponent(slot.Item!.Value).EntityName ?? string.Empty; 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"; if (slot.EjectVerbText == null) @@ -451,7 +455,7 @@ namespace Content.Shared.Containers.ItemSlots : EntityManager.GetComponent(args.Using.Value).EntityName ?? string.Empty; 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) {