Connecting and comunicating with server

Discussion in 'Plugin Development' started by Welite, Jun 14, 2014.

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

    Welite

    Hello, maybe this is not a right section for my question but I would like to ask more experienced java-minecraft programmers. I am trying to make a desktop and android app that would communicate with any minecraft server.


    Let me explain:
    I would like to make app that can connect to a minecraft server without minecraft client. Basically something like a "bot" would connect to the server and only feature I want is to let this "bot" write messages so I can for example chat from my smartphone with in-game minecraft players.



    There is my code, but there is something wrong, the server is not responding to my packets and I dont know why.


    Code:java
    1. public static void main(String args[]) throws IOException{
    2. Socket server = null;
    3. DataInputStream serverIn = null;
    4. DataOutputStream serverOut = null;
    5. BufferedReader input = null;
    6. int ConenctionHash, EID = 0;
    7. try {
    8. System.out.println("Server IP:");
    9. String serverIpString = input.readLine();
    10. System.out.println( "Port:");
    11. int port = Integer.parseInt(input.readLine());
    12.  
    13.  
    14.  
    15. server = new Socket(serverIpString,port);
    16. serverIn = new DataInputStream(server.getInputStream());
    17. serverOut = new DataOutputStream(server.getOutputStream());
    18.  
    19. //handshake
    20.  
    21. serverOut.writeByte(0x00);
    22. writeString("BOT;" + serverIpString + ":" + port, serverOut);
    23.  
    24. System.out.println(serverIn.read());
    25.  
    26. serverOut.writeByte(0x02);
    27. writeString("BOT;" + serverIpString + ":" + port, serverOut);
    28.  
    29.  
    30. System.out.println("Test...");
    31. System.out.println(serverIn.read());
    32.  
    33.  
    34. //Here it always returns -1
    35. if (serverIn.read() == 2) { //handshake response
    36. ConenctionHash = serverIn.readInt();
    37. System.out.println("handshake: " + ConenctionHash);
    38.  
    39. //login
    40. serverOut.writeByte(0x01);
    41. serverOut.writeInt(29);
    42. writeString("BOT",serverOut);
    43. writeString("",serverOut);
    44. serverOut.writeInt(0);
    45. serverOut.writeInt(0);
    46. serverOut.writeByte(0);
    47. serverOut.writeByte(0);
    48. serverOut.writeByte(0);
    49.  
    50.  
    51.  
    52. if (serverIn.read() == 1) { //login response
    53. System.out.println("Connected");
    54. while (true) {
    55. byte in = serverIn.readByte();
    56. System.out.println(in);
    57. if (in == 0x00) { //keep alive
    58. serverOut.writeByte(0x00);
    59. serverOut.writeInt(serverIn.readInt());
    60. }
    61. }
    62. }
    63. }
    64. } catch (UnknownHostException e) {
    65. System.err.println("Bad host");
    66. System.exit(1);
    67. } catch (IOException e) {
    68. System.err.println("I/O error :-{");
    69. System.exit(1);
    70. } catch (Exception e) {
    71. System.err.println("Bad IP?");
    72. System.exit(1);
    73. } finally {
    74. serverIn.close();
    75. serverOut.close();
    76. server.close();
    77. input.close();
    78. }
    79. }
    80.  
    81. public static void writeString(String string, DataOutputStream serverOut) {
    82.  
    83. try {
    84. serverOut.writeShort(string.length());
    85. serverOut.write(string.getBytes(Charset.forName("UTF-16")));
    86. } catch (Exception e) {
    87. e.printStackTrace();
    88. }
    89. }
    90.  
     
  2. Offline

    TheHandfish

    Uh...

    How does this have to do with Bukkit, again?
     
  3. Offline

    Welite

    Nothing but on Bukkit forums there is still section for stand-alone applications and I think I could public it here when its done, because chatting from your smartphone would be quite interesting :)
     
  4. Offline

    TheHandfish

    Yeah, but this is more of a general Java thing. It doesn't have to do with the API at all so, in my opinion, it doesn't really belong in here, or at least this section.

    This is for plugin development after all.
     
  5. Offline

    gjossep

    I dont think you can replicate a bot on the server, as it would make you replicate all of the packets that would be normaly be sent to the server. If you really want to try this is by trying to write a program that will run the server but also reads input over a socket that the program will write to the server command promt
     
  6. Offline

    Welite

    What ? Sorry but I absolute do not understand what you wrote.
     
  7. Offline

    gjossep

    Haha, sorry its late. Al try again. You can try to create all the same packets to act as a bot, but im not sure how many those are. What you can try is to make a java program that will connect with sockets with your phone. This program runs the server.jar file and thus can also covert your socket command into a server message. Hope its clearer now :p
     
  8. Offline

    Welite

    Hm ye, but I think that I do not understand how to correctly form and send the packet to the minecraft server.

    serverOut.writeByte(0x00);
    writeString("BOT;" + serverIpString + ":" + port, serverOut);


    I think I have an error here.
     
Thread Status:
Not open for further replies.

Share This Page