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>
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.