From b77fbeffa803e11dbf038a3720bd1cb2b3d4a8f0 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Mon, 3 Mar 2025 02:05:53 -0500 Subject: [PATCH] player/xplayer guards --- modules/framework/es_extended/server.lua | 24 +++++++++++- modules/framework/qb-core/server.lua | 29 +++++++++++++- modules/framework/qbx_core/server.lua | 50 +++++++++++++++++++----- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/modules/framework/es_extended/server.lua b/modules/framework/es_extended/server.lua index 4496eee..fb0c4fc 100644 --- a/modules/framework/es_extended/server.lua +++ b/modules/framework/es_extended/server.lua @@ -10,6 +10,7 @@ end Framework.GetPlayerDob = function(src) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end local dob = xPlayer.get("dateofbirth") return dob end @@ -18,6 +19,7 @@ end -- Returns the citizen ID of the player. Framework.GetPlayerIdentifier = function(src) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.getIdentifier() end @@ -25,6 +27,7 @@ end -- Returns the first and last name of the player. Framework.GetPlayerName = function(src) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.variables.firstName, xPlayer.variables.lastName end @@ -32,7 +35,9 @@ end -- Returns a table of items matching the specified name and if passed metadata from the player's inventory. -- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot} Framework.GetItem = function(src, item, _) - local playerItems = ESX.GetPlayerFromId(src).getInventory() + local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end + local playerItems = xPlayer.getInventory() local repackedTable = {} for _, v in pairs(playerItems) do if v.name == item then @@ -51,6 +56,7 @@ end -- Returns the count of items matching the specified name and if passed metadata from the player's inventory. Framework.GetItemCount = function(src, item, _) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.getInventoryItem(item).count end @@ -58,7 +64,9 @@ end -- Returns the entire inventory of the player as a table. -- returns {name = v.name, count = v.amount, _, _} Framework.GetPlayerInventory = function(src) - local playerItems = ESX.GetPlayerFromId(src).getInventory() + local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end + local playerItems = xPlayer.getInventory() local repackedTable = {} for _, v in pairs(playerItems) do if v.count > 0 then @@ -77,6 +85,7 @@ end -- Adds the specified metadata key and number value to the player's data. Framework.SetPlayerMetadata = function(src, metadata, value) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end xPlayer.setMeta(metadata, value, nil) return true end @@ -85,6 +94,7 @@ end -- Gets the specified metadata key and value to the player's data. Framework.GetPlayerMetadata = function(src, metadata) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.getMeta(metadata) or false end @@ -95,6 +105,7 @@ end -- phone_number, pincode Framework.GetStatus = function(src, column) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.get(column) or nil end @@ -132,6 +143,7 @@ end -- Returns the phone number of the player. Framework.GetPlayerPhone = function(src) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.get("phone_number") end @@ -139,6 +151,7 @@ end -- Returns the job name, label, grade name, and grade level of the player. Framework.GetPlayerJob = function(src) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.getJob().name, xPlayer.getJob().label, xPlayer.getJob().grade_label, xPlayer.getJob().grade end @@ -160,12 +173,14 @@ end -- Sets the player's job to the specified name and grade. Framework.SetPlayerJob = function(src, name, grade) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end if not ESX.DoesJobExist(name, grade) then lib.print.error("Job Does Not Exsist In Framework :NAME "..name.." Grade:"..grade) return end return xPlayer.setJob(name, grade, true) end Framework.ToggleDuty = function(src, status) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end local name = xPlayer.getJob().name local grade = xPlayer.getJob().grade xPlayer.setJob(name, grade, status) @@ -175,6 +190,7 @@ end -- Adds the specified amount to the player's account balance of the specified type. Framework.AddAccountBalance = function(src, _type, amount) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.addAccountMoney(_type, amount) end @@ -182,6 +198,7 @@ end -- Removes the specified amount from the player's account balance of the specified type. Framework.RemoveAccountBalance = function(src, _type, amount) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.removeAccountMoney(_type, amount) end @@ -189,6 +206,7 @@ end -- Returns the player's account balance of the specified type. Framework.GetAccountBalance = function(src, _type) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.getAccount(_type).money end @@ -196,6 +214,7 @@ end -- Adds the specified item to the player's inventory. Framework.AddItem = function(src, item, amount, slot, metadata) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.addInventoryItem(item, amount) end @@ -203,6 +222,7 @@ end -- Removes the specified item from the player's inventory. Framework.RemoveItem = function(src, item, amount, slot, metadata) local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then return end return xPlayer.removeInventoryItem(item, amount) end diff --git a/modules/framework/qb-core/server.lua b/modules/framework/qb-core/server.lua index 6ea0e5d..3b977a0 100644 --- a/modules/framework/qb-core/server.lua +++ b/modules/framework/qb-core/server.lua @@ -13,6 +13,7 @@ end -- Returns the citizen ID of the player. Framework.GetPlayerIdentifier = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.citizenid end @@ -21,12 +22,14 @@ end -- Returns the first and last name of the player. Framework.GetPlayerName = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.charinfo.firstname, playerData.charinfo.lastname end Framework.GetPlayerDob = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.charinfo.birthdate end @@ -36,6 +39,7 @@ end -- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot} Framework.GetItem = function(src, item, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local playerInventory = playerData.items local repackedTable = {} @@ -56,6 +60,7 @@ end -- Returns the count of items matching the specified name and if passed metadata from the player's inventory. Framework.GetItemCount = function(src, item, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local playerInventory = playerData.items local count = 0 @@ -72,6 +77,7 @@ end -- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot} Framework.GetPlayerInventory = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local playerInventory = playerData.items local repackedTable = {} @@ -88,6 +94,7 @@ end Framework.GetItemBySlot = function(src, slot) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local playerInventory = playerData.items local repack = {} @@ -112,6 +119,7 @@ end -- Adds the specified metadata key and number value to the player's data. Framework.SetPlayerMetadata = function(src, metadata, value) local player = QBCore.Functions.GetPlayer(src) + if not player then return end player.Functions.SetMetaData(metadata, value) return true end @@ -120,6 +128,7 @@ end -- Gets the specified metadata key to the player's data. Framework.GetPlayerMetadata = function(src, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.metadata[metadata] or false end @@ -128,6 +137,7 @@ end -- Adds the specified value to the player's stress level and updates the client HUD. Framework.AddStress = function(src, value) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newStress = playerData.metadata.stress + value player.Functions.SetMetaData('stress', Math.Clamp(newStress, 0, 100)) @@ -139,6 +149,7 @@ end -- Removes the specified value from the player's stress level and updates the client HUD. Framework.RemoveStress = function(src, value) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newStress = (playerData.metadata.stress or 0) - value player.Functions.SetMetaData('stress', Math.Clamp(newStress, 0, 100)) @@ -150,6 +161,7 @@ end -- Adds the specified value from the player's hunger level. Framework.AddHunger = function(src, value) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newHunger = (playerData.metadata.hunger or 0) + value player.Functions.SetMetaData('hunger', Math.Clamp(newHunger, 0, 100)) @@ -161,6 +173,7 @@ end -- Adds the specified value from the player's thirst level. Framework.AddThirst = function(src, value) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newThirst = (playerData.metadata.thirst or 0) + value player.Functions.SetMetaData('thirst', Math.Clamp(newThirst, 0, 100)) @@ -170,6 +183,7 @@ end Framework.GetHunger = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newHunger = (playerData.metadata.hunger or 0) return newHunger @@ -177,6 +191,7 @@ end Framework.GetThirst = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newThirst = (playerData.metadata.thirst or 0) return newThirst @@ -186,6 +201,7 @@ end -- Returns the phone number of the player. Framework.GetPlayerPhone = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.charinfo.phone end @@ -194,6 +210,7 @@ end -- Returns the gang name of the player. Framework.GetPlayerGang = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.gang.name end @@ -216,6 +233,7 @@ end -- Returns the job name, label, grade name, and grade level of the player. Framework.GetPlayerJob = function(src) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.job.name, playerData.job.label, playerData.job.grade.name, playerData.job.grade.level end @@ -224,11 +242,13 @@ end -- Sets the player's job to the specified name and grade. Framework.SetPlayerJob = function(src, name, grade) local player = QBCore.Functions.GetPlayer(src) + if not player then return end return player.Functions.SetJob(name, grade) end Framework.ToggleDuty = function(src, status) local player = QBCore.Functions.GetPlayer(src) + if not player then return end player.Functions.SetJobDuty(status) TriggerEvent('QBCore:Server:SetDuty', src, player.PlayerData.job.onduty) end @@ -237,6 +257,7 @@ end -- Adds the specified amount to the player's account balance of the specified type. Framework.AddAccountBalance = function(src, _type, amount) local player = QBCore.Functions.GetPlayer(src) + if not player then return end if _type == 'money' then _type = 'cash' end return player.Functions.AddMoney(_type, amount) end @@ -245,6 +266,7 @@ end -- Removes the specified amount from the player's account balance of the specified type. Framework.RemoveAccountBalance = function(src, _type, amount) local player = QBCore.Functions.GetPlayer(src) + if not player then return end if _type == 'money' then _type = 'cash' end return player.Functions.RemoveMoney(_type, amount) end @@ -252,7 +274,9 @@ end -- Framework.GetAccountBalance(src, _type) -- Returns the player's account balance of the specified type. Framework.GetAccountBalance = function(src, _type) - local playerData = QBCore.Functions.GetPlayer(src).PlayerData + local player = QBCore.Functions.GetPlayer(src) + if not player then return end + local playerData = player.PlayerData if _type == 'money' then _type = 'cash' end return playerData.money[_type] end @@ -261,6 +285,7 @@ end -- Adds the specified item to the player's inventory. Framework.AddItem = function(src, item, amount, slot, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "add", item = item, count = amount, slot = slot, metadata = metadata}) return player.Functions.AddItem(item, amount, slot, metadata) end @@ -269,6 +294,7 @@ end -- Removes the specified item from the player's inventory. Framework.RemoveItem = function(src, item, amount, slot, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "remove", item = item, count = amount, slot = slot, metadata = metadata}) return player.Functions.RemoveItem(item, amount, slot or nil) end @@ -278,6 +304,7 @@ end -- Notes, this is kinda a jank workaround. with the framework aside from updating the entire table theres not really a better way Framework.SetMetadata = function(src, item, slot, metadata) local player = QBCore.Functions.GetPlayer(src) + if not player then return end local slotFinder = Framework.GetPlayerInventory(src) local freeSlot = Table.FindFirstUnoccupiedSlot(slotFinder) local itemSlot = slot or nil diff --git a/modules/framework/qbx_core/server.lua b/modules/framework/qbx_core/server.lua index 7790468..ebd17b8 100644 --- a/modules/framework/qbx_core/server.lua +++ b/modules/framework/qbx_core/server.lua @@ -9,14 +9,18 @@ Framework.GetFrameworkName = function() end Framework.GetPlayerDob = function(src) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData return playerData.charinfo.birthdate end -- Framework.GetPlayerIdentifier(src) -- Returns the citizen ID of the player. Framework.GetPlayerIdentifier = function(src) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData if not playerData then return false end return playerData.citizenid end @@ -24,7 +28,9 @@ end -- Framework.GetPlayerName(src) -- Returns the first and last name of the player. Framework.GetPlayerName = function(src) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData return playerData.charinfo.firstname, playerData.charinfo.lastname end @@ -32,7 +38,9 @@ end -- Returns a table of items matching the specified name and if passed metadata from the player's inventory. -- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot} Framework.GetItem = function(src, item, metadata) - local playerInventory = QBox:GetPlayer(src).PlayerData.items + local player = QBox:GetPlayer(src) + if not player then return end + local playerInventory = player.PlayerData.items local repackedTable = {} for _, v in pairs(playerInventory) do if v.name == item and (not metadata or v.info == metadata) then @@ -50,7 +58,9 @@ end -- Framework.GetItemCount(src, item, metadata) -- Returns the count of items matching the specified name and if passed metadata from the player's inventory. Framework.GetItemCount = function(src, item, metadata) - local playerInventory = QBox:GetPlayer(src).PlayerData.items + local player = QBox:GetPlayer(src) + if not player then return end + local playerInventory = player.PlayerData.items local count = 0 for _, v in pairs(playerInventory) do if v.name == item and (not metadata or v.info == metadata) then @@ -64,7 +74,9 @@ end -- Returns the entire inventory of the player as a table. -- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot} Framework.GetPlayerInventory = function(src) - local playerItems = QBox:GetPlayer(src).PlayerData.items + local player = QBox:GetPlayer(src) + if not player then return end + local playerItems = player.PlayerData.items local repackedTable = {} for _, v in pairs(playerItems) do table.insert(repackedTable, { @@ -81,6 +93,7 @@ end -- Adds the specified metadata key and number value to the player's data. Framework.SetPlayerMetadata = function(src, metadata, value) local player = QBox:GetPlayer(src) + if not player then return end player.Functions.SetMetaData(metadata, value) return true end @@ -89,6 +102,7 @@ end -- Gets the specified metadata key to the player's data. Framework.GetPlayerMetadata = function(src, metadata) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData return playerData.metadata[metadata] or false end @@ -97,6 +111,7 @@ end -- Adds the specified value to the player's stress level and updates the client HUD. Framework.AddStress = function(src, value) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newStress = playerData.metadata.stress + value player.Functions.SetMetaData('stress', Math.Clamp(newStress, 0, 100)) @@ -108,6 +123,7 @@ end -- Removes the specified value from the player's stress level and updates the client HUD. Framework.RemoveStress = function(src, value) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newStress = (playerData.metadata.stress or 0) - value player.Functions.SetMetaData('stress', Math.Clamp(newStress, 0, 100)) @@ -119,6 +135,7 @@ end -- Adds the specified value from the player's hunger level. Framework.AddHunger = function(src, value) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newHunger = (playerData.metadata.hunger or 0) + value player.Functions.SetMetaData('hunger', Math.Clamp(newHunger, 0, 100)) @@ -130,6 +147,7 @@ end -- Adds the specified value from the player's thirst level. Framework.AddThirst = function(src, value) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newThirst = (playerData.metadata.thirst or 0) + value player.Functions.SetMetaData('thirst', Math.Clamp(newThirst, 0, 100)) @@ -139,6 +157,7 @@ end Framework.GetHunger = function(src) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newHunger = (playerData.metadata.hunger or 0) return newHunger @@ -146,6 +165,7 @@ end Framework.GetThirst = function(src) local player = QBox:GetPlayer(src) + if not player then return end local playerData = player.PlayerData local newThirst = (playerData.metadata.thirst or 0) return newThirst @@ -154,7 +174,9 @@ end -- Framework.GetPlayerPhone(src) -- Returns the phone number of the player. Framework.GetPlayerPhone = function(src) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData return playerData.charinfo.phone end @@ -187,11 +209,14 @@ end -- Sets the player's job to the specified name and grade. Framework.SetPlayerJob = function(src, name, grade) local player = QBox:GetPlayer(src) + if not player then return end return player.Functions.SetJob(name, grade) end Framework.ToggleDuty = function(src, status) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData QBox:SetJobDuty(playerData.citizenid, status) end @@ -199,6 +224,7 @@ end -- Adds the specified amount to the player's account balance of the specified type. Framework.AddAccountBalance = function(src, _type, amount) local player = QBox:GetPlayer(src) + if not player then return end if _type == 'money' then _type = 'cash' end return player.Functions.AddMoney(_type, amount) end @@ -207,6 +233,7 @@ end -- Removes the specified amount from the player's account balance of the specified type. Framework.RemoveAccountBalance = function(src, _type, amount) local player = QBox:GetPlayer(src) + if not player then return end if _type == 'money' then _type = 'cash' end return player.Functions.RemoveMoney(_type, amount) end @@ -214,7 +241,9 @@ end -- Framework.GetAccountBalance(src, _type) -- Returns the player's account balance of the specified type. Framework.GetAccountBalance = function(src, _type) - local playerData = QBox:GetPlayer(src).PlayerData + local player = QBox:GetPlayer(src) + if not player then return end + local playerData = player.PlayerData if _type == 'money' then _type = 'cash' end return playerData.money[_type] end @@ -223,6 +252,7 @@ end -- Adds the specified item to the player's inventory. Framework.AddItem = function(src, item, amount, slot, metadata) local player = QBox:GetPlayer(src) + if not player then return end TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "add", item = item, count = amount, slot = slot, metadata = metadata}) return player.Functions.AddItem(item, amount, slot, metadata) end @@ -231,6 +261,7 @@ end -- Removes the specified item from the player's inventory. Framework.RemoveItem = function(src, item, amount, slot, metadata) local player = QBox:GetPlayer(src) + if not player then return end TriggerClientEvent("community_bridge:client:inventory:updateInventory", src, {action = "remove", item = item, count = amount, slot = slot, metadata = metadata}) return player.Functions.RemoveItem(item, amount, slot) end @@ -240,6 +271,7 @@ end -- Notes, this is kinda a jank workaround. with the framework aside from updating the entire table theres not really a better way Framework.SetMetadata = function(src, item, slot, metadata) local player = QBox:GetPlayer(src) + if not player then return end player.Functions.RemoveItem(item, 1, slot) return player.Functions.AddItem(item, 1, slot, metadata) end