Skip to content

Commit e4b00b4

Browse files
committed
Add debug mode.
1 parent 838e07e commit e4b00b4

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

BasicInfoAPI/src/main/api/IServerInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ class ExtraDescr{
2626
ExtraDescr[] getExtraDescription();
2727
String getFaviconBase64();
2828
BufferedImage getFaviconImage();
29+
String getRawJSONString();
2930
}

BasicInfoAPI/src/main/conn/MinecraftServer.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,54 @@ public class MinecraftServer extends Thread implements IServerInfo {
2626
private int port=25565;
2727
private Response response=null;
2828
private boolean available=false;
29-
public MinecraftServer(String host,int port)throws Exception {
29+
private String jsonStr;
30+
private boolean debug=false;
31+
32+
public MinecraftServer(String host,int port,boolean debugMode)throws Exception{
33+
debug=debugMode;
34+
init(host,port);
35+
}
36+
public MinecraftServer(String host,int port)throws Exception{
37+
debug=false;
38+
init(host, port);
39+
}
40+
41+
42+
private void debugMsg(String str){
43+
if (debug){
44+
System.out.println("[APIDebug]"+str);
45+
}
46+
}
47+
48+
49+
50+
public void init(String host,int port)throws Exception {
51+
debugMsg("MakingSocket...");
3052
socket=new Socket(host,port);
3153
this.host=host;
3254
this.port=port;
3355
dataInputStream=new DataInputStream(socket.getInputStream());
3456
dataOutputStream=new DataOutputStream(socket.getOutputStream());
35-
57+
debugMsg("SocketMadeSuccessfully.");
3658
new PacketSend(0).addVarInt(-1)
3759
.addString(host)
3860
.addShort(port)
3961
.addVarInt(1).write(dataOutputStream);
4062
new PacketSend(0).write(dataOutputStream);
63+
debugMsg("WroteRequestPacket.");
4164
try {
42-
response = new Gson().fromJson(new PacketRecv(dataInputStream).popString(), Response.class);
65+
jsonStr=new PacketRecv(dataInputStream).popString();
66+
debugMsg("ReadJSONData:"+jsonStr);
67+
response = new Gson().fromJson(jsonStr, Response.class);
4368
if (response==null){
4469
available=false;
70+
debugMsg("ResponseIsNull.");
4571
throw new EOFException("Invalid server response.");
4672
}
73+
debugMsg("done.");
4774
available=true;
4875
}catch (EOFException e){//To change protocol.
76+
debugMsg("LegacyServer,protocolChanged.");
4977
socket=new Socket(host,port);
5078
dataInputStream=new DataInputStream(socket.getInputStream());
5179
dataOutputStream=new DataOutputStream(socket.getOutputStream());
@@ -57,6 +85,7 @@ public MinecraftServer(String host,int port)throws Exception {
5785
if (dataInputStream.readByte()==-1){
5886
dataInputStream.readByte();
5987
dataInputStream.readByte();
88+
debugMsg("ReadingResponseFromALegacyServer.");
6089
byte[] b=new byte[512];
6190
dataInputStream.read(b);
6291
ByteBase bbase= new ByteBase(b);
@@ -74,11 +103,12 @@ public MinecraftServer(String host,int port)throws Exception {
74103
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
75104
response.players.max=Integer.parseInt
76105
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
106+
debugMsg("done.");
77107
available=true;
78108
}
79109
}
80110
if (!available){//version lower then 1.4
81-
111+
debugMsg("LowerServer,protocolChanged.");
82112
socket=new Socket(host,port);
83113
dataInputStream=new DataInputStream(socket.getInputStream());
84114
dataOutputStream=new DataOutputStream(socket.getOutputStream());
@@ -89,6 +119,7 @@ public MinecraftServer(String host,int port)throws Exception {
89119
if (dataInputStream.readByte()==-1){
90120
dataInputStream.readByte();
91121
dataInputStream.readByte();
122+
debugMsg("ReadingResponseFromAVeryLowServer");
92123
byte[] b=new byte[512];
93124
dataInputStream.read(b);
94125
ByteBase bbase= new ByteBase(b);
@@ -106,6 +137,7 @@ public MinecraftServer(String host,int port)throws Exception {
106137
response.players=new Response.players();
107138
response.players.online=Integer.parseInt(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
108139
response.players.max=Integer.parseInt(new String(bbase.pop(zeroEnd),StandardCharsets.UTF_16BE));
140+
debugMsg("done.");
109141
available=true;
110142
}
111143
}
@@ -288,6 +320,10 @@ public BufferedImage getFaviconImage(){
288320
}
289321
return null;
290322
}
323+
@Override
324+
public String getRawJSONString(){
325+
return jsonStr;
326+
}
291327
private static BufferedImage base64ToBufferedImage(String base64)throws IOException {
292328
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
293329
byte[] bytes1 = decoder.decodeBuffer(base64);

BasicInfoAPI/src/test/conn/MinecraftServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class MinecraftServerTest {
1111
public static void main(String[] args)throws Exception {
12-
MinecraftServer minecraftServer=new MinecraftServer("localhost",25567);
12+
MinecraftServer minecraftServer=new MinecraftServer("cecelia.cn",25565,true);
1313
System.out.println("available:"+minecraftServer.isAvailable());
1414
System.out.println("version:name:"+minecraftServer.getVersionName()+" protocol:"+minecraftServer.getVersionProtocol());
1515
System.out.println("defaultDescription:color:"+minecraftServer.getDefaultDescriptionColor()+" text:"+minecraftServer.getDefaultDescriptionText());

0 commit comments

Comments
 (0)