Chat: Add string.split
This commit is contained in:
parent
80605008c9
commit
14a356ded1
@ -33,6 +33,8 @@ sending messages, intercepting messages, and registering chat commands.
|
||||
- [Sending Messages to Specific Players](#sending-messages-to-specific-players)
|
||||
- [Chat Commands](#chat-commands)
|
||||
- [Complex Subcommands](#complex-subcommands)
|
||||
- [Using string.split](#using-stringsplit)
|
||||
- [Using Lua patterns](#using-lua-patterns)
|
||||
- [Intercepting Messages](#intercepting-messages)
|
||||
|
||||
## Sending Messages to All Players
|
||||
@ -88,18 +90,45 @@ message to send to the user.
|
||||
|
||||
## Complex Subcommands
|
||||
|
||||
It is often required to make complex chat commands, such as:
|
||||
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.
|
||||
|
||||
* `/msg <to> <message>`
|
||||
* `/team join <teamname>`
|
||||
* `/team leave <teamname>`
|
||||
* `/team list`
|
||||
|
||||
This is usually done using [Lua patterns](https://www.lua.org/pil/20.2.html).
|
||||
Patterns are a way of extracting stuff from text using rules.
|
||||
### Using string.split
|
||||
|
||||
A string can be split up into words using `string.split(" ")`:
|
||||
|
||||
```lua
|
||||
local to, msg = string.match(param, "^([%a%d_-]+) (*+)$")
|
||||
local parts = param:split(" ")
|
||||
local cmd = parts[1]
|
||||
|
||||
if cmd == "join" then
|
||||
local team_name = parts[2]
|
||||
team.join(name, team_name)
|
||||
return true, "Joined team!"
|
||||
elseif cmd == "max_users" then
|
||||
local team_name = parts[2]
|
||||
local max_users = tonumber(parts[3])
|
||||
if team_name and max_users then
|
||||
return true, "Set max users of team " .. team_name .. " to " .. max_users
|
||||
else
|
||||
return false, "Usage: /team max_users <team_name> <number>`
|
||||
end
|
||||
else
|
||||
return false, "Command needed"
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
### 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
|
||||
contain spaces or more control is needed on how parameters are captured.
|
||||
|
||||
```lua
|
||||
local to, msg = param:match("^([%a%d_-]+) (*+)$")
|
||||
```
|
||||
|
||||
The above code implements `/msg <to> <message>`. Let's go through left to right:
|
||||
@ -123,13 +152,6 @@ Patterns would probably be the
|
||||
[lua-users.org tutorial](http://lua-users.org/wiki/PatternsTutorial)
|
||||
or the [PIL documentation](https://www.lua.org/pil/20.2.html).
|
||||
|
||||
<p class="book_hide">
|
||||
There is also a library written by the author of this book which can be used
|
||||
to make complex chat commands without patterns called
|
||||
<a href="https://gitlab.com/rubenwardy/ChatCmdBuilder">Chat Command Builder</a>.
|
||||
</p>
|
||||
|
||||
|
||||
## Intercepting Messages
|
||||
|
||||
To intercept a message, use register_on_chat_message:
|
||||
|
@ -30,7 +30,9 @@ Le mod possono interagire con la chat del giocatore, tra l'inviare messaggi, int
|
||||
- [Inviare messaggi a giocatori specifici](#inviare-messaggi-a-giocatori-specifici)
|
||||
- [Comandi](#comandi)
|
||||
- [Complex Subcommands](#complex-subcommands)
|
||||
- [Intercettare i messaggi](#interecettare-i-messaggi)
|
||||
- [Using string.split](#using-stringsplit)
|
||||
- [Using Lua patterns](#using-lua-patterns)
|
||||
- [Intercettare i messaggi](#intercettare-i-messaggi)
|
||||
|
||||
## Inviare messaggi a tutti i giocatori
|
||||
|
||||
@ -79,17 +81,46 @@ I comandi ritornano un massimo di due valori, dove il primo è un booleano che i
|
||||
|
||||
{% include notice.html notice=page.cmd_online %}
|
||||
|
||||
## Sottocomandi complessi
|
||||
|
||||
È spesso necessario creare dei comandi complessi, come per esempio:
|
||||
## Complex Subcommands
|
||||
|
||||
* `/msg <a> <messaggio>`
|
||||
* `/team entra <nometeam>`
|
||||
* `/team esci <nometeam>`
|
||||
* `/team elenco`
|
||||
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.
|
||||
|
||||
|
||||
### Using string.split
|
||||
|
||||
A string can be split up into words using `string.split(" ")`:
|
||||
|
||||
```lua
|
||||
local parts = param:split(" ")
|
||||
local cmd = parts[1]
|
||||
|
||||
if cmd == "join" then
|
||||
local team_name = parts[2]
|
||||
team.join(name, team_name)
|
||||
return true, "Joined team!"
|
||||
elseif cmd == "max_users" then
|
||||
local team_name = parts[2]
|
||||
local max_users = tonumber(parts[3])
|
||||
if team_name and max_users then
|
||||
return true, "Set max users of team " .. team_name .. " to " .. max_users
|
||||
else
|
||||
return false, "Usage: /team max_users <team_name> <number>`
|
||||
end
|
||||
else
|
||||
return false, "Command needed"
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
### 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
|
||||
contain spaces or more control is needed on how parameters are captured.
|
||||
|
||||
Questo viene solitamente reso possibile dai [pattern di Lua](https://www.lua.org/pil/20.2.html).
|
||||
I pattern sono un modo di estrapolare "cose" dal testo usando delle regole ben precise.
|
||||
|
||||
```lua
|
||||
local a, msg = string.match(param, "^([%a%d_-]+) (*+)$")
|
||||
|
Loading…
Reference in New Issue
Block a user