diff --git a/.idea/libraries/libs.xml b/.idea/libraries/libs.xml new file mode 100644 index 0000000..2df7eba --- /dev/null +++ b/.idea/libraries/libs.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/BasicInfoAPI/BasicInfoAPI.iml b/BasicInfoAPI/BasicInfoAPI.iml index 37cc804..5c05120 100644 --- a/BasicInfoAPI/BasicInfoAPI.iml +++ b/BasicInfoAPI/BasicInfoAPI.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/BasicInfoAPI/src/main/conn/MinecraftServer.java b/BasicInfoAPI/src/main/conn/MinecraftServer.java new file mode 100644 index 0000000..51b6361 --- /dev/null +++ b/BasicInfoAPI/src/main/conn/MinecraftServer.java @@ -0,0 +1,15 @@ +package main.conn; + + +/** + * 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{ + @Override + public void run(){ + + } +} diff --git a/BasicInfoAPI/src/test/conn/CommunicateTest.java b/BasicInfoAPI/src/test/conn/CommunicateTest.java new file mode 100644 index 0000000..5e1af6b --- /dev/null +++ b/BasicInfoAPI/src/test/conn/CommunicateTest.java @@ -0,0 +1,76 @@ +package test.conn; + +import com.google.gson.Gson; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +public class CommunicateTest { + static String host="192.168.1.3"; + static int port=25565; + static DataOutputStream dataOutputStream; + static DataInputStream dataInputStream; + public static void main(String[] args)throws Exception { + Socket socket=new Socket(host,port); + System.out.println("established."); + dataOutputStream=new DataOutputStream(socket.getOutputStream()); + dataInputStream=new DataInputStream(socket.getInputStream()); + System.out.println("streamObjectCreated."); + writeVarInt(18); + writeVarInt(0x00); + writeVarInt(754); + writeString("192.168.1.3"); + writeVarInt(25567); + writeVarInt(1); +// dataOutputStream.write(1); +// dataOutputStream.flush(); + System.out.println("wroteHandshake."); + writeVarInt(0); + System.out.println("recv:"+readString()); + } + public static void writeVarInt(int value)throws Exception { + do { + byte temp = (byte)(value & 0b01111111); + // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone + value >>>= 7; + if (value != 0) { + temp |= 0b10000000; + } + dataOutputStream.write(temp); + dataOutputStream.flush(); + } while (value != 0); + } + + static void writeString(String s)throws Exception{ + byte[] b=s.getBytes(); + writeVarInt(b.length); + dataOutputStream.write(b); + dataOutputStream.flush(); + } + static void writePacket(){} + public static String readString()throws Exception{ + int len=readVarInt(); + byte[] b=new byte[len]; + dataInputStream.read(b,0,len); + return new String(b); + } + public static int readVarInt()throws Exception { + 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; + } +} diff --git a/BasicInfoAPI/src/test/conn/FakeServerTest.java b/BasicInfoAPI/src/test/conn/FakeServerTest.java new file mode 100644 index 0000000..d368bc1 --- /dev/null +++ b/BasicInfoAPI/src/test/conn/FakeServerTest.java @@ -0,0 +1,66 @@ +package test.conn; + +import java.io.DataInputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +public class FakeServerTest { + static DataInputStream dataInputStream; + public static void main(String[] args)throws Exception { + ServerSocket socket=new ServerSocket(25567); + while (true){ + Socket client=socket.accept(); + System.out.println("newConn."); + try{ + dataInputStream=new DataInputStream(client.getInputStream()); + int len=0; + byte[] recv=new byte[2048]; + System.out.println("Length(varInt):"+readVarInt()); + System.out.println("PacketID(varInt):"+readVarInt()); + System.out.println("ProtocolVersion(varInt):"+readVarInt()); + System.out.println("ServerAddress(String):"+readString()); + System.out.println("ServerPort(UnsignedShort):"+readUnsignedShort()); + System.out.println("NextState(varIntEnum):"+readVarInt()); + while((len=dataInputStream.read(recv))!=-1){ + if (len==0) + continue; +// System.out.println("recv:"+new String(recv, StandardCharsets.UTF_8)); + for (int i=0;i<1024;i++){ + System.out.print(recv[i]+" "); + } + recv=new byte[1024]; + } + }catch (Exception e){ + e.printStackTrace(); + } + } + } + public static int readVarInt()throws Exception { + 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; + } + public static String readString()throws Exception{ + int len=readVarInt(); + byte[] b=new byte[len]; + dataInputStream.read(b,0,len); + return new String(b); + } + public static int readUnsignedShort()throws Exception{ + return dataInputStream.readUnsignedShort(); + } + +}