-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPingerExpansion.java
More file actions
337 lines (275 loc) · 10.3 KB
/
PingerExpansion.java
File metadata and controls
337 lines (275 loc) · 10.3 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
package com.extendedclip.papi.expansion.pinger;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
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.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.*;
@SuppressWarnings("unused")
public class PingerExpansion extends PlaceholderExpansion implements Cacheable, Taskable, Configurable {
private String online = "&aOnline";
private String offline = "&cOffline";
@Nullable
private LoadingCache<String, Future<Pinger>> cache;
private int interval = 60;
public Map<String, Object> getDefaults() {
Map<String, Object> defaults = new HashMap<>();
defaults.put("check_interval", 30);
defaults.put("online", "&aOnline");
defaults.put("offline", "&cOffline");
return defaults;
}
public void start() {
this.online = getString("online", this.online);
this.offline = getString("offline", this.offline);
int time = getInt("check_interval", 60);
if (time > 0) this.interval = time;
this.cache = CacheBuilder.newBuilder()
.concurrencyLevel(1)
.refreshAfterWrite(this.interval, TimeUnit.SECONDS)
.build(new PingerCacheLoader());
}
public void stop() {
if (this.cache == null) return;
this.cache.asMap().values().forEach(future -> future.cancel(true));
this.cache = null;
}
public void clear() {
if (this.cache == null) return;
this.cache.cleanUp();
}
public boolean canRegister() {
return true;
}
public @NotNull String getAuthor() {
return "clip";
}
public @NotNull String getIdentifier() {
return "pinger";
}
public @NotNull String getVersion() {
return "1.0.2";
}
public String onPlaceholderRequest(Player p, String identifier) {
final int place = identifier.indexOf("_");
if (place == -1)
return null;
final String type = identifier.substring(0, place).toLowerCase(Locale.ROOT);
final String address = identifier.substring(place + 1);
if (this.cache == null) return null;
try {
final Future<Pinger> future = this.cache.get(address);
final Pinger pinger = future.isDone() ? future.get() : null;
switch (type) {
case "motd":
if (pinger == null) return "";
return pinger.getMotd();
case "count":
case "players":
if (pinger == null) return "0";
return String.valueOf(pinger.getPlayersOnline());
case "max":
case "maxplayers":
if (pinger == null) return "0";
return String.valueOf(pinger.getMaxPlayers());
case "pingversion":
case "pingv":
if (pinger == null) return "-1";
return String.valueOf(pinger.getPingVersion());
case "gameversion":
case "version":
if (pinger == null) return "";
return pinger.getGameVersion();
case "online":
case "isonline":
if (pinger == null) return this.offline;
return this.online;
}
} catch (InterruptedException | ExecutionException ignored) {
}
return null;
}
private static 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,
Charset.forName("UTF-16BE"));
dataOutputStream.write(new byte[]{-2, 1});
int packetId = inputStream.read();
if (packetId == -1) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
if (packetId != 255) {
try {
socket.close();
} catch (IOException iOException) {
}
socket = null;
return false;
}
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;
}
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);
if (string.startsWith("&")) {
String[] data = string.split("\000");
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 {
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;
}
}
private static class PingerCacheLoader extends CacheLoader<String, Future<Pinger>> {
@Override
public Future<Pinger> load(final String key) {
final InetSocketAddress address = this.address(key);
final Pinger pinger = new Pinger(address.getHostName(), address.getPort());
return CompletableFuture.supplyAsync(() -> {
if (!pinger.fetchData()) return null;
return pinger;
});
}
private InetSocketAddress address(final String key) {
final int index = key.indexOf(":");
if (index == -1) return new InetSocketAddress(key, 25565);
final String host = key.substring(0, index);
final int port = Integer.parseInt(key.substring(index + 1));
return new InetSocketAddress(host, port);
}
}
}