PaperMC/patches/api/0163-Add-GS4-Query-event.patch

408 lines
13 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Mark Vainomaa <mikroskeem@mikroskeem.eu>
Date: Sun, 17 Mar 2019 21:46:27 +0200
Subject: [PATCH] Add GS4 Query event
diff --git a/src/main/java/com/destroystokyo/paper/event/server/GS4QueryEvent.java b/src/main/java/com/destroystokyo/paper/event/server/GS4QueryEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..5a8813c80e56d17ccaa35a7eef2f6bd70c7a3d96
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/GS4QueryEvent.java
@@ -0,0 +1,395 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.server;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import org.bukkit.Server;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
2024-02-01 10:15:57 +01:00
+ * This event is fired if server is getting queried over GS4 Query protocol.
+ * <br>
2021-06-11 14:02:28 +02:00
+ * Adapted from Velocity's ProxyQueryEvent
+ *
+ * @author Mark Vainomaa
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public final class GS4QueryEvent extends Event {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
2021-06-11 14:02:28 +02:00
+
+ private final QueryType queryType;
+ private final InetAddress querierAddress;
+ private QueryResponse response;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public GS4QueryEvent(final QueryType queryType, final InetAddress querierAddress, final QueryResponse response) {
2021-06-11 14:02:28 +02:00
+ super(true); // should always be called async
2024-02-01 10:15:57 +01:00
+ this.queryType = queryType;
+ this.querierAddress = querierAddress;
+ this.response = response;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get query type
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return query type
+ */
+ public QueryType getQueryType() {
2024-02-01 10:15:57 +01:00
+ return this.queryType;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get querier address
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return querier address
+ */
+ public InetAddress getQuerierAddress() {
2024-02-01 10:15:57 +01:00
+ return this.querierAddress;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get query response
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return query response
+ */
+ public QueryResponse getResponse() {
2024-02-01 10:15:57 +01:00
+ return this.response;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Set query response
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @param response query response
+ */
+ public void setResponse(final QueryResponse response) {
2021-06-11 14:02:28 +02:00
+ this.response = Preconditions.checkNotNull(response, "response");
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * The type of query
+ */
+ public enum QueryType {
+ /**
+ * Basic query asks only a subset of information, such as motd, game type (hardcoded to <pre>MINECRAFT</pre>), map,
+ * current players, max players, server port and server motd
+ */
+ BASIC,
+
+ /**
+ * Full query asks pretty much everything present on this event (only hardcoded values cannot be modified here).
+ */
+ FULL
+ }
+
+ public static final class QueryResponse {
2024-02-01 10:15:57 +01:00
+
2021-06-11 14:02:28 +02:00
+ private final String motd;
+ private final String gameVersion;
+ private final String map;
+ private final int currentPlayers;
+ private final int maxPlayers;
+ private final String hostname;
+ private final int port;
+ private final Collection<String> players;
+ private final String serverVersion;
+ private final Collection<PluginInformation> plugins;
+
+ private QueryResponse(final String motd, final String gameVersion, final String map, final int currentPlayers, final int maxPlayers, final String hostname, final int port, final Collection<String> players, final String serverVersion, final Collection<PluginInformation> plugins) {
2021-06-11 14:02:28 +02:00
+ this.motd = motd;
+ this.gameVersion = gameVersion;
+ this.map = map;
+ this.currentPlayers = currentPlayers;
+ this.maxPlayers = maxPlayers;
+ this.hostname = hostname;
+ this.port = port;
+ this.players = players;
+ this.serverVersion = serverVersion;
+ this.plugins = plugins;
+ }
+
+ /**
+ * Get motd which will be used to reply to the query. By default, it is {@link Server#getMotd()}.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return motd
+ */
+ public String getMotd() {
2024-02-01 10:15:57 +01:00
+ return this.motd;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get game version which will be used to reply to the query. By default, supported Minecraft versions range is sent.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return game version
+ */
+ public String getGameVersion() {
2024-02-01 10:15:57 +01:00
+ return this.gameVersion;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get map name which will be used to reply to the query. By default {@code world} is sent.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return map name
+ */
+ public String getMap() {
2024-02-01 10:15:57 +01:00
+ return this.map;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get current online player count which will be used to reply to the query.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return online player count
+ */
+ public int getCurrentPlayers() {
2024-02-01 10:15:57 +01:00
+ return this.currentPlayers;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get max player count which will be used to reply to the query.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return max player count
+ */
+ public int getMaxPlayers() {
2024-02-01 10:15:57 +01:00
+ return this.maxPlayers;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Get server (public facing) hostname.
+ *
2021-06-11 14:02:28 +02:00
+ * @return server hostname
+ */
+ public String getHostname() {
2024-02-01 10:15:57 +01:00
+ return this.hostname;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Get server (public facing) port.
+ *
2021-06-11 14:02:28 +02:00
+ * @return server port
+ */
+ public int getPort() {
2024-02-01 10:15:57 +01:00
+ return this.port;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get collection of players which will be used to reply to the query.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return collection of players
+ */
+ public Collection<String> getPlayers() {
2024-02-01 10:15:57 +01:00
+ return this.players;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get server software (name and version) which will be used to reply to the query.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return server software
+ */
+ public String getServerVersion() {
2024-02-01 10:15:57 +01:00
+ return this.serverVersion;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Get list of plugins which will be used to reply to the query.
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return collection of plugins
+ */
+ public Collection<PluginInformation> getPlugins() {
2024-02-01 10:15:57 +01:00
+ return this.plugins;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Creates a new {@link Builder} instance from data represented by this response.
+ *
2021-06-11 14:02:28 +02:00
+ * @return {@link QueryResponse} builder
+ */
+ public Builder toBuilder() {
+ return QueryResponse.builder()
+ .motd(this.getMotd())
+ .gameVersion(this.getGameVersion())
+ .map(this.getMap())
+ .currentPlayers(this.getCurrentPlayers())
+ .maxPlayers(this.getMaxPlayers())
+ .hostname(this.getHostname())
+ .port(this.getPort())
+ .players(this.getPlayers())
+ .serverVersion(this.getServerVersion())
+ .plugins(this.getPlugins());
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Creates a new {@link Builder} instance.
+ *
2021-06-11 14:02:28 +02:00
+ * @return {@link QueryResponse} builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * A builder for {@link QueryResponse} objects.
+ */
+ public static final class Builder {
+
+ private @MonotonicNonNull String motd;
+ private @MonotonicNonNull String gameVersion;
+ private @MonotonicNonNull String map;
+ private @MonotonicNonNull String hostname;
+ private @MonotonicNonNull String serverVersion;
2021-06-11 14:02:28 +02:00
+
+ private int currentPlayers;
+ private int maxPlayers;
+ private int port;
+
2024-02-01 10:15:57 +01:00
+ private final List<String> players = new ArrayList<>();
+ private final List<PluginInformation> plugins = new ArrayList<>();
2021-06-11 14:02:28 +02:00
+
+ private Builder() {
+ }
2021-06-11 14:02:28 +02:00
+
+ public Builder motd(final String motd) {
2021-06-11 14:02:28 +02:00
+ this.motd = Preconditions.checkNotNull(motd, "motd");
+ return this;
+ }
+
+ public Builder gameVersion(final String gameVersion) {
2021-06-11 14:02:28 +02:00
+ this.gameVersion = Preconditions.checkNotNull(gameVersion, "gameVersion");
+ return this;
+ }
+
+ public Builder map(final String map) {
2021-06-11 14:02:28 +02:00
+ this.map = Preconditions.checkNotNull(map, "map");
+ return this;
+ }
+
+ public Builder currentPlayers(final int currentPlayers) {
2021-06-11 14:02:28 +02:00
+ Preconditions.checkArgument(currentPlayers >= 0, "currentPlayers cannot be negative");
+ this.currentPlayers = currentPlayers;
+ return this;
+ }
+
+ public Builder maxPlayers(final int maxPlayers) {
2021-06-11 14:02:28 +02:00
+ Preconditions.checkArgument(maxPlayers >= 0, "maxPlayers cannot be negative");
+ this.maxPlayers = maxPlayers;
+ return this;
+ }
+
+ public Builder hostname(final String hostname) {
2021-06-11 14:02:28 +02:00
+ this.hostname = Preconditions.checkNotNull(hostname, "hostname");
+ return this;
+ }
+
+ public Builder port(final int port) {
2021-06-11 14:02:28 +02:00
+ Preconditions.checkArgument(port >= 1 && port <= 65535, "port must be between 1-65535");
+ this.port = port;
+ return this;
+ }
+
+ public Builder players(final Collection<String> players) {
2021-06-11 14:02:28 +02:00
+ this.players.addAll(Preconditions.checkNotNull(players, "players"));
+ return this;
+ }
+
+ public Builder players(final String... players) {
2021-06-11 14:02:28 +02:00
+ this.players.addAll(Arrays.asList(Preconditions.checkNotNull(players, "players")));
+ return this;
+ }
+
+ public Builder clearPlayers() {
+ this.players.clear();
+ return this;
+ }
+
+ public Builder serverVersion(final String serverVersion) {
2021-06-11 14:02:28 +02:00
+ this.serverVersion = Preconditions.checkNotNull(serverVersion, "serverVersion");
+ return this;
+ }
+
+ public Builder plugins(final Collection<PluginInformation> plugins) {
2021-06-11 14:02:28 +02:00
+ this.plugins.addAll(Preconditions.checkNotNull(plugins, "plugins"));
+ return this;
+ }
+
+ public Builder plugins(final PluginInformation... plugins) {
2021-06-11 14:02:28 +02:00
+ this.plugins.addAll(Arrays.asList(Preconditions.checkNotNull(plugins, "plugins")));
+ return this;
+ }
+
+ public Builder clearPlugins() {
+ this.plugins.clear();
+ return this;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Builds new {@link QueryResponse} with supplied data.
+ *
2021-06-11 14:02:28 +02:00
+ * @return response
+ */
+ public QueryResponse build() {
+ return new QueryResponse(
+ Preconditions.checkNotNull(this.motd, "motd"),
+ Preconditions.checkNotNull(this.gameVersion, "gameVersion"),
+ Preconditions.checkNotNull(this.map, "map"),
+ this.currentPlayers,
+ this.maxPlayers,
+ Preconditions.checkNotNull(this.hostname, "hostname"),
+ this.port,
+ ImmutableList.copyOf(this.players),
+ Preconditions.checkNotNull(this.serverVersion, "serverVersion"),
+ ImmutableList.copyOf(this.plugins)
2021-06-11 14:02:28 +02:00
+ );
+ }
+ }
+
+ /**
+ * Plugin information
+ */
+ public static class PluginInformation {
2024-02-01 10:15:57 +01:00
+
2021-06-11 14:02:28 +02:00
+ private String name;
+ private String version;
+
+ public PluginInformation(final String name, final String version) {
2021-06-11 14:02:28 +02:00
+ this.name = Preconditions.checkNotNull(name, "name");
+ this.version = Preconditions.checkNotNull(version, "version");
+ }
+
+ public String getName() {
2024-02-01 10:15:57 +01:00
+ return this.name;
2021-06-11 14:02:28 +02:00
+ }
+
+ public void setName(final String name) {
2021-06-11 14:02:28 +02:00
+ this.name = name;
+ }
+
+ public void setVersion(final String version) {
2021-06-11 14:02:28 +02:00
+ this.version = version;
+ }
+
+ public String getVersion() {
2024-02-01 10:15:57 +01:00
+ return this.version;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static PluginInformation of(final String name, final String version) {
2021-06-11 14:02:28 +02:00
+ return new PluginInformation(name, version);
+ }
+ }
+ }
+}