From 896e5fb67a8e6ef38a48b0f669dedc966d59a19b Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Wed, 7 Jul 2021 11:29:16 +0800 Subject: [PATCH] Change client side protocol version --- .../src/main/conn/MinecraftServer.java | 21 +++++++++++-- .../src/test/conn/ConnectMethodTest.java | 30 +++++++++++++++++++ .../src/test/conn/MinecraftServerTest.java | 4 +-- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 BasicInfoAPI/src/test/conn/ConnectMethodTest.java diff --git a/BasicInfoAPI/src/main/conn/MinecraftServer.java b/BasicInfoAPI/src/main/conn/MinecraftServer.java index b0df7c4..d71b842 100644 --- a/BasicInfoAPI/src/main/conn/MinecraftServer.java +++ b/BasicInfoAPI/src/main/conn/MinecraftServer.java @@ -11,6 +11,7 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Date; /** * A MinecraftServer instance provides methods to create connection to server and communicate with peer. @@ -31,11 +32,15 @@ public class MinecraftServer extends Thread implements IServerInfo { public MinecraftServer(String host,int port,boolean debugMode)throws Exception{ debug=debugMode; + long start=new Date().getTime(); init(host,port); + debugMsg("Spent:"+(new Date().getTime()-start)+"ms"); } public MinecraftServer(String host,int port)throws Exception{ debug=false; - init(host, port); + long start=new Date().getTime(); + init(host,port); + debugMsg("Spent:"+(new Date().getTime()-start)+"ms"); } @@ -55,7 +60,7 @@ public void init(String host,int port)throws Exception { dataInputStream=new DataInputStream(socket.getInputStream()); dataOutputStream=new DataOutputStream(socket.getOutputStream()); debugMsg("SocketMadeSuccessfully."); - new PacketSend(0).addVarInt(-1) + new PacketSend(0).addVarInt(755) .addString(host) .addShort(port) .addVarInt(1).write(dataOutputStream); @@ -64,7 +69,7 @@ public void init(String host,int port)throws Exception { try { jsonStr=new PacketRecv(dataInputStream).popString(); debugMsg("ReadJSONData:"+jsonStr); - response = new Gson().fromJson(jsonStr, Response.class); + response = new Gson().fromJson(jsonStr.endsWith("}")?jsonStr:jsonStr+"}", Response.class); if (response==null){ available=false; debugMsg("ResponseIsNull."); @@ -213,6 +218,16 @@ static class version{ } version version; String favicon; + static class modinfo{ + String type; + static class mod{ + String modid; + String version; + } + mod[] modList; + } + modinfo modinfo; + } @Override diff --git a/BasicInfoAPI/src/test/conn/ConnectMethodTest.java b/BasicInfoAPI/src/test/conn/ConnectMethodTest.java new file mode 100644 index 0000000..741e152 --- /dev/null +++ b/BasicInfoAPI/src/test/conn/ConnectMethodTest.java @@ -0,0 +1,30 @@ +package test.conn; + +import java.net.*; + +public class ConnectMethodTest { + static String host="s.maxkim.vip"; + public static void main(String[] args)throws Exception { + try + { + InetAddress ia1=InetAddress.getByName(host); + System.out.println(ia1.getHostName()); + System.out.println(ia1.getHostAddress()); + } + catch(UnknownHostException e) + { + e.printStackTrace(); + } + Socket socket=new Socket(); + socket.connect(new InetSocketAddress(getHost(),25565),10000); + + } + public static String getHost() { + try { + return IDN.toASCII(host); + } + catch (IllegalArgumentException illegalArgumentException2) { + return ""; + } + } +} \ No newline at end of file diff --git a/BasicInfoAPI/src/test/conn/MinecraftServerTest.java b/BasicInfoAPI/src/test/conn/MinecraftServerTest.java index 1de2ae1..b6b6ed7 100644 --- a/BasicInfoAPI/src/test/conn/MinecraftServerTest.java +++ b/BasicInfoAPI/src/test/conn/MinecraftServerTest.java @@ -9,7 +9,7 @@ public class MinecraftServerTest { public static void main(String[] args)throws Exception { - MinecraftServer minecraftServer=new MinecraftServer("cecelia.cn",25565,true); + MinecraftServer minecraftServer=new MinecraftServer("s.maxkim.vip",25565,true); System.out.println("available:"+minecraftServer.isAvailable()); System.out.println("version:name:"+minecraftServer.getVersionName()+" protocol:"+minecraftServer.getVersionProtocol()); System.out.println("defaultDescription:color:"+minecraftServer.getDefaultDescriptionColor()+" text:"+minecraftServer.getDefaultDescriptionText()); @@ -42,4 +42,4 @@ public FaviconDisplay(BufferedImage img){ this.bufferedImage=img; } } -} \ No newline at end of file +}