Paper/patches/api/0169-Add-GS4-Query-event.patch

437 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
2024-02-01 10:15:57 +01:00
index 0000000000000000000000000000000000000000..8edc33bde29e967cec488d0f5e2f1097291978a6
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/GS4QueryEvent.java
2024-02-01 10:15:57 +01:00
@@ -0,0 +1,424 @@
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;
2024-02-01 10:15:57 +01:00
+import org.bukkit.Server;
2021-06-11 14:02:28 +02:00
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2021-06-11 14:02:28 +02:00
+import org.jetbrains.annotations.NotNull;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
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
+ */
+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
2021-06-11 14:02:28 +02:00
+ public GS4QueryEvent(@NotNull QueryType queryType, @NotNull InetAddress querierAddress, @NotNull QueryResponse response) {
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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(@NotNull QueryResponse response) {
+ this.response = Preconditions.checkNotNull(response, "response");
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ @NotNull
+ 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 final static 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(String motd, String gameVersion, String map, int currentPlayers, int maxPlayers, String hostname, int port, Collection<String> players, String serverVersion, Collection<PluginInformation> plugins) {
+ 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;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Get motd which will be used to reply to the query. By default it is {@link Server#getMotd()}.
+ *
2021-06-11 14:02:28 +02:00
+ * @return motd
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ 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
+ */
+ @NotNull
+ public Builder toBuilder() {
+ return QueryResponse.builder()
+ .motd(getMotd())
+ .gameVersion(getGameVersion())
+ .map(getMap())
+ .currentPlayers(getCurrentPlayers())
+ .maxPlayers(getMaxPlayers())
+ .hostname(getHostname())
+ .port(getPort())
+ .players(getPlayers())
+ .serverVersion(getServerVersion())
+ .plugins(getPlugins());
+ }
+
+ /**
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
+ */
+ @NotNull
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * A builder for {@link QueryResponse} objects.
+ */
+ public static final class Builder {
+ private String motd;
+ private String gameVersion;
+ private String map;
+ private String hostname;
+ private String serverVersion;
+
+ 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() {}
+
+ @NotNull
+ public Builder motd(@NotNull String motd) {
+ this.motd = Preconditions.checkNotNull(motd, "motd");
+ return this;
+ }
+
+ @NotNull
+ public Builder gameVersion(@NotNull String gameVersion) {
+ this.gameVersion = Preconditions.checkNotNull(gameVersion, "gameVersion");
+ return this;
+ }
+
+ @NotNull
+ public Builder map(@NotNull String map) {
+ this.map = Preconditions.checkNotNull(map, "map");
+ return this;
+ }
+
+ @NotNull
+ public Builder currentPlayers(int currentPlayers) {
+ Preconditions.checkArgument(currentPlayers >= 0, "currentPlayers cannot be negative");
+ this.currentPlayers = currentPlayers;
+ return this;
+ }
+
+ @NotNull
+ public Builder maxPlayers(int maxPlayers) {
+ Preconditions.checkArgument(maxPlayers >= 0, "maxPlayers cannot be negative");
+ this.maxPlayers = maxPlayers;
+ return this;
+ }
+
+ @NotNull
+ public Builder hostname(@NotNull String hostname) {
+ this.hostname = Preconditions.checkNotNull(hostname, "hostname");
+ return this;
+ }
+
+ @NotNull
+ public Builder port(int port) {
+ Preconditions.checkArgument(port >= 1 && port <= 65535, "port must be between 1-65535");
+ this.port = port;
+ return this;
+ }
+
+ @NotNull
+ public Builder players(@NotNull Collection<String> players) {
+ this.players.addAll(Preconditions.checkNotNull(players, "players"));
+ return this;
+ }
+
+ @NotNull
+ public Builder players(@NotNull String... players) {
+ this.players.addAll(Arrays.asList(Preconditions.checkNotNull(players, "players")));
+ return this;
+ }
+
+ @NotNull
+ public Builder clearPlayers() {
+ this.players.clear();
+ return this;
+ }
+
+ @NotNull
+ public Builder serverVersion(@NotNull String serverVersion) {
+ this.serverVersion = Preconditions.checkNotNull(serverVersion, "serverVersion");
+ return this;
+ }
+
+ @NotNull
+ public Builder plugins(@NotNull Collection<PluginInformation> plugins) {
+ this.plugins.addAll(Preconditions.checkNotNull(plugins, "plugins"));
+ return this;
+ }
+
+ @NotNull
+ public Builder plugins(@NotNull PluginInformation... plugins) {
+ this.plugins.addAll(Arrays.asList(Preconditions.checkNotNull(plugins, "plugins")));
+ return this;
+ }
+
+ @NotNull
+ 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
+ */
+ @NotNull
+ public QueryResponse build() {
+ return new QueryResponse(
+ Preconditions.checkNotNull(motd, "motd"),
+ Preconditions.checkNotNull(gameVersion, "gameVersion"),
+ Preconditions.checkNotNull(map, "map"),
+ currentPlayers,
+ maxPlayers,
+ Preconditions.checkNotNull(hostname, "hostname"),
+ port,
+ ImmutableList.copyOf(players),
+ Preconditions.checkNotNull(serverVersion, "serverVersion"),
+ ImmutableList.copyOf(plugins)
+ );
+ }
+ }
+
+ /**
+ * 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(@NotNull String name, @NotNull String version) {
+ this.name = Preconditions.checkNotNull(name, "name");
+ this.version = Preconditions.checkNotNull(version, "version");
+ }
+
+ @NotNull
+ 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(@NotNull String name) {
+ this.name = name;
+ }
+
+ public void setVersion(@NotNull String version) {
+ this.version = version;
+ }
+
+ @NotNull
+ public String getVersion() {
2024-02-01 10:15:57 +01:00
+ return this.version;
2021-06-11 14:02:28 +02:00
+ }
+
+ @NotNull
+ public static PluginInformation of(@NotNull String name, @NotNull String version) {
+ return new PluginInformation(name, version);
+ }
+ }
+ }
+}