JSON Chat Lib - Easily create and send JSON messages to players.

Discussion in 'Resources' started by bobacadodl, Oct 27, 2013.

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

    bobacadodl

    EDIT: I recommend checking out Fanciful! Its quite a bit easier to use than this library :)

    https://github.com/bobacadodl/Bukkit-JSONMessageLib

    As you probably know, minecraft added several neat chat features. Using the /tellraw command, you can send messages to players
    • with custom colors
    • that display text when you hover over with your cursor
    • that performs or suggest a command when you click on it
    • that opens a url when you click on it
    With this lib, you can easily generate the JSON for these commands!
    It can send the text directly to the player, without using /tellraw, with packets. (Bukkit will probably add support for this very soon)

    To use this in your project, simply include the package org.json.simple, and all the files in com.bobacadodl.JSONChatLib except "JSONChatLib".

    the JSONChatLib class includes a simple usage example.

    Code:
            JSONChatMessage message = new JSONChatMessage("Hey, ", JSONChatColor.AQUA, null);
            JSONChatExtra extra = new JSONChatExtra("<Click This>", JSONChatColor.BLUE, Arrays.asList(JSONChatFormat.BOLD));
            extra.setHoverEvent(JSONChatHoverEventType.SHOW_TEXT, "Example Hover Text");
            extra.setClickEvent(JSONChatClickEventType.RUN_COMMAND, "/example-command");
            message.addExtra(extra);
            message.sendToPlayer(event.getPlayer());
    You can also use message.toString() to get the JSON (for when bukkit adds support for sending the json without packets :p)


    Suggestions and comments are welcome! Please let me know if you find any bugs!
     
    GregMC, spoljo666, ferrybig and 7 others like this.
  2. Offline

    stirante

    bobacadodl There is also open_file click event. And you don't have any item to json method or I just couldn't find any. Achievement enum would be cool + method to translate strings. I think that's all.
     
  3. Offline

    bobacadodl

    What does the open_file click event do? I can't find any good website explaining these
     
  4. Offline

    stirante

    It's probably used to open screenshots you took by clicking message about it in chat.
     
    bobacadodl likes this.
  5. Offline

    bobacadodl

    I'm currently making the library even easier to use, stay tuned :)

    EDIT: Apparently craftbukkit has a library for creating JSON already :O! I'm now working on converting to that.
     
  6. Offline

    spoony_loony

    This is wonderful!!!
    I patched up some of the work to live with protocollib.
    In the JSONChatMessage class replace the "senderToPlayer(Player player) method with the following:
    Code:
    public void sendToPlayer(Player player) {
    ProtocolManager manager = ProtocolLibrary.getProtocolManager();
    PacketContainer messagePacket = manager.createPacket(PacketType.Play.Server.CHAT);
    messagePacket.getChatComponents().write(0, WrappedChatComponent.fromJson(chatObject.toJSONString()));
    try {
    manager.sendServerPacket(player, messagePacket);
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    
    This is compatible with the 1.7.2 dev builds...
     
    bobacadodl likes this.
  7. Offline

    jorisk322

    In my opinion this is a core-part of the server, and should be supported by the Bukkit API. Any plans on creating a PR?
     
  8. Offline

    BungeeTheCookie

    Does everything awesome in the world have to have NMS???
     
    TheBigSuperCraft likes this.
  9. Offline

    bobacadodl

    This only includes one line of NMS. I'd say theres a 90% chance that it wont require NMS after bukkit finishes the API for 1.7.2
     
  10. Offline

    Garris0n

    Yes.
     
    glen3b likes this.
  11. Offline

    BungeeTheCookie

    Reflection plz??
     
  12. Offline

    Garris0n

    Reflection is for the weak. No, but really the util doesn't exactly work in its current state anyway. I made a PR....maybe somebody should translate it into reflection?
     
  13. Offline

    BungeeTheCookie

    Lol
     
  14. Offline

    Plo124

    bobacadodl
    How do you use the Craftbukkit JSON message?
     
  15. Offline

    spoony_loony

    bobacadodl
    Haha! I extended on this API, and made an API plugin on BukkitDev for this. It changes up the API a little, but makes it super simple to understand if new to code. http://dev.bukkit.org/bukkit-plugins/chatlib/
    I gave full credit to you bobacododl, because all the code except two classes are his.
     
  16. Offline

    Tirelessly

    spoony_loony What a total dick move (excuse my language). Did you even ask beforehand? Also, you only give credit to him here, not on the plugin page.
     
  17. Offline

    bobacadodl

    Tirelessly
    Its not a big deal. IMO, stuff in the resources section is meant to be free to use/modify. :)
     
  18. Offline

    Lionhard

    bobacadodl Hello, could you please explain me a bit more how to install this? I'm new to bukkit and I never before used libraries in my sourcecode, just external Jars like Vault etc.
    I copied both packages into my src folder and deleted the JSONChatLib.java, but it gave me errors. It seemed allright, because all other classes were OK without errors, just the JSONChatMessage.java gave me errors. Here they are:
    import net.minecraft.server.v1_7_R3.Packet3Chat;
    - The import net cannot be resolved.
    import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    - the import org.bukkit.craftbukkit cannot be resolved.

    Didnt work either with v1_6_R3, just tried to change to my current version.

    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(ChatSerializer.a(chatObject.toJSONString()), true));
    - Multiple markers at this line
    - CraftPlayer cannot be resolved to a type
    - ChatSerializer cannot be resolved
    - CraftPlayer cannot be resolved to a type
    - PacketPlayOutChat cannot be resolved
    to a type
    - CraftPlayer cannot be resolved to a type

    Can you please help me? :(
     
  19. Offline

    bobacadodl

    Lionhard
    You should make sure that you have the craftbukkit library referenced, because this uses NMS code. Perhaps you only have the bukkit library referenced?
     
  20. Offline

    Lionhard

    bobacadodl How to reference a craftbukkit library? Import it the same way like bukkit? In the Java build thing :D
     
  21. Offline

    bobacadodl

  22. Offline

    Lionhard

  23. Offline

    Lionhard

    bobacadodl As you told now about the fanciful Lib, I have actually seen it before you posted, but is there another way to install instead of that maven thing?
     
  24. Offline

    Garris0n

    You could download the source and jut put it in your plugin, I suppose...
     
  25. Offline

    Lionhard

    Garris0n Tried, doesn't work. timtower helped me alot, thanks to him, now I got this one to work but it will probably break in future updates (If iam not wrong) and so he gave me a link to the same ChatLib like this one, just which uses reflections, which gives me weird errors. There was first an error in the syntax, just had to enter a " at the end. then there came an error with bukkit not resolved, had to import and then a ton of errors threw up with solutions like: add throws excetion, surround with try / catch etc. When I used throws exception, and wanted to use it in my code i got again errors... :((
     
  26. Offline

    timtower Moderator Moderator

    Lionhard Create your plugin with this version, when you are done hand me the code and i will add reflection in it
     
    Garris0n and Lionhard like this.
  27. Offline

    Lionhard

    timtower :O cool, that would be awesome! Thanks!

    bobacadodl Hey, actually, are you able to add the extra somewhere in between the message? like have the message be "You have reached " <extra> " xp. click here to level up". (or something similar, was just an example.)

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

    Garris0n

    Yes just add two extras, the second one being "xp. click here to level up.".
     
  29. Offline

    Lionhard

  30. Offline

    Dragonphase

    bobacadodl
    stirante
    Add this to the JSONChatExtra.java file. There's probably an easier way to do this though:

    Code:java
    1. public void setHoverEvent(JSONChatHoverEventType action, ItemStack itemStack) {
    2. JSONObject hoverEvent = new JSONObject();
    3. hoverEvent.put("action", action.getTypeString());
    4.  
    5. String name = "" + (itemStack.getItemMeta().getDisplayName() != null ? itemStack.getItemMeta().getDisplayName() : capitalize(itemStack.getType().toString()));
    6. String value = "{id:" + itemStack.getTypeId() + ",Damage:" + itemStack.getDurability() + ",Count:" + itemStack.getAmount() + ",tag:{display:{Name:\"" + name + "\"";
    7.  
    8. if (itemStack.getItemMeta().hasLore()){
    9. value = value + ", Lore:[";
    10.  
    11. for (String lore : itemStack.getItemMeta().getLore()){
    12. value = value + (itemStack.getItemMeta().getLore().size() == 1 || itemStack.getItemMeta().getLore().get(itemStack.getItemMeta().getLore().size()-1) == lore ? ("\"" + lore + "\"") : ("\"" + lore + "\", "));
    13. }
    14.  
    15. value = value + "]";
    16. }
    17.  
    18. value = value + "}}}";
    19.  
    20. hoverEvent.put("value", value);
    21.  
    22. chatExtra.put("hoverEvent", hoverEvent);
    23. }
    24.  
    25. public static String capitalize(String string){
    26. String[] NameList = string.toLowerCase().replace("_", " ").replace("-", " ").split(" ");
    27. String Name = "";
    28.  
    29. for (String Word : NameList){
    30. Name += Word.substring(0, 1).toUpperCase() + Word.substring(1).toLowerCase() + " ";
    31. }
    32.  
    33. if (Name.endsWith(" ")) Name = Name.substring(0, Name.length()-1);
    34. return Name;
    35. }
     
Thread Status:
Not open for further replies.

Share This Page