Skip to content

Commit a9ae0b4

Browse files
authored
Merge pull request #1704 from esx-framework/player-class
refactor(es_extended/server/class/player): add proper type annotations
2 parents 4404e27 + ec49a12 commit a9ae0b4

File tree

9 files changed

+206
-187
lines changed

9 files changed

+206
-187
lines changed

[core]/es_extended/client/functions.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---@return boolean
2+
---@diagnostic disable-next-line: duplicate-set-field
23
function ESX.IsPlayerLoaded()
34
return ESX.PlayerLoaded
45
end
@@ -559,7 +560,8 @@ function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked)
559560
local modelHash = ESX.Streaming.RequestModel(model)
560561
if not modelHash then
561562
if promise then
562-
return promise:reject(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
563+
promise:reject(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
564+
return
563565
end
564566
error(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
565567
end

[core]/es_extended/client/imports/point.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local Point = ESX.Class()
22

3-
local nearby, loop = {}
3+
local nearby, loop = {}, nil
44

55
function Point:constructor(properties)
66
self.coords = properties.coords
@@ -23,7 +23,7 @@ function Point:constructor(properties)
2323
point:inside(#(coords - point.coords))
2424
end
2525
end
26-
Wait()
26+
Wait(0)
2727
end
2828
end)
2929
end

[core]/es_extended/client/modules/callback.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ end
9292

9393
---@param eventName string
9494
---@param ... any
95-
---@return any
95+
---@return ...
9696
function ESX.AwaitServerCallback(eventName, ...)
9797
local invokingResource = GetInvokingResource()
9898
local invoker = (invokingResource and invokingResource ~= "unknown") and invokingResource or "es_extended"

[core]/es_extended/imports.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ if GetResourceState("ox_lib") == "missing" then
218218
return nil, err or 'unknown error'
219219
end
220220

221+
---@diagnostic disable-next-line: duplicate-doc-alias
221222
---@alias PackageSearcher
222223
---| fun(modName: string): function loader
223-
---| fun(modName: string): nil, string errmsg
224+
---| fun(modName: string): nil|false, string errmsg
224225

225226
---@type PackageSearcher[]
226227
package.searchers = {

[core]/es_extended/server/classes/player.lua

Lines changed: 147 additions & 175 deletions
Large diffs are not rendered by default.

[core]/es_extended/server/functions.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ end
292292

293293
---@param key? string
294294
---@param val? string|table
295-
---@return table
295+
---@return xPlayer[]|table<any, xPlayer[]>
296296
function ESX.GetExtendedPlayers(key, val)
297297
if not key then
298298
return ESX.Table.ToArray(ESX.Players)
@@ -348,13 +348,13 @@ function ESX.GetNumPlayers(key, val)
348348
end
349349

350350
---@param source number
351-
---@return table
351+
---@return xPlayer?
352352
function ESX.GetPlayerFromId(source)
353353
return ESX.Players[tonumber(source)]
354354
end
355355

356356
---@param identifier string
357-
---@return table
357+
---@return xPlayer?
358358
function ESX.GetPlayerFromIdentifier(identifier)
359359
return Core.playersByIdentifier[identifier]
360360
end
@@ -367,16 +367,17 @@ end
367367

368368
---@param source number
369369
---@return boolean
370+
---@diagnostic disable-next-line: duplicate-set-field
370371
function ESX.IsPlayerLoaded(source)
371372
return ESX.Players[source] ~= nil
372373
end
373374

374375
---@param playerId number | string
375-
---@return string
376+
---@return string, number
376377
function ESX.GetIdentifier(playerId)
377378
local fxDk = GetConvarInt("sv_fxdkMode", 0)
378379
if fxDk == 1 then
379-
return "ESX-DEBUG-LICENCE"
380+
return "ESX-DEBUG-LICENCE", 0
380381
end
381382

382383
playerId = tostring(playerId)

[core]/es_extended/server/main.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ if not Config.CustomInventory then
417417
if itemType == "item_standard" then
418418
local sourceItem = sourceXPlayer.getInventoryItem(itemName)
419419

420+
if not sourceItem then
421+
return
422+
end
423+
420424
if itemCount < 1 or sourceItem.count < itemCount then
421425
return sourceXPlayer.showNotification(TranslateCap("imp_invalid_quantity"))
422426
end
@@ -453,6 +457,10 @@ if not Config.CustomInventory then
453457
end
454458

455459
local _, weapon = sourceXPlayer.getWeapon(itemName)
460+
if not weapon then
461+
return
462+
end
463+
456464
local _, weaponObject = ESX.GetWeapon(itemName)
457465
itemCount = weapon.ammo
458466
local weaponComponents = ESX.Table.Clone(weapon.components)
@@ -485,6 +493,9 @@ if not Config.CustomInventory then
485493
end
486494

487495
local _, weapon = sourceXPlayer.getWeapon(itemName)
496+
if not weapon then
497+
return
498+
end
488499

489500
if not targetXPlayer.hasWeapon(itemName) then
490501
sourceXPlayer.showNotification(TranslateCap("gave_weapon_noweapon", targetXPlayer.name))
@@ -511,12 +522,19 @@ if not Config.CustomInventory then
511522
local playerId = source
512523
local xPlayer = ESX.GetPlayerFromId(playerId)
513524

525+
if not xPlayer then
526+
return
527+
end
528+
514529
if itemType == "item_standard" then
515530
if not itemCount or itemCount < 1 then
516531
return xPlayer.showNotification(TranslateCap("imp_invalid_quantity"))
517532
end
518533

519534
local xItem = xPlayer.getInventoryItem(itemName)
535+
if not xItem then
536+
return
537+
end
520538

521539
if itemCount > xItem.count or xItem.count < 1 then
522540
return xPlayer.showNotification(TranslateCap("imp_invalid_quantity"))
@@ -532,6 +550,9 @@ if not Config.CustomInventory then
532550
end
533551

534552
local account = xPlayer.getAccount(itemName)
553+
if not account then
554+
return
555+
end
535556

536557
if itemCount > account.money or account.money < 1 then
537558
return xPlayer.showNotification(TranslateCap("imp_invalid_amount"))
@@ -547,6 +568,10 @@ if not Config.CustomInventory then
547568
if not xPlayer.hasWeapon(itemName) then return end
548569

549570
local _, weapon = xPlayer.getWeapon(itemName)
571+
if not weapon then
572+
return
573+
end
574+
550575
local _, weaponObject = ESX.GetWeapon(itemName)
551576
-- luacheck: ignore weaponPickupLabel
552577
local weaponPickupLabel = ""
@@ -569,6 +594,11 @@ if not Config.CustomInventory then
569594
RegisterNetEvent("esx:useItem", function(itemName)
570595
local source = source
571596
local xPlayer = ESX.GetPlayerFromId(source)
597+
598+
if not xPlayer then
599+
return
600+
end
601+
572602
local count = xPlayer.getInventoryItem(itemName).count
573603

574604
if count < 1 then
@@ -581,6 +611,10 @@ if not Config.CustomInventory then
581611
RegisterNetEvent("esx:onPickup", function(pickupId)
582612
local pickup, xPlayer, success = Core.Pickups[pickupId], ESX.GetPlayerFromId(source)
583613

614+
if not xPlayer then
615+
return
616+
end
617+
584618
if not pickup then return end
585619

586620
local playerPickupDistance = #(pickup.coords - xPlayer.getCoords(true))
@@ -623,6 +657,10 @@ end
623657
ESX.RegisterServerCallback("esx:getPlayerData", function(source, cb)
624658
local xPlayer = ESX.GetPlayerFromId(source)
625659

660+
if not xPlayer then
661+
return
662+
end
663+
626664
cb({
627665
identifier = xPlayer.identifier,
628666
accounts = xPlayer.getAccounts(),
@@ -646,6 +684,10 @@ end)
646684
ESX.RegisterServerCallback("esx:getOtherPlayerData", function(_, cb, target)
647685
local xPlayer = ESX.GetPlayerFromId(target)
648686

687+
if not xPlayer then
688+
return
689+
end
690+
649691
cb({
650692
identifier = xPlayer.identifier,
651693
accounts = xPlayer.getAccounts(),

[core]/es_extended/server/modules/callback.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ end
9898
---@param player number playerId
9999
---@param eventName string
100100
---@param ... any
101-
---@return any
101+
---@return ...
102102
function ESX.AwaitClientCallback(player, eventName, ...)
103103
local invokingResource = GetInvokingResource()
104104
local invoker = (invokingResource and invokingResource ~= "Unknown") and invokingResource or "es_extended"

[core]/es_extended/shared/functions.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ end
7676

7777
---@param weaponName string
7878
---@param weaponComponent string
79-
---@return table | nil
79+
---@return ESXWeaponComponent?
8080
function ESX.GetWeaponComponent(weaponName, weaponComponent)
8181
weaponName = string.upper(weaponName)
8282

8383
assert(weaponsByName[weaponName], "Invalid weapon name!")
8484
local weapon = Config.Weapons[weaponsByName[weaponName]]
8585

8686
for _, component in ipairs(weapon.components) do
87+
---@cast component ESXWeaponComponent
8788
if component.name == weaponComponent then
8889
return component
8990
end

0 commit comments

Comments
 (0)