[Tuturial] Get player' render distance, language, and more

Discussion in 'Resources' started by Plo124, Apr 23, 2014.

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

    Plo124

    Hi, I was looking on the Minecraft packet list, and came across this packet
    http://wiki.vg/Protocol#Client_Settings

    Then I thought it would be cool to be able to obtain a player's language and such, so I created a ProtocolLib handler for this.

    Code:java
    1. ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListener(){
    2. public Plugin getPlugin() {return Bukkit.getPluginManager().getPlugin("Amalthea");}
    3. public ListeningWhitelist getReceivingWhitelist() {return ListeningWhitelist.newBuilder().gamePhase(GamePhase.PLAYING).highest().types(PacketType.Play.Client.SETTINGS).build();}
    4. public ListeningWhitelist getSendingWhitelist() {return ListeningWhitelist.newBuilder().gamePhase(GamePhase.PLAYING).highest().types(PacketType.Play.Client.SETTINGS).build();}
    5. @SuppressWarnings("deprecation")
    6. @Override
    7. public void onPacketReceiving(PacketEvent e) {
    8. if (!(e.getPacket().getType() == PacketType.Play.Client.SETTINGS)) return;
    9. int renderdistance = wrapper.getIntegers().readSafely(0);
    10. int difficulty = wrapper.getIntegers().readSafely(2);
    11. String language = wrapper.getStrings().readSafely(0);
    12. // usually is en_US
    13. }
    14. }
    15. public void onPacketSending(PacketEvent e) {return;}
    16.  
    17. });


    This requires ProtocolLib which you can download here
     
    BlueMustache, Phasesaber and TigerHix like this.
  2. Offline

    bigteddy98

    Looks nice, but using TinyProtocol instead of ProtocolLib would make this way easier to implement it in our plugins I think.
     
    TigerHix likes this.
  3. Offline

    TigerHix

    Totally wow.
     
  4. Offline

    Plo124

    bigteddy98
    I havent used TinyProtocol yet, but your more than welcome to use the packet structure or my code anywhere else on the web.
     
  5. Offline

    GermanCoding

    Nice! Did not know that it is possible to get the client's locale, view distance, chat flags, chat colours, difficulty and whether capes are shown or not. Interesting possibilities...
     
  6. Offline

    Plo124

    GermanCoding
    Many possibilities. I've never seen this on a server before though.
     
  7. Offline

    crysis992

    I'm using this since a half year on my server ;) It's pretty nice and user friendly to send messages in the users locale
    Since i have lots of German and US players, I've created this little class

    Code:java
    1. public class language {
    2.  
    3. public void broadcastMessage(String de,String en) {
    4. for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    5.  
    6. if (getLanguage(p).equalsIgnoreCase("de")) {
    7. p.sendMessage(de);
    8. } else {
    9. p.sendMessage(en);
    10. }
    11. }
    12. }
    13. public void sendMessage(Player p, String de,String en) {
    14. if (getLanguage(p).equalsIgnoreCase("de")) {
    15. p.sendMessage(de);
    16. } else {
    17. p.sendMessage(en);
    18. }
    19. }
    20.  
    21. private static String getLanguage(Player p){
    22. try {
    23. Object obj = getMethod("getHandle", p.getClass()).invoke(p, (Object[]) null);
    24. Field f = obj.getClass().getDeclaredField("locale");
    25. f.setAccessible(true);
    26. String language = (String) f.get(obj);
    27.  
    28. if (language.equalsIgnoreCase("de_de")) {
    29. return "de";
    30. } else {
    31. return "en";
    32. }
    33. } catch (Exception e) {
    34. return "en";
    35. }
    36. }
    37.  
    38. private static Method getMethod(String n, Class<?> c) {
    39. for (Method m : c.getDeclaredMethods()) {
    40. if (m.getName().equals(n))
    41. return m;
    42. }
    43. return null;
    44. }


    It's alot more user friendy than having a global language for the plugins.
     
  8. Offline

    Plo124

    crysis992
    One of the many possibilities of this packet
     
  9. Offline

    BungeeTheCookie

    Plo124
    Im just gunna bump this for others to see

    Plo124
    But, how do you get a player's chat size?

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

    GermanCoding

  11. Offline

    Plo124

    BungeeTheCookie
    Not everything is sent, otherwise I would like to see Resource packs, and Master volume being sent to the server.
     
  12. Offline

    Gamecube762

    Interesting, would be good for creating a language lib for plugins where instead of the server defining the language, the players define it with their settings.
     
  13. Offline

    BlueMustache

    Plo124
    I really like your idea.
    Sadly, I am not very good at protocollib.
    Do you think you can make a LIB for this.
    Something like.

    player.getViewDistance()
    player.isCapesEnabled()
    etc..

    This would be greatly appreciated!
    - Blue :D
     
  14. Offline

    Plo124

    BlueMustache
    You just need to store a HashMap<UUID,Integer> of the render distance.
     
  15. Offline

    BlueMustache

    Plo124
    I know you could do that.
    I guess I'll try ProtocolLib again.
    Any good Tutorials on how to get started with it?
     
  16. Offline

    Plo124

    BlueMustache
    You can execute code after the int renderdistance, and then use that to set the HashMap.
     
  17. Offline

    Ultimate_n00b

    You know, Minecraft stores these in the EntityPlayer object. Go check it out.
     
    LegitJava and AdamQpzm like this.
Thread Status:
Not open for further replies.

Share This Page