Jump to content

Execute commands


Dzsozi (h03)

Recommended Posts

Posted

Hello community! I made my custom chat system, but I have one problem. I can't execute commands, for example /start resourcename. The chat outputs it like a simple text, everyone can see it, but nothing happens. So my question is how can I make it to execute the command that I wrote in the custom chat?

Here's my code:

  
function sendMessageToAllPlayers(thePlayer, cmd, ...) 
    local message = table.concat ( { ... }, " " ) 
    local nr, ng, nb = getPlayerNametagColor(thePlayer) 
    local name = getPlayerName(thePlayer) 
    if isTimer(antiSpam[thePlayer]) then 
        sendServerMessage("Másodpercenként egy üzenetet küldhetsz.", thePlayer) 
    else 
        outputChatBox("#48D4A3[GLOBAL]" .. RGBToHex(nr, ng, nb) .. " " .. name .. "#FFFFFF: " .. message, getRootElement(), 255, 255, 255, true ) 
        outputServerLog("[GLOBAL]: ".. name ..": "..message) 
        antiSpam[thePlayer] = setTimer(function(thePlayer) antiSpam[thePlayer] = nil end, 1000, 1, thePlayer) 
    end 
end 
addCommandHandler("Global",sendMessageToAllPlayers) 
  

P.S.: Sorry for the bad English if there was something written wrong, I'm from Hungary, but I hope that I was understandable!

Posted

Here's the full code:

function sendMessageToAllPlayers(thePlayer, cmd, ...) 
    local message = table.concat ( { ... }, " " ) 
    local nr, ng, nb = getPlayerNametagColor(thePlayer) 
    local name = getPlayerName(thePlayer) 
    if isTimer(antiSpam[thePlayer]) then 
        sendServerMessage("Másodpercenként egy üzenetet küldhetsz.", thePlayer) 
    else 
        outputChatBox("#48D4A3[GLOBAL]" .. RGBToHex(nr, ng, nb) .. " " .. name .. "#FFFFFF: " .. message, getRootElement(), 255, 255, 255, true ) 
        outputServerLog("[GLOBAL]: ".. name ..": "..message) 
        antiSpam[thePlayer] = setTimer(function(thePlayer) antiSpam[thePlayer] = nil end, 1000, 1, thePlayer) 
    end 
end 
addCommandHandler("Global",sendMessageToAllPlayers) 
  
function blockChatMessage(message) 
    cancelEvent() 
end 
addEventHandler("onPlayerChat", getRootElement(), blockChatMessage) 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
    bindKey(source,"x","down","chatbox","Global") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player,"x","down","chatbox","Global") 
    end 
end) 

Posted

Seems you are talking about the default MTA Chatbox, which is already able to execute commands.

Obivously this wont work for a custom input since its output via outputChatBox.

You could check for "/" as first character and then cancel it, so nothing happens at all.

Why people have to be able to execute commands in the Global Chat anyway?

Also, use getTickCount instead of Timers.

Posted

You could check for "/" as first character and then cancel it, so nothing happens at all.

Why people have to be able to execute commands in the Global Chat anyway?

1. You gave me an idea, but I don't know how to search for "/" as the first typed character. Because after this, I could execute it with executeCommandHandler function.

2. I don't want that commands will appear in the global chat. So people who made a mistake, don't have to afraid that the command is not working.

Posted

there are variuos ways to do it, like

local msgTable =  { ... } 
local message = table.concat ( msgTable,  " " ) 
local first = msgTable[1] 
if string.sub(first,1,1) == "/" then 
    local command = string.sub(first,2,string.len(first)) 
    --execute 
end 

Posted (edited)

Thank you! You helped me alot!

But, I made more functions, I made the local chat and made this command thingy for the local chat first, because this is gonna be the main chat. But I have one problem. If I write in any command, for example /debugscript 3 then nothing happens, I've added the resource to acl admin group, and debugscript doesn't show me errors.

Here's the current function:

  
local distance = 30 
  
function sendMessageToNearbyPlayers(thePlayer, cmd, ...) 
    local x, y, z = getElementPosition( thePlayer ) 
    local nr, ng, nb = getPlayerNametagColor(thePlayer) 
    local name = getPlayerName(thePlayer) 
    local affectedPlayers = { } 
    local msgTable =  { ... } 
    local message = table.concat ( msgTable,  " " ) 
    local first = msgTable[1] 
  
    if getElementData(thePlayer, "chat:spam") == 1 then 
        sendServerMessage("Másodpercenként egy üzenetet küldhetsz.", thePlayer) 
    else 
        if string.sub(first,1,1) == "/" then 
            local command = string.sub(first,2,string.len(first)) 
            executeCommandHandler( command, thePlayer ) 
        else 
            local shownto = 0 
            for index, nearbyPlayer in ipairs(getElementsByType("player")) do 
                if isElement(nearbyPlayer) and getDistanceBetweenPoints3D(x, y, z, getElementPosition(nearbyPlayer)) < ( distance or 20 ) then 
                    if not isPedDead(nearbyPlayer) and getElementDimension(thePlayer) == getElementDimension(nearbyPlayer) then 
                        outputChatBox("#F58C14[LOCAL]" .. RGBToHex(nr, ng, nb) .. " " .. name .. "#FFFFFF: " .. message, nearbyPlayer, 255, 255, 255, true ) 
                        outputServerLog("[LOCAL]: ".. name ..": "..message) 
                        table.insert(affectedPlayers, nearbyPlayer) 
                        shownto = shownto + 1 
                    end 
                end 
            end 
            setElementData(thePlayer, "chat:spam", 1) 
            setTimer(function(thePlayer) setElementData(thePlayer, "chat:spam", 0) end, 1000, 1, thePlayer) 
        end 
    end 
end 
addCommandHandler("Local",sendMessageToNearbyPlayers) 
  

What could be the problem?

EDIT: I've noticed that the commands are not working only with more arguments. I made a command to get the online admins list, and it's command is /admins. If i write it in the local chat then it outputs the admins, so the problem is with the more arguments, I think. For example there's one more parameter in the debugscript command, in this case the 3. So how could I fix this?

EDIT 2: UPDATED THE CODE!

Edited by Guest
Posted

Nothing is wrong with just providing next arguements. So it's simple as

executeCommandHandler( command, thePlayer, msgTable[2], msgTable[3], msgTable[4] ) 

Posted

Thanks! And is there any way to get the count of the arguments? So you don't have to make this:

  
msgTable[2], msgTable[3] ... 
  

but this:

  
msgTalbe[count] -- or something like this 
  

How is this possible?

Posted

So by that you mean there's no fix for this problem? So I can't make a custom chat that supports commands? Or what would be the best way to make custom chats that supports commands?

Posted

Yeah my bad, it would look like this

local msgTable =  { ... } 
table.remove(msgTable ,1) 
local argsWithSpaces = table.concat ( msgTable,  " " ) 
  
executeCommandHandler( command, thePlayer, argsWithSpaces ) 

Tho you can not execute hardcoded commands like debugscript, reconnect etc.

You can still get around this detecting what was the command and use functions to workaround:

Like if it was /debugscript, set it in your scripts and then create debug view like you did with your new chat system and hook it on onDebugMessage

And like if it was /reconnect use redirectPlayer to your own server, should work

Posted

It's kind of wasted tho, the onPlayerChat event already has all this functionality built in and the main reason for making custom chats are usually the look and feel and maybe a different key, instead of just doing cancelEvent() in there you could also add the code from "globalchat" into it, that would cancel regular chat outputs and show your custom outputs and still allow commands to be sent through, remember, you're making a custom chat system, not a custom command handler.

Posted
It's kind of wasted tho, the onPlayerChat event already has all this functionality built in and the main reason for making custom chats are usually the look and feel and maybe a different key, instead of just doing cancelEvent() in there you could also add the code from "globalchat" into it, that would cancel regular chat outputs and show your custom outputs and still allow commands to be sent through, remember, you're making a custom chat system, not a custom command handler.

Well there is actually something that is really missing, ability to send same message just by tapping arrow up on input box. I would include this ability in custom chat system

Posted
Well there is actually something that is really missing, ability to send same message just by tapping arrow up on input box. I would include this ability in custom chat system

Good point, especially after working in a terminal for a while, I surely miss that feature.

Posted

Depends.

The console got it since its mostly used for commands.

The chatbox is used for chatting. I don't know if that really makes sense.

Posted

I have to get back to this post. I've found a problem that I can't solve and can't manage to find out what's the problem. If I write for example say asd in console then I get these errors in debugscript:

qG8MFxH.png

What is this? And how could I solve this problem?

Here's my code (not the full, but the part that could contain the errors):

  
local localChatDistance = 30 
local time = getRealTime()  
local hours = time.hour  
local minutes = time.minute  
local seconds = time.second 
  
function timeFormat() 
    local realtime = getRealTime() 
    return 
    string.format("%02d:%02d:%02d", realtime.hour, realtime.minute, realtime.second) 
end 
  
function sendMessageToNearbyPlayers(thePlayer, cmd, ...) 
    local x, y, z = getElementPosition( thePlayer ) 
    local nr, ng, nb = getPlayerNametagColor(thePlayer) 
    local name = getPlayerName(thePlayer) 
     
    local affectedPlayers = { } 
    local msgTable =  { ... } 
    local message = table.concat ( msgTable,  " " ) 
    local message = string.gsub ( message, '#%x%x%x%x%x%x', '' ) 
    local chatTime = timeFormat() 
    local first = msgTable[1] 
    table.remove(msgTable, 1) 
     
    if getElementData(thePlayer, "chat:spam") == 1 then 
        sendServerMessage("Másodpercenként egy üzenetet küldhetsz.", thePlayer) 
    else 
        if string.sub(first,1,1) == "/" then 
            if getElementData(thePlayer, "chat.command:spam") == 1 then 
                sendServerMessage("Másodpercenként egy parancsot használhatsz.", thePlayer) 
            else 
                local command = string.sub(first, 2, string.len(first)) 
                local arguments = table.concat(msgTable, " ") 
                executeCommandHandler( command, thePlayer, arguments ) 
                triggerClientEvent(thePlayer, "executeClientCommand", thePlayer, command, arguments) 
                setElementData(thePlayer, "chat.command:spam", 1) 
                setTimer(function(thePlayer) setElementData(thePlayer, "chat.command:spam", 0) end, 1000, 1, thePlayer) 
                outputServerLog("[LOCAL][PARANCS]: ".. name ..": /"..command.." "..arguments) 
            end 
        else 
            local shownto = 0 
            for index, nearbyPlayer in ipairs(getElementsByType("player")) do 
                if isElement(nearbyPlayer) and getDistanceBetweenPoints3D(x, y, z, getElementPosition(nearbyPlayer)) < ( localChatDistance or 20 ) then 
                    if not isPedDead(nearbyPlayer) and getElementDimension(thePlayer) == getElementDimension(nearbyPlayer) then 
                        outputChatBox("[" .. chatTime .. "] " .. "[LOCAL]" .. RGBToHex(nr, ng, nb) .. " " .. name .. "#FFFFFF: " .. message, nearbyPlayer, 245, 140, 20, true ) 
                        outputServerLog("[LOCAL]: ".. name ..": "..message) 
                        table.insert(affectedPlayers, nearbyPlayer) 
                        shownto = shownto + 1 
                    end 
                end 
            end 
            setElementData(thePlayer, "chat:spam", 1) 
            setTimer(function(thePlayer) setElementData(thePlayer, "chat:spam", 0) end, 1000, 1, thePlayer) 
        end 
    end 
end 
addCommandHandler("Local",sendMessageToNearbyPlayers) 
  
function blockChatMessage(message) 
    cancelEvent() 
    sendMessageToNearbyPlayers(message) 
end 
addEventHandler("onPlayerChat", getRootElement(), blockChatMessage) 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
    bindKey(source,"t","down","chatbox","Local") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
    for index, player in pairs(getElementsByType("player")) do 
        bindKey(player,"t","down","chatbox","Local") 
    end 
end) 
  
addEventHandler("onPlayerQuit",getRootElement(), 
function () 
    unbindKey(source,"t","down","chatbox","Local") 
end) 
  
addEventHandler("onResourceStop",getResourceRootElement(getThisResource()), 
function () 
    for index, player in pairs(getElementsByType("player")) do 
        unbindKey(player,"t","down","chatbox","Local") 
    end 
end) 
  

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...