19
19
20
20
import com .velocitypowered .api .network .ProtocolVersion ;
21
21
import com .velocitypowered .api .proxy .Player ;
22
+ import com .velocitypowered .api .scheduler .ScheduledTask ;
22
23
import com .velocitypowered .proxy .protocol .MinecraftPacket ;
23
24
import com .velocitypowered .proxy .protocol .packet .ClientSettings ;
24
25
import com .velocitypowered .proxy .protocol .packet .PluginMessage ;
25
26
import com .velocitypowered .proxy .protocol .util .PluginMessageUtil ;
26
- import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
27
27
import java .text .MessageFormat ;
28
28
import java .util .List ;
29
+ import java .util .concurrent .TimeUnit ;
29
30
import net .elytrium .limboapi .api .Limbo ;
30
31
import net .elytrium .limboapi .api .LimboFactory ;
31
32
import net .elytrium .limboapi .api .chunk .VirtualChunk ;
39
40
import net .elytrium .limbofilter .stats .Statistics ;
40
41
import org .slf4j .Logger ;
41
42
42
- @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
43
43
public class BotFilterSessionHandler extends FallingCheckHandler {
44
44
45
- private static long TOTAL_TICKS ;
46
- private static double CAPTCHA_Y ;
47
- private static long TOTAL_TIME ;
45
+ public static long FALLING_CHECK_TOTAL_TIME ;
48
46
49
- private final Player player ;
47
+ private final Player proxyPlayer ;
50
48
private final LimboFilter plugin ;
51
49
private final Statistics statistics ;
52
50
private final Logger logger ;
@@ -56,22 +54,24 @@ public class BotFilterSessionHandler extends FallingCheckHandler {
56
54
private final MinecraftPacket fallingCheckChunk ;
57
55
private final MinecraftPacket fallingCheckView ;
58
56
57
+ private final long joinTime = System .currentTimeMillis ();
58
+ private ScheduledTask filterMainTask ;
59
+
59
60
private CheckState state = CheckState .valueOf (Settings .IMP .MAIN .CHECK_STATE );
60
- private LimboPlayer limboPlayer ;
61
+ private LimboPlayer player ;
61
62
private Limbo server ;
62
63
private String captchaAnswer ;
63
64
private int attempts = Settings .IMP .MAIN .CAPTCHA_ATTEMPTS ;
64
65
private int ignoredTicks = 0 ;
65
66
private int nonValidPacketsSize = 0 ;
66
- private long joinTime = System .currentTimeMillis ();
67
67
private boolean startedListening = false ;
68
68
private boolean checkedBySettings = false ;
69
69
private boolean checkedByBrand = false ;
70
70
71
- public BotFilterSessionHandler (Player player , LimboFilter plugin ) {
72
- super (player .getProtocolVersion ());
71
+ public BotFilterSessionHandler (Player proxyPlayer , LimboFilter plugin ) {
72
+ super (proxyPlayer .getProtocolVersion ());
73
73
74
- this .player = player ;
74
+ this .proxyPlayer = proxyPlayer ;
75
75
this .plugin = plugin ;
76
76
77
77
this .statistics = this .plugin .getStatistics ();
@@ -94,24 +94,31 @@ public BotFilterSessionHandler(Player player, LimboFilter plugin) {
94
94
@ Override
95
95
public void onSpawn (Limbo server , LimboPlayer player ) {
96
96
this .server = server ;
97
- this .limboPlayer = player ;
97
+ this .player = player ;
98
98
99
99
if (this .state == CheckState .ONLY_CAPTCHA ) {
100
100
this .sendCaptcha ();
101
101
} else if (this .state == CheckState .CAPTCHA_POSITION ) {
102
102
this .sendFallingCheckPackets ();
103
103
this .sendCaptcha ();
104
104
} else if (this .state == CheckState .ONLY_POSITION || this .state == CheckState .CAPTCHA_ON_POSITION_FAILED ) {
105
- if (this .player .getProtocolVersion ().compareTo (ProtocolVersion .MINECRAFT_1_8 ) >= 0 ) {
105
+ if (this .proxyPlayer .getProtocolVersion ().compareTo (ProtocolVersion .MINECRAFT_1_8 ) >= 0 ) {
106
106
if (!Settings .IMP .MAIN .STRINGS .CHECKING_TITLE .isEmpty () && !Settings .IMP .MAIN .STRINGS .CHECKING_SUBTITLE .isEmpty ()) {
107
- this .limboPlayer .writePacket (this .packets .getCheckingTitle ());
107
+ this .player .writePacket (this .packets .getCheckingTitle ());
108
108
}
109
109
}
110
- this .limboPlayer .writePacket (this .packets .getCheckingChat ());
110
+ this .player .writePacket (this .packets .getCheckingChat ());
111
111
this .sendFallingCheckPackets ();
112
112
}
113
113
114
- this .limboPlayer .flushPackets ();
114
+ this .player .flushPackets ();
115
+
116
+ this .filterMainTask = this .plugin .getServer ().getScheduler ().buildTask (this .plugin , () -> {
117
+ // TODO: Maybe check for max ping?
118
+ if (System .currentTimeMillis () - BotFilterSessionHandler .this .joinTime > Settings .IMP .MAIN .TIME_OUT ) {
119
+ BotFilterSessionHandler .this .disconnect (BotFilterSessionHandler .this .packets .getTimesUp (), true );
120
+ }
121
+ }).delay (1 , TimeUnit .SECONDS ).repeat (1 , TimeUnit .SECONDS ).schedule ();
115
122
}
116
123
117
124
@ Override
@@ -129,7 +136,7 @@ public void onMove() {
129
136
++this .nonValidPacketsSize ;
130
137
}
131
138
if (this .startedListening && this .state != CheckState .SUCCESSFUL ) {
132
- if (this .lastY == CAPTCHA_Y || this .onGround ) {
139
+ if (this .lastY == Settings . IMP . MAIN . COORDS . CAPTCHA_Y || this .onGround ) {
133
140
return ;
134
141
}
135
142
if (this .state == CheckState .ONLY_CAPTCHA ) {
@@ -142,7 +149,7 @@ public void onMove() {
142
149
++this .ignoredTicks ;
143
150
return ;
144
151
}
145
- if (this .ticks >= TOTAL_TICKS ) {
152
+ if (this .ticks >= Settings . IMP . MAIN . FALLING_CHECK_TICKS ) {
146
153
if (this .state == CheckState .CAPTCHA_POSITION ) {
147
154
this .changeStateToCaptcha ();
148
155
} else {
@@ -168,7 +175,7 @@ public void onMove() {
168
175
}
169
176
PreparedPacket expBuf = this .packets .getExperience ().get (this .ticks );
170
177
if (expBuf != null ) {
171
- this .limboPlayer .writePacketAndFlush (expBuf );
178
+ this .player .writePacketAndFlush (expBuf );
172
179
}
173
180
174
181
++this .ticks ;
@@ -179,7 +186,7 @@ public void onMove() {
179
186
public void onChat (String message ) {
180
187
if ((this .state == CheckState .CAPTCHA_POSITION || this .state == CheckState .ONLY_CAPTCHA )) {
181
188
if (message .equals (this .captchaAnswer )) {
182
- this .limboPlayer .writePacketAndFlush (this .packets .getResetSlot ());
189
+ this .player .writePacketAndFlush (this .packets .getResetSlot ());
183
190
this .finishCheck ();
184
191
} else if (--this .attempts != 0 ) {
185
192
this .sendCaptcha ();
@@ -195,21 +202,26 @@ public void onGeneric(Object packet) {
195
202
PluginMessage pluginMessage = (PluginMessage ) packet ;
196
203
if (PluginMessageUtil .isMcBrand (pluginMessage ) && !this .checkedByBrand ) {
197
204
String brand = PluginMessageUtil .readBrandMessage (pluginMessage .content ());
198
- this .logger .info ("{} has client brand {}" , this .player , brand );
205
+ this .logger .info ("{} has client brand {}" , this .proxyPlayer , brand );
199
206
if (!Settings .IMP .MAIN .BLOCKED_CLIENT_BRANDS .contains (brand )) {
200
207
this .checkedByBrand = true ;
201
208
}
202
209
}
203
210
} else if (packet instanceof ClientSettings ) {
204
- if (! this . checkedBySettings && Settings .IMP .MAIN .CHECK_CLIENT_SETTINGS ) {
211
+ if (Settings .IMP .MAIN .CHECK_CLIENT_SETTINGS && ! this . checkedBySettings ) {
205
212
this .checkedBySettings = true ;
206
213
}
207
214
}
208
215
}
209
216
217
+ @ Override
218
+ public void onDisconnect () {
219
+ this .filterMainTask .cancel ();
220
+ }
221
+
210
222
private void finishCheck () {
211
- if (System .currentTimeMillis () - this .joinTime < TOTAL_TIME && this .state != CheckState .ONLY_CAPTCHA ) {
212
- if (this .state == CheckState .CAPTCHA_POSITION && this .ticks < TOTAL_TICKS ) {
223
+ if (System .currentTimeMillis () - this .joinTime < FALLING_CHECK_TOTAL_TIME && this .state != CheckState .ONLY_CAPTCHA ) {
224
+ if (this .state == CheckState .CAPTCHA_POSITION && this .ticks < Settings . IMP . MAIN . FALLING_CHECK_TICKS ) {
213
225
this .state = CheckState .ONLY_POSITION ;
214
226
} else {
215
227
if (this .state == CheckState .CAPTCHA_ON_POSITION_FAILED ) {
@@ -221,70 +233,72 @@ private void finishCheck() {
221
233
return ;
222
234
}
223
235
224
- if (! this . checkedBySettings && Settings .IMP .MAIN .CHECK_CLIENT_SETTINGS ) {
236
+ if (Settings .IMP .MAIN .CHECK_CLIENT_SETTINGS && ! this . checkedBySettings ) {
225
237
this .disconnect (this .packets .getKickClientCheckSettings (), true );
226
238
}
227
- if (! this . checkedByBrand && Settings .IMP .MAIN .CHECK_CLIENT_BRAND ) {
239
+ if (Settings .IMP .MAIN .CHECK_CLIENT_BRAND && ! this . checkedByBrand ) {
228
240
this .disconnect (this .packets .getKickClientCheckBrand (), true );
229
241
}
230
242
231
243
this .state = CheckState .SUCCESSFUL ;
232
- this .plugin .cacheFilterUser (this .player );
244
+ this .plugin .cacheFilterUser (this .proxyPlayer );
233
245
234
246
if (this .plugin .checkCpsLimit (Settings .IMP .MAIN .FILTER_AUTO_TOGGLE .ONLINE_MODE_VERIFY )
235
247
|| this .plugin .checkCpsLimit (Settings .IMP .MAIN .FILTER_AUTO_TOGGLE .NEED_TO_RECONNECT )) {
236
248
this .disconnect (this .packets .getSuccessfulBotFilterDisconnect (), false );
237
249
} else {
238
- this .limboPlayer .writePacketAndFlush (this .packets .getSuccessfulBotFilterChat ());
239
- this .limboPlayer .disconnect ();
250
+ this .player .writePacketAndFlush (this .packets .getSuccessfulBotFilterChat ());
251
+ this .player .disconnect ();
240
252
}
241
253
}
242
254
243
255
private void sendFallingCheckPackets () {
244
- this .limboPlayer .writePacket (this .fallingCheckPos );
245
- if (this .player .getProtocolVersion ().compareTo (ProtocolVersion .MINECRAFT_1_14 ) >= 0 ) {
246
- this .limboPlayer .writePacket (this .fallingCheckView );
256
+ this .player .writePacket (this .fallingCheckPos );
257
+ if (this .proxyPlayer .getProtocolVersion ().compareTo (ProtocolVersion .MINECRAFT_1_14 ) >= 0 ) {
258
+ this .player .writePacket (this .fallingCheckView );
247
259
}
248
260
249
- this .limboPlayer .writePacket (this .fallingCheckChunk );
261
+ this .player .writePacket (this .fallingCheckChunk );
250
262
}
251
263
252
264
private void sendCaptcha () {
253
- ProtocolVersion version = this .player .getProtocolVersion ();
265
+ ProtocolVersion version = this .proxyPlayer .getProtocolVersion ();
254
266
CaptchaHandler captchaHandler = this .plugin .getCachedCaptcha ().randomCaptcha ();
255
267
String captchaAnswer = captchaHandler .getAnswer ();
256
268
this .setCaptchaAnswer (captchaAnswer );
257
269
Settings .MAIN .STRINGS strings = Settings .IMP .MAIN .STRINGS ;
258
270
if (this .attempts == Settings .IMP .MAIN .CAPTCHA_ATTEMPTS ) {
259
- this .limboPlayer .writePacket (
271
+ this .player .writePacket (
260
272
this .packets .createChatPacket (this .plugin .getFactory (), MessageFormat .format (strings .CHECKING_CAPTCHA_CHAT , this .attempts ))
261
273
);
262
274
if (version .compareTo (ProtocolVersion .MINECRAFT_1_8 ) >= 0 ) {
263
275
if (!strings .CHECKING_CAPTCHA_TITLE .isEmpty () && !strings .CHECKING_CAPTCHA_SUBTITLE .isEmpty ()) {
264
- this .limboPlayer .writePacket (
276
+ this .player .writePacket (
265
277
this .packets .createTitlePacket (
266
- this .plugin .getFactory (), strings .CHECKING_CAPTCHA_TITLE , MessageFormat .format (strings .CHECKING_CAPTCHA_SUBTITLE , this .attempts )
278
+ this .plugin .getFactory (),
279
+ MessageFormat .format (strings .CHECKING_CAPTCHA_TITLE , this .attempts ),
280
+ MessageFormat .format (strings .CHECKING_CAPTCHA_SUBTITLE , this .attempts )
267
281
)
268
282
);
269
283
}
270
284
}
271
285
} else {
272
- this .limboPlayer .writePacket (
286
+ this .player .writePacket (
273
287
this .packets .createChatPacket (this .plugin .getFactory (), MessageFormat .format (strings .CHECKING_WRONG_CAPTCHA_CHAT , this .attempts ))
274
288
);
275
289
}
276
- this .limboPlayer .writePacket (this .packets .getSetSlot ());
290
+ this .player .writePacket (this .packets .getSetSlot ());
277
291
for (Object packet : captchaHandler .getMapPacket (version )) {
278
- this .limboPlayer .writePacket (packet );
292
+ this .player .writePacket (packet );
279
293
}
280
294
281
- this .limboPlayer .flushPackets ();
295
+ this .player .flushPackets ();
282
296
}
283
297
284
298
private void fallingCheckFailed () {
285
299
if (this .state == CheckState .CAPTCHA_ON_POSITION_FAILED ) {
286
300
List <PreparedPacket > expList = this .packets .getExperience ();
287
- this .limboPlayer .writePacketAndFlush (expList .get (expList .size () - 1 ));
301
+ this .player .writePacketAndFlush (expList .get (expList .size () - 1 ));
288
302
this .changeStateToCaptcha ();
289
303
return ;
290
304
}
@@ -293,7 +307,7 @@ private void fallingCheckFailed() {
293
307
}
294
308
295
309
private void disconnect (PreparedPacket reason , boolean blocked ) {
296
- this .limboPlayer .closeWith (reason );
310
+ this .player .closeWith (reason );
297
311
if (blocked ) {
298
312
this .statistics .addBlockedConnection ();
299
313
}
@@ -304,8 +318,8 @@ private boolean checkY() {
304
318
}
305
319
306
320
private void setCaptchaPositionAndDisableFalling () {
307
- this .server .respawnPlayer (this .player );
308
- this .limboPlayer .writePacketAndFlush (this .packets .getNoAbilities ());
321
+ this .server .respawnPlayer (this .proxyPlayer );
322
+ this .player .writePacketAndFlush (this .packets .getNoAbilities ());
309
323
310
324
this .waitingTeleportId = this .validTeleportId ;
311
325
}
@@ -316,7 +330,7 @@ public void setCaptchaAnswer(String captchaAnswer) {
316
330
317
331
private void changeStateToCaptcha () {
318
332
this .state = CheckState .ONLY_CAPTCHA ;
319
- this .joinTime = System .currentTimeMillis () + TOTAL_TIME ;
333
+ // this.joinTime = System.currentTimeMillis() + this.fallingCheckTotalTime ;
320
334
this .setCaptchaPositionAndDisableFalling ();
321
335
if (this .captchaAnswer == null ) {
322
336
this .sendCaptcha ();
@@ -336,24 +350,12 @@ private MinecraftPacket createUpdateViewPosition(LimboFactory factory, int x, in
336
350
return (MinecraftPacket ) factory .instantiatePacket (BuiltInPackets .UpdateViewPosition , x >> 4 , z >> 4 );
337
351
}
338
352
339
- public static void reload () {
340
- TOTAL_TICKS = Settings .IMP .MAIN .FALLING_CHECK_TICKS ;
341
- TOTAL_TIME = (TOTAL_TICKS * 50 ) - 100 ;
342
- CAPTCHA_Y = Settings .IMP .MAIN .COORDS .CAPTCHA_Y ;
343
- }
344
-
345
- public static long getTotalTicks () {
346
- return TOTAL_TICKS ;
347
- }
348
-
349
- @ SuppressWarnings ("unused" )
350
353
public enum CheckState {
351
354
352
355
ONLY_POSITION ,
353
356
ONLY_CAPTCHA ,
354
357
CAPTCHA_POSITION ,
355
358
CAPTCHA_ON_POSITION_FAILED ,
356
- SUCCESSFUL ,
357
- FAILED
359
+ SUCCESSFUL
358
360
}
359
361
}
0 commit comments