mirror of
https://github.com/citizenfx/cfx-server-data.git
synced 2026-11-10 13:57:18 +01:00
Code modernize (#234)
* refactor: declare exports at runtime and drop the player-data provides alias Signed-off-by: Mathu-lmn <[email protected]> * refactor: pass handlers directly to RegisterNetEvent Signed-off-by: Mathu-lmn <[email protected]> * refactor: use CreateThread/Wait without the Citizen prefix Signed-off-by: Mathu-lmn <[email protected]> * chore: remove trailing whitespace, stray lines and unused locals Signed-off-by: Mathu-lmn <[email protected]> --------- Signed-off-by: Mathu-lmn <[email protected]>
This commit is contained in:
@@ -80,14 +80,12 @@ local function handleFountainStuff(source, id, pickup)
|
||||
end
|
||||
|
||||
-- event for picking up fountain->player
|
||||
RegisterNetEvent('money_fountain:tryPickup')
|
||||
AddEventHandler('money_fountain:tryPickup', function(id)
|
||||
RegisterNetEvent('money_fountain:tryPickup', function(id)
|
||||
handleFountainStuff(source, id, true)
|
||||
end)
|
||||
|
||||
-- event for donating player->fountain
|
||||
RegisterNetEvent('money_fountain:tryPlace')
|
||||
AddEventHandler('money_fountain:tryPlace', function(id)
|
||||
RegisterNetEvent('money_fountain:tryPlace', function(id)
|
||||
handleFountainStuff(source, id, false)
|
||||
end)
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@ local moneyTypes = {
|
||||
bank = `BANK_BALANCE`,
|
||||
}
|
||||
|
||||
RegisterNetEvent('money:displayUpdate')
|
||||
|
||||
AddEventHandler('money:displayUpdate', function(type, money)
|
||||
RegisterNetEvent('money:displayUpdate', function(type, money)
|
||||
local stat = moneyTypes[type]
|
||||
if not stat then return end
|
||||
StatSetInt(stat, math.floor(money))
|
||||
|
||||
@@ -12,5 +12,6 @@ game 'gta5'
|
||||
client_script 'client.lua'
|
||||
server_script 'server.lua'
|
||||
|
||||
--dependency 'cfx.re/playerData.v1alpha1'
|
||||
lua54 'yes'
|
||||
|
||||
dependency 'player-data'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
local playerData = exports['cfx.re/playerData.v1alpha1']
|
||||
local playerData = exports['player-data']
|
||||
|
||||
local validMoneyTypes = {
|
||||
bank = true,
|
||||
@@ -88,9 +88,7 @@ AddEventHandler('money:updated', function(data)
|
||||
end
|
||||
end)
|
||||
|
||||
RegisterNetEvent('money:requestDisplay')
|
||||
|
||||
AddEventHandler('money:requestDisplay', function()
|
||||
RegisterNetEvent('money:requestDisplay', function()
|
||||
local source = source
|
||||
local playerId = playerData:getPlayerId(source)
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
AddEventHandler('gameEventTriggered', function(eventName, args)
|
||||
if eventName == 'CEventNetworkEntityDamage' then
|
||||
local victim = args[1]
|
||||
local culprit = args[2]
|
||||
local isDead = args[6] == 1
|
||||
|
||||
if isDead then
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
local safePositions = {}
|
||||
|
||||
RegisterNetEvent('money:allowPickupNear')
|
||||
|
||||
AddEventHandler('money:allowPickupNear', function(pedId)
|
||||
RegisterNetEvent('money:allowPickupNear', function(pedId)
|
||||
local entity = NetworkGetEntityFromNetworkId(pedId)
|
||||
|
||||
Wait(250)
|
||||
@@ -19,9 +17,7 @@ AddEventHandler('money:allowPickupNear', function(pedId)
|
||||
safePositions[pedId] = coords
|
||||
end)
|
||||
|
||||
RegisterNetEvent('money:tryPickup')
|
||||
|
||||
AddEventHandler('money:tryPickup', function(entity)
|
||||
RegisterNetEvent('money:tryPickup', function(entity)
|
||||
if not safePositions[entity] then
|
||||
return
|
||||
end
|
||||
|
||||
@@ -10,7 +10,3 @@ fx_version 'cerulean'
|
||||
game 'common'
|
||||
|
||||
server_script 'server.lua'
|
||||
|
||||
provides {
|
||||
'cfx.re/playerData.v1alpha1'
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
-- it works in a fairly simple way: a set of identifiers is assigned to an account ID, and said
|
||||
-- account ID is then returned/added as state bag
|
||||
--
|
||||
-- it also implements the `cfx.re/playerData.v1alpha1` spec, which is exposed through the following:
|
||||
-- it also implements the `player-data` spec, which is exposed through the following:
|
||||
-- - getPlayerId(source: string)
|
||||
-- - getPlayerById(dbId: string)
|
||||
-- - getPlayerIdFromIdentifier(identifier: string)
|
||||
@@ -130,8 +130,6 @@ AddEventHandler('playerConnecting', function()
|
||||
end)
|
||||
|
||||
-- and migrate them to a 'joining' ID where possible
|
||||
RegisterNetEvent('playerJoining')
|
||||
|
||||
AddEventHandler('playerJoining', function(oldIdx)
|
||||
-- resource restart race condition
|
||||
local oldPlayer = players[tostring(oldIdx)]
|
||||
@@ -189,25 +187,10 @@ RegisterCommand('playerData', function(source, args)
|
||||
end
|
||||
end, true)
|
||||
|
||||
-- COMPATIBILITY for server versions that don't export provide
|
||||
local function getExportEventName(resource, name)
|
||||
return string.format('__cfx_export_%s_%s', resource, name)
|
||||
end
|
||||
|
||||
function AddExport(name, fn)
|
||||
if not Citizen.Traits or not Citizen.Traits.ProvidesExports then
|
||||
AddEventHandler(getExportEventName('cfx.re/playerData.v1alpha1', name), function(setCB)
|
||||
setCB(fn)
|
||||
end)
|
||||
end
|
||||
|
||||
exports(name, fn)
|
||||
end
|
||||
|
||||
-- exports
|
||||
AddExport('getPlayerIdFromIdentifier', getPlayerIdFromIdentifier)
|
||||
exports('getPlayerIdFromIdentifier', getPlayerIdFromIdentifier)
|
||||
|
||||
AddExport('getPlayerId', function(playerIdx)
|
||||
exports('getPlayerId', function(playerIdx)
|
||||
local player = players[tostring(playerIdx)]
|
||||
|
||||
if not player then
|
||||
@@ -217,6 +200,6 @@ AddExport('getPlayerId', function(playerIdx)
|
||||
return player.dbId
|
||||
end)
|
||||
|
||||
AddExport('getPlayerById', function(playerId)
|
||||
exports('getPlayerById', function(playerId)
|
||||
return playersById[tostring(playerId)]
|
||||
end)
|
||||
@@ -13,19 +13,6 @@ server_script 'playernames_api.lua'
|
||||
client_script 'playernames_cl.lua'
|
||||
server_script 'playernames_sv.lua'
|
||||
|
||||
-- make exports
|
||||
local exportList = {
|
||||
'setComponentColor',
|
||||
'setComponentAlpha',
|
||||
'setComponentVisibility',
|
||||
'setWantedLevel',
|
||||
'setHealthBarColor',
|
||||
'setNameTemplate'
|
||||
}
|
||||
|
||||
exports(exportList)
|
||||
server_exports(exportList)
|
||||
|
||||
-- add files
|
||||
files {
|
||||
'template/template.lua'
|
||||
|
||||
@@ -42,6 +42,13 @@ setHealthBarColor = getTriggerFunction('sehc')
|
||||
setNameTemplate = getTriggerFunction('tpl')
|
||||
setName = getTriggerFunction('name')
|
||||
|
||||
exports('setComponentColor', setComponentColor)
|
||||
exports('setComponentAlpha', setComponentAlpha)
|
||||
exports('setComponentVisibility', setComponentVisibility)
|
||||
exports('setWantedLevel', setWantedLevel)
|
||||
exports('setHealthBarColor', setHealthBarColor)
|
||||
exports('setNameTemplate', setNameTemplate)
|
||||
|
||||
if not io then
|
||||
io = { write = nil, open = nil }
|
||||
end
|
||||
|
||||
@@ -142,9 +142,7 @@ local function getSettings(id)
|
||||
return mpGamerTagSettings[i]
|
||||
end
|
||||
|
||||
RegisterNetEvent('playernames:configure')
|
||||
|
||||
AddEventHandler('playernames:configure', function(id, key, ...)
|
||||
RegisterNetEvent('playernames:configure', function(id, key, ...)
|
||||
local args = table.pack(...)
|
||||
|
||||
if key == 'tglc' then
|
||||
@@ -183,7 +181,7 @@ AddEventHandler('onResourceStop', function(name)
|
||||
end
|
||||
end)
|
||||
|
||||
SetTimeout(0, function()
|
||||
CreateThread(function()
|
||||
TriggerServerEvent('playernames:init')
|
||||
end)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ local function detectUpdates()
|
||||
SetTimeout(500, detectUpdates)
|
||||
|
||||
local template = GetConvar('playerNames_template', '[{{id}}] {{name}}')
|
||||
|
||||
|
||||
if curTemplate ~= template then
|
||||
setNameTemplate(-1, template)
|
||||
|
||||
@@ -20,7 +20,7 @@ local function detectUpdates()
|
||||
local newTag = formatPlayerNameTag(v, template)
|
||||
if newTag ~= curTags[v] then
|
||||
setName(v, newTag)
|
||||
|
||||
|
||||
curTags[v] = newTag
|
||||
end
|
||||
end
|
||||
@@ -37,8 +37,7 @@ AddEventHandler('playerDropped', function()
|
||||
activePlayers[source] = nil
|
||||
end)
|
||||
|
||||
RegisterNetEvent('playernames:init')
|
||||
AddEventHandler('playernames:init', function()
|
||||
RegisterNetEvent('playernames:init', function()
|
||||
reconfigure(source)
|
||||
activePlayers[source] = true
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user