How to check for clicked words in chat

Discussion in 'Spigot Plugin Development' started by pondamo, May 31, 2016.

  1. How would I go about getting a specific word a player clicked on in the chat and running a command based off of the word that was clicked.

    For example:
    pondamo: Hello bob, can I teleport to you?
    Bob: Sure just click on my name in the chat!
    pondamo: (clicks on bobs name and is teleported to bob)

    Thanks for any help!
     
  2. Use the TextComponent API and click events
     
  3. True I could do that... and thats probably the only way I will be able to do it but how would I get it to where its able to pinpoint the players name and then specifically teleport to that player?
    This is what I came up with so far but it doesnt exactly work.
    Code (Text):
    TextComponent message = new TextComponent(e.getMessage());
                    message.setClickEvent(new ClickEvent (ClickEvent.Action.RUN_COMMAND, ("/tpa " + message)));
                    player.spigot().sendMessage(message);
     
  4. Doesn't that suppose be the name of the player instead of `message`?
     
  5. Yes but how could I retreive the players name? I need to be able to figure out the word the player is clicking on then have them teleport to the player but I am lost on how I would go about retrieving the specific word the player clicked on
     
  6. Okay, i think i got your point. If you set the whole message as a one json, it does not click to a word, it clicks to all of the message. You can set json for each word, put them together and send. Better way to do this is checking each word in the message to see if it is a name of a player or not. Then replacing only player names with jsons.