From 9016162a49ee266f045bd01425e0fb5db050aae2 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 18 Sep 2018 17:24:28 -0400 Subject: [PATCH] Optimize and clarify GS4 query handler. --- .../proxy/protocol/netty/GS4QueryHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java index df093d78d..2628bc445 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java @@ -101,9 +101,6 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler queryResponse.writeByte(QUERY_TYPE_STAT); queryResponse.writeInt(sessionId); - // Fetch information - Collection players = server.getAllPlayers(); - // Start writing the response ResponseWriter responseWriter = new ResponseWriter(queryResponse, queryMessage.readableBytes() == 0); responseWriter.write("hostname", ComponentSerializers.PLAIN.serialize(server.getConfiguration().getMotdComponent())); @@ -114,12 +111,14 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler responseWriter.write("plugins", ""); responseWriter.write("map", "Velocity"); - responseWriter.write("numplayers", players.size()); + responseWriter.write("numplayers", server.getPlayerCount()); responseWriter.write("maxplayers", server.getConfiguration().getShowMaxPlayers()); responseWriter.write("hostport", server.getConfiguration().getBind().getPort()); responseWriter.write("hostip", server.getConfiguration().getBind().getHostString()); - responseWriter.writePlayers(players); + if (!responseWriter.isBasic) { + responseWriter.writePlayers(server.getAllPlayers()); + } break; } @@ -132,6 +131,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler ctx.writeAndFlush(responsePacket); } catch (Exception e) { logger.warn("Error while trying to handle a query packet from {}", msg.sender(), e); + // NB: Only need to explicitly release upon exception, writing the response out will decrement the reference + // count. responsePacket.release(); } }