diff --git a/server/events.lua b/server/events.lua index d543ca3..4b21014 100644 --- a/server/events.lua +++ b/server/events.lua @@ -2,51 +2,47 @@ AddEventHandler('playerDropped', function() local src = source - if QBCore.Players[src] then - local Player = QBCore.Players[src] - TriggerEvent('qb-log:server:CreateLog', 'joinleave', 'Dropped', 'red', '**' .. GetPlayerName(src) .. '** (' .. Player.PlayerData.license .. ') left..') - Player.Functions.Save() - _G.Player_Buckets[Player.PlayerData.license] = nil - QBCore.Players[src] = nil - end + if not QBCore.Players[src] then return end + local Player = QBCore.Players[src] + TriggerEvent('qb-log:server:CreateLog', 'joinleave', 'Dropped', 'red', '**' .. GetPlayerName(src) .. '** (' .. Player.PlayerData.license .. ') left..') + Player.Functions.Save() + Player_Buckets[Player.PlayerData.license] = nil + QBCore.Players[src] = nil end) -AddEventHandler('chatMessage', function(source, n, message) +AddEventHandler('chatMessage', function(source, _, message) local src = source - if string.sub(message, 1, 1) == '/' then - local args = QBCore.Shared.SplitStr(message, ' ') - local command = string.gsub(args[1]:lower(), '/', '') - CancelEvent() - if QBCore.Commands.List[command] then - local Player = QBCore.Functions.GetPlayer(src) - if Player then - local isGod = QBCore.Functions.HasPermission(src, 'god') - local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission) - local isPrincipal = IsPlayerAceAllowed(src, 'command') - table.remove(args, 1) - if isGod or hasPerm or isPrincipal then - if (QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and args[#QBCore.Commands.List[command].arguments] == nil) then - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.missing_args2'), 'error') - else - QBCore.Commands.List[command].callback(src, args) - end - else - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.no_access'), 'error') - end - end + if string.sub(message, 1, 1) ~= '/' then return end + local args = QBCore.Shared.SplitStr(message, ' ') + local command = string.gsub(args[1]:lower(), '/', '') + CancelEvent() + if not QBCore.Commands.List[command] then return end + local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end + local isGod = QBCore.Functions.HasPermission(src, 'god') + local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission) + local isPrincipal = IsPlayerAceAllowed(src, 'command') + table.remove(args, 1) + if isGod or hasPerm or isPrincipal then + if QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and not args[#QBCore.Commands.List[command].arguments] then + TriggerClientEvent('QBCore:Notify', src, Lang:t('error.missing_args2'), 'error') + else + QBCore.Commands.List[command].callback(src, args) end + else + TriggerClientEvent('QBCore:Notify', src, Lang:t('error.no_access'), 'error') end end) -- Player Connecting local function OnPlayerConnecting(name, setKickReason, deferrals) - local player = source + local src = source local license - local identifiers = GetPlayerIdentifiers(player) + local identifiers = GetPlayerIdentifiers(src) deferrals.defer() - -- mandatory wait! + -- Mandatory wait Wait(0) deferrals.update(string.format('Hello %s. Validating Your Rockstar License', name)) @@ -58,12 +54,12 @@ local function OnPlayerConnecting(name, setKickReason, deferrals) end end - -- mandatory wait! + -- Mandatory wait Wait(2500) deferrals.update(string.format('Hello %s. We are checking if you are banned.', name)) - local isBanned, Reason = QBCore.Functions.IsPlayerBanned(player) + local isBanned, Reason = QBCore.Functions.IsPlayerBanned(src) local isLicenseAlreadyInUse = QBCore.Functions.IsLicenseInUse(license) Wait(2500) @@ -81,7 +77,7 @@ local function OnPlayerConnecting(name, setKickReason, deferrals) Wait(1000) TriggerEvent('connectqueue:playerConnect', name, setKickReason, deferrals) end - --Add any additional defferals you may need! + -- Add any additional defferals you may need! end AddEventHandler('playerConnecting', OnPlayerConnecting) @@ -90,8 +86,8 @@ AddEventHandler('playerConnecting', OnPlayerConnecting) RegisterNetEvent('QBCore:server:CloseServer', function(reason) local src = source - if QBCore.Functions.HasPermission(src, 'admin') or QBCore.Functions.HasPermission(src, 'god') then - local reason = reason or 'No reason specified' + if QBCore.Functions.HasPermission(src, 'admin') then + reason = reason or 'No reason specified' QBCore.Config.Server.closed = true QBCore.Config.Server.closedReason = reason else @@ -101,7 +97,7 @@ end) RegisterNetEvent('QBCore:server:OpenServer', function() local src = source - if QBCore.Functions.HasPermission(src, 'admin') or QBCore.Functions.HasPermission(src, 'god') then + if QBCore.Functions.HasPermission(src, 'admin') then QBCore.Config.Server.closed = false else QBCore.Functions.Kick(src, 'You don\'t have permissions for this..', nil, nil) @@ -122,20 +118,19 @@ end) RegisterNetEvent('QBCore:UpdatePlayer', function() local src = source local Player = QBCore.Functions.GetPlayer(src) - if Player then - local newHunger = Player.PlayerData.metadata['hunger'] - QBCore.Config.Player.HungerRate - local newThirst = Player.PlayerData.metadata['thirst'] - QBCore.Config.Player.ThirstRate - if newHunger <= 0 then - newHunger = 0 - end - if newThirst <= 0 then - newThirst = 0 - end - Player.Functions.SetMetaData('thirst', newThirst) - Player.Functions.SetMetaData('hunger', newHunger) - TriggerClientEvent('hud:client:UpdateNeeds', src, newHunger, newThirst) - Player.Functions.Save() + if not Player then return end + local newHunger = Player.PlayerData.metadata['hunger'] - QBCore.Config.Player.HungerRate + local newThirst = Player.PlayerData.metadata['thirst'] - QBCore.Config.Player.ThirstRate + if newHunger <= 0 then + newHunger = 0 end + if newThirst <= 0 then + newThirst = 0 + end + Player.Functions.SetMetaData('thirst', newThirst) + Player.Functions.SetMetaData('hunger', newHunger) + TriggerClientEvent('hud:client:UpdateNeeds', src, newHunger, newThirst) + Player.Functions.Save() end) RegisterNetEvent('QBCore:Server:SetMetaData', function(meta, data) @@ -155,6 +150,7 @@ end) RegisterNetEvent('QBCore:ToggleDuty', function() local src = source local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end if Player.PlayerData.job.onduty then Player.Functions.SetJobDuty(false) TriggerClientEvent('QBCore:Notify', src, Lang:t('info.off_duty')) @@ -169,22 +165,21 @@ end) RegisterNetEvent('QBCore:Server:UseItem', function(item) local src = source - if item and item.amount > 0 then - if QBCore.Functions.CanUseItem(item.name) then - QBCore.Functions.UseItem(src, item) - end - end + if not item or item.amount <= 0 or not QBCore.Functions.CanUseItem(item.name) then return end + QBCore.Functions.UseItem(src, item) end) RegisterNetEvent('QBCore:Server:RemoveItem', function(itemName, amount, slot) local src = source local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end Player.Functions.RemoveItem(itemName, amount, slot) end) RegisterNetEvent('QBCore:Server:AddItem', function(itemName, amount, slot, info) local src = source local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end Player.Functions.AddItem(itemName, amount, slot, info) end) @@ -192,83 +187,74 @@ end) RegisterNetEvent('QBCore:CallCommand', function(command, args) local src = source - if QBCore.Commands.List[command] then - local Player = QBCore.Functions.GetPlayer(src) - if Player then - local isGod = QBCore.Functions.HasPermission(src, 'god') - local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission) - local isPrincipal = IsPlayerAceAllowed(src, 'command') - if (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) or isGod or hasPerm or isPrincipal then - if (QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and args[#QBCore.Commands.List[command].arguments] == nil) then - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.missing_args2'), 'error') - else - QBCore.Commands.List[command].callback(src, args) - end - else - TriggerClientEvent('QBCore:Notify', src, Lang:t('error.no_access'), 'error') - end + if not QBCore.Commands.List[command] then return end + local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end + local isGod = QBCore.Functions.HasPermission(src, 'god') + local hasPerm = QBCore.Functions.HasPermission(src, QBCore.Commands.List[command].permission) + local isPrincipal = IsPlayerAceAllowed(src, 'command') + if (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) or isGod or hasPerm or isPrincipal then + if QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and not args[#QBCore.Commands.List[command].arguments] then + TriggerClientEvent('QBCore:Notify', src, Lang:t('error.missing_args2'), 'error') + else + QBCore.Commands.List[command].callback(src, args) end + else + TriggerClientEvent('QBCore:Notify', src, Lang:t('error.no_access'), 'error') end end) -- Has Item Callback (can also use client function - QBCore.Functions.HasItem(item)) QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, amount) - local src = source local retval = false - local Player = QBCore.Functions.GetPlayer(src) - if Player then - if type(items) == 'table' then - local count = 0 - local finalcount = 0 - for k, v in pairs(items) do - if type(k) == 'string' then - finalcount = 0 - for i, _ in pairs(items) do - if i then - finalcount = finalcount + 1 - end - end - local item = Player.Functions.GetItemByName(k) - if item then - if item.amount >= v then - count = count + 1 - if count == finalcount then - retval = true - end - end - end - else - finalcount = #items - local item = Player.Functions.GetItemByName(v) - if item then - if amount then - if item.amount >= amount then - count = count + 1 - if count == finalcount then - retval = true - end - end - else - count = count + 1 - if count == finalcount then - retval = true - end + local Player = QBCore.Functions.GetPlayer(source) + if not Player then return cb(false) end + if type(items) == 'table' then + local count = 0 + local finalcount = 0 + for k, v in pairs(items) do + if type(k) == 'string' then + finalcount = 0 + for i, _ in pairs(items) do finalcount += 1 end + local item = Player.Functions.GetItemByName(k) + if item then + if item.amount >= v then + count += 1 + if count == finalcount then + retval = true end end end + else + finalcount = #items + local item = Player.Functions.GetItemByName(v) + if item then + if amount then + if item.amount >= amount then + count += 1 + if count == finalcount then + retval = true + end + end + else + count += 1 + if count == finalcount then + retval = true + end + end + end + end + end + else + local item = Player.Functions.GetItemByName(items) + if not item then return cb(false) end + if amount then + if item.amount >= amount then + retval = true end else - local item = Player.Functions.GetItemByName(items) - if item then - if amount then - if item.amount >= amount then - retval = true - end - else - retval = true - end - end + retval = true end end cb(retval)