aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0163-Add-GS4-Query-event.patch
blob: a153279317eb04d03028a9ad59058ddd0155d2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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..8edc33bde29e967cec488d0f5e2f1097291978a6
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/GS4QueryEvent.java
@@ -0,0 +1,424 @@
+package com.destroystokyo.paper.event.server;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import org.bukkit.Server;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.ApiStatus;
+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;
+
+/**
+ * This event is fired if server is getting queried over GS4 Query protocol.
+ * <br>
+ * Adapted from Velocity's ProxyQueryEvent
+ *
+ * @author Mark Vainomaa
+ */
+public final class GS4QueryEvent extends Event {
+
+    private static final HandlerList HANDLER_LIST = new HandlerList();
+
+    private final QueryType queryType;
+    private final InetAddress querierAddress;
+    private QueryResponse response;
+
+    @ApiStatus.Internal
+    public GS4QueryEvent(@NotNull QueryType queryType, @NotNull InetAddress querierAddress, @NotNull QueryResponse response) {
+        super(true); // should always be called async
+        this.queryType = queryType;
+        this.querierAddress = querierAddress;
+        this.response = response;
+    }
+
+    /**
+     * Get query type
+     *
+     * @return query type
+     */
+    @NotNull
+    public QueryType getQueryType() {
+        return this.queryType;
+    }
+
+    /**
+     * Get querier address
+     *
+     * @return querier address
+     */
+    @NotNull
+    public InetAddress getQuerierAddress() {
+        return this.querierAddress;
+    }
+
+    /**
+     * Get query response
+     *
+     * @return query response
+     */
+    @NotNull
+    public QueryResponse getResponse() {
+        return this.response;
+    }
+
+    /**
+     * Set query response
+     *
+     * @param response query response
+     */
+    public void setResponse(@NotNull QueryResponse response) {
+        this.response = Preconditions.checkNotNull(response, "response");
+    }
+
+    @NotNull
+    @Override
+    public HandlerList getHandlers() {
+        return HANDLER_LIST;
+    }
+
+    @NotNull
+    public static HandlerList getHandlerList() {
+        return HANDLER_LIST;
+    }
+
+    /**
+     * 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 {
+
+        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;
+        }
+
+        /**
+         * Get motd which will be used to reply to the query. By default it is {@link Server#getMotd()}.
+         *
+         * @return motd
+         */
+        @NotNull
+        public String getMotd() {
+            return this.motd;
+        }
+
+        /**
+         * Get game version which will be used to reply to the query. By default supported Minecraft versions range is sent.
+         *
+         * @return game version
+         */
+        @NotNull
+        public String getGameVersion() {
+            return this.gameVersion;
+        }
+
+        /**
+         * Get map name which will be used to reply to the query. By default {@code world} is sent.
+         *
+         * @return map name
+         */
+        @NotNull
+        public String getMap() {
+            return this.map;
+        }
+
+        /**
+         * Get current online player count which will be used to reply to the query.
+         *
+         * @return online player count
+         */
+        public int getCurrentPlayers() {
+            return this.currentPlayers;
+        }
+
+        /**
+         * Get max player count which will be used to reply to the query.
+         *
+         * @return max player count
+         */
+        public int getMaxPlayers() {
+            return this.maxPlayers;
+        }
+
+        /**
+         * Get server (public facing) hostname.
+         *
+         * @return server hostname
+         */
+        @NotNull
+        public String getHostname() {
+            return this.hostname;
+        }
+
+        /**
+         * Get server (public facing) port.
+         *
+         * @return server port
+         */
+        public int getPort() {
+            return this.port;
+        }
+
+        /**
+         * Get collection of players which will be used to reply to the query.
+         *
+         * @return collection of players
+         */
+        @NotNull
+        public Collection<String> getPlayers() {
+            return this.players;
+        }
+
+        /**
+         * Get server software (name and version) which will be used to reply to the query.
+         *
+         * @return server software
+         */
+        @NotNull
+        public String getServerVersion() {
+            return this.serverVersion;
+        }
+
+        /**
+         * Get list of plugins which will be used to reply to the query.
+         *
+         * @return collection of plugins
+         */
+        @NotNull
+        public Collection<PluginInformation> getPlugins() {
+            return this.plugins;
+        }
+
+        /**
+         * Creates a new {@link Builder} instance from data represented by this response.
+         *
+         * @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());
+        }
+
+        /**
+         * Creates a new {@link Builder} instance.
+         *
+         * @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;
+
+            private final List<String> players = new ArrayList<>();
+            private final List<PluginInformation> plugins = new ArrayList<>();
+
+            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;
+            }
+
+            /**
+             * Builds new {@link QueryResponse} with supplied data.
+             *
+             * @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 {
+
+            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() {
+                return this.name;
+            }
+
+            public void setName(@NotNull String name) {
+                this.name = name;
+            }
+
+            public void setVersion(@NotNull String version) {
+                this.version = version;
+            }
+
+            @NotNull
+            public String getVersion() {
+                return this.version;
+            }
+
+            @NotNull
+            public static PluginInformation of(@NotNull String name, @NotNull String version) {
+                return new PluginInformation(name, version);
+            }
+        }
+    }
+}