Skip to content

Commit

Permalink
Implemented basic info getting api
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jun 16, 2021
1 parent fe80d17 commit c79cbe2
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 10 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added BasicInfoAPI/libs/gson-2.8.7.jar
Binary file not shown.
13 changes: 9 additions & 4 deletions BasicInfoAPI/src/main/api/IServerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
*/
public interface IServerInfo {
class Player{
String name;
String id;
public String name;
public String id;
}
class ExtraDescr{
public String text;
public String color;
}
String getVersionName();
int getVersionProtocol();
int getMaxPlayer();
int getOnlinePlayer();
Player[] getPlayerList();
String getServerDescription();
String getDefaultDescriptionText();
String getDefaultDescriptionColor();
ExtraDescr[] getExtraDescription();
String getFavicon();
String getJSONRawData();
}
112 changes: 110 additions & 2 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,123 @@
package main.conn;


import com.google.gson.Gson;
import main.api.IServerInfo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
* A MinecraftServer instance provides methods to create connection to server and communicate with peer.
* This is the basic instance and any info-getting function is base on this class.
*
* @author Rock Chin
*/
public class MinecraftServer extends Thread{
public class MinecraftServer extends Thread implements IServerInfo {
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private String host;
private int port=25565;
private Response response=null;
public MinecraftServer(String host,int port)throws Exception {
socket=new Socket(host,port);
this.host=host;
this.port=port;
dataInputStream=new DataInputStream(socket.getInputStream());
dataOutputStream=new DataOutputStream(socket.getOutputStream());

new PacketSend(0).addVarInt(-1)
.addString(host)
.addShort(port)
.addVarInt(1).write(dataOutputStream);
new PacketSend(0).write(dataOutputStream);
response=new Gson().fromJson(new PacketRecv(dataInputStream).popString(),Response.class);
}


public class Response{
class description{
String text;
String color;
description[] extra;
}
description description;
class players{
int max;
int online;
class player{
String name;
String id;
}
player[] sample;
}
players players;
class version{
String name;
int protocol;
}
version version;
String favicon;
}


@Override
public String getVersionName() {
return response.version.name;
}

@Override
public int getVersionProtocol() {
return response.version.protocol;
}

@Override
public void run(){
public int getMaxPlayer() {
return response.players.max;
}

@Override
public int getOnlinePlayer() {
return response.players.online;
}

@Override
public Player[] getPlayerList() {
int len=response.players.sample.length;
Player[] players=new Player[len];
for (int i=0;i<len;i++){
players[i]=new Player();
players[i].name=response.players.sample[i].name;
players[i].id=response.players.sample[i].id;
}
return players;
}

@Override
public String getDefaultDescriptionText() {
return response.description.text;
}
@Override
public String getDefaultDescriptionColor(){
return response.description.color;
}
@Override
public ExtraDescr[] getExtraDescription(){
ExtraDescr[] extraDescrs=new ExtraDescr[response.description.extra.length];
for (int i=0;i<response.description.extra.length;i++){
extraDescrs[i]=new ExtraDescr();
extraDescrs[i].color=response.description.extra[i].color;
extraDescrs[i].text=response.description.extra[i].text;
}
return extraDescrs;
}
@Override
public String getFavicon() {
return response.favicon;
}

}
79 changes: 79 additions & 0 deletions BasicInfoAPI/src/main/conn/PacketRecv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main.conn;

import java.io.DataInputStream;
import java.io.IOException;

public class PacketRecv {
DataInputStream dataInputStream;
byte[] packet;
int id=0;
byte[] data;
int length;

private int index=0;
public PacketRecv(DataInputStream dataInputStream)throws IOException {
this.dataInputStream=dataInputStream;
length=readVarInt();
packet=new byte[length];
dataInputStream.read(packet,0,length);
id=popVarInt();
}
public byte popByte(){
return packet[index++];
}
public int pop(byte[] b){
return pop(b,0, packet.length-index);
}
public int pop(byte[] b,int off,int len){
for (int i=0;i<len;i++){
b[i]=packet[index+i+off];
}
return len;
}
public int popVarInt(){

int numRead = 0;
int result = 0;
byte read;
do {
read = popByte();
int value = (read & 0b01111111);
result |= (value << (7 * numRead));

numRead++;
if (numRead > 5) {
throw new RuntimeException("VarInt is too big");
}
} while ((read & 0b10000000) != 0);

return result;
}

public String popString(){
int len=popVarInt();
byte[] b=new byte[len];
pop(b,0,len);
return new String(b);
}



public int readVarInt()throws IOException {
int numRead = 0;
int result = 0;
byte read;
do {
read = dataInputStream.readByte();
int value = (read & 0b01111111);
result |= (value << (7 * numRead));

numRead++;
if (numRead > 5) {
throw new RuntimeException("VarInt is too big");
}
} while ((read & 0b10000000) != 0);

return result;
}

}
Loading

0 comments on commit c79cbe2

Please sign in to comment.