Solved Sending an TextComponent

Discussion in 'Spigot Plugin Development' started by Thomas_muur, May 22, 2016.

Thread Status:
Not open for further replies.
  1. Hello,

    I was trying to send an message to a player, and that if a player hovers on that message it shows text.
    So I did this:
    Code (Text):
                Player p = (Player) sender;
                TextComponent hmessage = new TextComponent( "Hover on me" );
                hmessage.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("hover!").create() ) );
                p.sendMessage( hmessage );
    But it gives an error at sendMessage, caus what I think is that you can't send a TextComponent with sendMessage, sendRawMessage doesn't work either. this is the error: The method sendMessage(String) in the type CommandSender is not applicable for the arguments (TextComponent)

    Can anyone help me?

  2. p.spigot().sendMessage()
    • Winner Winner x 1
  3. I am eating right now, but I will try when I am done :D
    thanks for the quick response!
    • Informative Informative x 1
  4. Yup, this is the way to send it.
  5. You can just do p.sendMessage("" + hmessage);
  6. But the recommended way is p.spigot().sendMessage(hmessage);
    • Agree Agree x 2
  7. yes indied it works, but I want to do it with a chat format, so I did this.

    Code (Text):
            TextComponent message = new TextComponent("§8[§7" + file.getConfig().getString(p.getName() + ".baan" + "§8]"));
            if (file.getConfig().getString(p.getName() + ".baan2") == null) {
            message.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§b" + p.getName() + "§3 zijn/haar banen: \n §3Baan 1: §b" + file.getConfig().getString(p.getName() + ".baan") + "\n §b" + p.getName() + "§3 heeft geen 2e baan").create() ) );
            } else {
            message.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§b" + p.getName() + "§3 zijn/haar banen: \n §3Baan 1: §b" + file.getConfig().getString(p.getName() + ".baan") + "\n §3Baan 2: §b" + file.getConfig().getString(p.getName() + ".baan2")).create() ) );
            if (p.hasPermission("deadwoodkd.minetopia.staff")) {
                e.setFormat("§3[§bLevel " + file.getConfig().getInt(p.getName() + ".level") + "§3] " + " §4" + p.getName() + "§f: " + e.getMessage());
    only if I want to add the TextComponent "message" in the chatformat, how do I ned to do that?
    caus I just can do:
    Code (Text):
                e.setFormat("§3[§bLevel " + file.getConfig().getInt(p.getName() + ".level") + "§3] " + message + " §4" + p.getName() + "§f: " + e.getMessage());
    but it gives an long output, so I think I need to add that spigot() stuff there to, how do I add the spigot() stuff here?
  8. Whats the output?
  9. this -,-
  10. MiniDigger


    it does not work with the format iirc, just cancel the event and send the message to all players using the spigot api method.
    • Agree Agree x 1
  11. Does e.spigot()... Work?
  12. MiniDigger


    e.spigot() enables you to access spigot api methods. What are you trying to do?
  13. You can try e.spigot().setFormat(); Maybe that will work? Since it includes the TextComponent(), but then again it does contain Strings, so I'm not sure. Just a possible suggestion! :)
  14. MiniDigger


    asynplayerchatevent has not spigot api...
    as I sugessted: cancel the even and send the chat compontent to all players using the spigot api...
  15. Ah, I see what you mean. Thanks for clearing it up ;)
  16. Do NOT cancel the event! This breaks compatibility with other plugins. Instead, as a workaround for Bukkit API destroying json components, add a chat packet listener and edit the json components at the packet level.

    1) Listen for chat event.
    2) Edit message as much as possible, without json components yet.
    3) Let event continue, uncanceled
    4) Listen for chat packet sending
    5) Edit message on the json component level
    6) Let packet countinue, uncanceled.
  17. MiniDigger


    it will not break if your eventhandler is on montior prio ;)
    I think canceling the event is a much cleaner solution then playing with the json string on packet level.
  18. You are not supposed to edit anything about the event in the monitor level, not even cancel it. This goes against the entire meaning of that priority level.. so, you can either cancel the highest priority and break compatibility with other plugins listening on the highest and monitor levels, or you can listen and edit the json components on the packet level. Either way, you're messing with json components... what do you think a TextComponent is? ;)

    I'll agree it's not the best of situations to be in. The best answer is to create a PR to fix the event so it can handle json components. I would do it, but my track record at making PRs isn't that great with Spigot. @md_5 will just decline it and rewrite it his own way, so I'll just stay out of it and let him write it his own way from the beginning. Doubt it will happen, so in the mean time editing on packet level is the best answer I can give other than forking spigot yourself or joining a more community driven fork of spigot that will accept such a PR from the community members.
    #18 BillyGalbreath, May 23, 2016
    Last edited: May 23, 2016
  19. No, you cannot. There is a big difference between sending it as a text message, and sending it as a ChatComponent.
  20. [​IMG]
    • Funny Funny x 8
Thread Status:
Not open for further replies.