Skip to content

Commit 6e884de

Browse files
authored
feat: add Chat format docs (#171)
* Create chat formatting guide in documentation * upload chat example image * Add chat-formatting page
1 parent ff1d11c commit 6e884de

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "Formatting the chat"
3+
description: "Learn how to format the chat using the PlayerChatEvent"
4+
authors:
5+
- name: "oskarscot"
6+
link: "https://oskar.scot"
7+
---
8+
9+
![chat format example](/assets/guides/chat-example.png)
10+
11+
The chat uses the PlayerChatEvent, it contains the formatter, the PlayerRef for the sender, the content as well as a list of targets (it's safe to assume the targets is the list of players who can see the chat message)
12+
You can cancel this event as well as modify the content and the formatter.
13+
14+
```java
15+
public class ChatFormatter {
16+
17+
public static void onPlayerChat(PlayerChatEvent event) {
18+
PlayerRef sender = event.getSender();
19+
if(event.getContent().equalsIgnoreCase("poo")) {
20+
event.setCancelled(true);
21+
sender.sendMessage(Message.raw("Hey, you cannot say that!").color(Color.RED));
22+
}
23+
24+
if(event.getContent().equalsIgnoreCase("you stink")) {
25+
event.setContent("i stink");
26+
}
27+
28+
event.setFormatter((playerRed, message) ->
29+
Message.join(
30+
Message.raw("[COOL] ").color(Color.RED),
31+
Message.raw(sender.getUsername()).color(Color.YELLOW),
32+
Message.raw(" : " + message).color(Color.PINK)
33+
));
34+
35+
}
36+
}
37+
```
38+
39+
The formatter is the following interface:
40+
41+
```java
42+
public interface Formatter {
43+
@Nonnull
44+
Message format(@Nonnull PlayerRef playerRef, @Nonnull String message);
45+
}
46+
}
47+
```

content/docs/en/guides/plugin/meta.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"pages": [
44
"setting-up-env.mdx",
55
"creating-commands.mdx",
6-
"creating-events.mdx"
6+
"creating-events.mdx",
7+
"chat-formatting.mdx"
78
],
89
"icon": "Terminal"
9-
}
10+
}
34.6 KB
Loading

0 commit comments

Comments
 (0)