Skip to content

Interactive Messages

PXAV edited this page Dec 31, 2020 · 1 revision

This tutorial will demonstrate to you how to create interaction with your players through the chat with click and hover events.

Creating a new message

A new InteractiveMessage can be created using a static factory method. This instance can be passed with the corresponding method in KelpPlayer:

kelpPlayer.sendInteractiveMessage(InteractiveMessage.create());

This won't send a real message to the player as the message has no components, so it is empty.

Adding components

An interactive message consists of different MessageComponents. Those can also be created via static factory:

MessageComponent.create();

Every message component has a text displayed in the chat. You can use normal color codes (§code) in the text, but if you want to use a style code (such as §o, §l, ...) please put a color code before to not confuse the color code interpreter. Then you can optionally configure click and hover events.

MessageComponent.create()
  .text("§7Click here to get a §6§lsteak")
  .showMessageOnHover("§6Click to get a steak.")
  .executeCommandOnClick("give @p cooked_beef 1");
Method Description Example
text(String) Sets the text displayed in the chat for the corresponding component text("§aHello World")
showMessageOnHover(String) When the player hovers over the component, the given message is shown. showMessageOnHover("§aClick here :)")
executeCommandOnClick(String) Lets the player execute the given command. The player has to have the permissions to do that. There is no need to add a / before the command. executeCommandOnClick("give @p cooked_beef 1")
suggestCommandOnClick(String) Suggests the given command to the player, which means that the command is written to the player's chat line but not yet executed. suggestCommandOnClick("party accept pxav")
openURLOnClick(String) openURLOnClick(URL) Opens the given URL to the player. Don't forget to add https:// in front of the URL if you are using web URLs. openURLOnClick("https://github.com/PXAV")
openFileOnClick(String) openFileOnClick(File) openFileOnClick(URL) Opens a file at the given path. The file has to be located on the client's local file system and not the server's. The effect looks like when clicking Open Resource Pack Folder in the Minecraft settings. openFileOnClick("C:\Users\user\Desktop\") openFileOnClick("/home/user/Desktop")
sendChatMessageOnClick(String) Let's the player send the given text into the chat. sendChatMessageOnClick("Hi!")
copyToClipboardOnClick(String) Copies the given text to the player's clipboard. WARNING: This is only available if you are running a new server version. 1.8 does not support this click event. copyToClipboardOnClick("Your-Server.ip")

A component can be added to its message using

InteractiveMessage#addComponent(component)

The components of a message are displayed in the same order as they are added to it.

Example

This example sends a message to an admin when a report has been created by a user. The admin can react with several different options.

KelpPlayer player = playerRepository.getKelpPlayer(event.getPlayer());
    player.sendMessage("§8[§2Kelp§8] §7A report has been created! ");
    player.sendMessage("§8[§2Kelp§8] §7Reason: §a" + reportReason);
    player.sendMessage("§8[§2Kelp§8] §7Accused: §a" + reportTarget);
    player.sendMessage("§8[§2Kelp§8] §7Please select an action§8:");

    player.sendInteractiveMessage(
      InteractiveMessage.create()
        .addComponent(MessageComponent.create()
          .text("§8[§2Kelp§8] ")) // prefix component (not clickable/hoverable)
        .addComponent(MessageComponent.create()
          .text("§a§lWATCH PLAYER")
          .showMessageOnHover("§aTeleport yourself to the player to validate the report")
          .executeCommandOnClick("teleport " + reportTarger))
        .addComponent(MessageComponent.create()
          .text("§c§lBAN PLAYER")
          .showMessageOnHover("§cBan the player immediately without confirming the report")
          .suggestCommandOnClick("ban " + reportTarget + " " + reportReason)) 
          // only suggests the command to the player to that it can be modified by an admin optionally
        .addComponent(MessageComponent.create()
          .text("§b§lOPEN WEBINTERFACE")
          .showMessageOnHover("§bOpens the web interface showing more information about the player")
          .openURLOnClick("https://interface.your-server.com/info/" + reportTarget))
    );
Clone this wiki locally