Solved How to detect when a player clicks text in chat with the chat component API and run a function

Discussion in 'Spigot Plugin Development' started by Megacrafter10, Sep 14, 2019 at 2:16 AM.

  1. Hi, so my question is very simple. I know you can do:
    Code (Java):
    TextComponent text = new TextComponent("click here");
    text.setClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/say hello world" ) );
    but how do I run actual code and not Minecraft commands?
    for example, how would I run a function when a palyer clicks the text?
    thanks for reading :)
  2. Don't think you can actually run a method, but you can create an exclusive command to run the method instead. Kind of dirty but gets the job done.
  3. oh, so I write a custom command, run it when the text is clicked and then detect the onCommand event? It would be nice if they added a way to run methods.
  4. It's out of the scope of Bukkit/Spigot. Clicking/interacting with ChatComponents happens entirely client-side, the information about what actions to take or what commands to run is captured/encoded in the String the moment it is sent.

    No packets are sent by the player (to my knowledge, at least) when they click the ChatComponents, so the server can't fire an event for something that isn't communicated.
  5. A hacky work around could be to register a command that is just a random string of numbers / letters (Something the player would never guess) and call a custom event on execution of said command. Just dumb enough to work.
  6. Thanks to everyone for explaining that to me
    • Friendly Friendly x 1