JSON Unicode character to unicode value

Discussion in 'Plugin Development' started by travja, Jun 9, 2014.

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

    travja

    NOT ORIGINAL QUESTION

    I need a way to convert unicode characters to their unicode values as if Minecraft were sending a packet of JSON. So before it sends, I would need to convert things like ' to \u0027. But the problem is, I don't know what characters are replaced and I don't know how to do it.
     
  2. Offline

    RingOfStorms

    So what are you asking? Alternatives or what? As far as I know packet interception is the only reliable way
     
  3. Offline

    Plo124

    travja
    I would just make a new Chat Handler, in which you have a chatQueue, and every second or so (remember it can lag some clients), send the gui and messages

    Edit: Youve edited your post which makes my post off context and makes me look lile an idiot. Please just make a new thread for this, edit is not for making a new post.
     
  4. Offline

    travja

    RingOfStorms I can get the packet but I don't know how to read what was sent in the packet. That is what I need help with.
     
  5. Offline

    RingOfStorms

    travja
    Alright so I dove into the client code a bit to see how it parsed the chat packet client side. It looks like you can get IChatBaseComponent from the chat packet, and inside of that there are two functions, one that will return an unformatted version of the message, and one with formatting. So you can use either to get what the message actually is. I havent tested any of this yet, but should work. I'm not entirely sure how to do this in protocol lib, but here is some nms examples.

    In Protocol lib, you can get the packet object by doing event.getPacket().getHandle() I do believe, then cast it to a PacketPlayOutChat.

    So first off get your chat packet. Then use reflection to get the private field "a", which is the IChatBaseComponent:
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/PacketPlayOutChat.java#L5

    From my understanding from the client code (http://i.gyazo.com/c2cac1df654b393988033f46f8453ff5.png)
    the obfuscated functions e() and c() are unformatted and formatted respectively.
    So knowing that, the next step is to call either c or e based on your needs (do you want formatted or just the basic text of the message)
    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/IChatBaseComponent.java#L15-17

    Some pseudo code: (Wrote this in the forum post and I haven't tested any of it)
    Code:java
    1.  
    2. //... Inside your packet protocol lib event
    3. PacketPlayOutChat chat = (PacketPlayOutChat) event.getPacket().getHandle();
    4.  
    5. Field chatBase = PacketPlayOutChat.class.getDeclaredField("a");
    6. chatBase.setAccessible(true);
    7. IChatBaseComponent chatBaseObj = (IChatBaseComponent) chatBase.get(chat);
    8.  
    9. String unformattedText = chatBaseObj.e();
    10. String formattedText= chatBaseObj.c();
    11.  
    12. //Do stuff
    13. [/java]
     
  6. Offline

    travja

    RingOfStorms I've adapted my current code enough that all I need is the JSON string of the message sent. Just so I can be able to keep hover events and click events. Would that be in the formatted text?
     
  7. Offline

    RingOfStorms

    Not 100% sure, I guess you can do some debugging and print out the two strings to the console to see the differences.
     
  8. Offline

    travja

    RingOfStorms ok, so formatted text helps me a little bit. It can give me the plain string so I can see how long it is. Here is the biggest problem I face now... how do I split the JSON and make it 2 JSON strings.

    So basically I'm making a menu something like this:
    Menu (open)

    -------=[ Welcome to our server ]=--------
    INFO
    --------------------------------------------
    CHAT
    CHAT....


    I need the chat to be updating according to incoming messages. Which works great with the system I have right now which purely takes the JSON of incoming messages and stores it for when the menu updates. The only problem is, when the incoming message is longer than 1 line in chat, it pushes the menu up out of the visible field in game. So basically what I need to do is be able to split the JSON string at about the 50th character but still have the formatting (hover events, click events, etc) attached to the part for the first line and the part for the 2nd line.

    If that makes any sense and you have a solution, please help! Thank you! -Trav
     
  9. Offline

    RingOfStorms


    Hmm I think you've hit some hairy waters with this one. It's going to require quite the custom parser on your end I think. Imagine if the hover encapsules both the two lines, but there is something else on only part of one line. You'd have to duplicate the effect and split the text somehow. Also there is no real way to determine where the second line occurs. You have bold text, re-sizable GUI scaling in the client and a bunch of other stuff that makes this kinda guess and hope they have default settings on.

    Have you thought of any other methods for the UI you need? Maybe the chat just isn't meant to be used in the way you want to :p

    When it comes to the splitting I'm not entirely sure how to. I think you'll need to learn all the minecraft chat json formatting and really understand it so you can make a special function that can read and split it up how you want.
     
  10. Offline

    travja

    New developments, I got it working. I just have a problem with unicode contained in the message. Things like ' or & get converted to their respective unicode values. I have no idea how to convert them before I store them.

    RingOfStorms I got it all figured out except my above post. That may be something you can help me with. Just converting ' to \u0027 before sending/storing it.

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

    Plo124

    travja
    2 things.

    1. Dont bump your post after 3mins. The rule is 24 hours.
    2. Why didnt you just make a new thread about this new question. it makes me and other people who helped you look like idiots because we are posting offtopic stuff like that.
     
  12. Offline

    travja

    I'm sorry. I didn't mean for it to be a bump.. Just an additional comment. And I didn't create a new thread as it kind of relates to it. All in all, I figured things out with minimal help from you guys as I figured it out myself.
     
  13. Offline

    Plo124

    travja
    Ok, mark thread as solved, and possibly post your solution so other people who have the same issue could possibly get a solution too.
     
Thread Status:
Not open for further replies.

Share This Page