use of net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket in project Client by MatHax.
the class ServerListPinger method add.
public void add(final MServerInfo entry, final Runnable runnable) throws UnknownHostException {
Timer timeoutTimer = new Timer();
ServerAddress serverAddress = ServerAddress.parse(entry.address);
timeoutTimer.schedule(new TimerTask() {
@Override
public void run() {
notifyDisconnectListeners();
}
}, 20000);
final ClientConnection clientConnection = ClientConnection.connect(new InetSocketAddress(InetAddress.getByName(serverAddress.getAddress()), serverAddress.getPort()), false);
failedToConnect = false;
this.clientConnections.add(clientConnection);
entry.label = "multiplayer.status.pinging";
entry.ping = -1L;
entry.playerListSummary = null;
clientConnection.setPacketListener(new ClientQueryPacketListener() {
private boolean sentQuery;
private boolean received;
private long startTime;
public void onResponse(QueryResponseS2CPacket packet) {
if (this.received)
clientConnection.disconnect(new TranslatableText("multiplayer.status.unrequested"));
else {
this.received = true;
ServerMetadata serverMetadata = packet.getServerMetadata();
if (serverMetadata.getDescription() != null)
entry.label = serverMetadata.getDescription().asString();
else
entry.label = "";
if (serverMetadata.getVersion() != null) {
entry.version = serverMetadata.getVersion().getGameVersion();
entry.protocolVersion = serverMetadata.getVersion().getProtocolVersion();
} else {
entry.version = "multiplayer.status.old";
entry.protocolVersion = 0;
}
if (serverMetadata.getPlayers() != null) {
entry.playerCountLabel = ServerListPinger.getPlayerCountLabel(serverMetadata.getPlayers().getOnlinePlayerCount(), serverMetadata.getPlayers().getPlayerLimit());
entry.playerCount = serverMetadata.getPlayers().getOnlinePlayerCount();
entry.playercountMax = serverMetadata.getPlayers().getPlayerLimit();
List<Text> list = Lists.newArrayList();
if (ArrayUtils.isNotEmpty(serverMetadata.getPlayers().getSample())) {
GameProfile[] gameProfiles = serverMetadata.getPlayers().getSample();
for (GameProfile gameProfile : gameProfiles) {
list.add(new LiteralText(gameProfile.getName()));
}
if (serverMetadata.getPlayers().getSample().length < serverMetadata.getPlayers().getOnlinePlayerCount())
list.add(new TranslatableText("multiplayer.status.and_more", serverMetadata.getPlayers().getOnlinePlayerCount() - serverMetadata.getPlayers().getSample().length));
entry.playerListSummary = list;
}
} else
entry.playerCountLabel = "multiplayer.status.unknown";
String string = null;
if (serverMetadata.getFavicon() != null) {
String string2 = serverMetadata.getFavicon();
if (string2.startsWith("data:image/png;base64,"))
string = string2.substring("data:image/png;base64,".length());
else
MatHax.LOG.error("Invalid server icon (unknown format)");
}
if (!Objects.equals(string, entry.getIcon())) {
entry.setIcon(string);
runnable.run();
}
this.startTime = Util.getMeasuringTimeMs();
clientConnection.send(new QueryPingC2SPacket(this.startTime));
this.sentQuery = true;
notifyDisconnectListeners();
}
}
public void onPong(QueryPongS2CPacket packet) {
long l = this.startTime;
long m = Util.getMeasuringTimeMs();
entry.ping = m - l;
clientConnection.disconnect(new TranslatableText("multiplayer.status.finished"));
}
public void onDisconnected(Text reason) {
if (!this.sentQuery) {
MatHax.LOG.error("Can't ping {}: {}", entry.address, reason.getString());
entry.label = "multiplayer.status.cannot_connect";
entry.playerCountLabel = "";
entry.playerCount = 0;
entry.playercountMax = 0;
ServerListPinger.this.ping(entry);
}
notifyDisconnectListeners();
}
public ClientConnection getConnection() {
return clientConnection;
}
});
try {
clientConnection.send(new HandshakeC2SPacket(serverAddress.getAddress(), serverAddress.getPort(), NetworkState.STATUS));
clientConnection.send(new QueryRequestC2SPacket());
} catch (Throwable throwable) {
MatHax.LOG.error("[MatHax] Couldn't send handshake", throwable);
}
}
Aggregations