Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PlayGunSound Extensions fix #386

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions EXILED/Exiled.API/Extensions/MirrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -167,20 +170,48 @@ public static ReadOnlyDictionary<string, string> RpcFullNames
/// <param name="itemType">Weapon' sound to play.</param>
/// <param name="volume">Sound's volume to set.</param>
/// <param name="audioClipId">GunAudioMessage's audioClipId to set (default = 0).</param>
[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()
}

/// <summary>
/// Plays a gun sound that only the <paramref name="player"/> can hear.
/// </summary>
/// <param name="player">Target to play.</param>
/// <param name="position">Position to play on.</param>
/// <param name="itemType">Weapon' sound to play.</param>
/// <param name="pitch">Speed of sound.</param>
/// <param name="clipIndex">Index of clip.</param>
public static void PlayGunSound(this Player player, Vector3 position, ItemType itemType, float pitch = 1, int clipIndex = 0)
{
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
{
Weapon = itemType,
AudioClipId = audioClipId,
MaxDistance = volume,
ShooterHub = player.ReferenceHub,
ShooterPosition = new RelativePosition(position),
};
writer.WriteUShort(NetworkMessageId<RoleSyncInfo>.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);
}

Features.Items.Firearm firearm = Item.Get<Features.Items.Firearm>(Server.Host.Inventory.ServerAddItem(itemType, ItemAddReason.AdminCommand));
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, () => // Seems like without this delay firearm is not 'ready'
{
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);
firearm.Destroy();

player.Connection.Send(new RoleSyncInfo(Server.Host.ReferenceHub, RoleTypeId.ClassD, player.ReferenceHub));
});
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions EXILED/Exiled.API/Features/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public ItemAddReason AddReason
set => Base.ServerAddReason = value;
}

/// <summary>
/// Gets the <see cref="ItemIdentifier"/> for this item.
/// </summary>
public ItemIdentifier Identifier => Base.ItemId;

/// <summary>
/// Gets an existing <see cref="Item"/> or creates a new instance of one.
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3461,8 +3461,14 @@ public void Reconnect(ushort newPort = 0, float delay = 5, bool reconnect = true
}

/// <inheritdoc cref="MirrorExtensions.PlayGunSound(Player, Vector3, ItemType, byte, byte)"/>
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)
{
}

/// <inheritdoc cref="MirrorExtensions.PlayGunSound(Player, Vector3, ItemType, float, int)"/>
public void PlayGunSound(ItemType itemType, float pitch = 1, int clipIndex = 0) =>
this.PlayGunSound(Position, itemType, pitch, clipIndex);

/// <inheritdoc cref="Map.PlaceBlood(Vector3, Vector3)"/>
public void PlaceBlood(Vector3 direction) => Map.PlaceBlood(Position, direction);
Expand Down
Loading