Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.thebmanswan541;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.nio.charset.Charset;
- import org.bukkit.ChatColor;
- public class ServerInfo {
- private String host;
- private int port;
- public ServerInfo(String host, int port) {
- this.host = host;
- this.port = port;
- }
- public ServerInfo(String host) {
- this.host = host;
- this.port = 25565;
- }
- public ServerInfo() {
- this.host = "0.0.0.0";
- this.port = 25565;
- }
- public int parseData(Connection connection) {
- try{
- @SuppressWarnings("resource")
- Socket socket = new Socket();
- OutputStream os;
- DataOutputStream dos;
- InputStream is;
- InputStreamReader isr;
- socket.setSoTimeout(2500);
- socket.connect(new InetSocketAddress(host, port), 2500);
- os = socket.getOutputStream();
- dos = new DataOutputStream(os);
- is = socket.getInputStream();
- isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
- dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
- int packetID = is.read();
- if (packetID == -1)
- System.out.println("EOS");
- if (packetID != 0xFF)
- System.out.println("Invalid Packet ID! "+packetID);
- int length = isr.read();
- if (length == -1)
- System.out.println("EOS");
- if (length == 0)
- System.out.println("Invalid length!");
- char[] chars = new char[length];
- if (isr.read(chars, 0, length) != length)
- System.out.println("EOS");
- String string = new String(chars);
- String[] data = string.split("\0");
- if (connection == Connection.PLAYERS_ONLINE) {
- return Integer.parseInt(data[4]);
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- public String parseData1(Connection connection) {
- try{
- @SuppressWarnings("resource")
- Socket socket = new Socket();
- OutputStream os;
- DataOutputStream dos;
- InputStream is;
- InputStreamReader isr;
- socket.setSoTimeout(2500);
- socket.connect(new InetSocketAddress(host, port), 2500);
- os = socket.getOutputStream();
- dos = new DataOutputStream(os);
- is = socket.getInputStream();
- isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
- dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
- int packetID = is.read();
- if (packetID == -1)
- System.out.println("EOS");
- if (packetID != 0xFF)
- System.out.println("Invalid Packet ID! "+packetID);
- int length = isr.read();
- if (length == -1)
- System.out.println("EOS");
- if (length == 0)
- System.out.println("Invalid length!");
- char[] chars = new char[length];
- if (isr.read(chars, 0, length) != length)
- System.out.println("EOS");
- String string = new String(chars);
- String[] data = string.split("\0");
- if (connection == Connection.PLAYERS_FRACTION) {
- return Integer.parseInt(data[4])+"/"+Integer.parseInt(data[5]);
- }
- }catch (Exception e) {
- return ChatColor.RED+"0";
- }
- return host;
- }
- public String parseData2(Connection connection) {
- try{
- @SuppressWarnings("resource")
- Socket socket = new Socket();
- OutputStream os;
- DataOutputStream dos;
- InputStream is;
- InputStreamReader isr;
- socket.setSoTimeout(2500);
- socket.connect(new InetSocketAddress(host, port), 2500);
- os = socket.getOutputStream();
- dos = new DataOutputStream(os);
- is = socket.getInputStream();
- isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
- dos.write(new byte[] { (byte) 0xFE, (byte) 0x01});
- int packetID = is.read();
- if (packetID == -1)
- System.out.println("EOS");
- if (packetID != 0xFF)
- System.out.println("Invalid Packet ID! "+packetID);
- int length = isr.read();
- if (length == -1)
- System.out.println("EOS");
- if (length == 0)
- System.out.println("Invalid length!");
- char[] chars = new char[length];
- if (isr.read(chars, 0, length) != length)
- System.out.println("EOS");
- if (connection == Connection.ONLINE) {
- return ChatColor.GREEN+"Online";
- }
- }catch (Exception e) {
- return ChatColor.RED+"Offline";
- }
- return host;
- }
- public enum Connection {
- PLAYERS_ONLINE, PLAYERS_FRACTION, ONLINE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement