Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server/player): more offline player checks #556

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,11 @@ function CreatePlayer(playerData, Offline)
---@param onDuty boolean
function self.Functions.SetJobDuty(onDuty)
self.PlayerData.job.onduty = not not onDuty -- Make sure the value is a boolean if nil is sent
TriggerEvent('QBCore:Server:SetDuty', self.PlayerData.source, self.PlayerData.job.onduty)
TriggerClientEvent('QBCore:Client:SetDuty', self.PlayerData.source, self.PlayerData.job.onduty)
self.Functions.UpdatePlayerData()
if not self.Offline then
TriggerEvent('QBCore:Server:SetDuty', self.PlayerData.source, self.PlayerData.job.onduty)
TriggerClientEvent('QBCore:Client:SetDuty', self.PlayerData.source, self.PlayerData.job.onduty)
self.Functions.UpdatePlayerData()
end
end

---@param key string
Expand All @@ -665,11 +667,15 @@ function CreatePlayer(playerData, Offline)
---@param val any
function self.Functions.SetMetaData(meta, val)
if not meta or type(meta) ~= 'string' then return end
if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') and self.PlayerData.source then

if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') then
val = lib.math.clamp(val, 0, 100)
Player(self.PlayerData.source).state:set(meta, val, true)
if not self.Offline then
Player(self.PlayerData.source).state:set(meta, val, true)
end
end
if (meta == 'dead' or meta == 'inlaststand') and self.PlayerData.source then

if (meta == 'dead' or meta == 'inlaststand') and not self.Offline then
Player(self.PlayerData.source).state:set('canUseWeapons', not val, true)
end

Expand All @@ -679,8 +685,10 @@ function CreatePlayer(playerData, Offline)
if meta == 'inlaststand' or meta == 'isdead' then
self.Functions.Save()
end
TriggerClientEvent('qbx_core:client:onSetMetaData', self.PlayerData.source, meta, oldVal, val)
TriggerEvent('qbx_core:server:onSetMetaData', meta, oldVal, val, self.PlayerData.source)
if not self.Offline then
TriggerClientEvent('qbx_core:client:onSetMetaData', self.PlayerData.source, meta, oldVal, val)
TriggerEvent('qbx_core:server:onSetMetaData', meta, oldVal, val, self.PlayerData.source)
end
end

---@param meta string
Expand Down Expand Up @@ -839,6 +847,7 @@ function CreatePlayer(playerData, Offline)
---@param slot? number
---@return boolean success
function self.Functions.AddItem(item, amount, slot, metadata)
assert(not self.Offline, 'unsupported for offline players')
return exports.ox_inventory:AddItem(self.PlayerData.source, item, amount, metadata, slot)
end

Expand All @@ -848,32 +857,37 @@ function CreatePlayer(playerData, Offline)
---@param slot? number
---@return boolean success
function self.Functions.RemoveItem(item, amount, slot)
assert(not self.Offline, 'unsupported for offline players')
return exports.ox_inventory:RemoveItem(self.PlayerData.source, item, amount, nil, slot)
end

---@deprecated use ox_inventory exports directly
---@param slot number
---@return any table
function self.Functions.GetItemBySlot(slot)
assert(not self.Offline, 'unsupported for offline players')
return qbItemCompat(exports.ox_inventory:GetSlot(self.PlayerData.source, slot))
end

---@deprecated use ox_inventory exports directly
---@param itemName string
---@return any table
function self.Functions.GetItemByName(itemName)
assert(not self.Offline, 'unsupported for offline players')
return qbItemCompat(exports.ox_inventory:GetSlotWithItem(self.PlayerData.source, itemName))
end

---@deprecated use ox_inventory exports directly
---@param itemName string
---@return any table
function self.Functions.GetItemsByName(itemName)
assert(not self.Offline, 'unsupported for offline players')
return qbItemCompat(exports.ox_inventory:GetSlotsWithItem(self.PlayerData.source, itemName))
end

---@deprecated use ox_inventory exports directly
function self.Functions.ClearInventory()
assert(not self.Offline, 'unsupported for offline players')
return exports.ox_inventory:ClearInventory(self.PlayerData.source)
end

Expand All @@ -898,7 +912,7 @@ function CreatePlayer(playerData, Offline)

---@deprecated call exports.qbx_core:Logout(source)
function self.Functions.Logout()
if self.Offline then return end -- Unsupported for Offline Players
assert(not self.Offline, 'unsupported for offline players')
Logout(self.PlayerData.source)
end

Expand Down