Skip to content

Commit c79cbe2

Browse files
committed
Implemented basic info getting api
1 parent fe80d17 commit c79cbe2

9 files changed

Lines changed: 435 additions & 10 deletions

File tree

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BasicInfoAPI/libs/gson-2.8.7.jar

235 KB
Binary file not shown.

BasicInfoAPI/src/main/api/IServerInfo.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
*/
77
public interface IServerInfo {
88
class Player{
9-
String name;
10-
String id;
9+
public String name;
10+
public String id;
11+
}
12+
class ExtraDescr{
13+
public String text;
14+
public String color;
1115
}
1216
String getVersionName();
1317
int getVersionProtocol();
1418
int getMaxPlayer();
1519
int getOnlinePlayer();
1620
Player[] getPlayerList();
17-
String getServerDescription();
21+
String getDefaultDescriptionText();
22+
String getDefaultDescriptionColor();
23+
ExtraDescr[] getExtraDescription();
1824
String getFavicon();
19-
String getJSONRawData();
2025
}
Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,123 @@
11
package main.conn;
22

33

4+
import com.google.gson.Gson;
5+
import main.api.IServerInfo;
6+
7+
import java.io.DataInputStream;
8+
import java.io.DataOutputStream;
9+
import java.io.IOException;
10+
import java.net.Socket;
11+
import java.nio.charset.StandardCharsets;
12+
413
/**
514
* A MinecraftServer instance provides methods to create connection to server and communicate with peer.
615
* This is the basic instance and any info-getting function is base on this class.
716
*
817
* @author Rock Chin
918
*/
10-
public class MinecraftServer extends Thread{
19+
public class MinecraftServer extends Thread implements IServerInfo {
20+
private Socket socket;
21+
private DataOutputStream dataOutputStream;
22+
private DataInputStream dataInputStream;
23+
private String host;
24+
private int port=25565;
25+
private Response response=null;
26+
public MinecraftServer(String host,int port)throws Exception {
27+
socket=new Socket(host,port);
28+
this.host=host;
29+
this.port=port;
30+
dataInputStream=new DataInputStream(socket.getInputStream());
31+
dataOutputStream=new DataOutputStream(socket.getOutputStream());
32+
33+
new PacketSend(0).addVarInt(-1)
34+
.addString(host)
35+
.addShort(port)
36+
.addVarInt(1).write(dataOutputStream);
37+
new PacketSend(0).write(dataOutputStream);
38+
response=new Gson().fromJson(new PacketRecv(dataInputStream).popString(),Response.class);
39+
}
40+
41+
42+
public class Response{
43+
class description{
44+
String text;
45+
String color;
46+
description[] extra;
47+
}
48+
description description;
49+
class players{
50+
int max;
51+
int online;
52+
class player{
53+
String name;
54+
String id;
55+
}
56+
player[] sample;
57+
}
58+
players players;
59+
class version{
60+
String name;
61+
int protocol;
62+
}
63+
version version;
64+
String favicon;
65+
}
66+
67+
68+
@Override
69+
public String getVersionName() {
70+
return response.version.name;
71+
}
72+
73+
@Override
74+
public int getVersionProtocol() {
75+
return response.version.protocol;
76+
}
77+
1178
@Override
12-
public void run(){
79+
public int getMaxPlayer() {
80+
return response.players.max;
81+
}
82+
83+
@Override
84+
public int getOnlinePlayer() {
85+
return response.players.online;
86+
}
1387

88+
@Override
89+
public Player[] getPlayerList() {
90+
int len=response.players.sample.length;
91+
Player[] players=new Player[len];
92+
for (int i=0;i<len;i++){
93+
players[i]=new Player();
94+
players[i].name=response.players.sample[i].name;
95+
players[i].id=response.players.sample[i].id;
96+
}
97+
return players;
98+
}
99+
100+
@Override
101+
public String getDefaultDescriptionText() {
102+
return response.description.text;
14103
}
104+
@Override
105+
public String getDefaultDescriptionColor(){
106+
return response.description.color;
107+
}
108+
@Override
109+
public ExtraDescr[] getExtraDescription(){
110+
ExtraDescr[] extraDescrs=new ExtraDescr[response.description.extra.length];
111+
for (int i=0;i<response.description.extra.length;i++){
112+
extraDescrs[i]=new ExtraDescr();
113+
extraDescrs[i].color=response.description.extra[i].color;
114+
extraDescrs[i].text=response.description.extra[i].text;
115+
}
116+
return extraDescrs;
117+
}
118+
@Override
119+
public String getFavicon() {
120+
return response.favicon;
121+
}
122+
15123
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main.conn;
2+
3+
import java.io.DataInputStream;
4+
import java.io.IOException;
5+
6+
public class PacketRecv {
7+
DataInputStream dataInputStream;
8+
byte[] packet;
9+
int id=0;
10+
byte[] data;
11+
int length;
12+
13+
private int index=0;
14+
public PacketRecv(DataInputStream dataInputStream)throws IOException {
15+
this.dataInputStream=dataInputStream;
16+
length=readVarInt();
17+
packet=new byte[length];
18+
dataInputStream.read(packet,0,length);
19+
id=popVarInt();
20+
}
21+
public byte popByte(){
22+
return packet[index++];
23+
}
24+
public int pop(byte[] b){
25+
return pop(b,0, packet.length-index);
26+
}
27+
public int pop(byte[] b,int off,int len){
28+
for (int i=0;i<len;i++){
29+
b[i]=packet[index+i+off];
30+
}
31+
return len;
32+
}
33+
public int popVarInt(){
34+
35+
int numRead = 0;
36+
int result = 0;
37+
byte read;
38+
do {
39+
read = popByte();
40+
int value = (read & 0b01111111);
41+
result |= (value << (7 * numRead));
42+
43+
numRead++;
44+
if (numRead > 5) {
45+
throw new RuntimeException("VarInt is too big");
46+
}
47+
} while ((read & 0b10000000) != 0);
48+
49+
return result;
50+
}
51+
52+
public String popString(){
53+
int len=popVarInt();
54+
byte[] b=new byte[len];
55+
pop(b,0,len);
56+
return new String(b);
57+
}
58+
59+
60+
61+
public int readVarInt()throws IOException {
62+
int numRead = 0;
63+
int result = 0;
64+
byte read;
65+
do {
66+
read = dataInputStream.readByte();
67+
int value = (read & 0b01111111);
68+
result |= (value << (7 * numRead));
69+
70+
numRead++;
71+
if (numRead > 5) {
72+
throw new RuntimeException("VarInt is too big");
73+
}
74+
} while ((read & 0b10000000) != 0);
75+
76+
return result;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)