Skip to content

Commit

Permalink
Merge branch 'dev' into EventNotCorrectlyRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 authored Mar 20, 2024
2 parents bc339de + d061805 commit 6b8cbd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
14 changes: 12 additions & 2 deletions Exiled.API/Extensions/RoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public static class RoleExtensions
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase(this RoleTypeId roleType, out PlayerRoleBase roleBase) => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);

/// <summary>
/// Tries to get the base <see cref="PlayerRoleBase"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <param name="roleBase">The <see cref="PlayerRoleBase"/> to return.</param>
/// <typeparam name="T">The type to cast the <see cref="PlayerRoleBase"/> to.</typeparam>
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase<T>(this RoleTypeId roleType, out T roleBase)
where T : PlayerRoleBase => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);

/// <summary>
/// Gets the <see cref="LeadingTeam"/>.
/// </summary>
Expand Down Expand Up @@ -120,11 +130,11 @@ public static class RoleExtensions
/// <returns>Returns a <see cref="SpawnLocation"/> representing the spawn, or <see langword="null"/> if no spawns were found.</returns>
public static SpawnLocation GetRandomSpawnLocation(this RoleTypeId roleType)
{
if (roleType.GetRoleBase() is IFpcRole fpcRole &&
if (roleType.TryGetRoleBase(out FpcStandardRoleBase fpcRole) &&
fpcRole.SpawnpointHandler != null &&
fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRotation))
{
return new SpawnLocation(roleType, position, horizontalRotation);
return new(roleType, position, horizontalRotation);
}

return null;
Expand Down
47 changes: 21 additions & 26 deletions Exiled.Events/Patches/Events/Player/Spawning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ namespace Exiled.Events.Patches.Events.Player
using System.Reflection;

using API.Features;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;

using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using PlayerRoles.FirstPersonControl.NetworkMessages;
using PlayerRoles.FirstPersonControl.Spawnpoints;

using UnityEngine;
Expand All @@ -27,8 +25,8 @@ namespace Exiled.Events.Patches.Events.Player
/// <summary>
/// Patches <see cref="RoleSpawnpointManager.Init"/> delegate.
/// Adds the <see cref="Handlers.Player.Spawning"/> event.
/// Fix for spawning in void.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Spawning))]
[HarmonyPatch]
internal static class Spawning
{
Expand All @@ -39,36 +37,33 @@ private static MethodInfo TargetMethod()

private static bool Prefix(ReferenceHub hub, PlayerRoleBase prevRole, PlayerRoleBase newRole)
{
if (newRole.ServerSpawnReason != RoleChangeReason.Destroyed && Player.TryGet(hub, out Player player))
{
Vector3 oldPosition = hub.transform.position;
float oldRotation = (prevRole as IFpcRole)?.FpcModule.MouseLook.CurrentVertical ?? 0;

if (newRole is IFpcRole fpcRole)
{
if (newRole.ServerSpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint) && fpcRole.SpawnpointHandler != null && fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRot))
{
oldPosition = position;
oldRotation = horizontalRot;
}
if (newRole.ServerSpawnReason == RoleChangeReason.Destroyed || Player.TryGet(hub, out Player player))
return true;

SpawningEventArgs ev = new(player, oldPosition, oldRotation, prevRole);
Vector3 oldPosition = hub.transform.position;
float oldRotation = (prevRole as IFpcRole)?.FpcModule.MouseLook.CurrentVertical ?? 0;

Handlers.Player.OnSpawning(ev);

hub.transform.position = ev.Position;
fpcRole.FpcModule.MouseLook.CurrentHorizontal = ev.HorizontalRotation;
hub.connectionToClient.Send(new FpcOverrideMessage(ev.Position, ev.HorizontalRotation), 0);
}
else
if (newRole is IFpcRole fpcRole)
{
if (newRole.ServerSpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint) && fpcRole.SpawnpointHandler != null && fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRot))
{
Handlers.Player.OnSpawning(new(player, oldPosition, oldRotation, prevRole));
oldPosition = position;
oldRotation = horizontalRot;
}

return false;
SpawningEventArgs ev = new(player, oldPosition, oldRotation, prevRole);

Handlers.Player.OnSpawning(ev);

player.Position = ev.Position;
fpcRole.FpcModule.MouseLook.CurrentHorizontal = ev.HorizontalRotation;
}
else
{
Handlers.Player.OnSpawning(new(player, oldPosition, oldRotation, prevRole));
}

return true;
return false;
}
}
}

0 comments on commit 6b8cbd4

Please sign in to comment.