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

Use external state management #593

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions bridge/qb/client/events.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require 'config.client'
local useExternalPlayerState = require 'config.shared'.useExternalPlayerState

-- Trigger Command
--- @deprecated
Expand All @@ -24,6 +25,8 @@ RegisterNetEvent('QBCore:Client:VehicleInfo', function(info)
TriggerEvent('QBCore:Client:'..info.event..'Vehicle', data)
end)

if useExternalPlayerState then return end

AddStateBagChangeHandler('hunger', ('player:%s'):format(cache.serverId), function(_, _, value)
TriggerEvent('hud:client:UpdateNeeds', value, LocalPlayer.state.thirst)
end)
Expand Down
3 changes: 3 additions & 0 deletions client/loops.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local useExternalPlayerState = require 'config.shared'.useExternalPlayerState
if useExternalPlayerState then return end

local statusInterval = require 'config.client'.statusIntervalSeconds
local playerState = LocalPlayer.state

Expand Down
1 change: 1 addition & 0 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
return {
serverName = 'Server',
defaultSpawn = vec4(-540.58, -212.02, 37.65, 208.88),
useExternalPlayerState = false,
notifyPosition = 'top-right', -- 'top' | 'top-right' | 'top-left' | 'bottom' | 'bottom-right' | 'bottom-left'
---@type { name: string, amount: integer, metadata: fun(source: number): table }[]
starterItems = { -- Character starting items
Expand Down
3 changes: 3 additions & 0 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local loggingConfig = require 'config.server'.logging
local serverName = require 'config.shared'.serverName
local logger = require 'modules.logger'
local queue = require 'server.queue'
local useExternalPlayerState = require 'config.shared'.useExternalPlayerState

-- Event Handler

Expand Down Expand Up @@ -196,6 +197,8 @@ RegisterNetEvent('QBCore:ToggleDuty', function()
end
end)

if useExternalPlayerState then return end

---Syncs the player's hunger, thirst, and stress levels with the statebags
---@param bagName string
---@param meta 'hunger' | 'thirst' | 'stress'
Expand Down
47 changes: 25 additions & 22 deletions server/loops.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
local config = require 'config.server'

local function removeHungerAndThirst(src, player)
local playerState = Player(src).state
if not playerState.isLoggedIn then return end
local newHunger = playerState.hunger - config.player.hungerRate
local newThirst = playerState.thirst - config.player.thirstRate

player.Functions.SetMetaData('thirst', math.max(0, newThirst))
player.Functions.SetMetaData('hunger', math.max(0, newHunger))

player.Functions.Save()
end

CreateThread(function()
local interval = 60000 * config.updateInterval
while true do
Wait(interval)
for src, player in pairs(QBX.Players) do
removeHungerAndThirst(src, player)
end
end
end)
local useExternalPlayerState = require 'config.shared'.useExternalPlayerState

local function pay(player)
local job = player.PlayerData.job
Expand Down Expand Up @@ -53,3 +32,27 @@ CreateThread(function()
end
end
end)

if useExternalPlayerState then return end

local function removeHungerAndThirst(src, player)
local playerState = Player(src).state
if not playerState.isLoggedIn then return end
local newHunger = playerState.hunger - config.player.hungerRate
local newThirst = playerState.thirst - config.player.thirstRate

player.Functions.SetMetaData('thirst', math.max(0, newThirst))
player.Functions.SetMetaData('hunger', math.max(0, newHunger))

player.Functions.Save()
end

CreateThread(function()
local interval = 60000 * config.updateInterval
while true do
Wait(interval)
for src, player in pairs(QBX.Players) do
removeHungerAndThirst(src, player)
end
end
end)
15 changes: 9 additions & 6 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local setJobReplaces = GetConvar('qbx:setjob_replaces', 'true') == 'true'
local setGangReplaces = GetConvar('qbx:setgang_replaces', 'true') == 'true'
local accounts = json.decode(GetConvar('inventory:accounts', '["money"]'))
local accountsAsItems = table.create(0, #accounts)
local useExternalPlayerState = require 'config.shared'.useExternalPlayerState

for i = 1, #accounts do
accountsAsItems[accounts[i]] = 0
Expand Down Expand Up @@ -454,7 +455,7 @@ function CheckPlayerData(source, playerData)
playerData.metadata.hunger = playerData.metadata.hunger or 100
playerData.metadata.thirst = playerData.metadata.thirst or 100
playerData.metadata.stress = playerData.metadata.stress or 0
if playerState then
if playerState and not useExternalPlayerState then
playerState:set('hunger', playerData.metadata.hunger, true)
playerState:set('thirst', playerData.metadata.thirst, true)
playerState:set('stress', playerData.metadata.stress, true)
Expand Down Expand Up @@ -549,9 +550,11 @@ function Logout(source)
local player = GetPlayer(source)
if not player then return end
local playerState = Player(source)?.state
player.PlayerData.metadata.hunger = playerState?.hunger or player.PlayerData.metadata.hunger
player.PlayerData.metadata.thirst = playerState?.thirst or player.PlayerData.metadata.thirst
player.PlayerData.metadata.stress = playerState?.stress or player.PlayerData.metadata.stress
if playerState and not useExternalPlayerState then
player.PlayerData.metadata.hunger = playerState?.hunger or player.PlayerData.metadata.hunger
player.PlayerData.metadata.thirst = playerState?.thirst or player.PlayerData.metadata.thirst
player.PlayerData.metadata.stress = playerState?.stress or player.PlayerData.metadata.stress
end

TriggerClientEvent('QBCore:Client:OnPlayerUnload', source)
TriggerEvent('QBCore:Server:OnPlayerUnload', source)
Expand Down Expand Up @@ -678,7 +681,7 @@ function CreatePlayer(playerData, Offline)
TriggerClientEvent('qbx_core:client:onSetMetaData', self.PlayerData.source, meta, oldVal, val)
TriggerEvent('qbx_core:server:onSetMetaData', meta, oldVal, val, self.PlayerData.source)

if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') then
if (meta == 'hunger' or meta == 'thirst' or meta == 'stress') and playerState and not useExternalPlayerState then
val = lib.math.clamp(val, 0, 100)
if playerState[meta] ~= val then
playerState:set(meta, val, true)
Expand Down Expand Up @@ -1023,7 +1026,7 @@ function Save(source)
playerData.metadata.health = GetEntityHealth(ped)
playerData.metadata.armor = GetPedArmour(ped)

if playerState.isLoggedIn then
if playerState.isLoggedIn and not useExternalPlayerState then
playerData.metadata.hunger = playerState.hunger or 0
playerData.metadata.thirst = playerState.thirst or 0
playerData.metadata.stress = playerState.stress or 0
Expand Down