JSON chat messages?

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 29, 2017.

  1. Hey,

    I want to make a player message (when they chat) clickable, so when they click it an function in my plugin gets called

    I didn't try anything yet cuz google didnt help me out,

    I want to know this
    Can you set an message to a JSON one?

    Because otherwise I should somehow get the format and do it that way.

    And is there a way I can make it that it doesnt care the server version, if yes please link me to a (working) tutorial
     
  2. Lol "goggle didn't help me"? Ok.
    https://www.spigotmc.org/wiki/the-chat-component-api/
    And to make this version independent just use the bungee imports
     
  3. Didn't know it was called like that, I was searching on something completely different lol.

    I'll look into that thanks
     
  4. Here's a similar method that is version independent that i used some time ago for clickable messages,

    Code (Text):

    String displayedMessage = "Insert Message here";
    String command = "Insert your Command here";

    BaseComponent[] components = TextComponent.fromLegacyText(displayedMessage);
    ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND,command);

    for (BaseComponent component : components) {
        component.setClickEvent(clickEvent);
    }

    player.spigot().sendMessage(components);
     
     
  5. Can I run a method in my plugin too? I hope so
     
  6. Mas

    Mas

    • Useful Useful x 1
  7. Alright, I tried this:

    Code (Text):
    package me.kyllian.Translated.listeners;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import net.md_5.bungee.api.chat.BaseComponent;
    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 OnAsyncPlayerChatEvent implements Listener {
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent e) {
            String mes = e.getMessage();
            String command = "translate " + e.getMessage();
           
            BaseComponent[] components = TextComponent.fromLegacyText(mes);
            ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, command);
            HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click on me!").create());
           
            for (BaseComponent component : components) {
                component.setClickEvent(clickEvent);
                component.setHoverEvent(hoverEvent);
            }
           
            e.setMessage(components);
        }
    }
     
    But I cant put the message in the e.setMessage method, .toString() doesn't work too.

    Is there any way of doing this? I've seen it before
     
  8. Cancel the message.

    Iterate over the receivers
    and do player.spigot.sendMessage(components);
     
  9. Actually, you can't just set that as the message, but you can cancel the event and send the message to every player
     
  10. Mas

    Mas

    AsyncPlayerChatEvent#setMessage can only take a string as the argument.
    Best workaround is as said above, iterate through e.getRecipients() and send them the components with Player#spigot().sendMessage(components);
     
  11. Alright, I kinda understand it.

    When I do this:

    Code (Text):
    package me.kyllian.Translated.listeners;

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

    import net.md_5.bungee.api.chat.BaseComponent;
    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 OnAsyncPlayerChatEvent implements Listener {

        @EventHandler(priority = EventPriority.LOWEST)
        public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent e) {
            String mes = e.getFormat().replace("%1", e.getPlayer().getName()).replace("%2", e.getMessage());
            String command = "translate " + e.getMessage();

            BaseComponent[] components = TextComponent.fromLegacyText(mes);
            ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, command);
            HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                    new ComponentBuilder("Click on me!").create());

            for (BaseComponent component : components) {
                component.setClickEvent(clickEvent);
                component.setHoverEvent(hoverEvent);
            }

            for (Player online : Bukkit.getOnlinePlayers()) {
                online.spigot().sendMessage(components);
            }
        }
    }
    The output is:

    [​IMG]

    why?
     
  12. because you haven't cancelled the event first
     
  13. I know, But I wanted to compare them. I mean, there isn't any colour in the messages. That should be in the format, right?
     
  14. looks like the chatcolor for the op players in the default chat
     
  15. Indeed, but I need to make sure the color is in the message too, any idea how?
     
  16. hmm maybe you could check if the player is op and then add the color there too
     
  17. If you cancel PlayerChatEvents in order to rewrite - modify them, don't forget about logging.
     
    • Like Like x 1
  18. Alright I tried the following:

    Code (Text):
    package me.kyllian.Translated.listeners;

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

    import me.kyllian.Translated.Translated;
    import net.md_5.bungee.api.chat.BaseComponent;
    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 OnAsyncPlayerChatEvent implements Listener {

         private Translated main = Translated.getInstance();

        @EventHandler(priority = EventPriority.LOWEST)
        public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent e) {
            // e.setCancelled(true);
            Bukkit.broadcastMessage(e.getFormat());
            String mes = e.getFormat().replaceFirst("%1", e.getPlayer().getName()).replace("%2", e.getMessage());
            String command = "translate " + e.getMessage();

            BaseComponent[] components = TextComponent.fromLegacyText(mes);
            ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, command);
            HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                    new ComponentBuilder("Click on me!").create());

            for (BaseComponent component : components) {
                component.setClickEvent(clickEvent);
                component.setHoverEvent(hoverEvent);
            }

            for (Player online : Bukkit.getOnlinePlayers()) {
                online.spigot().sendMessage(components);
            }
            Bukkit.getScheduler().scheduleSyncDelayedTask(main ,new Runnable(){
                @Override
                public void run() {
                    Bukkit.broadcastMessage(e.getFormat());
                }
            }, 20);
        }
    }
     
    Output:

    [​IMG]

    It just f*cking ignores the format
     
  19. Guess this plugin idea is not possible then, Nice.....
     
    • Like Like x 1
  20. Idk what u mean sorry cant help :C