Solved Clicking a player's name in chat

Discussion in 'Spigot Plugin Development' started by BenPlaysMC, Oct 17, 2018.

Thread Status:
Not open for further replies.
  1. So I'm currently making a tiny plugin where when any player types in chat, you can click their name and it would suggest a command. I know there are some outdated libraries that I would really love to avoid using and I know it has something to do with JSON I believe so it would be great if somebody could explain it a bit more on how to do it.
     
    • Like Like x 1
  2. Code (Java):
            IChatBaseComponent comp = IChatBaseComponent.ChatSerializer.a("JSON");
            PacketPlayOutChat packet = new PacketPlayOutChat(comp);
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
     
    You can generate JSON there: http://minecraft.tools/en/tellraw.php
     
    • Funny Funny x 1
  3. Cancel the event and use

    Code (Text):

            TextComponent msg = new TextComponent(event.getPlayer().getDisplayName());
            msg.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "cmd"));
    for (Player players : event.getRecipients()) { players.spigot.sendMessage(msg); }
     
    I'm on my phone, so it's not 100% working.
     
    • Like Like x 1
  4. You're right as far as TextComponents go, but perhaps instead of cancelling the event it would be wiser to clear the message recipients and not send any message at all if the event is already canceled, just to play nice with other plugins :p
     
    • Like Like x 1
  5. By clearing the recipients I don't get any message at all when I type in chat so how would I test it. Sorry if it was a stupid question, maybe I'm not getting what you're saying. Here's my code.

    Code (Java):
    package me.reprevise.whisperclick.events;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;

    public class OnChat implements Listener {

        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            // event.getRecipients().clear();

            if (event.getMessage().contains(event.getPlayer().getDisplayName())) {
                event.setCancelled(true);
                TextComponent msg = new TextComponent(event.getMessage());
                msg.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/w " + event.getPlayer().getName()));
                msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                        new ComponentBuilder("Click to send a Message!").create()));

                event.setMessage(msg.getText());
            }

        }

    }
     
    To add on: When I look in the console though, my message is there though.

    ALSO: I don't think it will play nicely if they have a custom chat plugin that formats the chat unless I dig out their name in the message and only set that to have the text and then send the whole message.
     
    #5 BenPlaysMC, Oct 17, 2018
    Last edited: Oct 17, 2018
    • Like Like x 1
  6. Don't mind me, it might not really make a difference ^^;
     
    • Like Like x 1
  7. What are you trying to accomplish?
     
    • Like Like x 1
  8. When a player types in chat, another player can click on just their name and it would suggest the whisper command.
     
    • Like Like x 1
  9. Code (Text):

    TextComponent message = new TextComponent(event.getPlayer().getDisplayName());
    message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/w " + event.getPlayer().getName()));
    message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to private message.").create()));
    message.addExtra(ChatColor.WHITE + " > " + event.getMessage());
    for (Player players : event.getRecipients()) { players.spigot().sendMessage(message); }
     
     
    • Like Like x 2
  10. Thanks for all the help.
     
    • Like Like x 1
    • Agree Agree x 1
Thread Status:
Not open for further replies.