Chat: Restructure layout

This commit is contained in:
rubenwardy 2022-06-18 21:36:02 +01:00
parent 4506a8c3da
commit 83067e5027

View File

@ -8,19 +8,21 @@ redirect_from: /en/chapters/chat.html
cmd_online:
level: warning
title: Offline players can run commands
message: <p>A player name is passed instead of a player object because mods
can run commands on behalf of offline players. For example, the IRC
bridge allows players to run commands without joining the game.</p>
message: |
A player name is passed instead of a player object because mods
can run commands on behalf of offline players. For example, the IRC
bridge allows players to run commands without joining the game.
<p>So make sure that you don't assume that the player is online.
You can check by seeing if <pre>minetest.get_player_by_name</pre> returns a player.</p>
So make sure that you don't assume that the player is online.
You can check by seeing if `minetest.get_player_by_name` returns a player.
cb_cmdsprivs:
level: warning
title: Privileges and Chat Commands
message: The shout privilege isn't needed for a player to trigger this callback.
This is because chat commands are implemented in Lua, and are just
chat messages that begin with a /.
message: |
The shout privilege isn't needed for a player to trigger this callback.
This is because chat commands are implemented in Lua, and are just
chat messages that begin with a /.
---
@ -29,17 +31,20 @@ cb_cmdsprivs:
Mods can interact with player chat, including
sending messages, intercepting messages, and registering chat commands.
- [Sending Messages to All Players](#sending-messages-to-all-players)
- [Sending Messages to Specific Players](#sending-messages-to-specific-players)
- [Sending Messages](#sending-messages)
- [To All Players](#to-all-players)
- [To Specific Players](#to-specific-players)
- [Chat Commands](#chat-commands)
- [Complex Subcommands](#complex-subcommands)
- [Using string.split](#using-stringsplit)
- [Using Lua patterns](#using-lua-patterns)
- [Accepting Multiple Arguments](#accepting-multiple-arguments)
- [Using string.split](#using-stringsplit)
- [Using Lua patterns](#using-lua-patterns)
- [Intercepting Messages](#intercepting-messages)
## Sending Messages to All Players
## Sending Messages
To send a message to every player in the game, call the chat_send_all function.
### To All Players
To send a message to every player in the game, call the `chat_send_all` function.
```lua
minetest.chat_send_all("This is a chat message to all players")
@ -53,9 +58,9 @@ Here is an example of how this appears in-game:
The message appears on a separate line to distinguish it from in-game player chat.
## Sending Messages to Specific Players
### To Specific Players
To send a message to a specific player, call the chat_send_player function:
To send a message to a specific player, call the `chat_send_player` function:
```lua
minetest.chat_send_player("player1", "This is a chat message for player1")
@ -82,20 +87,25 @@ minetest.register_chatcommand("foo", {
In the above snippet, `interact` is listed as a required
[privilege](privileges.html) meaning that only players with the `interact` privilege can run the command.
`param` is a string containing everything a player writes after the chatcommand
name. For example, if a user types `/grantme one,two,three` then `param` will be
`one,two,three`.
Chat commands can return up to two values,
the first being a Boolean indicating success, and the second being a
message to send to the user.
{% include notice.html notice=page.cmd_online %}
## Complex Subcommands
### Accepting Multiple Arguments
It's common for chat commands to require multiple arguments, for example,
`/team join <team_name>`. There are two ways
of doing this, either using Minetest's string split or Lua patterns.
<a name="complex-subcommands"></a>
`param` gives you all the arguments to a chat command in a single string. It's
common for chat commands to need to extract multiple arguments. There are two
ways of doing this, either using Minetest's string split or Lua patterns.
### Using string.split
#### Using string.split
A string can be split up into words using `string.split(" ")`:
@ -120,8 +130,7 @@ else
end
```
### Using Lua patterns
#### Using Lua patterns
[Lua patterns](https://www.lua.org/pil/20.2.html) are a way of extracting stuff
from text using rules. They're best suited for when there are arguments that can