[HELP] minecraft 1.7 get motd from other server

Discussion in 'Plugin Development' started by mechoriet, Aug 12, 2014.

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

    mechoriet

    Hi,

    I`m trying to get the motd of a different server in my setup but

    how to i catch and reviece the json notes and handle them so i can show the on a bord/sign

    for a minigame lobby

    i was looking arround bukkti forums but only found the old stuff, that you had to stript the code with a symbol

    but i read somewhere that they changed it in 1.7 to a json packet thingy but how do i catch it and process it so i can use it


    Kind regards.
    Mechoriet.
     
  2. Offline

    mechoriet

    is there a way to fix this fast else i`m gonna contruct a mysql thing for the meanwhile of i can go true with my dev of my network
     
  3. Offline

    Gater12

  4. Offline

    mechoriet

    Gater12
    but how do i make a contruction to fetch/recieve it
    i know they changed it in 1.7 to a json response but how do i process it to get the motd of a server
     
  5. Offline

    xTigerRebornx

    mehcoriet Either parse the JSON yourself or use a library (like Google's GSON) to parse it for you
     
  6. Offline

    mechoriet

    is there a premade thing or do i need to get indepth with it to write my own thing
     
  7. Offline

    xTigerRebornx

    mehcoriet There is probably a snippet somewhere, you'll have to look hard to find it though.
     
  8. Offline

    MCMatters

    When I get in my computer I'll give the code, but it starts by opening a new Socket(ip, port);
    Then a datainputstream in = socket.getImput(); (or maybe socket.getOutput(); )
    Then dataoutputstream out = socket.getImput(); (or maybe socket.getOutput(); )
    I don't know the decoding atm
     
  9. Offline

    bobacadodl

  10. Offline

    fireblast709

    How about no spoonfeed?
    Doesn't really look like what he needs (since he needs to request it, and that request isn't really treated in the resource linked)
    mehcoriet What you need to do, is opening a (client) Socket, and manually write packet fields into the InputStream in order to trigger the server to reply with the MOTD.
    Code:
    socket = create_socket(host, post);
     
    // First we write a Handshake packet
    // Handshake packet ID
    writeInt(socket, 0);
    // protocol, 4 for 1.7.2+
    writeInt(socket, 4);
    // The server would like to know which hostname we used
    // And which port we used. This can be used to proxy players to different servers
    // But that is outside the scope of this snippet ;3
    writeString(socket, host);
    writeInt(socket, port);
    // Next we write our next state. This informs the server with our intentions
    // 1 = status (ping, MOTD), 2 = login (actual attempt to join)
    writeInt(socket, 1);
     
    // Now that the server knows we want the MOTD, it will set our
    // PacketListener to a PacketStatusListener, so it knows how to reply
    // to our requests. If you used login, it will be a LoginListener, which will cause
    // some confusion between the server and the client. When you send a packet
    // with ID 0, the client intents to request the status of the server, while
    // the server thinks (if the next state above is 2) that we try to login.
    //
    // Poor server...
     
    // To move on, we start by actually writing the status request
    // Status Request packet ID
    writeInt(socket, 0);
     
    // Now... we wait. But let's not be idle while we do.
    // We immediately start of by waiting for a packet ID
    id = readInt(socket)
    if(id != 0)
        throw error "Whoops, it seems the server didn't understand us :c"
    // It seems we came to a mutual understanding, let's read the data
    length = readInt(socket);
    bytes = readByteArray(socket);
    // A small bonus for the byte[] -> String conversion
    json = new String(bytes, Charsets.UTF_8);
    Then all that rests, is to parse the JSON.
     
  11. Offline

    mechoriet

    fireblast709 hi thanks for the code and annotations so i knolw whats happening gonna code it today i`m letting the thread open if i get stuck
     
  12. Offline

    thijs_a


    Hi,

    When i use your code, it doesn't know anything of the code
    Like
    Code:java
    1. writeInt(socket, 0);

    He doesn't know what write int is.
    Do you know what i must put in front of every peace of code?

    - Thijs
     
  13. Offline

    xTigerRebornx

    thijs_a Its not code, its pseudocode, as in, its only concepts, not actual code. If you want code that does it, write it based off of that logic.
     
  14. Offline

    MCMatters

    Code:java
    1. public String getMOTD(String ip, int port){
    2. try {
    3. Socket socket = new Socket(ip, port);
    4. DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    5. DataInputStream in = new DataInputStream(socket.getInputStream());
    6.  
    7. out.write(0xFE);
    8.  
    9. int b;
    10. while ((b = in.read()) != -1) {
    11. if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24) {
    12. str.append((char) b);
    13. }
    14. }
    15.  
    16. String[] data = str.toString().split("§");
    17. socket.close();
    18. return data[0];
    19. } catch (IOException e) {
    20. return ChatColor.DARK_RED + "Server Offline!";
    21. }
    22. }


    MOTD = args[0];
    ONLINE PLAYERS = args[1];
    MAX PLAYERS args[2];

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

    xTigerRebornx

    MCMatters Would help if you actually read the post, he wants help with creating a version that works for the new Protocol updates, which use JSON now rather then the § symbol.
     
Thread Status:
Not open for further replies.

Share This Page