From ea43b8ff60486c1aab12afa4b964d9075d491a02 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 23 Oct 2018 22:04:15 +0300 Subject: [PATCH 1/2] Add Query event --- .../api/event/query/ProxyQueryEvent.java | 83 +++++ .../api/event/query/package-info.java | 4 + .../api/proxy/config/ProxyConfig.java | 7 + .../api/proxy/server/QueryResponse.java | 318 ++++++++++++++++++ .../proxy/config/VelocityConfiguration.java | 24 +- .../proxy/protocol/netty/GS4QueryHandler.java | 116 +++++-- 6 files changed, 524 insertions(+), 28 deletions(-) create mode 100644 api/src/main/java/com/velocitypowered/api/event/query/ProxyQueryEvent.java create mode 100644 api/src/main/java/com/velocitypowered/api/event/query/package-info.java create mode 100644 api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java diff --git a/api/src/main/java/com/velocitypowered/api/event/query/ProxyQueryEvent.java b/api/src/main/java/com/velocitypowered/api/event/query/ProxyQueryEvent.java new file mode 100644 index 000000000..7b64ffbf5 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/query/ProxyQueryEvent.java @@ -0,0 +1,83 @@ +package com.velocitypowered.api.event.query; + +import com.google.common.base.Preconditions; +import com.velocitypowered.api.proxy.server.QueryResponse; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.net.InetAddress; + +/** + * This event is fired if proxy is getting queried over GS4 Query protocol + */ +public final class ProxyQueryEvent { + private final QueryType queryType; + private final InetAddress querierAddress; + private QueryResponse response; + + public ProxyQueryEvent(QueryType queryType, InetAddress querierAddress, QueryResponse response) { + this.queryType = Preconditions.checkNotNull(queryType, "queryType"); + this.querierAddress = Preconditions.checkNotNull(querierAddress, "querierAddress"); + this.response = Preconditions.checkNotNull(response, "response"); + } + + /** + * Get query type + * @return query type + */ + @NonNull + public QueryType getQueryType() { + return queryType; + } + + /** + * Get querier address + * @return querier address + */ + @NonNull + public InetAddress getQuerierAddress() { + return querierAddress; + } + + /** + * Get query response + * @return query response + */ + @NonNull + public QueryResponse getResponse() { + return response; + } + + /** + * Set query response + * @param response query response + */ + public void setResponse(@NonNull QueryResponse response) { + this.response = Preconditions.checkNotNull(response, "response"); + } + + @Override + public String toString() { + return "ProxyQueryEvent{" + + "queryType=" + queryType + + ", querierAddress=" + querierAddress + + ", response=" + response + + '}'; + } + + /** + * The type of query + */ + public enum QueryType { + /** + * Basic query asks only a subset of information, such as hostname, game type (hardcoded to
MINECRAFT
), map, + * current players, max players, proxy port and proxy hostname + */ + BASIC, + + /** + * Full query asks pretty much everything present on this event (only hardcoded values cannot be modified here). + */ + FULL + ; + } +} diff --git a/api/src/main/java/com/velocitypowered/api/event/query/package-info.java b/api/src/main/java/com/velocitypowered/api/event/query/package-info.java new file mode 100644 index 000000000..42fb5a47f --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/query/package-info.java @@ -0,0 +1,4 @@ +/** + * Provides events for handling GS4 queries. + */ +package com.velocitypowered.api.event.query; \ No newline at end of file diff --git a/api/src/main/java/com/velocitypowered/api/proxy/config/ProxyConfig.java b/api/src/main/java/com/velocitypowered/api/proxy/config/ProxyConfig.java index 85cbaa30d..9285b1fab 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/config/ProxyConfig.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/config/ProxyConfig.java @@ -29,6 +29,13 @@ public interface ProxyConfig { */ String getQueryMap(); + /** + * Whether GameSpy 4 queries should show plugins installed on + * Velocity by default + * @return show plugins in query + */ + boolean shouldQueryShowPlugins(); + /** * Get the MOTD component shown in the tab list * @return the motd component diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java new file mode 100644 index 000000000..0edd95bc8 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java @@ -0,0 +1,318 @@ +package com.velocitypowered.api.proxy.server; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.proxy.config.ProxyConfig; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * GS4 query response. This class is immutable. + */ +public final class QueryResponse { + private final String hostname; + private final String gameVersion; + private final String map; + private final int currentPlayers; + private final int maxPlayers; + private final String proxyHost; + private final int proxyPort; + private final Collection players; + private final String proxyVersion; + private final Collection plugins; + + private QueryResponse(String hostname, String gameVersion, String map, int currentPlayers, int maxPlayers, String proxyHost, int proxyPort, Collection players, String proxyVersion, Collection plugins) { + this.hostname = hostname; + this.gameVersion = gameVersion; + this.map = map; + this.currentPlayers = currentPlayers; + this.maxPlayers = maxPlayers; + this.proxyHost = proxyHost; + this.proxyPort = proxyPort; + this.players = players; + this.proxyVersion = proxyVersion; + this.plugins = plugins; + } + + /** + * Get hostname which will be used to reply to the query. By default it is {@link ProxyConfig#getMotdComponent()} in plain text without colour codes. + * @return hostname + */ + @NonNull + public String getHostname() { + return hostname; + } + + /** + * Get game version which will be used to reply to the query. By default supported Minecraft versions range is sent. + * @return game version + */ + @NonNull + public String getGameVersion() { + return gameVersion; + } + + /** + * Get map name which will be used to reply to the query. By default {@link ProxyConfig#getQueryMap()} is sent. + * @return map name + */ + @NonNull + public String getMap() { + return map; + } + + /** + * Get current online player count which will be used to reply to the query. + * @return online player count + */ + public int getCurrentPlayers() { + return currentPlayers; + } + + /** + * Get max player count which will be used to reply to the query. + * @return max player count + */ + public int getMaxPlayers() { + return maxPlayers; + } + + /** + * Get proxy (public facing) hostname + * @return proxy hostname + */ + @NonNull + public String getProxyHost() { + return proxyHost; + } + + /** + * Get proxy (public facing) port + * @return proxy port + */ + public int getProxyPort() { + return proxyPort; + } + + /** + * Get collection of players which will be used to reply to the query. + * @return collection of players + */ + @NonNull + public Collection getPlayers() { + return players; + } + + /** + * Get server software (name and version) which will be used to reply to the query. + * @return server software + */ + @NonNull + public String getProxyVersion() { + return proxyVersion; + } + + /** + * Get list of plugins which will be used to reply to the query. + * @return collection of plugins + */ + @NonNull + public Collection getPlugins() { + return plugins; + } + + + /** + * Creates a new {@link Builder} instance from data represented by this response + * @return {@link QueryResponse} builder + */ + @NonNull + public Builder toBuilder() { + return QueryResponse.builder() + .hostname(getHostname()) + .gameVersion(getGameVersion()) + .map(getMap()) + .currentPlayers(getCurrentPlayers()) + .maxPlayers(getMaxPlayers()) + .proxyHost(getProxyHost()) + .proxyPort(getProxyPort()) + .players(getPlayers()) + .proxyVersion(getProxyVersion()) + .plugins(getPlugins()); + } + + /** + * Creates a new {@link Builder} instance + * @return {@link QueryResponse} builder + */ + @NonNull + public static Builder builder() { + return new Builder(); + } + + /** + * A builder for {@link QueryResponse} objects. + */ + public static final class Builder { + private String hostname; + private String gameVersion; + private String map; + private int currentPlayers; + private int maxPlayers; + private String proxyHost; + private int proxyPort; + private Collection players = new ArrayList<>(); + private String proxyVersion; + private List plugins = new ArrayList<>(); + + private Builder() {} + + @NonNull + public Builder hostname(@NonNull String hostname) { + this.hostname = Preconditions.checkNotNull(hostname, "hostname"); + return this; + } + + @NonNull + public Builder gameVersion(@NonNull String gameVersion) { + this.gameVersion = Preconditions.checkNotNull(gameVersion, "gameVersion"); + return this; + } + + @NonNull + public Builder map(@NonNull String map) { + this.map = Preconditions.checkNotNull(map, "map"); + return this; + } + + @NonNull + public Builder currentPlayers(int currentPlayers) { + Preconditions.checkArgument(currentPlayers >= 0, "currentPlayers cannot be negative"); + this.currentPlayers = currentPlayers; + return this; + } + + @NonNull + public Builder maxPlayers(int maxPlayers) { + Preconditions.checkArgument(maxPlayers >= 0, "maxPlayers cannot be negative"); + this.maxPlayers = maxPlayers; + return this; + } + + @NonNull + public Builder proxyHost(@NonNull String proxyHost) { + this.proxyHost = Preconditions.checkNotNull(proxyHost, "proxyHost"); + return this; + } + + @NonNull + public Builder proxyPort(int proxyPort) { + Preconditions.checkArgument(proxyPort >= 1 && proxyPort <= 65535, "proxyPort must be between 1-65535"); + this.proxyPort = proxyPort; + return this; + } + + @NonNull + public Builder players(@NonNull Collection players) { + this.players.addAll(Preconditions.checkNotNull(players, "players")); + return this; + } + + @NonNull + public Builder players(@NonNull String... players) { + this.players.addAll(Arrays.asList(Preconditions.checkNotNull(players, "players"))); + return this; + } + + @NonNull + public Builder clearPlayers() { + this.players.clear(); + return this; + } + + @NonNull + public Builder proxyVersion(@NonNull String proxyVersion) { + this.proxyVersion = Preconditions.checkNotNull(proxyVersion, "proxyVersion"); + return this; + } + + @NonNull + public Builder plugins(@NonNull Collection plugins) { + this.plugins.addAll(Preconditions.checkNotNull(plugins, "plugins")); + return this; + } + + @NonNull + public Builder plugins(@NonNull PluginInformation... plugins) { + this.plugins.addAll(Arrays.asList(Preconditions.checkNotNull(plugins, "plugins"))); + return this; + } + + @NonNull + public Builder clearPlugins() { + this.plugins.clear(); + return this; + } + + /** + * Builds new {@link QueryResponse} with supplied data + * @return response + */ + @NonNull + public QueryResponse build() { + return new QueryResponse( + hostname, + gameVersion, + map, + currentPlayers, + maxPlayers, + proxyHost, + proxyPort, + ImmutableList.copyOf(players), + proxyVersion, + ImmutableList.copyOf(plugins) + ); + } + } + + /** + * Plugin information + */ + public static class PluginInformation { + private String name; + private String version; + + public PluginInformation(@NonNull String name, @Nullable String version) { + this.name = name; + this.version = version; + } + + @NonNull + public String getName() { + return name; + } + + public void setName(@NonNull String name) { + this.name = name; + } + + public void setVersion(@Nullable String version) { + this.version = version; + } + + @Nullable + public String getVersion() { + return version; + } + + @NonNull + public static PluginInformation of(@NonNull String name, @Nullable String version) { + return new PluginInformation(name, version); + } + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index 39ff30899..df11d1bf0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -213,6 +213,11 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi return query.getQueryMap(); } + @Override + public boolean shouldQueryShowPlugins() { + return query.shouldQueryShowPlugins(); + } + public String getMotd() { return motd; } @@ -504,6 +509,10 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi @ConfigKey("map") private String queryMap = "Velocity"; + @Comment("Whether plugins should be shown in query response by default or not") + @ConfigKey("show-plugins") + private boolean showPlugins = false; + private Query() { } @@ -533,13 +542,18 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi return queryMap; } + public boolean shouldQueryShowPlugins() { + return showPlugins; + } + @Override public String toString() { - return "Query{" - + "queryEnabled=" + queryEnabled - + ", queryPort=" + queryPort - + ", queryMap=" + queryMap - + '}'; + return "Query{" + + "queryEnabled=" + queryEnabled + + ", queryPort=" + queryPort + + ", queryMap='" + queryMap + '\'' + + ", showPlugins=" + showPlugins + + '}'; } } } 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 79c7e6b08..8710dd94c 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 @@ -3,7 +3,10 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.collect.ImmutableSet; +import com.velocitypowered.api.event.query.ProxyQueryEvent; +import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.server.QueryResponse; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.protocol.ProtocolConstants; import io.netty.buffer.ByteBuf; @@ -17,9 +20,16 @@ import org.apache.logging.log4j.Logger; import java.net.InetAddress; import java.nio.charset.StandardCharsets; import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static com.velocitypowered.api.event.query.ProxyQueryEvent.QueryType.BASIC; +import static com.velocitypowered.api.event.query.ProxyQueryEvent.QueryType.FULL; public class GS4QueryHandler extends SimpleChannelInboundHandler { private static final Logger logger = LogManager.getLogger(GS4QueryHandler.class); @@ -46,6 +56,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler .expireAfterWrite(30, TimeUnit.SECONDS) .build(); + private volatile List pluginInformationList = null; + private final VelocityServer server; public GS4QueryHandler(VelocityServer server) { @@ -81,6 +93,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler queryResponse.writeByte(QUERY_TYPE_HANDSHAKE); queryResponse.writeInt(sessionId); writeString(queryResponse, Integer.toString(challengeToken)); + ctx.writeAndFlush(responsePacket); break; } @@ -97,28 +110,51 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler throw new IllegalStateException("Invalid query packet"); } - // Packet header - queryResponse.writeByte(QUERY_TYPE_STAT); - queryResponse.writeInt(sessionId); + // Build query response + QueryResponse response = QueryResponse.builder() + .hostname(ComponentSerializers.PLAIN.serialize(server.getConfiguration().getMotdComponent())) + .gameVersion(ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING) + .map(server.getConfiguration().getQueryMap()) + .currentPlayers(server.getPlayerCount()) + .maxPlayers(server.getConfiguration().getShowMaxPlayers()) + .proxyPort(server.getConfiguration().getBind().getPort()) + .proxyHost(server.getConfiguration().getBind().getHostString()) + .players(server.getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList())) + .proxyVersion("Velocity") + .plugins(server.getConfiguration().shouldQueryShowPlugins() ? getRealPluginInformation() : Collections.emptyList()) + .build(); - // Start writing the response - ResponseWriter responseWriter = new ResponseWriter(queryResponse, queryMessage.readableBytes() == 0); - responseWriter.write("hostname", ComponentSerializers.PLAIN.serialize(server.getConfiguration().getMotdComponent())); - responseWriter.write("gametype", "SMP"); + boolean isBasic = queryMessage.readableBytes() == 0; - responseWriter.write("game_id", "MINECRAFT"); - responseWriter.write("version", ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING); - responseWriter.write("plugins", ""); + // Call event and write response + server.getEventManager().fire(new ProxyQueryEvent(isBasic ? BASIC : FULL, senderAddress, response)).whenCompleteAsync((event, exc) -> { + // Packet header + queryResponse.writeByte(QUERY_TYPE_STAT); + queryResponse.writeInt(sessionId); - responseWriter.write("map", server.getConfiguration().getQueryMap()); - 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()); + // Start writing the response + ResponseWriter responseWriter = new ResponseWriter(queryResponse, isBasic); + responseWriter.write("hostname", event.getResponse().getHostname()); + responseWriter.write("gametype", "SMP"); + + responseWriter.write("game_id", "MINECRAFT"); + responseWriter.write("version", event.getResponse().getGameVersion()); + responseWriter.writePlugins(event.getResponse().getProxyVersion(), event.getResponse().getPlugins()); + + responseWriter.write("map", event.getResponse().getMap()); + responseWriter.write("numplayers", event.getResponse().getCurrentPlayers()); + responseWriter.write("maxplayers", event.getResponse().getMaxPlayers()); + responseWriter.write("hostport", event.getResponse().getProxyPort()); + responseWriter.write("hostip", event.getResponse().getProxyHost()); + + if (!responseWriter.isBasic) { + responseWriter.writePlayers(event.getResponse().getPlayers()); + } + + // Send the response + ctx.writeAndFlush(responsePacket); + }, ctx.channel().eventLoop()); - if (!responseWriter.isBasic) { - responseWriter.writePlayers(server.getAllPlayers()); - } break; } @@ -126,9 +162,6 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler throw new IllegalStateException("Invalid query type: " + type); } } - - // Send the response - 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 @@ -142,6 +175,22 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler buf.writeByte(0x00); } + private List getRealPluginInformation() { + // Effective Java, Third Edition; Item 83: Use lazy initialization judiciously + List res = pluginInformationList; + if (res == null) { + synchronized (this) { + if (pluginInformationList == null) { + pluginInformationList = res = server.getPluginManager().getPlugins().stream() + .map(PluginContainer::getDescription) + .map(desc -> QueryResponse.PluginInformation.of(desc.getName().orElse(desc.getId()), desc.getVersion().orElse(null))) + .collect(Collectors.toList()); + } + } + } + return res; + } + private static class ResponseWriter { private final ByteBuf buf; private final boolean isBasic; @@ -180,7 +229,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler // Ends packet k/v body writing and writes stat player list to // the packet if this writer is initialized for full stat response - void writePlayers(Collection players) { + void writePlayers(Collection players) { if (isBasic) { return; } @@ -189,8 +238,29 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler buf.writeByte(0x00); buf.writeBytes(QUERY_RESPONSE_FULL_PADDING2); - players.forEach(player -> writeString(buf, player.getUsername())); + players.forEach(player -> writeString(buf, player)); buf.writeByte(0x00); } + + void writePlugins(String serverVersion, Collection plugins) { + if (isBasic) + return; + + StringBuilder pluginsString = new StringBuilder(); + pluginsString.append(serverVersion).append(':').append(' '); + Iterator iterator = plugins.iterator(); + while (iterator.hasNext()) { + QueryResponse.PluginInformation info = iterator.next(); + pluginsString.append(info.getName()); + if (info.getVersion() != null) { + pluginsString.append(' ').append(info.getVersion()); + } + if (iterator.hasNext()) { + pluginsString.append(';').append(' '); + } + } + + writeString(buf, pluginsString.toString()); + } } } From 447e7d1d50e1f1fd443ad85d8277702fdbf064f1 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Sat, 27 Oct 2018 21:38:20 +0300 Subject: [PATCH 2/2] Adapt to upcoming Checker framework related changes --- .../api/proxy/server/QueryResponse.java | 83 ++++++++----------- .../proxy/protocol/netty/GS4QueryHandler.java | 2 + 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java index 0edd95bc8..2da751e6f 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/QueryResponse.java @@ -3,7 +3,7 @@ package com.velocitypowered.api.proxy.server; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.proxy.config.ProxyConfig; -import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; @@ -43,7 +43,6 @@ public final class QueryResponse { * Get hostname which will be used to reply to the query. By default it is {@link ProxyConfig#getMotdComponent()} in plain text without colour codes. * @return hostname */ - @NonNull public String getHostname() { return hostname; } @@ -52,7 +51,6 @@ public final class QueryResponse { * Get game version which will be used to reply to the query. By default supported Minecraft versions range is sent. * @return game version */ - @NonNull public String getGameVersion() { return gameVersion; } @@ -61,7 +59,6 @@ public final class QueryResponse { * Get map name which will be used to reply to the query. By default {@link ProxyConfig#getQueryMap()} is sent. * @return map name */ - @NonNull public String getMap() { return map; } @@ -86,7 +83,6 @@ public final class QueryResponse { * Get proxy (public facing) hostname * @return proxy hostname */ - @NonNull public String getProxyHost() { return proxyHost; } @@ -103,7 +99,6 @@ public final class QueryResponse { * Get collection of players which will be used to reply to the query. * @return collection of players */ - @NonNull public Collection getPlayers() { return players; } @@ -112,7 +107,6 @@ public final class QueryResponse { * Get server software (name and version) which will be used to reply to the query. * @return server software */ - @NonNull public String getProxyVersion() { return proxyVersion; } @@ -121,7 +115,6 @@ public final class QueryResponse { * Get list of plugins which will be used to reply to the query. * @return collection of plugins */ - @NonNull public Collection getPlugins() { return plugins; } @@ -131,7 +124,6 @@ public final class QueryResponse { * Creates a new {@link Builder} instance from data represented by this response * @return {@link QueryResponse} builder */ - @NonNull public Builder toBuilder() { return QueryResponse.builder() .hostname(getHostname()) @@ -150,7 +142,6 @@ public final class QueryResponse { * Creates a new {@link Builder} instance * @return {@link QueryResponse} builder */ - @NonNull public static Builder builder() { return new Builder(); } @@ -159,101 +150,98 @@ public final class QueryResponse { * A builder for {@link QueryResponse} objects. */ public static final class Builder { + @MonotonicNonNull private String hostname; + + @MonotonicNonNull private String gameVersion; + + @MonotonicNonNull private String map; + + @MonotonicNonNull + private String proxyHost; + + @MonotonicNonNull + private String proxyVersion; + private int currentPlayers; private int maxPlayers; - private String proxyHost; private int proxyPort; - private Collection players = new ArrayList<>(); - private String proxyVersion; + + private List players = new ArrayList<>(); private List plugins = new ArrayList<>(); private Builder() {} - @NonNull - public Builder hostname(@NonNull String hostname) { + public Builder hostname(String hostname) { this.hostname = Preconditions.checkNotNull(hostname, "hostname"); return this; } - @NonNull - public Builder gameVersion(@NonNull String gameVersion) { + public Builder gameVersion(String gameVersion) { this.gameVersion = Preconditions.checkNotNull(gameVersion, "gameVersion"); return this; } - @NonNull - public Builder map(@NonNull String map) { + public Builder map(String map) { this.map = Preconditions.checkNotNull(map, "map"); return this; } - @NonNull public Builder currentPlayers(int currentPlayers) { Preconditions.checkArgument(currentPlayers >= 0, "currentPlayers cannot be negative"); this.currentPlayers = currentPlayers; return this; } - @NonNull public Builder maxPlayers(int maxPlayers) { Preconditions.checkArgument(maxPlayers >= 0, "maxPlayers cannot be negative"); this.maxPlayers = maxPlayers; return this; } - @NonNull - public Builder proxyHost(@NonNull String proxyHost) { + public Builder proxyHost(String proxyHost) { this.proxyHost = Preconditions.checkNotNull(proxyHost, "proxyHost"); return this; } - @NonNull public Builder proxyPort(int proxyPort) { Preconditions.checkArgument(proxyPort >= 1 && proxyPort <= 65535, "proxyPort must be between 1-65535"); this.proxyPort = proxyPort; return this; } - @NonNull - public Builder players(@NonNull Collection players) { + public Builder players(Collection players) { this.players.addAll(Preconditions.checkNotNull(players, "players")); return this; } - @NonNull - public Builder players(@NonNull String... players) { + public Builder players(String... players) { this.players.addAll(Arrays.asList(Preconditions.checkNotNull(players, "players"))); return this; } - @NonNull public Builder clearPlayers() { this.players.clear(); return this; } - @NonNull - public Builder proxyVersion(@NonNull String proxyVersion) { + public Builder proxyVersion(String proxyVersion) { this.proxyVersion = Preconditions.checkNotNull(proxyVersion, "proxyVersion"); return this; } - @NonNull - public Builder plugins(@NonNull Collection plugins) { + public Builder plugins(Collection plugins) { this.plugins.addAll(Preconditions.checkNotNull(plugins, "plugins")); return this; } - @NonNull - public Builder plugins(@NonNull PluginInformation... plugins) { + public Builder plugins(PluginInformation... plugins) { this.plugins.addAll(Arrays.asList(Preconditions.checkNotNull(plugins, "plugins"))); return this; } - @NonNull public Builder clearPlugins() { this.plugins.clear(); return this; @@ -263,18 +251,17 @@ public final class QueryResponse { * Builds new {@link QueryResponse} with supplied data * @return response */ - @NonNull public QueryResponse build() { return new QueryResponse( - hostname, - gameVersion, - map, + Preconditions.checkNotNull(hostname, "hostname"), + Preconditions.checkNotNull(gameVersion, "gameVersion"), + Preconditions.checkNotNull(map, "map"), currentPlayers, maxPlayers, - proxyHost, + Preconditions.checkNotNull(proxyHost, "proxyHost"), proxyPort, ImmutableList.copyOf(players), - proxyVersion, + Preconditions.checkNotNull(proxyVersion, "proxyVersion"), ImmutableList.copyOf(plugins) ); } @@ -287,17 +274,16 @@ public final class QueryResponse { private String name; private String version; - public PluginInformation(@NonNull String name, @Nullable String version) { - this.name = name; - this.version = version; + public PluginInformation(String name, String version) { + this.name = Preconditions.checkNotNull(name, "name"); + this.version = Preconditions.checkNotNull(version, "version"); } - @NonNull public String getName() { return name; } - public void setName(@NonNull String name) { + public void setName(String name) { this.name = name; } @@ -310,8 +296,7 @@ public final class QueryResponse { return version; } - @NonNull - public static PluginInformation of(@NonNull String name, @Nullable String version) { + public static PluginInformation of(String name, @Nullable String version) { return new PluginInformation(name, version); } } 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 8710dd94c..e9a6a016e 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 @@ -16,6 +16,7 @@ import io.netty.channel.socket.DatagramPacket; import net.kyori.text.serializer.ComponentSerializers; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import java.net.InetAddress; import java.nio.charset.StandardCharsets; @@ -56,6 +57,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler .expireAfterWrite(30, TimeUnit.SECONDS) .build(); + @MonotonicNonNull private volatile List pluginInformationList = null; private final VelocityServer server;