From e0ef5045d54e36d0bdfc64ec3f1af005d02db5db Mon Sep 17 00:00:00 2001 From: buddizer <10159765+buddizer@users.noreply.github.com> Date: Fri, 28 Jul 2023 01:40:50 -0600 Subject: [PATCH] refactor: use cache.ped, notify, convar locale switch, convert commands 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 --- client/main.lua | 56 ++++++++++---------- config.lua | 8 +-- fxmanifest.lua | 1 + locales/de.lua | 11 ++-- locales/en.lua | 2 +- locales/es.lua | 21 ++++---- locales/et.lua | 11 ++-- locales/fr.lua | 11 ++-- locales/ro.lua | 11 ++-- server/main.lua | 136 +++++++++++++++++++++++------------------------- 10 files changed, 138 insertions(+), 130 deletions(-) diff --git a/client/main.lua b/client/main.lua index 66c036d..a908801 100644 --- a/client/main.lua +++ b/client/main.lua @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -266,11 +264,11 @@ 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 @@ -278,7 +276,7 @@ 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 @@ -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@") @@ -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) @@ -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) @@ -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 @@ -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 @@ -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() @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/config.lua b/config.lua index f60f398..16f0ce7 100644 --- a/config.lua +++ b/config.lua @@ -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', - } + } } } diff --git a/fxmanifest.lua b/fxmanifest.lua index 45afa61..60a4ce5 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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', } diff --git a/locales/de.lua b/locales/de.lua index f14bbb0..bb7b56d 100644 --- a/locales/de.lua +++ b/locales/de.lua @@ -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 diff --git a/locales/en.lua b/locales/en.lua index 5bbbc27..5a1b082 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -40,7 +40,7 @@ local Translations = { } -Lang = Locale:new({ +Lang = Lang or Locale:new({ phrases = Translations, warnOnMissing = true }) diff --git a/locales/es.lua b/locales/es.lua index 191014b..7a6ff47 100644 --- a/locales/es.lua +++ b/locales/es.lua @@ -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ó!', }, @@ -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', @@ -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 diff --git a/locales/et.lua b/locales/et.lua index 1f8489d..ceb2720 100644 --- a/locales/et.lua +++ b/locales/et.lua @@ -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 diff --git a/locales/fr.lua b/locales/fr.lua index 7fd4bb8..f669912 100644 --- a/locales/fr.lua +++ b/locales/fr.lua @@ -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 diff --git a/locales/ro.lua b/locales/ro.lua index 8f57b93..904909a 100644 --- a/locales/ro.lua +++ b/locales/ro.lua @@ -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 diff --git a/server/main.lua b/server/main.lua index 490c1e4..1dcda30 100644 --- a/server/main.lua +++ b/server/main.lua @@ -18,17 +18,7 @@ RegisterNetEvent('qb-vehiclekeys:server:GiveVehicleKeys', function(receiver, pla local giver = source if HasKeys(giver, plate) then - TriggerClientEvent('ox_lib:notify', giver, { - id = 'give_car_keys', - description = Lang:t("notify.gave_keys"), - duration = 5000, - style = { - backgroundColor = '#141517', - color = '#ffffff' - }, - icon = 'check', - iconColor = '#27AE60' - }) + TriggerClientEvent('QBCore:Notify', giver, Lang:t("notify.gave_keys")) if type(receiver) == 'table' then for _,r in ipairs(receiver) do GiveKeys(receiver[r], plate) @@ -37,17 +27,7 @@ RegisterNetEvent('qb-vehiclekeys:server:GiveVehicleKeys', function(receiver, pla GiveKeys(receiver, plate) end else - TriggerClientEvent('ox_lib:notify', giver, { - id = 'server_no_keys', - description = Lang:t("notify.no_keys"), - duration = 5000, - style = { - backgroundColor = '#141517', - color = '#ffffff' - }, - icon = 'xmark', - iconColor = '#C0392B' - }) + TriggerClientEvent('QBCore:Notify', giver, Lang:t("notify.no_keys")) end end) @@ -83,24 +63,13 @@ end) ----------------------- ---- Functions ---- ----------------------- - function GiveKeys(id, plate) local citizenid = QBCore.Functions.GetPlayer(id).PlayerData.citizenid if not VehicleList[plate] then VehicleList[plate] = {} end VehicleList[plate][citizenid] = true - - TriggerClientEvent('ox_lib:notify', id, { - id = 'server_id_get_keys', - description = Lang:t("notify.keys_taken"), - duration = 5000, - style = { - backgroundColor = '#141517', - color = '#ffffff' - }, - icon = 'circle-info', - iconColor = '#2980B9' - }) + + TriggerClientEvent('QBCore:Notify', id, Lang:t('notify.keys_taken')) TriggerClientEvent('qb-vehiclekeys:client:AddKeys', id, plate) end @@ -122,45 +91,70 @@ function HasKeys(id, plate) return false end -QBCore.Commands.Add("givekeys", Lang:t("addcom.givekeys"), {{name = Lang:t("addcom.givekeys_id"), help = Lang:t("addcom.givekeys_id_help")}}, false, function(source, args) - local src = source - TriggerClientEvent('qb-vehiclekeys:client:GiveKeys', src, tonumber(args[1])) +lib.addCommand('givekeys', { + help = Lang:t("addcom.givekeys"), + params = { + { + name = Lang:t("addcom.givekeys_id"), + type = 'number', + help = Lang:t("addcom.givekeys_id_help"), + optional = true + }, + }, + restricted = false, +}, function (source, args) + local src = source + TriggerClientEvent('qb-vehiclekeys:client:GiveKeys', src, args.id) end) -QBCore.Commands.Add("addkeys", Lang:t("addcom.addkeys"), {{name = Lang:t("addcom.addkeys_id"), help = Lang:t("addcom.addkeys_id_help")}, {name = Lang:t("addcom.addkeys_plate"), help = Lang:t("addcom.addkeys_plate_help")}}, true, function(source, args) - local src = source - if not args[1] or not args[2] then - TriggerClientEvent('ox_lib:notify', src, { - id = 'server_notify_fpid', - description = Lang:t("notify.fpid"), - duration = 5000, - style = { - backgroundColor = '#141517', - color = '#ffffff' - }, - icon = 'xmark', - iconColor = '#C0392B' - }) +lib.addCommand('addkeys', { + help = Lang:t("addcom.addkeys"), + params = { + { + name = 'id', + type = 'number', + help = Lang:t("addcom.addkeys_id_help"), + optional = true + }, + { + name = 'plate', + type = 'string', + help = Lang:t("addcom.addkeys_plate_help"), + optional = true + }, + }, + restricted = 'group.admin', +}, function (source, args) + local src = source + if not args.id or not args.plate then + TriggerClientEvent('QBCore:Notify', src, Lang:t("notify.fpid")) return end - GiveKeys(tonumber(args[1]), args[2]) -end, 'admin') - -QBCore.Commands.Add("removekeys", Lang:t("addcom.remove_keys"), {{name = Lang:t("addcom.remove_keys_id"), help = Lang:t("addcom.remove_keys_id_help")}, {name = Lang:t("addcom.remove_keys_plate"), help = Lang:t("addcom.remove_keys_plate_help")}}, true, function(source, args) - local src = source - if not args[1] or not args[2] then - TriggerClientEvent('ox_lib:notify', src, { - id = 'server_notify_fpid', - description = Lang:t("notify.fpid"), - duration = 5000, - style = { - backgroundColor = '#141517', - color = '#ffffff' - }, - icon = 'xmark', - iconColor = '#C0392B' - }) + GiveKeys(args.id, args.plate) +end) + +lib.addCommand('removekeys', { + help = Lang:t("addcom.remove_keys"), + params = { + { + name = 'id', + type = 'number', + help = Lang:t("addcom.remove_keys_id_help"), + optional = true + }, + { + name = 'plate', + type = 'string', + help = Lang:t("addcom.remove_keys_plate_help"), + optional = true + } + }, + restricted = 'group.admin', +}, function (source, args) + local src = source + if not args.id or not args.plate then + TriggerClientEvent('QBCore:Notify', src, Lang:t("notify.fpid")) return end - RemoveKeys(tonumber(args[1]), args[2]) -end, 'admin') + RemoveKeys(args.id, args.plate) +end)