2015-09-25 02:00:15 +03:00
|
|
|
---
|
2015-09-25 02:01:52 +03:00
|
|
|
title: Chat and Commands
|
2015-09-25 02:00:15 +03:00
|
|
|
layout: default
|
2017-08-26 18:40:30 +03:00
|
|
|
root: ../../
|
2015-09-25 02:00:15 +03:00
|
|
|
---
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
Mods can interact with player chat, including
|
2015-09-25 02:00:15 +03:00
|
|
|
sending messages, intercepting messages and registering chat commands.
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
* [Sending messages to all players](#sending-messages-to-all-players)
|
2017-08-29 14:17:21 +03:00
|
|
|
* [Sending messages to specific players](#sending-messages-to-specific-players)
|
2017-08-29 13:41:24 +03:00
|
|
|
* [Chat commands](#chat-commands)
|
|
|
|
* [Complex subcommands](#complex-subcommands)
|
|
|
|
* [Intercepting messages](#intercepting-messages)
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
## Sending messages to all players
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
To send a message to every player in the game, call the chat_send_all function.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.chat_send_all("This is a chat message to all players")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
Here is an example of how this appears in-game:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-26 21:01:51 +03:00
|
|
|
<player1> Look at this entrance
|
|
|
|
This is a chat message to all players
|
|
|
|
<player2> What about it?
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
The message appears on a separate line to distinguish it from in-game player chat.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
## Sending messages to specific players
|
|
|
|
|
|
|
|
To send a message to a specific player, call the chat_send_player function:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.chat_send_player("player1", "This is a chat message for player1")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
This message displays in the same manner as messages to all players, but is
|
|
|
|
only visible to the named player, in this case player1.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2015-09-25 02:09:34 +03:00
|
|
|
### Older mods
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
Occasionally you'll see mods where the chat_send_player function includes a
|
|
|
|
boolean:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.chat_send_player("player1", "This is a server message", true)
|
2015-09-25 02:09:34 +03:00
|
|
|
minetest.chat_send_player("player1", "This is a server message", false)
|
2015-09-25 02:00:15 +03:00
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
The boolean is no longer used, and has no affect
|
2016-04-09 16:27:06 +03:00
|
|
|
<sup>[[commit]](https://github.com/minetest/minetest/commit/9a3b7715e2c2390a3a549d4e105ed8c18defb228)</sup>.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
## Chat commands
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
To register a chat command, for example /foo, use register_chatcommand:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.register_chatcommand("foo", {
|
2017-08-26 21:01:51 +03:00
|
|
|
privs = {
|
|
|
|
interact = true
|
|
|
|
},
|
|
|
|
func = function(name, param)
|
|
|
|
return true, "You said " .. param .. "!"
|
|
|
|
end
|
2015-09-25 02:00:15 +03:00
|
|
|
})
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
Calling /foo bar will display `You said bar!` in the chat console.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
You can restrict which players are able to run commands:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
privs = {
|
2017-08-26 21:01:51 +03:00
|
|
|
interact = true
|
2015-09-25 02:00:15 +03:00
|
|
|
},
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
This means only players with the `interact` [privilege](privileges.html) 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:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
return true, "You said " .. param .. "!"
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
This returns two values, a boolean which shows the command succeeded
|
|
|
|
and the chat message to send to the player.
|
|
|
|
|
|
|
|
A player name, instead of a player object, is passed because
|
|
|
|
**the player might not actually be in-game, but may be running commands from IRC**.
|
|
|
|
Due to this, you should not assume `minetest.get_player_by_name`, or any other
|
|
|
|
function that requires an in-game player, will work in a chat command call back.
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
`minetest.show_formspec` also won't work when a command is run from IRC, so you
|
|
|
|
should provide a text only version. For example, the email mod allows both `/inbox`
|
|
|
|
to show a formspec, and `/inbox text` to send information to chat.
|
2016-01-08 17:01:10 +03:00
|
|
|
|
2016-06-17 03:08:44 +03:00
|
|
|
## Complex subcommands
|
|
|
|
|
|
|
|
It is often required to make complex chat commands, such as:
|
|
|
|
|
|
|
|
* /msg <name> <message>
|
|
|
|
* /team join <teamname>
|
|
|
|
* /team leave <teamname>
|
|
|
|
* /team list
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
Many mods implement this using Lua patterns; however, a much easier
|
|
|
|
approach is to use a mod library. See rubenwardy's
|
|
|
|
[Complex Chat Commands](chat_complex.html).
|
2016-06-17 03:08:44 +03:00
|
|
|
|
|
|
|
|
2015-09-25 02:00:15 +03:00
|
|
|
## Intercepting messages
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
To intercept a message, use register_on_chat_message:
|
2015-09-25 02:00:15 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.register_on_chat_message(function(name, message)
|
2017-08-26 21:01:51 +03:00
|
|
|
print(name .. " said " .. message)
|
|
|
|
return false
|
2015-09-25 02:00:15 +03:00
|
|
|
end)
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-08-29 13:41:24 +03:00
|
|
|
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
|
2015-09-25 02:00:15 +03:00
|
|
|
work the same.
|
|
|
|
|
|
|
|
**WARNING: CHAT COMMANDS ARE ALSO INTERCEPTED.** If you only want to catch
|
|
|
|
player messages, you need to do this:
|
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
minetest.register_on_chat_message(function(name, message)
|
2017-08-26 21:01:51 +03:00
|
|
|
if message:sub(1, 1) == "/" then
|
|
|
|
print(name .. " ran chat command")
|
|
|
|
return false
|
|
|
|
end
|
2015-09-25 02:00:15 +03:00
|
|
|
|
2017-08-26 21:01:51 +03:00
|
|
|
print(name .. " said " .. message)
|
|
|
|
return false
|
2015-09-25 02:00:15 +03:00
|
|
|
end)
|
|
|
|
{% endhighlight %}
|