Minecraft不响应Response数据包

时间:2016-02-09 09:22:53

标签: java network-programming minecraft packets

我正在使用this库从头开始创建自己的服务器软件。

现在客户端向服务器发送握手数据包,服务器使用握手编解码器对其进行解码,并返回带有响应编解码器编码的响应消息,但是我从未获得Ping数据包。

这是我将响应消息编码到服务器的代码。

public class ResponseCodec implements Codec<ResponseMessage> {
@Override
public ResponseMessage decode(DataInputStream dataInputStream) {
    String json = ByteUtilities.readUTF8(dataInputStream);
    return new ResponseMessage(json);
}

@Override
public DataOutputStream encode(DataOutputStream dataOutputStream, ResponseMessage responseMessage) {
    ByteArrayOutputStream packetArray = new ByteArrayOutputStream();
    DataOutputStream packetStream = new DataOutputStream(packetArray);
    ByteUtilities.writeVarInt(packetStream, 0x00);
    ByteUtilities.writeUTF8(packetStream, responseMessage.getJson());
    ByteUtilities.writeVarInt(dataOutputStream, packetArray.toByteArray().length);
    try {
        dataOutputStream.write(packetArray.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dataOutputStream;
}
}

这是我的连接监听器:

server.setConnectionListener(new ConnectionListener() {
        @Override
        public void onConnect(Socket socket, DataInputStream dataInputStream) throws Exception {
            //header
            int packetSize = ByteUtilities.readVarInt(dataInputStream);
            int packetId = ByteUtilities.readVarInt(dataInputStream);

            // writing handlers
            OutputStream outputStream = socket.getOutputStream();
            DataOutputStream socketStream = new DataOutputStream(outputStream);

            //identification
            if(packetId == 0x00) {
                // decode
                HandshakeCodec codec = new HandshakeCodec();
                HandshakeMessage message = codec.decode(dataInputStream);
                if(!(message.getProtocolVersion() == 47)) {
                    System.out.println("Client " + message.getAddress() + ":" + message.getPort() + " seems to have a incompatible client.");
                }
                ResponseCodec responseCodec = new ResponseCodec();
                StringBuilder text = new StringBuilder();
                BufferedReader formatReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("ResponseFormat.txt")));
                String line = formatReader.readLine();
                while (line != null) {
                    text.append(line);
                    text.append(System.lineSeparator());
                    line = formatReader.readLine();
                }
                String everything = text.toString();
                String accountedWithVariables = everything
                        .replaceAll("MAX_PLAYERS", "" + maxPlayers)
                        .replaceAll("ONLINE_PLAYERS", "" + BasicServer.onlinePlayers)
                        .replaceAll("MOTD", motd);
                ResponseMessage responseMessage = new ResponseMessage(accountedWithVariables);
                responseCodec.encode(socketStream, responseMessage);
            }

            if(packetId == 0x01) {
                long pingLong = dataInputStream.readLong();
                PongCodec codec = new PongCodec();
                PongMessage message = new PongMessage(pingLong);
                codec.encode(socketStream, message);
            }
        }

        @Override
        public void onCaughtException(Exception e) {
            System.out.println("Main thread has recieved a exception: " + e.getMessage());
            e.printStackTrace();
        }
    });

0 个答案:

没有答案