JsonText

Discussion in 'Spigot Plugin Development' started by RayoxHD, May 17, 2017.

  1. Hello,

    I need to find out how to replace player chat name in the chatevent with a json text i already have the json tetx but it on display its in the text like so:
    http://prntscr.com/f8xyf3

    Code (Text):
        public static String getChatName(Player player) {
            return "{\"text\":\"PlayerName\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"§3Server Rank: §6" + getGroupPrefix(getGroup(player))
                    + "§3Name: §6" + player.getName()
                    + "§3UUID: §6" + player.getUniqueId()
                    + "§3Balance: §6" + getMoney(player)
                    + "§3Player Rank: §6" + getRank(player)
                    + "\"}}";
        }
    Code (Text):
    /*
    @Author: Takarien
    You may not publish this Code anywhere without the Permission of the Author.
    © 2017
    */
    package me.takarien.skyblockextra.listener;

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

    import me.takarien.skyblockextra.util.Core;

    public class ChatListener implements Listener{
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
           
            e.setFormat(player.getName() + " | " + Core.getChatName(player) + e.getMessage());
           
        }

    }
     
     
  2. will try and report back.
     
  3. Still not working im returning the TextComponent but it still displays the jsonstring.
     
    • Useful Useful x 1
  4. That's the problem. Don't use the fomat to set the message content. Neither use it to convert anything to JSON.
    To learn more about formats see: java.util.Formatter#syntax.
    If you want to modify the message, use #setMessage of the event, but this only accepts plain strings. So i guess you can't use this event to send JSON messages.
    Try:
    • Get the format and the message
    • Apply the format on the message
    • Set up your JSON component
    • Apply the formatted message on it
    • Cancel the event
    • Send the JSON message using Player.Spigot#sendMessage to all players
     
  5. So i got this JsonMessage and im sending it to all player via sendRawMessage but it still doesnt work. waht am i doing wrong? .-.
    Code (Text):
        public static IChatBaseComponent getChatName(Player player) {
            return ChatSerializer.a("{\"text\":\"PlayerName\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"§3Server Rank: §6" + getGroupPrefix(getGroup(player))
                    + "\n§3Name: §6" + player.getName()
                    + "\n§3UUID: §6" + player.getUniqueId()
                    + "\n§3Balance: §6" + getMoney(player)
                    + "\n§3Player Rank: §6" + getRank(player)
                    + "\"}}");
                   
        }
     
  6. #sendRawMessage is something completely different. It has nothing to do with JSON messages.
    #sendRawMessage is made for Conversation.

    EDIT: Furthermore please post the fully qualified path of ChatSerializer#a. Do you hook into NMS to deserialize a string into JSON text?
    Just don't!
    Use net.md_5.bungee.chat.TextComponentSerializer#deserialize or something like this.
     
    #8 Michel_0, May 17, 2017
    Last edited: May 17, 2017
  7. Chnaged it but still getting the raw String.
    Code (Text):
        public static BaseComponent getChatName(Player player) {
            return new TextComponent("{\"text\":\"PlayerName\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"§3Server Rank: §6" + getGroupPrefix(getGroup(player))
                    + "\n§3Name: §6" + player.getName()
                    + "\n§3UUID: §6" + player.getUniqueId()
                    + "\n§3Balance: §6" + getMoney(player)
                    + "\n§3Player Rank: §6" + getRank(player)
                    + "\"}}");
                   
        }
    Code (Text):
    /*
    @Author: Takarien
    You may not publish this Code anywhere without the Permission of the Author.
    © 2017
    */
    package me.takarien.skyblockextra.listener;

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

    import me.takarien.skyblockextra.util.Core;

    public class ChatListener implements Listener{
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            e.setCancelled(true);
           
            for(Player pl : e.getRecipients()) {
               
                pl.spigot().sendMessage(Core.getChatName(player));
               
            }
           
        }

    }
     
     
  8. I am working on something with that uses this text too. You really should look at the wiki that @Michel_0 provided.
     
    • Useful Useful x 1
  9. Looking into the doc was helpful here are my results:

    Code (Text):
        public static BaseComponent getChatName(Player player, String msg) {
            TextComponent text = new TextComponent(player.getDisplayName());
            text.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§3Name: §6" + player.getName()
                    + "\n§3UUID: §6" + player.getUniqueId().toString()
                    + "\n§3Server Rank: §6" + getGroupPrefix(getGroup(player)).replace("&", "§")
                    + "\n§3Balance: §6$" + getMoney(player)
                    + "\n§3Player Rank: §6" + getRank(player)
                    ).create()));
            text.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player.getName() + " "));
            text.addExtra(" §8>> "+ getMessageColor(player) + msg);
           
            return text ;
        }
    Code (Text):
    /*
    @Author: Takarien
    You may not publish this Code anywhere without the Permission of the Author.
    © 2017
    */
    package me.takarien.skyblockextra.listener;

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

    import me.takarien.skyblockextra.util.Core;

    public class ChatListener implements Listener{
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            e.setCancelled(true);
           
            for(Player pl : e.getRecipients()) {
               
                pl.spigot().sendMessage(Core.getChatName(player, e.getMessage()));
               
            }
           
        }

    }