diff --git a/BasicInfoAPI/src/main/api/IServerInfo.java b/BasicInfoAPI/src/main/api/IServerInfo.java index 3192663..264edc8 100644 --- a/BasicInfoAPI/src/main/api/IServerInfo.java +++ b/BasicInfoAPI/src/main/api/IServerInfo.java @@ -13,6 +13,7 @@ class ExtraDescr{ public String text; public String color; } + boolean isAvailable(); String getVersionName(); int getVersionProtocol(); int getMaxPlayer(); diff --git a/BasicInfoAPI/src/main/conn/MinecraftServer.java b/BasicInfoAPI/src/main/conn/MinecraftServer.java index ecdde9e..6c9036a 100644 --- a/BasicInfoAPI/src/main/conn/MinecraftServer.java +++ b/BasicInfoAPI/src/main/conn/MinecraftServer.java @@ -24,6 +24,7 @@ public class MinecraftServer extends Thread implements IServerInfo { private String host; private int port=25565; private Response response=null; + private boolean available=false; public MinecraftServer(String host,int port)throws Exception { socket=new Socket(host,port); this.host=host; @@ -38,6 +39,11 @@ public MinecraftServer(String host,int port)throws Exception { new PacketSend(0).write(dataOutputStream); try { response = new Gson().fromJson(new PacketRecv(dataInputStream).popString(), Response.class); + if (response==null){ + available=false; + throw new EOFException("Invalid server response."); + } + available=true; }catch (EOFException e){//To change protocol. socket=new Socket(host,port); dataInputStream=new DataInputStream(socket.getInputStream()); @@ -67,6 +73,7 @@ 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)); + available=true; } } } @@ -80,6 +87,11 @@ private class ByteBase{ int index=0; ByteBase(byte[] byteArr){ this.arr=byteArr; +// System.out.print("list:"); +// for (byte b:byteArr){ +// System.out.print(b+" "); +// } +// System.out.println(); } public byte[] pop(byte[] end){ ArrayList byteArrayList=new ArrayList<>(); @@ -136,6 +148,10 @@ static class version{ String favicon; } + @Override + public boolean isAvailable(){ + return available; + } @Override public String getVersionName() { diff --git a/BasicInfoAPI/src/test/conn/MinecraftServerTest.java b/BasicInfoAPI/src/test/conn/MinecraftServerTest.java index b1a0a8f..455367d 100644 --- a/BasicInfoAPI/src/test/conn/MinecraftServerTest.java +++ b/BasicInfoAPI/src/test/conn/MinecraftServerTest.java @@ -6,6 +6,7 @@ public class MinecraftServerTest { public static void main(String[] args)throws Exception { MinecraftServer minecraftServer=new MinecraftServer("localhost",25565); + 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()); IServerInfo.ExtraDescr[] extraDescrs=minecraftServer.getExtraDescription(); diff --git a/README.md b/README.md index f891a96..1ed4599 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,23 @@ This repo contains several modules.Provides APIs for bot making or related proje ## BasicInfoAPI -Supports to get basic info of a Minecraft server.Using the official client's way to get info that can be listed in Minecraft client's server list. -Click the dir link to get further info. +Supports to get basic info of a Minecraft server. +Using the official client's way to get info that can be listed in Minecraft client's server list. +Click the dir link to get further info. +Supported server version: +1.17.x +1.16.x +1.15.x +1.14.x +1.13.x +1.12.x +1.11.x +1.10.x +1.9.x +1.8.x +1.7.x +1.6.x +1.4.x +1.5.x -Latest Release:0.1 \ No newline at end of file +Latest Release:0.2 \ No newline at end of file