From 5047eefe9ad4820497654677d4df74956b2b1005 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Thu, 24 Feb 2022 00:25:12 +0000 Subject: [PATCH] Feat(Admin): Better TPM command integrates https://github.com/douth-c/citizen-tpm/blob/main/client.lua TPM function --- [esx]/es_extended/client/main.lua | 104 ++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/[esx]/es_extended/client/main.lua b/[esx]/es_extended/client/main.lua index f333e618..6eb2eea1 100644 --- a/[esx]/es_extended/client/main.lua +++ b/[esx]/es_extended/client/main.lua @@ -515,29 +515,91 @@ end RegisterNetEvent("esx:tpm") AddEventHandler("esx:tpm", function() +local PlayerPedId = PlayerPedId +local GetEntityCoords = GetEntityCoords +local GetGroundZFor_3dCoord = GetGroundZFor_3dCoord + ESX.TriggerServerCallback("esx:isUserAdmin", function(admin) if admin then - local WaypointHandle = GetFirstBlipInfoId(8) - if DoesBlipExist(WaypointHandle) then - local waypointCoords = GetBlipInfoIdCoord(WaypointHandle) - - for height = 1, 1000 do - SetPedCoordsKeepVehicle(ESX.PlayerData.ped, waypointCoords["x"], waypointCoords["y"], height + 0.0) - - local foundGround, zPos = GetGroundZFor_3dCoord(waypointCoords["x"], waypointCoords["y"], height + 0.0) - - if foundGround then - SetPedCoordsKeepVehicle(ESX.PlayerData.ped, waypointCoords["x"], waypointCoords["y"], height + 0.0) - - break - end - - Wait(0) - end - TriggerEvent('chatMessage', "Successfully Teleported") - else - TriggerEvent('chatMessage', "No Waypoint Set") - end + local blipMarker = GetFirstBlipInfoId(8) + if not DoesBlipExist(blipMarker) then + ESX.ShowNotification('No Waypoint Set.', true, false, 140) + return 'marker' + end + + -- Fade screen to hide how clients get teleported. + DoScreenFadeOut(650) + while not IsScreenFadedOut() do + Wait(0) + end + + local ped, coords = PlayerPedId(), GetBlipInfoIdCoord(blipMarker) + local vehicle = GetVehiclePedIsIn(ped, false) + local oldCoords = GetEntityCoords(ped) + + -- Unpack coords instead of having to unpack them while iterating. + -- 825.0 seems to be the max a player can reach while 0.0 being the lowest. + local x, y, groundZ, Z_START = coords['x'], coords['y'], 850.0, 950.0 + local found = false + if vehicle > 0 then + FreezeEntityPosition(vehicle, true) + else + FreezeEntityPosition(ped, true) + end + + for i = Z_START, 0, -25.0 do + local z = i + if (i % 2) ~= 0 then + z = Z_START - i + end + + NewLoadSceneStart(x, y, z, x, y, z, 50.0, 0) + local curTime = GetGameTimer() + while IsNetworkLoadingScene() do + if GetGameTimer() - curTime > 1000 then + break + end + Wait(0) + end + NewLoadSceneStop() + SetPedCoordsKeepVehicle(ped, x, y, z) + + while not HasCollisionLoadedAroundEntity(ped) do + RequestCollisionAtCoord(x, y, z) + if GetGameTimer() - curTime > 1000 then + break + end + Wait(0) + end + + -- Get ground coord. As mentioned in the natives, this only works if the client is in render distance. + found, groundZ = GetGroundZFor_3dCoord(x, y, z, false) + if found then + Wait(0) + SetPedCoordsKeepVehicle(ped, x, y, groundZ) + break + end + Wait(0) + end + + -- Remove black screen once the loop has ended. + DoScreenFadeIn(650) + if vehicle > 0 then + FreezeEntityPosition(vehicle, false) + else + FreezeEntityPosition(ped, false) + end + + if not found then + -- If we can't find the coords, set the coords to the old ones. + -- We don't unpack them before since they aren't in a loop and only called once. + SetPedCoordsKeepVehicle(ped, oldCoords['x'], oldCoords['y'], oldCoords['z'] - 1.0) + ESX.ShowNotification('Successfully Teleported', true, false, 140) + end + + -- If Z coord was found, set coords in found coords. + SetPedCoordsKeepVehicle(ped, x, y, groundZ) + ESX.ShowNotification('Successfully Teleported', true, false, 140) end end) end)