diff --git a/EXILED/Exiled.API/Extensions/MirrorExtensions.cs b/EXILED/Exiled.API/Extensions/MirrorExtensions.cs index 1e8187c09..2c982ee16 100644 --- a/EXILED/Exiled.API/Extensions/MirrorExtensions.cs +++ b/EXILED/Exiled.API/Extensions/MirrorExtensions.cs @@ -15,20 +15,23 @@ namespace Exiled.API.Extensions using System.Reflection.Emit; using System.Text; + using AudioPooling; using Exiled.API.Enums; + using Exiled.API.Features.Items; using Features; using Features.Pools; - + using InventorySystem; + using InventorySystem.Items; + using InventorySystem.Items.Firearms; + using InventorySystem.Items.Firearms.Modules; + using MEC; using Mirror; - using PlayerRoles; using PlayerRoles.FirstPersonControl; using PlayerRoles.PlayableScps.Scp049.Zombies; using PlayerRoles.Voice; using RelativePositioning; - using Respawning; - using UnityEngine; /// @@ -167,20 +170,54 @@ public static ReadOnlyDictionary RpcFullNames /// Weapon' sound to play. /// Sound's volume to set. /// GunAudioMessage's audioClipId to set (default = 0). + [Obsolete("This method is not working. Use PlayGunSound(Player, Vector3, ItemType, float, int, bool) overload instead.")] public static void PlayGunSound(this Player player, Vector3 position, ItemType itemType, byte volume, byte audioClipId = 0) { - // TODO: Not finish - /* - GunAudioMessage message = new() + } + + /// + /// Plays a gun sound that only the can hear. + /// + /// Target to play. + /// Position to play on. + /// Weapon's sound to play. + /// Speed of sound. + /// Index of clip. + public static void PlayGunSound(this Player player, Vector3 position, FirearmType itemType, float pitch = 1, int clipIndex = 0) + { + if (itemType is FirearmType.ParticleDisruptor or FirearmType.None) + return; + + Features.Items.Firearm firearm = Features.Items.Firearm.ItemTypeToFirearmInstance[itemType]; + + if (firearm == null) + return; + + using (NetworkWriterPooled writer = NetworkWriterPool.Get()) { - Weapon = itemType, - AudioClipId = audioClipId, - MaxDistance = volume, - ShooterHub = player.ReferenceHub, - ShooterPosition = new RelativePosition(position), - }; + writer.WriteUShort(NetworkMessageId.Id); + new RoleSyncInfo(Server.Host.ReferenceHub, RoleTypeId.ClassD, player.ReferenceHub).Write(writer); + writer.WriteRelativePosition(new RelativePosition(0, 0, 0, 0, false)); + writer.WriteUShort(0); + player.Connection.Send(writer); + } + + firearm.BarrelAmmo = 1; + firearm.BarrelMagazine.IsCocked = true; + player.SendFakeSyncVar(Server.Host.Inventory.netIdentity, typeof(Inventory), nameof(Inventory.NetworkCurItem), firearm.Identifier); - player.Connection.Send(message);*/ + if (!firearm.Base.TryGetModule(out AudioModule audioModule)) + return; + + Timing.CallDelayed(0.1f, () => // due to selecting item we need to delay shot a bit + { + audioModule.SendRpc(player.ReferenceHub, writer => + audioModule.ServerSend(writer, clipIndex, pitch, MixerChannel.Weapons, 12f, position, false)); + + player.SendFakeSyncVar(Server.Host.Inventory.netIdentity, typeof(Inventory), nameof(Inventory.NetworkCurItem), ItemIdentifier.None); + + player.Connection.Send(new RoleSyncInfo(Server.Host.ReferenceHub, Server.Host.Role, player.ReferenceHub)); + }); } /// diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index 7e46a408f..c2e587ee6 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -195,6 +195,11 @@ public ItemAddReason AddReason set => Base.ServerAddReason = value; } + /// + /// Gets the for this item. + /// + public ItemIdentifier Identifier => Base.ItemId; + /// /// Gets an existing or creates a new instance of one. /// diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index c0bc8f40b..9202be937 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -3461,8 +3461,14 @@ public void Reconnect(ushort newPort = 0, float delay = 5, bool reconnect = true } /// - public void PlayGunSound(ItemType type, byte volume, byte audioClipId = 0) => - MirrorExtensions.PlayGunSound(this, Position, type, volume, audioClipId); + [Obsolete("Use PlayGunSound(Player, Vector3, ItemType, byte, byte) instead.")] + public void PlayGunSound(ItemType type, byte volume, byte audioClipId = 0) + { + } + + /// + public void PlayGunSound(FirearmType itemType, float pitch = 1, int clipIndex = 0) => + this.PlayGunSound(Position, itemType, pitch, clipIndex); /// public void PlaceBlood(Vector3 direction) => Map.PlaceBlood(Position, direction);