-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
435 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.