From 26538b8148a7f8c8df0e8bb82076143137e83f1e Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 14 Sep 2022 01:20:50 +1000 Subject: [PATCH] refactor(imports): assign modules directly to lib namespace Mostly a change for improved intellisense with sumneko lua. Includes some type fixes and deprecation notices. --- .editorconfig | 5 ++- imports/addCommand/server.lua | 8 ++--- imports/callback/client.lua | 2 +- imports/callback/server.lua | 2 +- imports/disableControls/client.lua | 6 ++-- imports/getClosestPlayer/client.lua | 6 ++-- imports/getClosestVehicle/client.lua | 6 ++-- imports/getCore/shared.lua | 11 +++++- imports/getNearbyPlayers/client.lua | 48 ++++++++++++++------------ imports/getNearbyVehicles/client.lua | 6 ++-- imports/locale/shared.lua | 13 +++---- imports/logger/server.lua | 4 +-- imports/player/client.lua | 6 ++++ imports/player/server.lua | 11 +++--- imports/points/client.lua | 5 ++- imports/raycast/client.lua | 5 ++- imports/table/shared.lua | 2 ++ imports/zones/client.lua | 11 ++++-- resource/interface/client/progress.lua | 2 +- 19 files changed, 98 insertions(+), 61 deletions(-) diff --git a/.editorconfig b/.editorconfig index 71cadc6..a127b12 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,11 +7,10 @@ root = true end_of_line = lf insert_final_newline = false charset = utf-8 -trim_trailing_whitespace = true +trim_trailing_whitespace = false indent_size = 2 indent_style = space max_line_length = 120 [*.lua] -indent_size = 4 -indent_style = tab \ No newline at end of file +indent_size = 4 \ No newline at end of file diff --git a/imports/addCommand/server.lua b/imports/addCommand/server.lua index db4800e..dec999a 100644 --- a/imports/addCommand/server.lua +++ b/imports/addCommand/server.lua @@ -37,18 +37,18 @@ end ---@param name string ---@param callback function ---@param parameters table -local function addCommand(group, name, callback, parameters, help) +function lib.addCommand(group, name, callback, parameters, help) if not group then group = 'builtin.everyone' end if type(name) == 'table' then for i = 1, #name do - addCommand(group, name[i], callback, parameters, help) + lib.addCommand(group, name[i], callback, parameters, help) end else chatSuggestion(name, parameters, help) RegisterCommand(name, function(source, args) - source = tonumber(source) + source = tonumber(source) --[[@as number]] if parameters then for i = 1, #parameters do @@ -100,7 +100,7 @@ local function addCommand(group, name, callback, parameters, help) end end -return addCommand +return lib.addCommand --[[ Example AddCommand('group.admin', {'additem', 'giveitem'}, function(source, args) diff --git a/imports/callback/client.lua b/imports/callback/client.lua index 3cbe40f..b9d55fd 100644 --- a/imports/callback/client.lua +++ b/imports/callback/client.lua @@ -67,7 +67,7 @@ lib.callback = setmetatable({}, { ---@param delay number prevent the event from being called for the given time --- Sends an event to the server and halts the current thread until a response is returned. function lib.callback.await(event, delay, ...) - return triggerServerCallback(_, event, delay, false, ...) + return triggerServerCallback(nil, event, delay, false, ...) end local function callbackResponse(success, result, ...) diff --git a/imports/callback/server.lua b/imports/callback/server.lua index f31374f..dfb345c 100644 --- a/imports/callback/server.lua +++ b/imports/callback/server.lua @@ -48,7 +48,7 @@ lib.callback = setmetatable({}, { ---@param playerId number --- Sends an event to a client and halts the current thread until a response is returned. function lib.callback.await(event, playerId, ...) - return triggerClientCallback(_, event, playerId, false, ...) + return triggerClientCallback(nil, event, playerId, false, ...) end local function callbackResponse(success, result, ...) diff --git a/imports/disableControls/client.lua b/imports/disableControls/client.lua index dc391b0..068bfe7 100644 --- a/imports/disableControls/client.lua +++ b/imports/disableControls/client.lua @@ -43,7 +43,7 @@ local keys = {} local DisableControlAction = DisableControlAction local pairs = pairs -return setmetatable(disableControls, { +lib.disableControls = setmetatable(disableControls, { __index = keys, __newindex = keys, __call = function() @@ -51,4 +51,6 @@ return setmetatable(disableControls, { DisableControlAction(0, k, true) end end -}) \ No newline at end of file +}) + +return lib.disableControls \ No newline at end of file diff --git a/imports/getClosestPlayer/client.lua b/imports/getClosestPlayer/client.lua index e5ce99b..064df74 100644 --- a/imports/getClosestPlayer/client.lua +++ b/imports/getClosestPlayer/client.lua @@ -4,7 +4,7 @@ ---@return number? playerId ---@return number? playerPed ---@return vector3? playerCoords -return function(coords, maxDistance, includePlayer) +function lib.getClosestPlayer(coords, maxDistance, includePlayer) local players = GetActivePlayers() local closestId, closestPed, closestCoords maxDistance = maxDistance or 2.0 @@ -27,4 +27,6 @@ return function(coords, maxDistance, includePlayer) end return closestId, closestPed, closestCoords -end \ No newline at end of file +end + +return lib.getClosestPlayer diff --git a/imports/getClosestVehicle/client.lua b/imports/getClosestVehicle/client.lua index 4083b87..ec87fac 100644 --- a/imports/getClosestVehicle/client.lua +++ b/imports/getClosestVehicle/client.lua @@ -3,7 +3,7 @@ ---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. ---@return number? vehicle ---@return vector3? vehicleCoords -return function(coords, maxDistance, includePlayerVehicle) +function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle) local vehicles = GetGamePool('CVehicle') local closestVehicle, closestCoords maxDistance = maxDistance or 2.0 @@ -24,4 +24,6 @@ return function(coords, maxDistance, includePlayerVehicle) end return closestVehicle, closestCoords -end \ No newline at end of file +end + +return lib.getClosestVehicle diff --git a/imports/getCore/shared.lua b/imports/getCore/shared.lua index b7dfc5e..633381d 100644 --- a/imports/getCore/shared.lua +++ b/imports/getCore/shared.lua @@ -1,10 +1,16 @@ +--[[ + This module was experimental and won't be worked on or used further. + May be removed in the future. +]] + local Core = { Ox = 'ox_core', QB = 'qb-core', ESX = 'es_extended', } -return function() +---@deprecated +function lib.getCore() local result Citizen.CreateThreadNow(function() @@ -53,6 +59,7 @@ return function() if not success then error(result) end if framework == Core.Ox then + ---@diagnostic disable-next-line: undefined-global result = Ox end @@ -67,3 +74,5 @@ return function() return result end + +return lib.getCore diff --git a/imports/getNearbyPlayers/client.lua b/imports/getNearbyPlayers/client.lua index da831a1..36dea67 100644 --- a/imports/getNearbyPlayers/client.lua +++ b/imports/getNearbyPlayers/client.lua @@ -2,30 +2,32 @@ ---@param maxDistance number The max distance to check. ---@param includePlayer boolean Whether or not to include the current player. ---@return number[] -return function(coords, maxDistance, includePlayer) - local players = GetActivePlayers() - local nearby = {} - local count = 0 - maxDistance = maxDistance or 2.0 +function lib.getNearbyPlayers(coords, maxDistance, includePlayer) + local players = GetActivePlayers() + local nearby = {} + local count = 0 + maxDistance = maxDistance or 2.0 - for i = 1, #players do - local playerId = players[i] + for i = 1, #players do + local playerId = players[i] - if playerId ~= cache.playerId or includePlayer then - local playerPed = GetPlayerPed(playerId) - local playerCoords = GetEntityCoords(playerPed) - local distance = #(coords - playerCoords) + if playerId ~= cache.playerId or includePlayer then + local playerPed = GetPlayerPed(playerId) + local playerCoords = GetEntityCoords(playerPed) + local distance = #(coords - playerCoords) - if distance < maxDistance then - count += 1 - nearby[count] = { - id = playerId, - ped = playerPed, - coords = playerCoords, - } - end - end - end + if distance < maxDistance then + count += 1 + nearby[count] = { + id = playerId, + ped = playerPed, + coords = playerCoords, + } + end + end + end - return nearby -end \ No newline at end of file + return nearby +end + +return lib.getNearbyPlayers diff --git a/imports/getNearbyVehicles/client.lua b/imports/getNearbyVehicles/client.lua index e1836f1..29c54ef 100644 --- a/imports/getNearbyVehicles/client.lua +++ b/imports/getNearbyVehicles/client.lua @@ -2,7 +2,7 @@ ---@param maxDistance number The max distance to check. ---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. ---@return number[] -return function(coords, maxDistance, includePlayerVehicle) +function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle) local vehicles = GetGamePool('CVehicle') local nearby = {} local count = 0 @@ -26,4 +26,6 @@ return function(coords, maxDistance, includePlayerVehicle) end return nearby -end \ No newline at end of file +end + +return lib.getNearbyVehicles \ No newline at end of file diff --git a/imports/locale/shared.lua b/imports/locale/shared.lua index 3e86b30..a3b2d7c 100644 --- a/imports/locale/shared.lua +++ b/imports/locale/shared.lua @@ -14,15 +14,16 @@ function locale(str, ...) return ("Translation for '%s' does not exist"):format(str) end -local function loadLocale(locale) +function lib.loadLocale(locale) + if not locale then + locale = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en' + end + local resourceName = GetCurrentResourceName() local JSON = LoadResourceFile(resourceName, ('locales/%s.json'):format(locale)) or LoadResourceFile(resourceName, ('locales/en.json'):format(locale)) dict = JSON and json.decode(JSON) or {} end -AddEventHandler('ox_lib:setLocale', loadLocale) +AddEventHandler('ox_lib:setLocale', lib.loadLocale) -return function() - local lang = lib.service == 'server' and lib.getServerLocale() or GetExternalKvpString('ox_lib', 'locale') or 'en' - loadLocale(lang) -end +return lib.loadLocale \ No newline at end of file diff --git a/imports/logger/server.lua b/imports/logger/server.lua index c63e6fa..d1e13d5 100644 --- a/imports/logger/server.lua +++ b/imports/logger/server.lua @@ -5,7 +5,7 @@ if key ~= '' then local resourceName = GetCurrentResourceName() key = key:gsub("[\'\"]", '') - return function(source, event, message, ...) + function lib.logger(source, event, message, ...) local data = json.encode({ hostname = resourceName, service = event, @@ -27,4 +27,4 @@ if key ~= '' then end end -return function() end +return lib.logger or function() end diff --git a/imports/player/client.lua b/imports/player/client.lua index 8b4f36a..5e710ca 100644 --- a/imports/player/client.lua +++ b/imports/player/client.lua @@ -1,3 +1,8 @@ +--[[ + This module was experimental and won't be worked on or used further. + May be removed in the future. +]] + local CPlayer = {} function CPlayer:__index(index, ...) @@ -22,6 +27,7 @@ function CPlayer:getDistance(coords) return #(self:getCoords() - coords) end +---@deprecated function lib.getPlayer() return CPlayer end diff --git a/imports/player/server.lua b/imports/player/server.lua index 045b442..365a575 100644 --- a/imports/player/server.lua +++ b/imports/player/server.lua @@ -1,3 +1,8 @@ +--[[ + This module was experimental and won't be worked on or used further. + May be removed in the future. +]] + local CPlayer = {} function CPlayer:__index(index, ...) @@ -23,13 +28,11 @@ function CPlayer:getDistance(coords) end function CPlayer:getPed() - if update or not self.ped then - self.ped = GetPlayerPed(self.source) - end - + self.ped = GetPlayerPed(self.source) return self.ped end +---@deprecated function lib.getPlayer() return CPlayer end diff --git a/imports/points/client.lua b/imports/points/client.lua index e4daf26..3463e60 100644 --- a/imports/points/client.lua +++ b/imports/points/client.lua @@ -23,6 +23,7 @@ CreateThread(function() if distance <= point.distance then point.currentDistance = distance + ---@diagnostic disable-next-line: need-check-nil if distance < (closest?.currentDistance or point.distance) then closest = point end @@ -56,7 +57,7 @@ CreateThread(function() end end) -return { +lib.points = { new = function(...) local args = {...} local id = #points + 1 @@ -94,3 +95,5 @@ return { return closest end } + +return lib.points diff --git a/imports/raycast/client.lua b/imports/raycast/client.lua index 7b7a80c..5a5134b 100644 --- a/imports/raycast/client.lua +++ b/imports/raycast/client.lua @@ -1,7 +1,6 @@ local StartShapeTestLosProbe = StartShapeTestLosProbe local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord -local raycast = {} ---@param flags number? Defaults to 1|2|8|16 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586) ---@param p8 number? A bit mask with bits 1, 2, 4, or 7 relating to collider types. 4 and 7 are usually used. @@ -10,7 +9,7 @@ local raycast = {} ---@return vector3 endCoords ---@return vector3 surfaceNormal ---@return number materialHash -function raycast.cam(flags, p8) +function lib.raycast.cam(flags, p8) local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5) local destination = coords + normal * 10 local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z, @@ -27,4 +26,4 @@ function raycast.cam(flags, p8) end end -return raycast \ No newline at end of file +return lib.raycast \ No newline at end of file diff --git a/imports/table/shared.lua b/imports/table/shared.lua index 058988b..096b7b9 100644 --- a/imports/table/shared.lua +++ b/imports/table/shared.lua @@ -55,4 +55,6 @@ local function table_deepclone(tbl) end table.deepclone = table_deepclone +lib.table = table + return table \ No newline at end of file diff --git a/imports/zones/client.lua b/imports/zones/client.lua index 556362b..74086e6 100644 --- a/imports/zones/client.lua +++ b/imports/zones/client.lua @@ -100,6 +100,7 @@ local function getTriangles(polygon) for i = 1, #sides do local side = sides[i] + ---@type number | function local direction = side[1].y - side[2].y direction = direction > 0 and up or down table.sort(side, direction) @@ -113,6 +114,7 @@ local function getTriangles(polygon) local c, d if aHorizontal[2] then + ---@type number | function local direction = a.x - (a.x ~= aHorizontal[1].x and aHorizontal[1].x or aHorizontal[2].x) direction = direction > 0 and right or left table.sort(aHorizontal, direction) @@ -129,6 +131,7 @@ local function getTriangles(polygon) end if bHorizontal[2] then + ---@type number | function local direction = b.x - (b.x ~= bHorizontal[1].x and bHorizontal[1].x or bHorizontal[2].x) direction = direction > 0 and right or left table.sort(bHorizontal, direction) @@ -324,7 +327,7 @@ end local function debugSphere(self) DrawMarker(28, self.coords.x, self.coords.y, self.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, self.radius, self.radius, - self.radius, 255, 42, 24, 100, false, false, false, true, false, false, false) + self.radius, 255, 42, 24, 100, false, false, 0, true, false, false, false) end local function contains(self, coords) @@ -349,7 +352,7 @@ local function convertToVector(coords) return coords end -return { +lib.zones = { poly = function(data) data.id = #Zones + 1 data.thickness = data.thickness or 4 @@ -414,4 +417,6 @@ return { Zones[data.id] = data return data end, -} \ No newline at end of file +} + +return lib.zones diff --git a/resource/interface/client/progress.lua b/resource/interface/client/progress.lua index 15c263e..7141a78 100644 --- a/resource/interface/client/progress.lua +++ b/resource/interface/client/progress.lua @@ -149,7 +149,7 @@ function lib.cancelProgress() if not progress then error('No progress bar is active') elseif not progress.canCancel then - error(("Progress bar '%s' cannot be cancelled"):format(id)) + error('Progress bar cannot be cancelled') end progress = false