Skip to content

Instantly share code, notes, and snippets.

@Sheigutn
Last active February 28, 2022 14:32
Show Gist options
  • Select an option

  • Save Sheigutn/ab15675c672565c9a40e to your computer and use it in GitHub Desktop.

Select an option

Save Sheigutn/ab15675c672565c9a40e to your computer and use it in GitHub Desktop.
Server ping API
import com.google.gson.Gson;
import sun.misc.BASE64Decoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class PingResponse {
private Version version;
private Players players;
private String description;
private String favicon;
public Version getMinecraftVersion()
{
return version;
}
public Players getPlayers()
{
return players;
}
public String getDescription()
{
return description;
}
public String getFavicon()
{
try{
return favicon.replace("data:image/png;base64,", "");
}
catch(Exception ex)
{
return null;
}
}
public class Version
{
private String name;
private int protocol;
public String getName()
{
return name;
}
public int getProtocol()
{
return protocol;
}
}
public class Players
{
private int max;
private int online;
private PingPlayer[] sample;
public int getOnlinePlayers()
{
return online;
}
public int getMaxPlayers()
{
return max;
}
public PingPlayer[] getPlayerInfo()
{
return sample;
}
public class PingPlayer
{
private String name;
private String id;
public String getName()
{
return name;
}
public String getID()
{
return id;
}
}
}
public static PingResponse pingServer(String hostname, int port) throws IOException, UnknownHostException
{
Socket socket = new Socket(hostname, port);
DataOutputStream doss = new DataOutputStream(socket.getOutputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.write(0x00);
VarInt.writeVarInt(5, dos);
VarInt.writeVarInt(hostname.length(), dos);
dos.write(hostname.getBytes());
dos.writeShort(port);
VarInt.writeVarInt(1, dos);
VarInt.writeVarInt(baos.toByteArray().length, doss);
doss.write(baos.toByteArray());
VarInt.writeVarInt(1, doss);
doss.write(0x00);
DataInputStream dis = new DataInputStream(socket.getInputStream());
VarInt.readVarInt(dis);
int id = dis.readByte();
if(id != 0x00) return null;
int length = VarInt.readVarInt(dis);
byte[] jsonBa = new byte[length];
dis.readFully(jsonBa);
dis.close();
doss.close();
dos.close();
socket.close();
Gson gson = new Gson();
return gson.fromJson(new String(jsonBa), PingResponse.class);
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class VarInt {
public static void writeVarInt(int paramInt, DataOutputStream ser) throws IOException{
while (true) {
if ((paramInt & 0xFFFFFF80) == 0L) {
ser.writeByte(paramInt);
return;
}
ser.writeByte((paramInt & 0x7F) | 0x80);
paramInt >>>= 7;
}
}
public static int readVarInt(DataInputStream ser) throws IOException{
int l = 0;
int i = 0;
while (true)
{
int j = ser.readByte();
l |= (j & 0x7F) << i++ * 7;
if (i > 10) {
throw new RuntimeException("VarInt too big");
}
if ((j & 0x80) != 128) {
break;
}
}
return l;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment