-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPingerExpansion.java
More file actions
350 lines (295 loc) · 11.9 KB
/
PingerExpansion.java
File metadata and controls
350 lines (295 loc) · 11.9 KB
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
package com.extendedclip.papi.expansion.pinger;
import me.clip.placeholderapi.expansion.Cacheable;
import me.clip.placeholderapi.expansion.Configurable;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Taskable;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class PingerExpansion extends PlaceholderExpansion implements Cacheable, Taskable, Configurable {
private BukkitTask pingTask = null;
private String online = "&aOnline";
private String offline = "&cOffline";
private final Map<String, Pinger> servers = new ConcurrentHashMap<>();
private final Map<String, InetSocketAddress> toPing = new ConcurrentHashMap<>();
private int interval = 60;
public Map<String, Object> getDefaults() {
Map<String, Object> defaults = new HashMap<>();
defaults.put("check_interval", Integer.valueOf(30));
defaults.put("online", "&aOnline");
defaults.put("offline", "&cOffline");
return defaults;
}
public void start() {
String on = getString("online", "&aOnline");
this.online = (on != null) ? on : "&aOnline";
String off = getString("offline", "&cOffline");
this.offline = (off != null) ? off : "&cOffline";
int time = getInt("check_interval", 60);
if (time > 0)
this.interval = time;
this.pingTask = (new BukkitRunnable() {
public void run() {
if (PingerExpansion.this.toPing.isEmpty())
return;
for (Map.Entry<String, InetSocketAddress> address : PingerExpansion.this.toPing.entrySet()) {
PingerExpansion.Pinger r;
try {
r = new PingerExpansion.Pinger(address.getValue().getHostName(), address.getValue().getPort());
if (r.fetchData()) {
PingerExpansion.this.servers.put(address.getKey(), r);
continue;
}
if (PingerExpansion.this.servers.containsKey(address.getKey()))
PingerExpansion.this.servers.remove(address.getKey());
} catch (Exception exception) {
}
}
}
}).runTaskTimerAsynchronously(getPlaceholderAPI(), 20L, 20L * this.interval);
}
public void stop() {
try {
this.pingTask.cancel();
} catch (Exception exception) {
}
this.pingTask = null;
}
public void clear() {
this.servers.clear();
this.toPing.clear();
}
public boolean canRegister() {
return true;
}
public String getAuthor() {
return "clip";
}
public String getIdentifier() {
return "pinger";
}
public String getPlugin() {
return null;
}
public String getVersion() {
return "1.0.1";
}
public String onPlaceholderRequest(Player p, String identifier) {
int place = identifier.indexOf("_");
if (place == -1)
return null;
String type = identifier.substring(0, place);
String address = identifier.substring(place + 1);
Pinger r = null;
for (String a : this.servers.keySet()) {
if (a.equalsIgnoreCase(address)) {
r = this.servers.get(a);
break;
}
}
if (r == null)
if (!this.toPing.containsKey(address)) {
int port = 25565;
String add = address;
if (address.contains(":")) {
add = address.substring(0, address.indexOf(":"));
try {
port = Integer.parseInt(address.substring(address.indexOf(":") + 1));
} catch (Exception exception) {
}
}
this.toPing.put(address, new InetSocketAddress(add, port));
}
if (type.equalsIgnoreCase("motd"))
return (r != null) ? r.getMotd() : "";
if (type.equalsIgnoreCase("count") || type.equalsIgnoreCase("players"))
return (r != null) ? String.valueOf(r.getPlayersOnline()) : "0";
if (type.equalsIgnoreCase("max") || type.equalsIgnoreCase("maxplayers"))
return (r != null) ? String.valueOf(r.getMaxPlayers()) : "0";
if (type.equalsIgnoreCase("pingversion") || type.equalsIgnoreCase("pingv"))
return (r != null) ? String.valueOf(r.getPingVersion()) : "-1";
if (type.equalsIgnoreCase("gameversion") || type.equalsIgnoreCase("version"))
return (r != null && r.getGameVersion() != null) ? r.getGameVersion() : "";
if (type.equalsIgnoreCase("online") || type.equalsIgnoreCase("isonline"))
return (r != null) ? this.online : this.offline;
return null;
}
public final class Pinger {
private String address = "localhost";
private int port = 25565;
private int timeout = 2000;
private int pingVersion = -1;
private int protocolVersion = -1;
private String gameVersion;
private String motd;
private int playersOnline = -1;
private int maxPlayers = -1;
public Pinger(String address, int port) {
setAddress(address);
setPort(port);
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return this.address;
}
public void setPort(int port) {
this.port = port;
}
public int getPort() {
return this.port;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getTimeout() {
return this.timeout;
}
private void setPingVersion(int pingVersion) {
this.pingVersion = pingVersion;
}
public int getPingVersion() {
return this.pingVersion;
}
private void setProtocolVersion(int protocolVersion) {
this.protocolVersion = protocolVersion;
}
public int getProtocolVersion() {
return this.protocolVersion;
}
private void setGameVersion(String gameVersion) {
this.gameVersion = gameVersion;
}
public String getGameVersion() {
return this.gameVersion;
}
private void setMotd(String motd) {
this.motd = motd;
}
public String getMotd() {
return this.motd;
}
private void setPlayersOnline(int playersOnline) {
this.playersOnline = playersOnline;
}
public int getPlayersOnline() {
return this.playersOnline;
}
private void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
public int getMaxPlayers() {
return this.maxPlayers;
}
public boolean fetchData() {
try {
Socket socket = new Socket();
socket.setSoTimeout(this.timeout);
socket.connect(
new InetSocketAddress(getAddress(), getPort()),
getTimeout());
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,
StandardCharsets.UTF_16BE);
// Send server list ping request, in 1.4-1.5 format
// See https://wiki.vg/Server_List_Ping#1.4_to_1.5
dataOutputStream.write(0xFE);
dataOutputStream.write(0x01);
// Then, read the ping response (a kick packet)
// Beta 1.8 - 1.3 servers should respond with this format: https://wiki.vg/Server_List_Ping#Beta_1.8_to_1.3
// 1.4+ servers should respond with this format: https://wiki.vg/Server_List_Ping#1.4_to_1.5
// which uses the same response format as: https://wiki.vg/Server_List_Ping#1.6
// Read packet ID field (1 byte), should be 0xFF (kick packet ID)
int packetId = inputStream.read();
if (packetId == -1) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
if (packetId != 0xFF) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
// Read string length field (2 bytes)
int length = inputStreamReader.read();
if (length == -1) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
if (length == 0) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
// Read string (length bytes)
char[] chars = new char[length];
if (inputStreamReader.read(chars, 0, length) != length) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
String string = new String(chars);
// Read the fields of the string
if (string.startsWith("§")) {
// If the string starts with '§', the server is probably running 1.4+
// See https://wiki.vg/Server_List_Ping#1.4_to_1.5
// and https://wiki.vg/Server_List_Ping#1.6
// In this format, fields are delimited by '\0' characters
String[] data = string.split("\0");
setPingVersion(Integer.parseInt(data[0].substring(1)));
setProtocolVersion(Integer.parseInt(data[1]));
setGameVersion(data[2]);
setMotd(data[3]);
setPlayersOnline(Integer.parseInt(data[4]));
setMaxPlayers(Integer.parseInt(data[5]));
} else {
// If the string doesn't start with '§', the server is probably running Beta 1.8 - 1.3
// See https://wiki.vg/Server_List_Ping#Beta_1.8_to_1.3
// In this format, fields are delimited by '§' characters
String[] data = string.split("§");
setMotd(data[0]);
setPlayersOnline(Integer.parseInt(data[1]));
setMaxPlayers(Integer.parseInt(data[2]));
}
dataOutputStream.close();
outputStream.close();
inputStreamReader.close();
inputStream.close();
socket.close();
} catch (SocketException exception) {
return false;
} catch (IOException exception) {
return false;
}
return true;
}
}
}