Skip to content

Commit

Permalink
refactor: use cache.ped, notify, convar locale switch, convert comman…
Browse files Browse the repository at this point in the history
…ds to ox_lib (#6)

* fixes: use cache.ped, notifyV2, convar locale switch, convert commands to ox_lib

* fix commands, spanish locales

* fix spaces, givekeys command

* send

* fix: send notifies correctly

* i'm retarded

* fix locale name for spanish

---------

Co-authored-by: mockdot <[email protected]>
  • Loading branch information
mockdot and mockdot authored Jul 28, 2023
1 parent 81e82f7 commit e0ef504
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 130 deletions.
56 changes: 27 additions & 29 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ local IsHotwiring = false
-----------------------
---- Threads ----
-----------------------

CreateThread(function()
while true do
local sleep = 1000
if LocalPlayer.state.isLoggedIn then
sleep = 100

local ped = PlayerPedId()
local ped = cache.ped
local entering = GetVehiclePedIsTryingToEnter(ped)
local carIsImmune = false
if entering ~= 0 and not isBlacklistedVehicle(entering) then
Expand Down Expand Up @@ -88,7 +87,7 @@ CreateThread(function()
local vehicle = GetVehiclePedIsIn(ped)
local plate = QBCore.Functions.GetPlate(vehicle)

if GetPedInVehicleSeat(vehicle, -1) == PlayerPedId() and not HasKeys(plate) and not isBlacklistedVehicle(vehicle) and not AreKeysJobShared(vehicle) then
if GetPedInVehicleSeat(vehicle, -1) == cache.ped and not HasKeys(plate) and not isBlacklistedVehicle(vehicle) and not AreKeysJobShared(vehicle) then
sleep = 0

local vehiclePos = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, 1.0, 0.5)
Expand Down Expand Up @@ -172,7 +171,7 @@ end)
RegisterNetEvent('qb-vehiclekeys:client:AddKeys', function(plate)
KeysList[plate] = true

local ped = PlayerPedId()
local ped = cache.ped
if IsPedInAnyVehicle(ped, false) then
local vehicle = GetVehiclePedIsIn(ped)
local vehicleplate = QBCore.Functions.GetPlate(vehicle)
Expand All @@ -188,8 +187,8 @@ RegisterNetEvent('qb-vehiclekeys:client:RemoveKeys', function(plate)
end)

RegisterNetEvent('qb-vehiclekeys:client:ToggleEngine', function()
local EngineOn = GetIsVehicleEngineRunning(GetVehiclePedIsIn(PlayerPedId()))
local vehicle = GetVehiclePedIsIn(PlayerPedId(), true)
local EngineOn = GetIsVehicleEngineRunning(GetVehiclePedIsIn(cache.ped))
local vehicle = GetVehiclePedIsIn(cache.ped, true)
if HasKeys(QBCore.Functions.GetPlate(vehicle)) then
if EngineOn then
SetVehicleEngineOn(vehicle, false, false, true)
Expand All @@ -208,7 +207,7 @@ RegisterNetEvent('qb-vehiclekeys:client:GiveKeys', function(id)
if id and type(id) == "number" then -- Give keys to specific ID
GiveKeys(id, targetPlate)
else
if IsPedSittingInVehicle(PlayerPedId(), targetVehicle) then -- Give keys to everyone in vehicle
if IsPedSittingInVehicle(cache.ped, targetVehicle) then -- Give keys to everyone in vehicle
local otherOccupants = GetOtherPlayersInVehicle(targetVehicle)
for p=1,#otherOccupants do
TriggerServerEvent('qb-vehiclekeys:server:GiveVehicleKeys', GetPlayerServerId(NetworkGetPlayerIndexFromPed(otherOccupants[p])), targetPlate)
Expand All @@ -218,7 +217,7 @@ RegisterNetEvent('qb-vehiclekeys:client:GiveKeys', function(id)
end
end
else
lib.notify({ description = Lang:t("notify.no_keys"), type = 'error' })
QBCore.Functions.Notify(Lang:t("notify.no_keys"), 'error')
end
end
end)
Expand All @@ -227,7 +226,6 @@ RegisterNetEvent('lockpicks:UseLockpick', function(isAdvanced)
LockpickDoor(isAdvanced)
end)


-- Backwards Compatibility ONLY -- Remove at some point --
RegisterNetEvent('vehiclekeys:client:SetOwner', function(plate)
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
Expand All @@ -239,11 +237,11 @@ end)
-----------------------

function GiveKeys(id, plate)
local distance = #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(GetPlayerPed(GetPlayerFromServerId(id))))
local distance = #(GetEntityCoords(cache.ped) - GetEntityCoords(GetPlayerPed(GetPlayerFromServerId(id))))
if distance < 1.5 and distance > 0.0 then
TriggerServerEvent('qb-vehiclekeys:server:GiveVehicleKeys', id, plate)
else
lib.notify({ description = Lang:t("notify.not_near"), type = 'error' })
QBCore.Functions.Notify(Lang:t("notify.not_near"), 'error')
end
end

Expand All @@ -266,19 +264,19 @@ function loadAnimDict(dict)
end

function GetVehicleInDirection(coordFromOffset, coordToOffset)
local ped = PlayerPedId()
local ped = cache.ped
local coordFrom = GetOffsetFromEntityInWorldCoords(ped, coordFromOffset.x, coordFromOffset.y, coordFromOffset.z)
local coordTo = GetOffsetFromEntityInWorldCoords(ped, coordToOffset.x, coordToOffset.y, coordToOffset.z)

local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, PlayerPedId(), 0)
local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, cache.ped, 0)
local _, _, _, _, vehicle = GetShapeTestResult(rayHandle)
return vehicle
end

-- If in vehicle returns that, otherwise tries 3 different raycasts to get the vehicle they are facing.
-- Raycasts picture: https://i.imgur.com/FRED0kV.png
function GetVehicle()
local vehicle = GetVehiclePedIsIn(PlayerPedId())
local vehicle = GetVehiclePedIsIn(cache.ped)

local RaycastOffsetTable = {
{ ['fromOffset'] = vector3(0.0, 0.0, 0.0), ['toOffset'] = vector3(0.0, 20.0, -10.0) }, -- Waist to ground 45 degree angle
Expand Down Expand Up @@ -321,7 +319,7 @@ function ToggleVehicleLocks(veh)
if veh then
if not isBlacklistedVehicle(veh) then
if HasKeys(QBCore.Functions.GetPlate(veh)) or AreKeysJobShared(veh) then
local ped = PlayerPedId()
local ped = cache.ped
local vehLockStatus = GetVehicleDoorLockStatus(veh)

loadAnimDict("anim@mp_player_intmenu@key_fob@")
Expand All @@ -332,10 +330,10 @@ function ToggleVehicleLocks(veh)
NetworkRequestControlOfEntity(veh)
if vehLockStatus == 1 then
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(veh), 2)
lib.notify({ description = Lang:t("notify.vehicle_locked"), type = 'inform' })
QBCore.Functions.Notify(Lang:t("notify.vehicle_locked"), 'inform')
else
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(veh), 1)
lib.notify({ description = Lang:t("notify.vehicle_unlocked"), type = 'inform' })
QBCore.Functions.Notify(Lang:t("notify.vehicle_unlocked"), 'inform')
end

SetVehicleLights(veh, 2)
Expand All @@ -346,7 +344,7 @@ function ToggleVehicleLocks(veh)
Wait(300)
ClearPedTasks(ped)
else
lib.notify({ description = Lang:t("notify.no_keys"), type = 'error' })
QBCore.Functions.Notify(Lang:t("notify.no_keys"), 'error')
end
else
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(veh), 1)
Expand All @@ -358,7 +356,7 @@ function GetOtherPlayersInVehicle(vehicle)
local otherPeds = {}
for seat=-1,GetVehicleModelNumberOfSeats(GetEntityModel(vehicle))-2 do
local pedInSeat = GetPedInVehicleSeat(vehicle, seat)
if IsPedAPlayer(pedInSeat) and pedInSeat ~= PlayerPedId() then
if IsPedAPlayer(pedInSeat) and pedInSeat ~= cache.ped then
otherPeds[#otherPeds+1] = pedInSeat
end
end
Expand All @@ -377,7 +375,7 @@ function GetPedsInVehicle(vehicle)
end

function IsBlacklistedWeapon()
local weapon = GetSelectedPedWeapon(PlayerPedId())
local weapon = GetSelectedPedWeapon(cache.ped)
if weapon ~= nil then
for _, v in pairs(Config.NoCarjackWeapons) do
if weapon == GetHashKey(v) then
Expand All @@ -389,7 +387,7 @@ function IsBlacklistedWeapon()
end

function LockpickDoor(isAdvanced)
local ped = PlayerPedId()
local ped = cache.ped
local pos = GetEntityCoords(ped)
local vehicle = QBCore.Functions.GetClosestVehicle()

Expand All @@ -410,10 +408,10 @@ function LockpickFinishCallback(success)
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
lastPickedVehicle = vehicle

if GetPedInVehicleSeat(vehicle, -1) == PlayerPedId() then
if GetPedInVehicleSeat(vehicle, -1) == cache.ped then
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', QBCore.Functions.GetPlate(vehicle))
else
lib.notify({ description = Lang:t("notify.vehicle_lockedpick"), type = 'success' })
QBCore.Functions.Notify(Lang:t("notify.vehicle_lockedpick"), 'success')
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), 1)
end

Expand All @@ -435,7 +433,7 @@ end

function Hotwire(vehicle, plate)
local hotwireTime = math.random(Config.minHotwireTime, Config.maxHotwireTime)
local ped = PlayerPedId()
local ped = cache.ped
IsHotwiring = true

SetVehicleAlarm(vehicle, true)
Expand All @@ -456,7 +454,7 @@ function Hotwire(vehicle, plate)
if (math.random() <= Config.HotwireChance) then
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
else
lib.notify({ description = Lang:t("notify.failed_lockedpick"), type = 'error' })
QBCore.Functions.Notify(Lang:t("notify.failed_lockedpick"), 'error')
end
Wait(Config.TimeBetweenHotwires)
IsHotwiring = false
Expand Down Expand Up @@ -488,7 +486,7 @@ function CarjackVehicle(target)
-- Cancel progress bar if: Ped dies during robbery, car gets too far away
CreateThread(function()
while isCarjacking do
local distance = #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(target))
local distance = #(GetEntityCoords(cache.ped) - GetEntityCoords(target))
if IsPedDeadOrDying(target) or distance > 7.5 then
lib.cancelProgress()
end
Expand All @@ -506,7 +504,7 @@ function CarjackVehicle(target)
car = true,
},
}) then
local hasWeapon, weaponHash = GetCurrentPedWeapon(PlayerPedId(), true)
local hasWeapon, weaponHash = GetCurrentPedWeapon(cache.ped, true)
if hasWeapon and isCarjacking then
local carjackChance
if Config.CarjackChance[tostring(GetWeapontypeGroup(weaponHash))] then
Expand All @@ -530,7 +528,7 @@ function CarjackVehicle(target)
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', plate)
else
lib.notify({ description = Lang:t("notify.carjack_failed"), type = 'error' })
QBCore.Functions.Notify(Lang:t("notify.carjack_failed"), 'error')
MakePedFlee(target)
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
end
Expand Down Expand Up @@ -566,7 +564,7 @@ end

function MakePedFlee(ped)
SetPedFleeAttributes(ped, 0, 0)
TaskReactAndFleePed(ped, PlayerPedId())
TaskReactAndFleePed(ped, cache.ped)
end

function DrawText3D(x, y, z, text)
Expand Down
8 changes: 4 additions & 4 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ Config.SharedKeys = { -- Share keys amongst employees. Employees can lock/unlock
['police'] = { -- Job name
requireOnduty = false,
vehicles = {
'police', -- Vehicle model
'police2', -- Vehicle model
}
'police', -- Vehicle model
'police2', -- Vehicle model
}
},

['mechanic'] = {
requireOnduty = false,
vehicles = {
'towtruck',
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version '1.0.1'
shared_scripts {
'@qb-core/shared/locale.lua',
'locales/en.lua',
'locales/*.lua',
'@ox_lib/init.lua',
'config.lua',
}
Expand Down
11 changes: 7 additions & 4 deletions locales/de.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ local Translations = {

}

Lang = Locale:new({
phrases = Translations,
warnOnMissing = true
})
if GetConvar('qb_locale', 'en') == 'de' then
Lang = Locale:new({
phrases = Translations,
warnOnMissing = true,
fallbackLang = Lang,
})
end
2 changes: 1 addition & 1 deletion locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local Translations = {

}

Lang = Locale:new({
Lang = Lang or Locale:new({
phrases = Translations,
warnOnMissing = true
})
21 changes: 12 additions & 9 deletions locales/es.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ local Translations = {
notify = {
no_keys = 'No tienes las llaves de este vehículo',
not_near = 'No hay nadie cerca a quién darle las llaves',
vehicle_locked = '¡Vehículo cerrado!',
vehicle_unlocked = '¡Vehículo abierto!',
vehicle_lockedpick = '¡Lograste abrir la cerradura!',
vehicle_locked = 'Vehículo cerrado',
vehicle_unlocked = 'Vehículo abierto',
vehicle_lockedpick = 'Lograste abrir la cerradura',
failed_lockedpick = 'No logras encontrar las llaves y te frustras',
gave_keys = 'Has entregado las llaves',
keys_taken = '¡Has recibido las llaves al vehículo!',
keys_taken = 'Has recibido las llaves del vehículo',
fpid = 'Llena los argumentos de ID y placa del jugador',
carjack_failed = '¡Robo de carro falló!',
},
Expand All @@ -17,7 +17,7 @@ local Translations = {
attempting_carjack = 'Intentando robar carro...',
},
info = {
skeys = '[H] - Buscar llaves',
search_keys = '[H] - Buscar llaves',
toggle_locks = 'Habilitar/deshabilitar seguro de carro',
vehicle_theft = 'Robo de vehículo en progreso. Tipo: ',
engine = 'Encender/apagar motor',
Expand All @@ -40,7 +40,10 @@ local Translations = {

}

Lang = Locale:new({
phrases = Translations,
warnOnMissing = true
})
if GetConvar('qb_locale', 'en') == 'es' then
Lang = Locale:new({
phrases = Translations,
warnOnMissing = true,
fallbackLang = Lang,
})
end
11 changes: 7 additions & 4 deletions locales/et.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ local Translations = {

}

Lang = Locale:new({
phrases = Translations,
warnOnMissing = true
})
if GetConvar('qb_locale', 'en') == 'et' then
Lang = Locale:new({
phrases = Translations,
warnOnMissing = true,
fallbackLang = Lang,
})
end
11 changes: 7 additions & 4 deletions locales/fr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ local Translations = {

}

Lang = Locale:new({
phrases = Translations,
warnOnMissing = true
})
if GetConvar('qb_locale', 'en') == 'fr' then
Lang = Locale:new({
phrases = Translations,
warnOnMissing = true,
fallbackLang = Lang,
})
end
11 changes: 7 additions & 4 deletions locales/ro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ local Translations = {

}

Lang = Locale:new({
phrases = Translations,
warnOnMissing = true
})
if GetConvar('qb_locale', 'en') == 'ro' then
Lang = Locale:new({
phrases = Translations,
warnOnMissing = true,
fallbackLang = Lang,
})
end
Loading

0 comments on commit e0ef504

Please sign in to comment.