Skip to content

Commit

Permalink
Change client side protocol version
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jul 7, 2021
1 parent e4b00b4 commit 896e5fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
21 changes: 18 additions & 3 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
}


Expand All @@ -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);
Expand All @@ -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.");
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions BasicInfoAPI/src/test/conn/ConnectMethodTest.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
}
4 changes: 2 additions & 2 deletions BasicInfoAPI/src/test/conn/MinecraftServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -42,4 +42,4 @@ public FaviconDisplay(BufferedImage img){
this.bufferedImage=img;
}
}
}
}

0 comments on commit 896e5fb

Please sign in to comment.