From 138daddb8b4c4323ff501e0aee4551eec3825cb9 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 25 Feb 2018 00:25:46 +0000 Subject: [PATCH] ChatCmdBuilder: Add notes to be less misleading, also document luapatterns --- _data/links_en.yml | 2 +- en/chapters/chat.md | 42 ++++++++++++++++++++++++++++++------- en/chapters/chat_complex.md | 13 +++++++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/_data/links_en.yml b/_data/links_en.yml index f754469..0ecd64a 100644 --- a/_data/links_en.yml +++ b/_data/links_en.yml @@ -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 diff --git a/en/chapters/chat.md b/en/chapters/chat.md index 10626af..7544b90 100644 --- a/en/chapters/chat.md +++ b/en/chapters/chat.md @@ -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 -* /team join -* /team leave -* /team list +* `/msg ` +* `/team join ` +* `/team leave ` +* `/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 `. 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 diff --git a/en/chapters/chat_complex.md b/en/chapters/chat_complex.md index 064abcc..cba7697 100644 --- a/en/chapters/chat_complex.md +++ b/en/chapters/chat_complex.md @@ -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 `, `/team join ` or `/team leave `. +This chapter will show you how to make complex chat commands with ChatCmdBuilder, +such as `/msg `, `/team join ` or `/team leave `. + +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.