Skip to content

Instantly share code, notes, and snippets.

@EnderGamingFilms
Created August 11, 2022 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EnderGamingFilms/ef0c60468fd2d8f36cca8e42c9221eb2 to your computer and use it in GitHub Desktop.
Save EnderGamingFilms/ef0c60468fd2d8f36cca8e42c9221eb2 to your computer and use it in GitHub Desktop.
(Bukkit/Spigot/Paper) How to create clickable/hoverable messages and send them to a player.

I have looked EVERYWHERE online and couldn't find anything to do this so I tried to figure it out myself. Here is what I have found while doing this.

To do this I use some libraries from KyoriPowered.

Add this to your pom.xml (you don't need to add and repositories)

   <dependencies>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>text-api</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>net.kyori</groupId>
            <artifactId>text-adapter-bukkit</artifactId>
            <version>3.0.3</version>
        </dependency>
    </dependencies>

Creating a message

Create a TextComponent

final TextComponent message = TextComponent.builder()

Add a coloured message to it

message.content("Cool message")
  .color(TextColor.YELLOW);

Add a new line

message.append(TextComponent.newLine());

Add a message with hover text

TextComponent hoverable = TextComponent.builder();
TextComponent hovertext = TextComponent.builder()
  .content("You hovered")
  .color(TextColor.AQUA)
  .build();
  
hoverable.content("Hover here")
  .color(TextColor.GOLD)
  .hoverEvent(HoverEvent.showText(hovertext))

message.append(hoverable);

Add clickable text that runs a command

TextComponent clickable = TextComponent.builder();
clickable.content("Click me!")
  .color(TextColor.RED)
  .clickEvent(ClickEvent.runCommand("/say i clicked text"));
  
 message.append(clickable)

Send the text to the client

TextAdapter.sendComponent(player, message.build());

THIS EXAMPLE MIGHT NOT WORK.

In case it doesn't, I have attached a working version that is sent when a player clicks a barrier in a chest menu.

public class GUI implements Listener {
private UUID id;
@EventHandler
public void onDoAThing(InventoryClickEvent e) {
Inventory menu = e.getInventory();
ItemStack nm = menu.getItem(0);
if (nm.getItemMeta().getDisplayName().equalsIgnoreCase(name)) {
e.setCancelled(true);
ItemStack item = e.getCurrentItem();
if (item.getItemMeta().getDisplayName().equalsIgnoreCase(Chat.clr("&4&lclose"))) {
closed = true;
e.getWhoClicked().closeInventory();
final TextComponent msg = TextComponent.builder()
.content("Please run ")
.color(TextColor.RED)
.append(TextComponent.builder("/punish GUI_" + id.toString())
.color(TextColor.YELLOW)
.hoverEvent(HoverEvent
.showText(TextComponent.builder()
.content("Run command automatically")
.color(TextColor.YELLOW)
.build()))
.clickEvent(ClickEvent.runCommand("/punishdel GUI_" + id.toString())))
.build();
TextAdapter.sendComponent(player, msg);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment