|
| 1 | +--- |
| 2 | +order: 4 |
| 3 | +authors: |
| 4 | + - DerEchtePilz |
| 5 | + - willkroboth |
| 6 | + - JorelAli |
| 7 | +--- |
| 8 | + |
| 9 | +# Chat preview |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Chat preview is a feature introduced in Minecraft 1.19 that allows the server to display a preview of a chat message to the client before the client sends their message to the server. This chat preview feature is also compatible with `/say` and `/msg`, as well as the `ChatArgument` and `AdventureChatArgument` classes. |
| 14 | + |
| 15 | +:::danger Minecraft version support |
| 16 | + |
| 17 | +The chat preview feature is only present in Minecraft versions 1.19, 1.19.1 and 1.19.2. [Chat preview was removed in 1.19.3](https://minecraft.wiki/w/Java_Edition_1.19.3#General_2), so this feature is unfortunately no longer usable in Minecraft 1.19.3 and beyond. |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +## Enabling chat preview |
| 22 | + |
| 23 | +To use chat preview, your server must have `previews-chat` set to `true` in the `server.properties` file: |
| 24 | + |
| 25 | +```properties |
| 26 | +... |
| 27 | +previews-chat=true // [!code focus] |
| 28 | +... |
| 29 | +``` |
| 30 | + |
| 31 | +For players that want to use chat preview, they must have `Chat Preview` enabled in `Options > Chat Settings...` |
| 32 | + |
| 33 | +## Specifying a chat preview function |
| 34 | + |
| 35 | +The `ChatArgument` and `AdventureChatArgument` classes include a method, `withPreview`: |
| 36 | + |
| 37 | +```java |
| 38 | +public T withPreview(PreviewableFunction preview); |
| 39 | +``` |
| 40 | + |
| 41 | +The method `withPreview(PreviewableFunction preview)` lets you generate a preview to send to the client. This method takes in the `PreviewableFunction` functional interface, which is a function that takes in a `PreviewInfo` and returns either a `BaseComponent[]` (for `ChatArgument`) or a `Component` (for `AdventureChatArgument`): |
| 42 | + |
| 43 | +```java |
| 44 | +public T generatePreview(PreviewInfo info) throws WrapperCommandSyntaxException; |
| 45 | +``` |
| 46 | + |
| 47 | +The `PreviewInfo` class is a record containing the following: |
| 48 | + |
| 49 | +```java |
| 50 | +public record PreviewInfo<T> { |
| 51 | + Player player(); |
| 52 | + String input(); |
| 53 | + String fullInput(); |
| 54 | + T parsedInput(); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The following methods are as follows: |
| 59 | + |
| 60 | +```java |
| 61 | +Player player(); |
| 62 | +``` |
| 63 | + |
| 64 | +`player()` is the player currently typing a chat preview. |
| 65 | + |
| 66 | +```java |
| 67 | +String input(); |
| 68 | +``` |
| 69 | + |
| 70 | +`input()` is the current input for the current `ChatArgument` or `AdventureChatArgument`. If a user is typing `/mycommand hellowor¦` and the command syntax is `/mycommand <ChatArgument>`, the result of `input()` would be `"hellowor"`. |
| 71 | + |
| 72 | +```java |
| 73 | +String fullInput(); |
| 74 | +``` |
| 75 | + |
| 76 | +`fullInput()` is the full input that the player has typed, including the leading `/` symbol which is required to start a command. If a user is typing `/mycommand hellowor¦`, the result of `fullInput()` would be `"/mycommand hellowor"`. |
| 77 | + |
| 78 | +```java |
| 79 | +T parsedInput(); |
| 80 | +``` |
| 81 | + |
| 82 | +`parsedInput()` is similar to `input()`, except it has been parsed by the CommandAPI's argument parser. This is a representation of what the argument in the executor would look like. For a `ChatArgument` the return type is `BaseComponent[]`, and for `AdventureChatArgument` the return type is `Component`. |
| 83 | + |
| 84 | +## Using the chat preview function as the argument's value |
| 85 | + |
| 86 | +The `ChatArgument` and `AdventureChatArgument` classes also include a method, `usePreview`: |
| 87 | + |
| 88 | +```java |
| 89 | +public T usePreview(boolean usePreview); |
| 90 | +``` |
| 91 | + |
| 92 | +The `usePreview(boolean usePreview)` method lets you specify whether you would like the previewing function to be used as the argument's value during execution. If set to `true`, when the command's `.executes()` method is called, the argument value (e.g. `arg[0]`) will be the same as the content generated by the function provided to `withPreview()`. |
| 93 | + |
| 94 | +## Chat preview examples |
| 95 | + |
| 96 | +::::tip Example – Using chat preview |
| 97 | + |
| 98 | +Say we wanted to make our own `/broadcast` command that allowed the user to use `&` chat colors. We can use chat preview to show users what the result of their `/broadcast` command would look like before running the command. We'll use the following command syntax: |
| 99 | + |
| 100 | +```mccmd |
| 101 | +/broadcast <message> |
| 102 | +``` |
| 103 | + |
| 104 | +Because the `ChatArgument` and `AdventureChatArgument` can support entity selectors (such as `@p`), it's best to use the `info.parsedInput()` method to handle parsed entity selectors. In our code, we use the `.withPreview()` method and take the parsed input and convert it to plain text. We then convert the plain text with `&` characters into component text to be displayed to the user. |
| 105 | + |
| 106 | +For execution, we do the same procedure, because the text that the user enters still has `&` characters that need to be converted into a component. |
| 107 | + |
| 108 | +**Using Adventure Chat API** |
| 109 | + |
| 110 | +:::tabs |
| 111 | +===Java |
| 112 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/chat/ChatPreview.java#chatPreviewAdventureExample |
| 113 | +===Kotlin |
| 114 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/chat/ChatPreview.kt#chatPreviewAdventureExample |
| 115 | +::: |
| 116 | + |
| 117 | +**Using Legacy Chat API** |
| 118 | + |
| 119 | +:::tabs |
| 120 | +===Java |
| 121 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/chat/ChatPreview.java#chatPreviewLegacyExample |
| 122 | +===Kotlin |
| 123 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/chat/ChatPreview.kt#chatPreviewLegacyExample |
| 124 | +::: |
| 125 | + |
| 126 | +:::: |
| 127 | + |
| 128 | +::::tip Example – Using chat preview with `usePreview()` |
| 129 | + |
| 130 | +Extending on the example above where we created a `/broadcast` command with chat preview support, we can simplify the code by using `.usePreview(true)` to use the preview function as the value of our argument in our executor function. We'll use the same command syntax as the previous example: |
| 131 | + |
| 132 | +```mccmd |
| 133 | +/broadcast <message> |
| 134 | +``` |
| 135 | + |
| 136 | +By using `.usePreview(true)`, we don't have to re-translate `&` formatting codes into their corresponding components because that has already been done by the preview function specified in `.withPreview()` method. |
| 137 | + |
| 138 | +**Using Adventure Chat API** |
| 139 | + |
| 140 | +:::tabs |
| 141 | +===Java |
| 142 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/chat/ChatPreview.java#usePreviewAdventureExample |
| 143 | +===Kotlin |
| 144 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/chat/ChatPreview.kt#usePreviewAdventureExample |
| 145 | +::: |
| 146 | + |
| 147 | +**Using Legacy Chat API** |
| 148 | + |
| 149 | +:::tabs |
| 150 | +===Java |
| 151 | +<<< @/../reference-code/src/main/java/createcommands/arguments/types/chat/ChatPreview.java#usePreviewLegacyExample |
| 152 | +===Kotlin |
| 153 | +<<< @/../reference-code/src/main/kotlin/createcommands/arguments/types/chat/ChatPreview.kt#usePreviewLegacyExample |
| 154 | +::: |
| 155 | + |
| 156 | +:::: |
0 commit comments