Begann with chat.md

This commit is contained in:
debiankaios 2022-08-28 00:40:57 +02:00
parent e2f2c32d1c
commit c1299491f9
2 changed files with 168 additions and 0 deletions

166
_de/players/chat.md Normal file
View File

@ -0,0 +1,166 @@
---
title: Chat und Commands
layout: default
root: ../..
idx: 4.2
description: Registrierung eines Chatbefehls und Behandlung von Chatnachrichten mit register_on_chat_message
redirect_from: /de/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>
<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>
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 /.
---
## Einleitung <!-- omit in toc -->
Mods können mit dem Spielerchat interagieren, einschließlich
Senden von Nachrichten, Abfangen von Nachrichten und Registrieren von Chat-Befehlen.
- [Senden von Nachrichten an alle Spieler](#senden-von-nachrichten-an-alle-spieler)
- [Sending Messages to Specific Players](#sending-messages-to-specific-players)
- [Chat Commands](#chat-commands)
- [Complex Subcommands](#complex-subcommands)
- [Intercepting Messages](#intercepting-messages)
## Senden von Nachrichten an alle Spieler
Um eine Nachricht an alle Spieler im Spiel zu senden, rufe die Funktion chat_send_all auf.
```lua
minetest.chat_send_all("Dies ist eine Chat-Nachricht an alle Spieler")
```
Hier ist ein Beispiel dafür, wie dies im Spiel aussieht:
<player1> Sehen Sie sich diesen Eingang an
Dies ist eine Chat-Nachricht an alle Spieler
<player2> Was ist damit?
Die Nachricht erscheint in einer separaten Zeile, um sie vom Spieler-Chat im Spiel zu unterscheiden.
## Sending Messages to Specific Players
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")
```
This message displays in the same manner as messages to all players, but is
only visible to the named player, in this case, player1.
## Chat Commands
To register a chat command, for example `/foo`, use `register_chatcommand`:
```lua
minetest.register_chatcommand("foo", {
privs = {
interact = true,
},
func = function(name, param)
return true, "You said " .. param .. "!"
end,
})
```
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.
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
It is often required to make complex chat commands, such as:
* `/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.
```lua
local to, msg = string.match(param, "^([%a%d_-]+) (*+)$")
```
The above code implements `/msg <to> <message>`. Let's 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 accept any digit.
* `[%a%d_-]` means accept any letter or digit or `_` or `-`.
* `+` means match the thing before one or more times.
* `*` means match any character in this context.
* `$` means match the end of the string.
Put simply, the pattern matches the name (a word with only letters/numbers/-/_),
then a space, then the message (one or more of any character). The name and
message are returned, because they're surrounded by 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).
<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:
```lua
minetest.register_on_chat_message(function(name, message)
print(name .. " said " .. message)
return false
end)
```
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
work the same, because `nil` is returned implicitly and is treated like false.
{% include notice.html notice=page.cb_cmdsprivs %}
You should make sure you take into account that it may be a chat command,
or the user may not have `shout`.
```lua
minetest.register_on_chat_message(function(name, message)
if message:sub(1, 1) == "/" then
print(name .. " ran chat command")
elseif minetest.check_player_privs(name, { shout = true }) then
print(name .. " said " .. message)
else
print(name .. " tried to say " .. message ..
" but doesn't have shout")
end
return false
end)
```

View File

@ -6,7 +6,9 @@ Attachment = Anhang
block = Mapblock block = Mapblock
bulk = bulk bulk = bulk
Chapter = Kapitel Chapter = Kapitel
chat message = Chat-Nachricht
child = Kind child = Kind
Command = Command
Cubic Nodes = Würfelförmiger Block Cubic Nodes = Würfelförmiger Block
craft slots = Handwerksplätze craft slots = Handwerksplätze
Damage = Schaden Damage = Schaden