forked from Simnation/Main
211 lines
6.1 KiB
Lua
211 lines
6.1 KiB
Lua
![]() |
local enabled = true
|
||
|
|
||
|
Citizen.CreateThread(function()
|
||
|
while (true) do
|
||
|
local playerPed = PlayerPedId()
|
||
|
if (DoesEntityExist(playerPed)) then
|
||
|
TriggerServerEvent("mh_Parking:syncPlayerPosition", GetEntityCoords(playerPed))
|
||
|
end
|
||
|
|
||
|
Citizen.Wait(3000)
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
Citizen.CreateThread(function()
|
||
|
while (true) do
|
||
|
Citizen.Wait(5 * 60 * 1000)
|
||
|
|
||
|
local vehicle = GetVehiclePedIsIn(PlayerPedId())
|
||
|
if (DoesEntityExist(vehicle)) then
|
||
|
TriggerEvent("mh_Parking:updateVehicle", vehicle)
|
||
|
end
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
Citizen.CreateThread(function()
|
||
|
Citizen.Wait(3000)
|
||
|
|
||
|
local isInVehicle = false
|
||
|
local currentVehiclePlate = nil
|
||
|
local wasDead = false
|
||
|
|
||
|
while (true) do
|
||
|
Citizen.Wait(50)
|
||
|
|
||
|
local playerPed = PlayerPedId()
|
||
|
|
||
|
if (not isInVehicle and IsPedInAnyVehicle(playerPed)) then
|
||
|
-- entered vehicle
|
||
|
|
||
|
isInVehicle = true
|
||
|
|
||
|
if (enabled) then
|
||
|
local vehicle = GetVehiclePedIsIn(playerPed, false)
|
||
|
if (NetworkGetEntityIsNetworked(vehicle) and not IsVehicleBlacklisted(vehicle)) then
|
||
|
local networkId = NetworkGetNetworkIdFromEntity(vehicle)
|
||
|
SetNetworkIdCanMigrate(netId, true)
|
||
|
local modifications = GetVehicleModifications(vehicle)
|
||
|
|
||
|
currentVehiclePlate = GetVehicleNumberPlateText(vehicle)
|
||
|
|
||
|
TriggerServerEvent("mh_Parking:enteredVehicle", networkId, currentVehiclePlate, modifications)
|
||
|
|
||
|
Log("Vehicle " .. currentVehiclePlate .. " entered")
|
||
|
end
|
||
|
end
|
||
|
elseif (isInVehicle and not IsPedInAnyVehicle(playerPed, false)) then
|
||
|
-- left vehicle
|
||
|
|
||
|
isInVehicle = false
|
||
|
|
||
|
if (enabled and not wasDead) then
|
||
|
local vehicle = GetVehiclePedIsIn(playerPed, true)
|
||
|
|
||
|
if (vehicle ~= 0) then
|
||
|
if (NetworkGetEntityIsNetworked(vehicle) and not IsVehicleBlacklisted(vehicle)) then
|
||
|
local networkId = NetworkGetNetworkIdFromEntity(vehicle)
|
||
|
SetNetworkIdCanMigrate(netId, true)
|
||
|
local modifications = GetVehicleModifications(vehicle)
|
||
|
|
||
|
TriggerServerEvent("mh_Parking:leftVehicle", networkId, modifications)
|
||
|
|
||
|
Log("Vehicle " .. GetVehicleNumberPlateText(vehicle) .. " left")
|
||
|
|
||
|
-- if vehicle is deleted soon after
|
||
|
Citizen.CreateThread(function()
|
||
|
local veh = vehicle
|
||
|
local plate = GetVehicleNumberPlateText(veh)
|
||
|
local startTime = GetGameTimer()
|
||
|
local playerPed = PlayerPedId()
|
||
|
local playerPos
|
||
|
|
||
|
while (true) do
|
||
|
Citizen.Wait(0)
|
||
|
|
||
|
if (GetGameTimer() - startTime >= 1000) then
|
||
|
break
|
||
|
end
|
||
|
|
||
|
if (not DoesEntityExist(veh)) then
|
||
|
TriggerServerEvent("mh_Parking:deleteVehicle", plate, false)
|
||
|
|
||
|
Log("Vehicle " .. currentVehiclePlate .. " left and deleted after exit")
|
||
|
|
||
|
currentVehiclePlate = nil
|
||
|
break
|
||
|
end
|
||
|
|
||
|
playerPed = PlayerPedId()
|
||
|
playerPos = GetEntityCoords(playerPed)
|
||
|
vehPos = GetEntityCoords(veh)
|
||
|
|
||
|
if (Vdist(playerPos.x, playerPos.y, playerPos.z, vehPos.x, vehPos.y, vehPos.z) > 50.0) then
|
||
|
currentVehiclePlate = nil
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
|
||
|
currentVehiclePlate = nil
|
||
|
end)
|
||
|
end
|
||
|
elseif (currentVehiclePlate) then
|
||
|
TriggerServerEvent("mh_Parking:deleteVehicle", currentVehiclePlate, false)
|
||
|
|
||
|
Log("Vehicle " .. currentVehiclePlate .. " left and deleted")
|
||
|
|
||
|
currentVehiclePlate = nil
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
if (not wasDead and IsPlayerDead(PlayerId())) then
|
||
|
wasDead = true
|
||
|
elseif (wasDead and not IsPlayerDead(PlayerId())) then
|
||
|
Citizen.CreateThread(function()
|
||
|
Citizen.Wait(1000)
|
||
|
|
||
|
wasDead = false
|
||
|
end)
|
||
|
end
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
RegisterNetEvent("mh_Parking:setVehicleMods")
|
||
|
AddEventHandler("mh_Parking:setVehicleMods", function(netId, plate, modifications)
|
||
|
if (not IsModelInCdimage(modifications[1])) then
|
||
|
Log("Setting mods for " .. plate .. " failed. The model does NOT exist.")
|
||
|
TriggerServerEvent("mh_Parking:modelDoesNotExist", plate)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
local timer = GetGameTimer()
|
||
|
while (not NetworkDoesEntityExistWithNetworkId(netId)) do
|
||
|
Citizen.Wait(0)
|
||
|
|
||
|
if (GetGameTimer() - 10000 > timer) then
|
||
|
Log("Setting mods for " .. plate .. " failed after 10s")
|
||
|
TriggerServerEvent("mh_Parking:setVehicleModsFailed", plate)
|
||
|
return
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local vehicle = NetworkGetEntityFromNetworkId(netId)
|
||
|
|
||
|
if (DoesEntityExist(vehicle) and NetworkHasControlOfEntity(vehicle)) then
|
||
|
Log("Setting modifications for vehicle " .. plate)
|
||
|
|
||
|
SetVehicleModifications(vehicle, plate, modifications)
|
||
|
TriggerServerEvent("mh_Parking:setVehicleModsSuccess", plate)
|
||
|
else
|
||
|
Log("Setting mods failed for vehicle " .. plate .. ". Vehicle does not exist")
|
||
|
TriggerServerEvent("mh_Parking:setVehicleModsFailed", plate)
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
AddEventHandler("mh_Parking:updateVehicle", function(vehicle)
|
||
|
if (vehicle == nil) then
|
||
|
Log("^1[ERROR] \"vehicle\" was nil while trying to update a vehicle!")
|
||
|
return
|
||
|
end
|
||
|
|
||
|
if (DoesEntityExist(vehicle) and NetworkGetEntityIsNetworked(vehicle) and not IsVehicleBlacklisted(vehicle)) then
|
||
|
Log("Triggering manual update of vehicle")
|
||
|
|
||
|
local networkId = NetworkGetNetworkIdFromEntity(vehicle)
|
||
|
local modifications = GetVehicleModifications(vehicle)
|
||
|
|
||
|
TriggerServerEvent("mh_Parking:updateVehicle", networkId, modifications)
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
RegisterNetEvent("mh_Parking:enable")
|
||
|
AddEventHandler("mh_Parking:enable", function(enable)
|
||
|
if (enable == nil) then
|
||
|
Log("^1[ERROR] \"enable\" was nil while trying to enable/disable script!")
|
||
|
return
|
||
|
end
|
||
|
|
||
|
enabled = enable
|
||
|
|
||
|
if (enabled) then
|
||
|
Log("AdvancedParking enabled")
|
||
|
else
|
||
|
Log("AdvancedParking disabled")
|
||
|
end
|
||
|
end)
|
||
|
|
||
|
RegisterNetEvent("mh_Parking:notification")
|
||
|
AddEventHandler("mh_Parking:notification", function(msg)
|
||
|
SetNotificationTextEntry('STRING')
|
||
|
AddTextComponentSubstringPlayerName(msg)
|
||
|
DrawNotification(false, true)
|
||
|
end)
|
||
|
|
||
|
RegisterNetEvent("mh_Parking:renderScorched")
|
||
|
AddEventHandler("mh_Parking:renderScorched", function(vehicleNetId, scorched)
|
||
|
local vehicle = NetworkGetEntityFromNetworkId(vehicleNetId)
|
||
|
if (DoesEntityExist(vehicle)) then
|
||
|
SetEntityRenderScorched(vehicle, scorched)
|
||
|
end
|
||
|
end)
|