Skip to content

Commit

Permalink
Add debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jun 23, 2021
1 parent 838e07e commit e4b00b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions BasicInfoAPI/src/main/api/IServerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ class ExtraDescr{
ExtraDescr[] getExtraDescription();
String getFaviconBase64();
BufferedImage getFaviconImage();
String getRawJSONString();
}
44 changes: 40 additions & 4 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,54 @@ public class MinecraftServer extends Thread implements IServerInfo {
private int port=25565;
private Response response=null;
private boolean available=false;
public MinecraftServer(String host,int port)throws Exception {
private String jsonStr;
private boolean debug=false;

public MinecraftServer(String host,int port,boolean debugMode)throws Exception{
debug=debugMode;
init(host,port);
}
public MinecraftServer(String host,int port)throws Exception{
debug=false;
init(host, port);
}


private void debugMsg(String str){
if (debug){
System.out.println("[APIDebug]"+str);
}
}



public void init(String host,int port)throws Exception {
debugMsg("MakingSocket...");
socket=new Socket(host,port);
this.host=host;
this.port=port;
dataInputStream=new DataInputStream(socket.getInputStream());
dataOutputStream=new DataOutputStream(socket.getOutputStream());

debugMsg("SocketMadeSuccessfully.");
new PacketSend(0).addVarInt(-1)
.addString(host)
.addShort(port)
.addVarInt(1).write(dataOutputStream);
new PacketSend(0).write(dataOutputStream);
debugMsg("WroteRequestPacket.");
try {
response = new Gson().fromJson(new PacketRecv(dataInputStream).popString(), Response.class);
jsonStr=new PacketRecv(dataInputStream).popString();
debugMsg("ReadJSONData:"+jsonStr);
response = new Gson().fromJson(jsonStr, Response.class);
if (response==null){
available=false;
debugMsg("ResponseIsNull.");
throw new EOFException("Invalid server response.");
}
debugMsg("done.");
available=true;
}catch (EOFException e){//To change protocol.
debugMsg("LegacyServer,protocolChanged.");
socket=new Socket(host,port);
dataInputStream=new DataInputStream(socket.getInputStream());
dataOutputStream=new DataOutputStream(socket.getOutputStream());
Expand All @@ -57,6 +85,7 @@ public MinecraftServer(String host,int port)throws Exception {
if (dataInputStream.readByte()==-1){
dataInputStream.readByte();
dataInputStream.readByte();
debugMsg("ReadingResponseFromALegacyServer.");
byte[] b=new byte[512];
dataInputStream.read(b);
ByteBase bbase= new ByteBase(b);
Expand All @@ -74,11 +103,12 @@ public MinecraftServer(String host,int port)throws Exception {
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
response.players.max=Integer.parseInt
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
debugMsg("done.");
available=true;
}
}
if (!available){//version lower then 1.4

debugMsg("LowerServer,protocolChanged.");
socket=new Socket(host,port);
dataInputStream=new DataInputStream(socket.getInputStream());
dataOutputStream=new DataOutputStream(socket.getOutputStream());
Expand All @@ -89,6 +119,7 @@ public MinecraftServer(String host,int port)throws Exception {
if (dataInputStream.readByte()==-1){
dataInputStream.readByte();
dataInputStream.readByte();
debugMsg("ReadingResponseFromAVeryLowServer");
byte[] b=new byte[512];
dataInputStream.read(b);
ByteBase bbase= new ByteBase(b);
Expand All @@ -106,6 +137,7 @@ public MinecraftServer(String host,int port)throws Exception {
response.players=new Response.players();
response.players.online=Integer.parseInt(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
response.players.max=Integer.parseInt(new String(bbase.pop(zeroEnd),StandardCharsets.UTF_16BE));
debugMsg("done.");
available=true;
}
}
Expand Down Expand Up @@ -288,6 +320,10 @@ public BufferedImage getFaviconImage(){
}
return null;
}
@Override
public String getRawJSONString(){
return jsonStr;
}
private static BufferedImage base64ToBufferedImage(String base64)throws IOException {
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64);
Expand Down
2 changes: 1 addition & 1 deletion 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("localhost",25567);
MinecraftServer minecraftServer=new MinecraftServer("cecelia.cn",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

0 comments on commit e4b00b4

Please sign in to comment.