mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-30 01:38:52 +00:00
Override existing commands, and fixed rare bug with cb args all nil
This commit is contained in:
+77
-73
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user