30
30
import systems .kinau .fishingbot .network .ping .ServerPinger ;
31
31
import systems .kinau .fishingbot .network .protocol .NetworkHandler ;
32
32
import systems .kinau .fishingbot .network .protocol .ProtocolConstants ;
33
+ import systems .kinau .fishingbot .network .proxy .ServerProxyHandler ;
33
34
import systems .kinau .fishingbot .network .realms .Realm ;
34
35
import systems .kinau .fishingbot .network .realms .RealmsAPI ;
35
36
@@ -53,6 +54,7 @@ public class Bot {
53
54
@ Getter @ Setter private int serverPort ;
54
55
@ Getter @ Setter private AuthData authData ;
55
56
@ Getter @ Setter private boolean wontConnect = false ;
57
+ @ Getter private final boolean proxyMode ;
56
58
57
59
@ Getter private EventManager eventManager ;
58
60
@ Getter private CommandRegistry commandRegistry ;
@@ -68,8 +70,9 @@ public class Bot {
68
70
@ Getter private File logsFolder = new File ("logs" );
69
71
@ Getter private File accountFile = new File ("account.json" );
70
72
71
- public Bot (CommandLine cmdLine ) {
73
+ public Bot (CommandLine cmdLine , boolean proxyMode ) {
72
74
FishingBot .getInstance ().setCurrentBot (this );
75
+ this .proxyMode = proxyMode ;
73
76
this .eventManager = new EventManager ();
74
77
this .moduleManager = new ModuleManager ();
75
78
if (!cmdLine .hasOption ("nogui" ))
@@ -123,7 +126,7 @@ public Bot(CommandLine cmdLine) {
123
126
FishingBot .getI18n ().info ("config-loaded-from" , new File (getConfig ().getPath ()).getAbsolutePath ());
124
127
125
128
// error if credentials are default credentials
126
- if (
getConfig ().
getUserName ().
equals (
"[email protected] " )) {
129
+ if (
! proxyMode && getConfig ().
getUserName ().
equals (
"[email protected] " )) {
127
130
FishingBot .getI18n ().warning ("credentials-not-set" );
128
131
if (!cmdLine .hasOption ("nogui" ))
129
132
Dialogs .showCredentialsNotSet ();
@@ -242,10 +245,8 @@ public Bot(CommandLine cmdLine) {
242
245
port = Integer .parseInt (ipAndPort .split (":" )[1 ]);
243
246
}
244
247
245
- //Ping server
246
- FishingBot .getI18n ().info ("server-pinging" , ip , String .valueOf (port ), getConfig ().getDefaultProtocol ());
247
- ServerPinger sp = new ServerPinger (ip , port );
248
- sp .ping ();
248
+ setServerHost (ip );
249
+ setServerPort (port );
249
250
}
250
251
251
252
public FishingModule getFishingModule () {
@@ -260,16 +261,54 @@ public DiscordModule getDiscordModule() {
260
261
return (DiscordModule ) getModuleManager ().getLoadedModule (DiscordModule .class ).orElse (null );
261
262
}
262
263
263
- public void start (CommandLine cmdLine ) {
264
+ public void ping () {
265
+ if (!isProxyMode ())
266
+ setServerProtocol (ProtocolConstants .getProtocolId (FishingBot .getInstance ().getCurrentBot ().getConfig ().getDefaultProtocol ()));
267
+ FishingBot .getI18n ().info ("server-pinging" , getServerHost (), String .valueOf (getServerPort ()), ProtocolConstants .getVersionString (getServerProtocol ()));
268
+ ServerPinger sp = new ServerPinger (getServerHost (), getServerPort ());
269
+ sp .ping ();
270
+ }
271
+
272
+ private boolean canStart (CommandLine cmdLine ) {
264
273
if (isRunning () || isPreventStartup ()) {
265
274
FishingBot .getInstance ().setCurrentBot (null );
266
275
if (cmdLine .hasOption ("nogui" ))
267
- return ;
276
+ return false ;
268
277
FishingBot .getInstance ().getMainGUIController ().updateStartStop ();
269
278
FishingBot .getInstance ().getMainGUIController ().enableStartStop ();
279
+ return false ;
280
+ }
281
+ return true ;
282
+ }
283
+
284
+ public void start (CommandLine cmdLine ) {
285
+ if (canStart (cmdLine ))
286
+ connect ();
287
+ }
288
+
289
+ public void injectProxy (CommandLine cmdLine , ServerProxyHandler proxyConnection ) {
290
+ if (!canStart (cmdLine )) {
291
+ try {
292
+ if (proxyConnection .getServerSocket () != null && !proxyConnection .getServerSocket ().isClosed ())
293
+ proxyConnection .getServerSocket ().close ();
294
+ if (proxyConnection .getServerSocket () != null && !proxyConnection .getServerSocket ().isClosed ())
295
+ proxyConnection .getClientSocket ().close ();
296
+ } catch (Exception ex ) {
297
+ ex .printStackTrace ();
298
+ }
270
299
return ;
271
300
}
272
- connect ();
301
+
302
+ setRunning (true );
303
+ this .socket = proxyConnection .getServerSocket ();
304
+ this .net = new NetworkHandler (proxyConnection );
305
+ startup (new LootHistory ());
306
+ }
307
+
308
+ public void shutdownProxyConnection () {
309
+ setRunning (false );
310
+ reset ();
311
+ shutdown ();
273
312
}
274
313
275
314
private boolean authenticate (File accountFile ) {
@@ -299,10 +338,87 @@ private void registerCommands() {
299
338
getCommandRegistry ().registerCommand (new SwapCommand ());
300
339
}
301
340
302
- private void connect () {
303
- String serverName = getServerHost ();
304
- int port = getServerPort ();
341
+ private void enableModules (LootHistory savedLootHistory ) {
342
+ getModuleManager ().enableModule (new HandshakeModule (getServerHost (), getServerPort ()));
343
+ getModuleManager ().enableModule (new LoginModule ());
344
+ getModuleManager ().enableModule (new ClientDefaultsModule ());
345
+ getModuleManager ().enableModule (new FishingModule (savedLootHistory ));
346
+ getModuleManager ().enableModule (new ChatProxyModule ());
305
347
348
+ if (getConfig ().isStartTextEnabled ())
349
+ getModuleManager ().enableModule (new ChatCommandModule ());
350
+
351
+ if (getConfig ().isWebHookEnabled ())
352
+ getModuleManager ().enableModule (new DiscordModule ());
353
+
354
+ if (getConfig ().isAutoLootEjectionEnabled ())
355
+ getModuleManager ().enableModule (new EjectionModule ());
356
+
357
+ if (getConfig ().isTimerEnabled ())
358
+ getModuleManager ().enableModule (new TimerModule ());
359
+ }
360
+
361
+ private void startup (LootHistory savedLootHistory ) {
362
+ registerCommands ();
363
+
364
+ if (FishingBot .getInstance ().getMainGUIController () != null && !getEventManager ().isRegistered (FishingBot .getInstance ().getMainGUIController ()))
365
+ getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
366
+
367
+ // enable required modules
368
+
369
+ enableModules (savedLootHistory );
370
+
371
+ // init item handler & player
372
+
373
+ new ItemHandler (getServerProtocol ());
374
+ this .player = new Player ();
375
+ }
376
+
377
+ private void reset () {
378
+ if (getPlayer () != null )
379
+ getEventManager ().unregisterListener (getPlayer ());
380
+ getEventManager ().getRegisteredListener ().clear ();
381
+ getEventManager ().getClassToInstanceMapping ().clear ();
382
+ getModuleManager ().disableAll ();
383
+ this .socket = null ;
384
+ this .net = null ;
385
+ this .player = null ;
386
+ }
387
+
388
+ private boolean canAutoReconnect () {
389
+ if (getConfig ().isAutoReconnect () && !isPreventReconnect ()) {
390
+ FishingBot .getI18n ().info ("bot-automatic-reconnect" , String .valueOf (getConfig ().getAutoReconnectTime ()));
391
+
392
+ try {
393
+ Thread .sleep (getConfig ().getAutoReconnectTime () * 1000 );
394
+ } catch (InterruptedException ignore ) { }
395
+
396
+ if (getAuthData () == null ) {
397
+ if (getConfig ().isOnlineMode ())
398
+ authenticate (accountFile );
399
+ else {
400
+ FishingBot .getI18n ().info ("credentials-using-offline-mode" , getConfig ().getUserName ());
401
+ authData = new AuthData (null , null , null , getConfig ().getUserName ());
402
+ }
403
+ }
404
+ return true ;
405
+ }
406
+ return false ;
407
+ }
408
+
409
+ private void shutdown () {
410
+ FishingBot .getInstance ().setCurrentBot (null );
411
+ if (FishingBot .getInstance ().getMainGUIController () != null ) {
412
+ FishingBot .getInstance ().getMainGUIController ().updateStartStop ();
413
+ FishingBot .getInstance ().getMainGUIController ().updatePlayPaused ();
414
+ try {
415
+ Thread .sleep (1000 );
416
+ } catch (InterruptedException ignore ) { }
417
+ FishingBot .getInstance ().getMainGUIController ().enableStartStop ();
418
+ }
419
+ }
420
+
421
+ private void connect () {
306
422
LootHistory savedLootHistory = new LootHistory ();
307
423
308
424
do {
@@ -321,41 +437,11 @@ private void connect() {
321
437
continue ;
322
438
}
323
439
}
324
- this .socket = new Socket (serverName , port );
440
+ this .socket = new Socket (getServerHost (), getServerPort () );
325
441
326
442
this .net = new NetworkHandler ();
327
443
328
- registerCommands ();
329
- if (FishingBot .getInstance ().getMainGUIController () != null )
330
- getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
331
-
332
- if (FishingBot .getInstance ().getMainGUIController () != null && !getEventManager ().isRegistered (FishingBot .getInstance ().getMainGUIController ()))
333
- getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
334
-
335
- // enable required modules
336
-
337
- getModuleManager ().enableModule (new HandshakeModule (serverName , port ));
338
- getModuleManager ().enableModule (new LoginModule (getAuthData ().getUsername ()));
339
- getModuleManager ().enableModule (new ClientDefaultsModule ());
340
- getModuleManager ().enableModule (new FishingModule (savedLootHistory ));
341
- getModuleManager ().enableModule (new ChatProxyModule ());
342
-
343
- if (getConfig ().isStartTextEnabled ())
344
- getModuleManager ().enableModule (new ChatCommandModule ());
345
-
346
- if (getConfig ().isWebHookEnabled ())
347
- getModuleManager ().enableModule (new DiscordModule ());
348
-
349
- if (getConfig ().isAutoLootEjectionEnabled ())
350
- getModuleManager ().enableModule (new EjectionModule ());
351
-
352
- if (getConfig ().isTimerEnabled ())
353
- getModuleManager ().enableModule (new TimerModule ());
354
-
355
- // init item handler & player
356
-
357
- new ItemHandler (getServerProtocol ());
358
- this .player = new Player ();
444
+ startup (savedLootHistory );
359
445
360
446
// add shutdown hook
361
447
@@ -372,7 +458,7 @@ private void connect() {
372
458
373
459
while (running ) {
374
460
try {
375
- net .readData ();
461
+ net .readDataFromServer ();
376
462
} catch (Exception ex ) {
377
463
ex .printStackTrace ();
378
464
FishingBot .getI18n ().warning ("packet-could-not-be-received" );
@@ -384,48 +470,17 @@ private void connect() {
384
470
FishingBot .getI18n ().severe ("bot-could-not-be-started" , e .getMessage ());
385
471
} finally {
386
472
try {
387
- if (socket != null )
473
+ if (socket != null && ! socket . isClosed () )
388
474
this .socket .close ();
389
475
} catch (IOException e ) {
390
476
e .printStackTrace ();
391
477
}
392
478
393
- if (getPlayer () != null )
394
- getEventManager ().unregisterListener (getPlayer ());
395
- getEventManager ().getRegisteredListener ().clear ();
396
- getEventManager ().getClassToInstanceMapping ().clear ();
397
479
if (getFishingModule () != null )
398
480
savedLootHistory = getFishingModule ().getLootHistory ();
399
- getModuleManager ().disableAll ();
400
- this .socket = null ;
401
- this .net = null ;
402
- this .player = null ;
403
- }
404
- if (getConfig ().isAutoReconnect () && !isPreventReconnect ()) {
405
- FishingBot .getI18n ().info ("bot-automatic-reconnect" , String .valueOf (getConfig ().getAutoReconnectTime ()));
406
-
407
- try {
408
- Thread .sleep (getConfig ().getAutoReconnectTime () * 1000 );
409
- } catch (InterruptedException ignore ) { }
410
-
411
- if (getAuthData () == null ) {
412
- if (getConfig ().isOnlineMode ())
413
- authenticate (accountFile );
414
- else {
415
- FishingBot .getI18n ().info ("credentials-using-offline-mode" , getConfig ().getUserName ());
416
- authData = new AuthData (null , null , null , getConfig ().getUserName ());
417
- }
418
- }
481
+ reset ();
419
482
}
420
- } while (getConfig ().isAutoReconnect () && !isPreventReconnect ());
421
- FishingBot .getInstance ().setCurrentBot (null );
422
- if (FishingBot .getInstance ().getMainGUIController () != null ) {
423
- FishingBot .getInstance ().getMainGUIController ().updateStartStop ();
424
- FishingBot .getInstance ().getMainGUIController ().updatePlayPaused ();
425
- try {
426
- Thread .sleep (1000 );
427
- } catch (InterruptedException ignore ) { }
428
- FishingBot .getInstance ().getMainGUIController ().enableStartStop ();
429
- }
483
+ } while (canAutoReconnect ());
484
+ shutdown ();
430
485
}
431
486
}
0 commit comments