This repository has been archived by the owner on May 13, 2024. It is now read-only.
forked from PaperMC/Waterfall
-
Notifications
You must be signed in to change notification settings - Fork 2
/
0028-Proxy-query-event.patch
215 lines (207 loc) · 8.18 KB
/
0028-Proxy-query-event.patch
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
From 02d85afb1a967d4b744be4c31c4709e3d20fb0f1 Mon Sep 17 00:00:00 2001
From: minecrafter <unknown@unknown>
Date: Sun, 3 Jul 2016 04:03:21 -0400
Subject: [PATCH] Proxy query event
diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/QueryResult.java b/api/src/main/java/io/github/waterfallmc/waterfall/QueryResult.java
new file mode 100644
index 00000000..9a262c8a
--- /dev/null
+++ b/api/src/main/java/io/github/waterfallmc/waterfall/QueryResult.java
@@ -0,0 +1,59 @@
+package io.github.waterfallmc.waterfall;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NonNull;
+
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+public class QueryResult {
+ /**
+ * The message of the day to return.
+ */
+ @NonNull
+ private String motd;
+ /**
+ * The game type to return, usually SMP.
+ */
+ @NonNull
+ private String gameType;
+ /**
+ * The world name to return.
+ */
+ @NonNull
+ private String worldName;
+ /**
+ * The number of players currently online.
+ */
+ private int onlinePlayers;
+ /**
+ * The maximum number of players that can be online.
+ */
+ private int maxPlayers;
+ /**
+ * The port for this server.
+ */
+ private int port;
+ /**
+ * The hostname for this server.
+ */
+ @NonNull
+ private String address;
+ /**
+ * The game ID for this server, usually MINECRAFT.
+ */
+ @NonNull
+ private String gameId;
+ /**
+ * The players currently online.
+ */
+ @NonNull
+ private final List<String> players;
+ /**
+ * The version to return for this server.
+ */
+ @NonNull
+ private String version;
+}
diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/event/ProxyQueryEvent.java b/api/src/main/java/io/github/waterfallmc/waterfall/event/ProxyQueryEvent.java
new file mode 100644
index 00000000..220d59f0
--- /dev/null
+++ b/api/src/main/java/io/github/waterfallmc/waterfall/event/ProxyQueryEvent.java
@@ -0,0 +1,28 @@
+package io.github.waterfallmc.waterfall.event;
+
+import io.github.waterfallmc.waterfall.QueryResult;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+import net.md_5.bungee.api.config.ListenerInfo;
+import net.md_5.bungee.api.plugin.Event;
+
+/**
+ * This event will be posted whenever a Query request is received.
+ */
+@AllArgsConstructor
+@EqualsAndHashCode(callSuper = false)
+@Data
+public class ProxyQueryEvent extends Event {
+ /**
+ * The listener associated with this query.
+ */
+ @NonNull
+ private final ListenerInfo listener;
+ /**
+ * The query to return.
+ */
+ @NonNull
+ private QueryResult result;
+}
diff --git a/query/src/main/java/net/md_5/bungee/query/QueryHandler.java b/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
index 9b81197f..ac99d02c 100644
--- a/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
+++ b/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
@@ -2,6 +2,8 @@ package net.md_5.bungee.query;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
+import io.github.waterfallmc.waterfall.QueryResult;
+import io.github.waterfallmc.waterfall.event.ProxyQueryEvent;
import io.netty.buffer.ByteBuf;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.ChannelHandlerContext;
@@ -9,10 +11,12 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import java.net.InetAddress;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
+import java.util.stream.Collectors;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ProxyServer;
@@ -95,19 +99,30 @@ public class QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
throw new IllegalStateException( "No session!" );
}
+ // Waterfall start
+ List<String> players = bungee.getPlayers().stream().map(ProxiedPlayer::getName).collect(Collectors.toList());
+
+ ProxyQueryEvent event = new ProxyQueryEvent(listener, new QueryResult(listener.getMotd(), "SMP", "Waterfall_Proxy",
+ bungee.getOnlineCount(), listener.getMaxPlayers(), listener.getHost().getPort(),
+ listener.getHost().getHostString(), "MINECRAFT", players, bungee.getGameVersion()));
+ QueryResult result = bungee.getPluginManager().callEvent(event).getResult();
+ // Waterfall end
+
out.writeByte( 0x00 );
out.writeInt( sessionId );
if ( in.readableBytes() == 0 )
{
// Short response
- writeString( out, listener.getMotd() ); // MOTD
- writeString( out, "SMP" ); // Game Type
- writeString( out, "BungeeCord_Proxy" ); // World Name
- writeNumber( out, bungee.getOnlineCount() ); // Online Count
- writeNumber( out, listener.getMaxPlayers() ); // Max Players
- writeShort( out, listener.getHost().getPort() ); // Port
- writeString( out, listener.getHost().getHostString() ); // IP
+ // Waterfall start
+ writeString( out, result.getMotd() ); // MOTD
+ writeString( out, result.getGameType() ); // Game Type
+ writeString( out, result.getWorldName() ); // World Name
+ writeNumber( out, result.getOnlinePlayers() ); // Online Count
+ writeNumber( out, result.getMaxPlayers() ); // Max Players
+ writeShort( out, result.getPort() ); // Port
+ writeString( out, result.getAddress() ); // IP
+ // Waterfall end
} else if ( in.readableBytes() == 4 )
{
// Long Response
@@ -117,18 +132,20 @@ public class QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
} );
Map<String, String> data = new LinkedHashMap<>();
- data.put( "hostname", listener.getMotd() );
- data.put( "gametype", "SMP" );
+ // Waterfall start
+ data.put( "hostname", result.getMotd() );
+ data.put( "gametype", result.getGameType() );
// Start Extra Info
- data.put( "game_id", "MINECRAFT" );
- data.put( "version", bungee.getGameVersion() );
- data.put( "plugins", "" );
+ data.put( "game_id", result.getGameId() );
+ data.put( "version", result.getVersion() );
+ data.put( "plugins", "" ); // TODO: Allow population?
// End Extra Info
- data.put( "map", "BungeeCord_Proxy" );
- data.put( "numplayers", Integer.toString( bungee.getOnlineCount() ) );
- data.put( "maxplayers", Integer.toString( listener.getMaxPlayers() ) );
- data.put( "hostport", Integer.toString( listener.getHost().getPort() ) );
- data.put( "hostip", listener.getHost().getHostString() );
+ data.put( "map", result.getWorldName() );
+ data.put( "numplayers", Integer.toString( result.getOnlinePlayers() ) );
+ data.put( "maxplayers", Integer.toString( result.getMaxPlayers() ) );
+ data.put( "hostport", Integer.toString( result.getPort() ) );
+ data.put( "hostip", result.getAddress() );
+ // Waterfall end
for ( Map.Entry<String, String> entry : data.entrySet() )
{
@@ -140,10 +157,7 @@ public class QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
// Padding
writeString( out, "\01player_\00" );
// Player List
- for ( ProxiedPlayer p : bungee.getPlayers() )
- {
- writeString( out, p.getName() );
- }
+ result.getPlayers().stream().forEach(p -> writeString(out, p)); // Waterfall
out.writeByte( 0x00 ); // Null
} else
{
--
2.42.0