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

Improvements for working with OneSync/Infinity. #233

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
102 changes: 78 additions & 24 deletions vMenu/CommonFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,27 +389,63 @@ public static async void QuitGame()
/// </summary>
/// <param name="playerId"></param>
/// <param name="inVehicle"></param>
public static async void TeleportToPlayer(int playerId, bool inVehicle = false)
public static async Task TeleportToPlayer(IPlayer player, bool inVehicle = false)
{
// If the player exists.
if (NetworkIsPlayerActive(playerId))
if (player.IsActive || player is InfinityPlayer)
{
int playerPed = GetPlayerPed(playerId);
if (Game.PlayerPed.Handle == playerPed)
Vector3 playerPos;
bool wasActive = true;

if (player.IsActive)
{
Notify.Error("Sorry, you can ~r~~h~not~h~ ~s~teleport to yourself!");
return;
}
Ped playerPedObj = player.Character;
if (Game.PlayerPed == playerPedObj)
{
Notify.Error("Sorry, you can ~r~~h~not~h~ ~s~teleport to yourself!");
return;
}

// Get the coords of the other player.
Vector3 playerPos = GetEntityCoords(playerPed, true);
// Get the coords of the other player.
playerPos = GetEntityCoords(playerPedObj.Handle, true);
}
else
{
playerPos = await MainMenu.RequestPlayerCoordinates(player.ServerId);
wasActive = false;
}

// Then await the proper loading/teleporting.
await TeleportToCoords(playerPos);

// Wait until the player has been created.
while (player.Character == null)
{
await Delay(0);
}

var playerId = player.Handle;
var playerPed = player.Character.Handle;

// If the player should be teleported inside the other player's vehcile.
if (inVehicle)
{
// Wait until the target player vehicle has loaded, if they weren't active beforehand.
if (!wasActive)
{
var startWait = GetGameTimer();

while (!IsPedInAnyVehicle(playerPed, false))
{
await Delay(0);

if ((GetGameTimer() - startWait) > 1500)
{
break;
}
}
}

// Is the other player inside a vehicle?
if (IsPedInAnyVehicle(playerPed, false))
{
Expand Down Expand Up @@ -690,7 +726,7 @@ public static async void TeleportToWp()
/// <param name="player"></param>
/// <param name="askUserForReason"></param>
/// <param name="providedReason"></param>
public static async void KickPlayer(Player player, bool askUserForReason, string providedReason = "You have been kicked.")
public static async void KickPlayer(IPlayer player, bool askUserForReason, string providedReason = "You have been kicked.")
{
if (player != null)
{
Expand Down Expand Up @@ -728,7 +764,7 @@ public static async void KickPlayer(Player player, bool askUserForReason, string
/// </summary>
/// <param name="player">Player to ban.</param>
/// <param name="forever">Ban forever or ban temporarily.</param>
public static async void BanPlayer(Player player, bool forever)
public static async void BanPlayer(IPlayer player, bool forever)
{
string banReason = await GetUserInput(windowTitle: "Enter Ban Reason", defaultText: "Banned by staff.", maxInputLength: 200);
if (!string.IsNullOrEmpty(banReason) && banReason.Length > 1)
Expand Down Expand Up @@ -786,7 +822,7 @@ public static async void BanPlayer(Player player, bool forever)
/// Kill player
/// </summary>
/// <param name="player"></param>
public static void KillPlayer(Player player) => TriggerServerEvent("vMenu:KillPlayer", player.ServerId);
public static void KillPlayer(IPlayer player) => TriggerServerEvent("vMenu:KillPlayer", player.ServerId);

/// <summary>
/// Kill yourself.
Expand Down Expand Up @@ -901,19 +937,24 @@ public static async void CommitSuicide()
/// Summon player.
/// </summary>
/// <param name="player"></param>
public static void SummonPlayer(Player player) => TriggerServerEvent("vMenu:SummonPlayer", player.ServerId);
public static void SummonPlayer(IPlayer player) => TriggerServerEvent("vMenu:SummonPlayer", player.ServerId);
#endregion

#region Spectate function
private static int currentlySpectatingPlayer = -1;
public static async void SpectatePlayer(Player player, bool forceDisable = false)
public static async void SpectatePlayer(IPlayer player, bool forceDisable = false)
{
if (forceDisable)
{
NetworkSetInSpectatorMode(false, 0); // disable spectating.
}
else
{
if (!player.IsActive)
{
await TeleportToPlayer(player);
}

if (player.Handle == Game.Player.Handle)
{
if (NetworkIsInSpectatorMode())
Expand All @@ -934,12 +975,17 @@ public static async void SpectatePlayer(Player player, bool forceDisable = false
{
if (NetworkIsInSpectatorMode())
{
if (currentlySpectatingPlayer != player.Handle)
if (currentlySpectatingPlayer != player.Handle && player.Character != null)
{
DoScreenFadeOut(500);
while (IsScreenFadingOut()) await Delay(0);
NetworkSetInSpectatorMode(false, 0);
NetworkSetInSpectatorMode(true, player.Character.Handle);

if (player.Character != null)
{
NetworkSetInSpectatorMode(false, 0);
NetworkSetInSpectatorMode(true, player.Character.Handle);
}

DoScreenFadeIn(500);
Notify.Success($"You are now spectating ~g~<C>{GetSafePlayerName(player.Name)}</C>~s~.", false, true);
currentlySpectatingPlayer = player.Handle;
Expand All @@ -956,13 +1002,21 @@ public static async void SpectatePlayer(Player player, bool forceDisable = false
}
else
{
DoScreenFadeOut(500);
while (IsScreenFadingOut()) await Delay(0);
NetworkSetInSpectatorMode(false, 0);
NetworkSetInSpectatorMode(true, player.Character.Handle);
DoScreenFadeIn(500);
Notify.Success($"You are now spectating ~g~<C>{GetSafePlayerName(player.Name)}</C>~s~.", false, true);
currentlySpectatingPlayer = player.Handle;
if (player.Character != null)
{
DoScreenFadeOut(500);
while (IsScreenFadingOut()) await Delay(0);

if (player.Character != null)
{
NetworkSetInSpectatorMode(false, 0);
NetworkSetInSpectatorMode(true, player.Character.Handle);
}

DoScreenFadeIn(500);
Notify.Success($"You are now spectating ~g~<C>{GetSafePlayerName(player.Name)}</C>~s~.", false, true);
currentlySpectatingPlayer = player.Handle;
}
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions vMenu/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,18 @@ private void KillMe(string sourceName)
/// Teleport to the specified player.
/// </summary>
/// <param name="targetPlayer"></param>
private void SummonPlayer(string targetPlayer)
private async void SummonPlayer(string targetPlayer)
{
TeleportToPlayer(GetPlayerFromServerId(int.Parse(targetPlayer)));
// ensure the player list is requested in case of Infinity
MainMenu.PlayersList.RequestPlayerList();
await MainMenu.PlayersList.WaitRequested();

var player = MainMenu.PlayersList.FirstOrDefault(a => a.ServerId == int.Parse(targetPlayer));

if (player != null)
{
_ = TeleportToPlayer(player);
}
}

/// <summary>
Expand Down
50 changes: 9 additions & 41 deletions vMenu/FunctionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class FunctionsController : BaseScript
{
private int LastVehicle = 0;
private bool SwitchedVehicle = false;
private Dictionary<int, string> playerList = new Dictionary<int, string>();
private List<int> deadPlayers = new List<int>();
private float cameraRotationHeading = 0f;

Expand Down Expand Up @@ -68,12 +67,6 @@ class FunctionsController : BaseScript
/// </summary>
public FunctionsController()
{
// Load the initial playerlist.
foreach (Player p in Players)
{
playerList.Add(p.Handle, p.Name);
}

// Add all tick functions.
Tick += GcTick;
Tick += GeneralTasks;
Expand All @@ -90,7 +83,6 @@ public FunctionsController()
Tick += MiscSettings;
Tick += MiscRecordingKeybinds;
Tick += DeathNotifications;
Tick += JoinQuitNotifications;
Tick += UpdateLocation;
Tick += ManageCamera;
Tick += PlayerBlipsControl;
Expand Down Expand Up @@ -1118,51 +1110,27 @@ private async Task MiscRecordingKeybinds()
}
}
}
#region Join / Quit notifications
#region Join / Quit notifications (via events)
/// <summary>
/// Runs join/quit notification checks.
/// </summary>
/// <returns></returns>
private async Task JoinQuitNotifications()
[EventHandler("vMenu:PlayerJoinQuit")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll end up moving this to the events handler class but it's fine for now.

private void OnJoinQuitNotification(string playerName, string dropReason)
{
if (MainMenu.PermissionsSetupComplete && MainMenu.MiscSettingsMenu != null)
{
// Join/Quit notifications
if (MainMenu.MiscSettingsMenu.JoinQuitNotifications && IsAllowed(Permission.MSJoinQuitNotifs))
{
PlayerList plist = Players;
Dictionary<int, string> pl = new Dictionary<int, string>();
foreach (Player p in plist)
if (dropReason == null)
{
pl.Add(p.Handle, p.Name);
}
await Delay(0);
// new player joined.
if (pl.Count > playerList.Count)
{
foreach (KeyValuePair<int, string> player in pl)
{
if (!playerList.Contains(player))
{
Notify.Custom($"~g~<C>{GetSafePlayerName(player.Value)}</C>~s~ joined the server.");
await Delay(0);
}
}
Notify.Custom($"~g~<C>{GetSafePlayerName(playerName)}</C>~s~ joined the server.");
}
// player left.
else if (pl.Count < playerList.Count)
else
{
foreach (KeyValuePair<int, string> player in playerList)
{
if (!pl.Contains(player))
{
Notify.Custom($"~r~<C>{GetSafePlayerName(player.Value)}</C>~s~ left the server.");
await Delay(0);
}
}
Notify.Custom($"~r~<C>{GetSafePlayerName(playerName)}</C>~s~ left the server. ~c~({GetSafePlayerName(dropReason)})");
}
playerList = pl;
await Delay(100);
}
}
}
Expand Down Expand Up @@ -2175,7 +2143,7 @@ private async Task PlayerBlipsControl()
{
bool enabled = MainMenu.MiscSettingsMenu.ShowPlayerBlips && IsAllowed(Permission.MSPlayerBlips);

foreach (Player p in MainMenu.PlayersList)
foreach (IPlayer p in MainMenu.PlayersList)
{
// continue only if this player is valid.
if (p != null && NetworkIsPlayerActive(p.Handle) && p.Character != null && p.Character.Exists())
Expand All @@ -2188,7 +2156,7 @@ private async Task PlayerBlipsControl()
// if blips are enabled and the player has permisisons to use them.
if (enabled)
{
if (p != Game.Player)
if (!p.IsLocal)
{
int ped = p.Character.Handle;
int blip = GetBlipFromEntity(ped);
Expand Down
Loading