|
1 | 1 | package main.conn; |
2 | 2 |
|
3 | 3 |
|
| 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 | + |
4 | 13 | /** |
5 | 14 | * A MinecraftServer instance provides methods to create connection to server and communicate with peer. |
6 | 15 | * This is the basic instance and any info-getting function is base on this class. |
7 | 16 | * |
8 | 17 | * @author Rock Chin |
9 | 18 | */ |
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 | + |
11 | 78 | @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 | + } |
13 | 87 |
|
| 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; |
14 | 103 | } |
| 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 | + |
15 | 123 | } |
0 commit comments