ChatCmdBuilder: Add notes to be less misleading, also document luapatterns

master
rubenwardy 2018-02-25 00:25:46 +00:00
parent a177c74f6d
commit 138daddb8b
3 changed files with 46 additions and 11 deletions

View File

@ -49,7 +49,7 @@
num: 10
link: chapters/chat.html
- title: Complex Chat Commands
- title: Chat Command Builder
num: 11
link: chapters/chat_complex.html

View File

@ -106,14 +106,42 @@ to show a formspec, and `/inbox text` to send information to chat.
It is often required to make complex chat commands, such as:
* /msg <name> <message>
* /team join <teamname>
* /team leave <teamname>
* /team list
* `/msg <to> <message>`
* `/team join <teamname>`
* `/team leave <teamname>`
* `/team list`
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).
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.
{% highlight lua %}
local to, msg = string.match(param, "^([%a%d_-]+) (*+)$")
{% endhighlight %}
The above implements `/msg <to> <message>`. Lets 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](http://lua-users.org/wiki/PatternsTutorial)
or the [PIL documentation](https://www.lua.org/pil/20.2.html).
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](chat_complex.html).
## Intercepting Messages

View File

@ -1,13 +1,17 @@
---
title: Complex Chat Commands
title: Chat Command Builder
layout: default
root: ../../
---
## Introduction
This chapter will show you how to make complex chat commands, such as
`/msg <name> <message>`, `/team join <teamname>` or `/team leave <teamname>`.
This chapter will show you how to make complex chat commands with ChatCmdBuilder,
such as `/msg <name> <message>`, `/team join <teamname>` or `/team leave <teamname>`.
Note that ChatCmdBuilder is a library created by the author of this book, and most
modders tend to use the method outlined in the
[chat commnds](chat.html#complex-subcommands) chapter.
* Why ChatCmdBuilder?
* Routes.
@ -100,6 +104,9 @@ end)
## Installing ChatCmdBuilder
The source code can be found and downloaded on
[Github](https://github.com/rubenwardy/ChatCmdBuilder/).
There are two ways to install:
1. Install ChatCmdBuilder as a mod and depend on it.