From 4b648a8bef4bd897891abd5508d5f4e5a1f63331 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Mon, 17 Feb 2020 22:48:44 +0100 Subject: [PATCH] Override existing commands, and fixed rare bug with cb args all nil --- server/functions.lua | 150 ++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 73 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 6cfd5860..e9f0d5be 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -30,103 +30,107 @@ ESX.RegisterCommand = function(name, group, cb, allowConsole, suggestion) end if ESX.RegisteredCommands[name] then - print(('[es_extended] [^3WARNING^7] An command "%s" is already registered'):format(name)) - else - if suggestion then - if not suggestion.arguments then suggestion.arguments = {} end - if not suggestion.help then suggestion.help = '' end - end + print(('[es_extended] [^3WARNING^7] An command "%s" is already registered, overriding command'):format(name)) + end - ESX.RegisteredCommands[name] = {group = group, cb = cb, allowConsole = allowConsole, suggestion = suggestion} + if suggestion then + if not suggestion.arguments then suggestion.arguments = {} end + if not suggestion.help then suggestion.help = '' end + end - RegisterCommand(name, function(playerId, args, rawCommand) - local command = ESX.RegisteredCommands[name] + ESX.RegisteredCommands[name] = {group = group, cb = cb, allowConsole = allowConsole, suggestion = suggestion} - if not command.allowConsole and playerId == 0 then - print('[es_extended] [^3WARNING^7] That command can not be run from console') - else - local xPlayer, error = ESX.GetPlayerFromId(playerId), nil + RegisterCommand(name, function(playerId, args, rawCommand) + local command = ESX.RegisteredCommands[name] - if command.suggestion then - if command.suggestion.validate then - if #args ~= #command.suggestion.arguments then - error = ('Argument count mismatch (passed %s, wanted %s)'):format(#args, #command.suggestion.arguments) - end + if not command.allowConsole and playerId == 0 then + print('[es_extended] [^3WARNING^7] That command can not be run from console') + else + local xPlayer, error = ESX.GetPlayerFromId(playerId), nil + + if command.suggestion then + if command.suggestion.validate then + if #args ~= #command.suggestion.arguments then + error = ('Argument count mismatch (passed %s, wanted %s)'):format(#args, #command.suggestion.arguments) end + end - if not error and command.suggestion.arguments then - local newArgs = {} + if not error and command.suggestion.arguments then + local newArgs = {} - for k,v in ipairs(command.suggestion.arguments) do - if v.type then - if v.type == 'number' then - local newArg = tonumber(args[k]) + for k,v in ipairs(command.suggestion.arguments) do + if v.type then + if v.type == 'number' then + local newArg = tonumber(args[k]) - if newArg then - newArgs[v.name] = newArg - else - error = ('Argument #%s type mismatch (passed string, wanted number)'):format(k) - end - elseif v.type == 'player' then - local targetPlayer = tonumber(args[k]) + if newArg then + newArgs[v.name] = newArg + else + error = ('Argument #%s type mismatch (passed string, wanted number)'):format(k) + end + elseif v.type == 'player' or v.type == 'playerId' then + local targetPlayer = tonumber(args[k]) - if targetPlayer then - local xTargetPlayer = ESX.GetPlayerFromId(targetPlayer) + if targetPlayer then + local xTargetPlayer = ESX.GetPlayerFromId(targetPlayer) - if xTargetPlayer then + if xTargetPlayer then + if v.type == 'player' then newArgs[v.name] = xTargetPlayer else - error = 'Player not online' + newArgs[v.name] = targetPlayer end else - error = ('Argument #%s type mismatch (passed string, wanted number)'):format(k) + error = 'Player not online' end - elseif v.type == 'string' then - newArgs[v.name] = args[k] - elseif v.type == 'item' then - if ESX.Items[args[k]] then - newArgs[v.name] = args[k] - else - error = _U('invalid_item') - end - elseif v.type == 'weapon' then - if ESX.GetWeapon(args[k]) then - newArgs[v.name] = string.upper(args[k]) - else - error = 'Invalid weapon' - end - elseif v.type == 'any' then - newArgs[v.name] = args[k] + else + error = ('Argument #%s type mismatch (passed string, wanted number)'):format(k) end + elseif v.type == 'string' then + newArgs[v.name] = args[k] + elseif v.type == 'item' then + if ESX.Items[args[k]] then + newArgs[v.name] = args[k] + else + error = _U('invalid_item') + end + elseif v.type == 'weapon' then + if ESX.GetWeapon(args[k]) then + newArgs[v.name] = string.upper(args[k]) + else + error = 'Invalid weapon' + end + elseif v.type == 'any' then + newArgs[v.name] = args[k] end - - if error then break end end - args = newArgs + if error then break end end - end - if error then - if playerId == 0 then - print(('[es_extended] [^3WARNING^7] %s^7'):format(error)) - else - xPlayer.triggerEvent('chat:addMessage', {args = {'^1SYSTEM', error}}) - end - else - cb(xPlayer, args, function(msg) - if playerId == 0 then - print(('[es_extended] [^3WARNING^7] %s^7'):format(msg)) - else - xPlayer.triggerEvent('chat:addMessage', {args = {'^1SYSTEM', msg}}) - end - end) + args = newArgs end end - end, true) - ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, name)) - end + if error then + if playerId == 0 then + print(('[es_extended] [^3WARNING^7] %s^7'):format(error)) + else + xPlayer.triggerEvent('chat:addMessage', {args = {'^1SYSTEM', error}}) + end + else + cb(xPlayer or false, args, function(msg) + if playerId == 0 then + print(('[es_extended] [^3WARNING^7] %s^7'):format(msg)) + else + xPlayer.triggerEvent('chat:addMessage', {args = {'^1SYSTEM', msg}}) + end + end) + end + end + end, true) + + ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, name)) end ESX.ClearTimeout = function(id)