Better way to do this

Discussion in 'Plugin Development' started by Konkz, Jan 20, 2014.

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

    Konkz

    Basically this code:

    Code:java
    1. ((CraftPlayer) player).getHandle().playerConnection
    2. .sendPacket(new PacketPlayOutChat(
    3. ChatSerializer
    4. .a("{\"text\":\"\",\"extra\":[{\"text\":\"Go on me you poo\",\"color\":\"dark_green\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"String inside the box\"}}]}")));


    Does this:
    [​IMG]

    How can I re-make this line, get same affect but where it says 'String inside this box' I can make it so it says:
    Code:
    Health: Player Health
    ^ Instead of Player Health I'd get the actual value from p.getHealth();
     
  2. Offline

    CraftBang

    You can just close the " than do + player.getHealth() ? I'm not really into packets but I guess you can do something like that?
     
  3. Offline

    Konkz


    No - that does not work. I tried multiple ways of adding player.getHealth() but none of them gave me a result without error.
     
  4. Offline

    Stoux

    Sadly I do not have an answer for you. I'm very interested in this tho. Do you have a link to more info about the possibilities with that package/chatserializer?
     
  5. Offline

    _Cookie_

    Konkz
    Maybe use
    double d = p.getHealth();
     
  6. Offline

    Konkz

    Could you add that into the code I added? I'm not by my computer and I can't try to visualise how it would look like.
     
  7. Offline

    _Cookie_

    I would but I'm on my iPad sorry haha
     
  8. Offline

    Konkz

    Haha, I'm on my iPhone.

    Stoux - I will link you in 6 hours when i'm on PC, I find the methods very complicated but this was the only way I worked around it.
     
    Stoux likes this.
  9. Offline

    Warreo

    Konkz

    Does putting any sort of " + something + " break it? Because maybe you could try parsing the health double to a string then adding it?
     
  10. Offline

    Konkz

    Tried that, I tried +, " ", /, : - everything I can think of.
     
  11. Offline

    Warreo

    Konkz This is most definitely interesting, any chance a string builder works? Or even defining the string prior than the argument?
     
  12. Offline

    Konkz


    Thanks to Mojang, this was the most strict way this works.
     
  13. Offline

    Warreo

    Konkz Ok, so I went and looked into this some, obviously the method uses a JSON string, so what you should do is use the JSON library and use the toJsonString() method if you want to include variables that are not a static known. Not 100% sure that will work, but it's a good starting point. :)
     
  14. Offline

    Konkz


    My friend which is a Developer, a really good one too, is able to understand this. As soon as he finishes his current project he will help me with this.
     
  15. Offline

    Tirelessly

    I can't imagine a scenario in which this wouldn't work:

    Code:
    String.format("{\"text\":\"\",\"extra\":[{\"text\":\"Go on me you poo\",\"color\":\"dark_green\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Player Health: %.2f\"}}]})", player.getHealth());
    
     
    Warreo likes this.
  16. Offline

    Konkz

    I didn't test that as I was busy head butting the wall. Seems like it would, but I got this to work:
    Code:java
    1. .a("{\"text\":\"\",\"extra\":[{\"text\":\"Go on me you poo\",\"color\":\"dark_green\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Players Health: " + String.valueOf(player.getHealth()) + "\"}}]}")));
     
  17. Offline

    CraftBang

    Konkz that's what I told you, you can just add the + and than another string..
    Nothing really special at all xD
     
  18. Offline

    Konkz

    gg im a blonde.
     
    CraftBang likes this.
  19. Offline

    Tirelessly

    Konkz Might be better to use my method so that there's no chance players get: "Player Health: 20.0000000001"

    If that happens, use what I did.
     
    CraftBang likes this.
  20. Konkz Sorry if this is irrelevant but where did you hear about ChatSerializer?
    I'm mainly asking cause I think it would be cool to use for some plugins but I don't know all the methods like a click event. I've tried searching for docs on it but cannot find them

    Thanks
     
  21. Offline

    Warreo

  22. Offline

    d3v1n302418

    You can always try String.valueOf(p.getHealth()) somewhere in there. But I'm not quite sure...
     
  23. Offline

    Jake6177


    Did you read at all? Not to be rude, but that was in the code he showed.
     
    Warreo likes this.
  24. Offline

    Konkz

    My friend, I think he got it off a JSON Code Syntax somewhere. Not too sure.

    I really do not understand that, I looked at it multiple times and I just do not understand it.

    Gg wp

    Thanks for clearing that up. :)
     
  25. Offline

    Warreo

    Konkz Haha maybe this will make it more clear then, just look at the actual code. :)
     
  26. Offline

    Konkz


    I tried, but I can't see how to make it work etc. I will be doing more research into it, right now I'm continuing learning Java, whereas JSON and stuff is more ahead of the level I am at right now.
     
  27. Offline

    Warreo

    Konkz That link I had, shows the actual code used, so you can literately use the string you had on the topic and interchange bits and pieces with ones from that code, the code just has different things you can do with that String.
     
Thread Status:
Not open for further replies.

Share This Page