Skip to content

Commit

Permalink
refactor: remove the loop & add config option (#21)
Browse files Browse the repository at this point in the history
* Remove the loop

* put client configs in config/client.lua

* refactor: explicitly define files to client.

---------

Co-authored-by: Solareon <[email protected]>
  • Loading branch information
TonybynMp4 and solareon authored Feb 7, 2024
1 parent ba72f3c commit e69de4d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
45 changes: 13 additions & 32 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local config = require 'config.shared'
local config = require 'config.client'
local sharedConfig = require 'config.shared'
local radioMenu = false
local onRadio = false
local radioChannel = 0
local radioVolume = 50
local hasRadio = false
local micClicks = true

local function connectToRadio(channel)
Expand Down Expand Up @@ -53,36 +53,22 @@ local function toggleRadio(toggle)
end
end

local function doRadioCheck()
hasRadio = exports.ox_inventory:Search('count', 'radio') > 0
end

local function isRadioOn()
return onRadio
end

exports('IsRadioOn', isRadioOn)

-- Handles state right when the player selects their character and location.
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
doRadioCheck()
end)

-- Resets state on logout, in case of character change.
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
hasRadio = false
leaveradio()
end)

AddEventHandler('ox_inventory:itemCount', function(itemName, totalCount)
if itemName ~= 'radio' then return end
hasRadio = totalCount > 0
end)

-- Handles state if resource is restarted live.
AddEventHandler('onResourceStart', function(resource)
if GetCurrentResourceName() ~= resource then return end
doRadioCheck()
if totalCount <= 0 and radioChannel ~= 0 then
leaveradio()
end
end)

RegisterNetEvent('qbx_radio:client:use', function()
Expand Down Expand Up @@ -110,8 +96,8 @@ RegisterNUICallback('joinRadio', function(data, cb)
return
end

local frequency = not config.whitelistSubChannels and math.floor(rchannel) or rchannel
if config.restrictedChannels[frequency] and (not config.restrictedChannels[frequency][QBX.PlayerData.job.name] or not QBX.PlayerData.job.onduty) then
local frequency = not sharedConfig.whitelistSubChannels and math.floor(rchannel) or rchannel
if sharedConfig.restrictedChannels[frequency] and (not sharedConfig.restrictedChannels[frequency][QBX.PlayerData.job.name] or not QBX.PlayerData.job.onduty) then
exports.qbx_core:Notify(locale('restricted_channel'), 'error')
cb('ok')
return
Expand Down Expand Up @@ -195,15 +181,10 @@ RegisterNUICallback('escape', function(_, cb)
cb('ok')
end)

CreateThread(function()
while true do
Wait(1000)
if LocalPlayer.state.isLoggedIn and onRadio then
if not hasRadio or QBX.PlayerData.metadata.isdead or QBX.PlayerData.metadata.inlaststand then
if radioChannel ~= 0 then
leaveradio()
end
end
if config.leaveOnDeath then
AddStateBagChangeHandler('isDead', ('player:%s'):format(cache.serverId), function(_, _, value)
if value and onRadio and radioChannel ~= 0 then
leaveradio()
end
end
end)
end)
end
10 changes: 10 additions & 0 deletions config/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
return {
maxFrequency = 500, -- Max amount of available channel frequencies to use

-- Should the radio turn off when the player is dead (Talking is disabled anyway)
leaveOnDeath = true,

---@type number
-- How many decimal places to use for the subchannel.
decimalPlaces = 2,
}
10 changes: 2 additions & 8 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
return {
maxFrequency = 500, -- Max amount of available channel frequencies to use

-- If false restrictedChannels restricts all decimals, if true you need to manually add each subchannel (100.01, 100.02 etc)
whitelistSubChannels = false,

---@type number
-- How many decimal places to use for the subchannel.
decimalPlaces = 2,


---@alias channelNumber number
---@type table<channelNumber, {jobName: boolean, jobName2: boolean}>
restrictedChannels = {
Expand Down Expand Up @@ -52,4 +46,4 @@ return {
ambulance = true
}
}
}
}
1 change: 1 addition & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ files {
'html/css/style.css',
'html/img/radio.png',
'config/shared.lua',
'config/client.lua',
'locales/*.json'
}

Expand Down

0 comments on commit e69de4d

Please sign in to comment.