JSON and PlayerChatEvent Help!

Discussion in 'Plugin Development' started by dlange, Mar 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    dlange

    So i am making a plugin where when a player talks it adds a prefix to their chat and when you click it it takes you to a site.
    This is what i got:
    Code:
        public void chatClickable1(Player p, String link1, String link2, String message) {
            String url = link1+p.getName()+link2;
            IChatBaseComponent comp = ChatSerializer.a("{\"text\":\"[L]\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Open player's panel\"},\"clickEvent\":{\"action\":\"open_url\",\"value\":\""+url+"\"},\"extra\":[{\"text\":\""+message+"\"}]}");
            PacketPlayOutChat packet = new PacketPlayOutChat(comp);
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
    For the JSON method.
    This is how i use it:
    Code:
                   event.setCancelled(true);
                    String message2 = (event.getPlayer().getDisplayName() + ChatColor.GRAY + ": " + ChatColor.WHITE + event.getMessage());
    
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        if (p.hasPermission("staff")) {
                            chatClickable1(p, link1, link2, message2);
                        } else {
                            p.sendMessage(message2);
                        }
                    }
    Now it works, but every time i send a message with MORE than 1 word it sends it the amount of words i have in it. This is a huge problem for me.
    If anyone can help it would be very appreciated!

    ~ dlange
     
  2. What do you mean it sends the amount of words? That it sends the message for each word you have entered, the amount of words in an int value or something else?
     
  3. Offline

    dlange

    @megamichiel say i typed: "Hello i am dlange", that sentence has 4 words, so it sends the message 4 times
     
  4. @dlange
    This part of code looks fine, would you mind showing the whole method?
     
  5. Offline

    nlthijs48

  6. Offline

    dlange

    @megamichiel Sure -
    Code:
        public void chatClickable1(Player p, String link1, String link2, String message) {
            String url = link1+p.getName()+link2;
            IChatBaseComponent comp = ChatSerializer.a("{\"text\":\"[L] \",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Open player's ban panel\"},\"clickEvent\":{\"action\":\"open_url\",\"value\":\""+url+"\"},\"extra\":[{\"text\":\""+message+"\"}]}");
            PacketPlayOutChat packet = new PacketPlayOutChat(comp);
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
    and this is how i use it in PlayerChatEvent -
    Code:
                {
                    event.setCancelled(true);
                    String link1 = "privateurl1";
                    String link2 = "privateurl2";
                    String message2 = (event.getPlayer().getDisplayName() + ChatColor.GRAY + ": " + ChatColor.WHITE + event.getMessage());
    
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        if (p.hasPermission("orchestra.staff")) {
                            chatClickable1(p, link1, link2, message2);
                        } else {
                            p.sendMessage(message2);
                        }
                    }
                }
    And @nlthijs48 ill try that thanks

    EDIT: @nlthijs48 can you explain how i would use that?
     
  7. @dlange
    The code should be fine, I can't find anything that is wrong.
    About what nlthijs48 said, you can just copy the method inside FancyMessageFormatConverter to your class, and then use instead of calling e.getMessage() use quoteStringJson(e.getMessage())
     
  8. Offline

    dlange

    @megamichiel Ah ok thanks i will try this!

    @megamichiel That didn't work, but i messed around a bit and found the problem, this line:
    Code:
            IChatBaseComponent comp = ChatSerializer.a("{\"text\":\"[L] \",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"View player's table\"},\"clickEvent\":{\"action\":\"open_url\",\"value\":\""+url+"\"}}");
    BUT when i add the 'message' string to the end it doesn't work. How would i add another text area after that with an already defined string?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  9. Offline

    dlange

    Bump! :)
     
  10. Offline

    nlthijs48

    @dlange Do you get a stacktrace when you try to use it? Because that might be very useful to post here. It might be helpful to first create the tellraw string you need with a tool like minecraftjson.com with simply some placeholder text values in it, and after that change it for use in your code (escape special characters, replace placeholders with correct values).
     
Thread Status:
Not open for further replies.

Share This Page