Solved Multiple clickable chat components in a single message

Discussion in 'Spigot Plugin Development' started by jetp250, Apr 30, 2017.

  1. Hey, so, I'm not familiar with the ChatComponent API that we can use for clickable messages; so say I have something like this:
    Code (Java):
    final Player receipient = (Player) sender;
    final ComponentBuilder message = new ComponentBuilder(availableChannels);
    for (Channel channel : plugin.getChannels()) {
        message.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "ch join" + channel.getName()));
    }
    Now, you see straight away that won't work for obvious reasons.
    How can I add multiple clickable messages in a single message, so I'd have like:
    Code (Text):
    String 1, String 2, String 3, String 4...
    and each of those would do a different action?

    Rather confused about this, any help would be highly appreciated.

    EDIT: Solved!
    Here's what I ended up with:
    Code (Java):
    final Player receipient = (Player) sender;
    final ComponentBuilder message = new ComponentBuilder(availableChannels);
    for (Channel channel : plugin.getChannels()) {
        message.append(ChatColor.GRAY + channel.getName());
        message.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ch join" + channel.getName()));
        message.event(new HoverEvent(Action.SHOW_TEXT,
        TextComponent.fromLegacyText("\u00a77Click to join the channel " + channel.getName() + "!")));
    }
    receipient.spigot().sendMessage(message.create());
    Yep, pretty ugly, cleaning it up; but it works.
     
    #1 jetp250, Apr 30, 2017
    Last edited: Apr 30, 2017
    • Useful Useful x 1
  2. sothatsit

    Patron

    You will need to create a seperate component for each of the seperate clickable sections of the message. I am not exactly sure what API you are using for this, but it will have something that allows you to do this. It will be something like creating seperate base text components and then combining them into one list of chat components.
     
  3. Oh, sorry for that; using the Spigot's built-in API, which should work even though I'm not using Bungee?
    Anyhow, I was assuming there would be some sort of 'append' method on the builder (well yes; there is one, but it accepts text) and ...
    Yeah, I have no idea about what I'm doing and how this works.

    How I'm expecting this to work;
    Say I have a ComponentBuilder.
    Now, I'd like to add components to that builder, each component having their own event.
    This builder would now contain all of the components, and when I'd call 'create()', it'd turn it to a single message (or BaseComponent[] it seems?) and I could send that to the player.

    However, I can only add an event for the builder itself - or so it seems - which is rather confusing.

    That said, I may have just got an idea, I'll be back.

    EDIT: Ah, yes, pretty sure I solved it. The ComponentBuilder#Event adds the event to the current text component (i.e the one on the creation of ComponentBuilder or one added via .append(String)).
    Should've figured as much.

    Will be back with the full code
     
    • Like Like x 1