diff --git a/resources/[system]/chat/cl_chat.lua b/resources/[system]/chat/cl_chat.lua index 2c5a653..7173dbd 100644 --- a/resources/[system]/chat/cl_chat.lua +++ b/resources/[system]/chat/cl_chat.lua @@ -5,6 +5,7 @@ RegisterNetEvent('chatMessage') RegisterNetEvent('chat:addTemplate') RegisterNetEvent('chat:addMessage') RegisterNetEvent('chat:addSuggestion') +RegisterNetEvent('chat:addSuggestions') RegisterNetEvent('chat:removeSuggestion') RegisterNetEvent('chat:clear') @@ -35,7 +36,7 @@ AddEventHandler('__cfx_internal:serverPrint', function(msg) SendNUIMessage({ type = 'ON_MESSAGE', message = { - color = { 0, 0, 0 }, + templateId = 'print', multiline = true, args = { msg } } @@ -60,6 +61,15 @@ AddEventHandler('chat:addSuggestion', function(name, help, params) }) end) +AddEventHandler('chat:addSuggestions', function(suggestions) + for _, suggestion in ipairs(suggestions) do + SendNUIMessage({ + type = 'ON_SUGGESTION_ADD', + suggestion = suggestion + }) + end +end) + AddEventHandler('chat:removeSuggestion', function(name) SendNUIMessage({ type = 'ON_SUGGESTION_REMOVE', @@ -103,9 +113,36 @@ RegisterNUICallback('chatResult', function(data, cb) cb('ok') end) +local function refreshCommands() + if GetRegisteredCommands then + local registeredCommands = GetRegisteredCommands() + + local suggestions = {} + + for _, command in ipairs(registeredCommands) do + if IsAceAllowed(('command.%s'):format(command.name)) then + table.insert(suggestions, { + name = '/' .. command.name, + help = '' + }) + end + end + + TriggerEvent('chat:addSuggestions', suggestions) + end +end + +AddEventHandler('onClientResourceStart', function(resName) + Wait(500) + + refreshCommands() +end) + RegisterNUICallback('loaded', function(data, cb) TriggerServerEvent('chat:init'); + refreshCommands() + cb('ok') end) diff --git a/resources/[system]/chat/html/App.js b/resources/[system]/chat/html/App.js index 7cb1fbb..c3ade18 100644 --- a/resources/[system]/chat/html/App.js +++ b/resources/[system]/chat/html/App.js @@ -68,6 +68,9 @@ window.APP = { if (!suggestion.params) { suggestion.params = []; //TODO Move somewhere else } + if (this.suggestions.find(a => a.name == suggestion.name)) { + return; + } this.suggestions.push(suggestion); }, ON_SUGGESTION_REMOVE({ name }) { diff --git a/resources/[system]/chat/html/config.default.js b/resources/[system]/chat/html/config.default.js index 7e57105..6c8a820 100644 --- a/resources/[system]/chat/html/config.default.js +++ b/resources/[system]/chat/html/config.default.js @@ -6,6 +6,7 @@ window.CONFIG = { templates: { //You can add static templates here 'default': '{0}: {1}', 'defaultAlt': '{0}', + 'print': '
{0}
', 'example:important': '

^2{0}

' }, fadeTimeout: 7000, diff --git a/resources/[system]/chat/sv_chat.lua b/resources/[system]/chat/sv_chat.lua index bc8a6e1..a619f85 100644 --- a/resources/[system]/chat/sv_chat.lua +++ b/resources/[system]/chat/sv_chat.lua @@ -34,7 +34,7 @@ AddEventHandler('__cfx_internal:commandFallback', function(command) end) -- player join messages -AddEventHandler('playerConnecting', function() +AddEventHandler('chat:init', function() TriggerClientEvent('chatMessage', -1, '', { 255, 255, 255 }, '^2* ' .. GetPlayerName(source) .. ' joined.') end) @@ -44,4 +44,36 @@ end) RegisterCommand('say', function(source, args, rawCommand) TriggerClientEvent('chatMessage', -1, (source == 0) and 'console' or GetPlayerName(source), { 255, 255, 255 }, rawCommand:sub(5)) +end) + +-- command suggestions for clients +local function refreshCommands(player) + if GetRegisteredCommands then + local registeredCommands = GetRegisteredCommands() + + local suggestions = {} + + for _, command in ipairs(registeredCommands) do + if IsPlayerAceAllowed(player, ('command.%s'):format(command.name)) then + table.insert(suggestions, { + name = '/' .. command.name, + help = '' + }) + end + end + + TriggerClientEvent('chat:addSuggestions', player, suggestions) + end +end + +AddEventHandler('chat:init', function() + refreshCommands(source) +end) + +AddEventHandler('onServerResourceStart', function(resName) + Wait(500) + + for _, player in ipairs(GetPlayers()) do + refreshCommands(player) + end end) \ No newline at end of file