5.7 KiB
title | layout | root | idx | description | redirect_from | cmd_online | cb_cmdsprivs | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chat and Commands | default | ../.. | 4.2 | Registering a chatcommand and handling chat messages with register_on_chat_message | /en/chapters/chat.html |
|
|
Introduction
Mods can interact with player chat, including sending messages, intercepting messages and registering chat commands.
- Sending Messages to All Players
- Sending Messages to Specific Players
- Chat Commands
- Complex Subcommands
- Intercepting Messages
Sending Messages to All Players
To send a message to every player in the game, call the chat_send_all function.
{% highlight lua %} minetest.chat_send_all("This is a chat message to all players") {% endhighlight %}
Here is an example of how this appears in-game:
<player1> Look at this entrance
This is a chat message to all players
<player2> What about it?
The message appears on a separate line to distinguish it from in-game player chat.
Sending Messages to Specific Players
To send a message to a specific player, call the chat_send_player function:
{% highlight lua %} minetest.chat_send_player("player1", "This is a chat message for player1") {% endhighlight %}
This message displays in the same manner as messages to all players, but is only visible to the named player, in this case player1.
Chat Commands
To register a chat command, for example /foo, use register_chatcommand:
{% highlight lua %} minetest.register_chatcommand("foo", { privs = { interact = true }, func = function(name, param) return true, "You said " .. param .. "!" end }) {% endhighlight %}
Calling /foo bar will display You said bar!
in the chat console.
You can restrict which players are able to run commands:
{% highlight lua %} privs = { interact = true }, {% endhighlight %}
This means only players with the interact
privilege can run the
command. Other players will see an error message informing them of which
privilege they're missing. If the player has the necessary privileges, the command
will run and the message will be sent:
{% highlight lua %} return true, "You said " .. param .. "!" {% endhighlight %}
This returns two values, a Boolean which shows the command succeeded and the chat message to send to the player.
{% include notice.html notice=page.cmd_online %}
Complex Subcommands
It is often required to make complex chat commands, such as:
/msg <to> <message>
/team join <teamname>
/team leave <teamname>
/team list
This is usually done using Lua patterns. Patterns are a way of extracting stuff from text using rules.
{% highlight lua %} local to, msg = string.match(param, "^([%a%d_-]+) (*+)$") {% endhighlight %}
The above implements /msg <to> <message>
. Let's go through left to right:
^
means match the start of the string.()
is a matching group - anything that matches stuff in here will be returned from string.match.[]
means accept characters in this list.%a
means accept any letter and%d
means any digit.[%d%a_-]
means accept any letter or digit or_
or-
.+
means match the last thing one or more times.*
means match any character in this context.$
means match the end of the string.
Put simply, this matches the name (a word with only letters/numbers/-/_), then a space, then the message (one of more of any character). The name and message are returned, as they're surrounded in parentheses.
That's how most mods implement complex chat commands. A better guide to Lua Patterns would probably be the lua-users.org tutorial or the PIL documentation.
There is also a library written by the author of this book which can be used to make complex chat commands without Patterns called ChatCmdBuilder.
Intercepting Messages
To intercept a message, use register_on_chat_message:
{% highlight lua %} minetest.register_on_chat_message(function(name, message) print(name .. " said " .. message) return false end) {% endhighlight %}
By returning false, you allow the chat message to be sent by the default
handler. You can actually remove the line return false
, and it would still
work the same.
{% include notice.html notice=page.cb_cmdsprivs %}
You should make sure you take into account that it may be a chat command,
or the user may not have shout
.
{% highlight lua %} minetest.register_on_chat_message(function(name, message) if message:sub(1, 1) == "/" then print(name .. " ran chat command") elseif minetest.check_player_privs(name, { shout = true }) then print(name .. " said " .. message) else print(name .. " tried to say " .. message .. " but doesn't have shout") end
return false
end) {% endhighlight %}