Skip to content

Chat Preview API#1050

Merged
BillyGalbreath merged 2 commits into
ver/1.19from
ChatPreviewAPI
Jul 17, 2022
Merged

Chat Preview API#1050
BillyGalbreath merged 2 commits into
ver/1.19from
ChatPreviewAPI

Conversation

@BillyGalbreath

@BillyGalbreath BillyGalbreath commented Jul 12, 2022

Copy link
Copy Markdown
Contributor

Opening this as a PR to get feedback before merging in.

Speak now, or forever hold your peace ;)

How this works

  1. The client will send a chat preview packet to the server with every keystroke into the chat textbox containing a query string of the textbox contents.
  2. The server will try to decorate this query and return it to the client.
  3. The client will determine if the decorated text differs from the text box contents when the chat message is sent (enter key pressed).
    • If the decoration differs, the signature is made from the contents of the decoration.
    • If the text is the same, the signature is made from the contents of the original message.
  4. The server will receive the chat message and try to decorate the message again
    • If the decoration differs, the signature of the decoration is used to verify the message
    • If the text is the same, the signature of the original message is used to verify the message
  5. The server then displays the original message in the chat box.
    • Note that because of this, your preview should match whatever your chat event does for the desired results.

Example usage

This is a simple plugin to convert raw input to minimessage, do not do this on production..

public class TestPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new Listener() {
            @EventHandler
            public void on(PlayerPreviewChatEvent event) {
                event.setQuery(handleChat(event.getQuery()));
            }

            @EventHandler
            public void on(AsyncChatEvent event) {
                event.message(handleChat(event.message()));
            }

            private Component handleChat(Component component) {
                String raw = LegacyComponentSerializer.legacyAmpersand().serialize(component);
                return MiniMessage.miniMessage().deserialize(raw);
            }
        }, this);
    }
}

image

image

@BillyGalbreath BillyGalbreath marked this pull request as draft July 12, 2022 03:25
@YouHaveTrouble

This comment was marked as outdated.

@LOOHP

This comment was marked as outdated.

@YouHaveTrouble

This comment was marked as outdated.

@LOOHP

This comment was marked as outdated.

@YouHaveTrouble

This comment was marked as outdated.

@LOOHP

This comment was marked as outdated.

@BillyGalbreath

This comment was marked as outdated.

@BillyGalbreath BillyGalbreath marked this pull request as ready for review July 17, 2022 02:59

@encode42 encode42 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested, but code looks good to me

@YouHaveTrouble YouHaveTrouble left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested for a little bit, seems to work just fine.

@BillyGalbreath BillyGalbreath merged commit 8aa5c25 into ver/1.19 Jul 17, 2022
@granny granny deleted the ChatPreviewAPI branch July 31, 2022 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants