11
11
import systems .kinau .fishingbot .auth .AuthData ;
12
12
import systems .kinau .fishingbot .auth .Authenticator ;
13
13
import systems .kinau .fishingbot .bot .Player ;
14
+ import systems .kinau .fishingbot .bot .loot .LootHistory ;
14
15
import systems .kinau .fishingbot .event .EventManager ;
15
16
import systems .kinau .fishingbot .gui .Dialogs ;
17
+ import systems .kinau .fishingbot .gui .GUIController ;
16
18
import systems .kinau .fishingbot .i18n .I18n ;
17
19
import systems .kinau .fishingbot .io .config .SettingsConfig ;
18
20
import systems .kinau .fishingbot .io .logging .LogFormatter ;
19
- import systems .kinau .fishingbot .modules .ChatProxyModule ;
20
- import systems .kinau .fishingbot .modules .ClientDefaultsModule ;
21
- import systems .kinau .fishingbot .modules .HandshakeModule ;
22
- import systems .kinau .fishingbot .modules .LoginModule ;
21
+ import systems .kinau .fishingbot .modules .*;
23
22
import systems .kinau .fishingbot .modules .command .ChatCommandModule ;
24
23
import systems .kinau .fishingbot .modules .command .CommandRegistry ;
25
24
import systems .kinau .fishingbot .modules .command .commands .*;
26
25
import systems .kinau .fishingbot .modules .discord .DiscordModule ;
27
26
import systems .kinau .fishingbot .modules .ejection .EjectionModule ;
28
27
import systems .kinau .fishingbot .modules .fishing .FishingModule ;
29
28
import systems .kinau .fishingbot .modules .fishing .ItemHandler ;
29
+ import systems .kinau .fishingbot .modules .timer .TimerModule ;
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 ;
38
38
import java .net .Socket ;
39
39
import java .util .Arrays ;
40
40
import java .util .List ;
41
+ import java .util .Optional ;
41
42
import java .util .concurrent .atomic .AtomicBoolean ;
42
43
import java .util .logging .FileHandler ;
43
44
@@ -55,11 +56,9 @@ public class Bot {
55
56
56
57
@ Getter private EventManager eventManager ;
57
58
@ Getter private CommandRegistry commandRegistry ;
59
+ @ Getter private ModuleManager moduleManager ;
58
60
59
61
@ Getter private Player player ;
60
- @ Getter private ClientDefaultsModule clientModule ;
61
- @ Getter private ChatProxyModule chatProxyModule ;
62
- @ Getter private EjectionModule ejectModule ;
63
62
64
63
@ Getter private Socket socket ;
65
64
@ Getter private NetworkHandler net ;
@@ -72,6 +71,7 @@ public class Bot {
72
71
public Bot (CommandLine cmdLine ) {
73
72
FishingBot .getInstance ().setCurrentBot (this );
74
73
this .eventManager = new EventManager ();
74
+ this .moduleManager = new ModuleManager ();
75
75
if (!cmdLine .hasOption ("nogui" ))
76
76
getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
77
77
@@ -132,9 +132,16 @@ public Bot(CommandLine cmdLine) {
132
132
}
133
133
134
134
// authenticate player if online-mode is set
135
- if (getConfig ().isOnlineMode ())
136
- authenticate (accountFile );
137
- else {
135
+ if (getConfig ().isOnlineMode ()) {
136
+ boolean authSuccessful = authenticate (accountFile );
137
+ if (!authSuccessful ) {
138
+ setPreventStartup (true );
139
+ FishingBot .getI18n ().severe ("credentials-invalid" );
140
+ if (!cmdLine .hasOption ("nogui" )) {
141
+ Dialogs .showCredentialsInvalid (GUIController ::openWebpage );
142
+ }
143
+ }
144
+ } else {
138
145
FishingBot .getI18n ().info ("credentials-using-offline-mode" , getConfig ().getUserName ());
139
146
this .authData = new AuthData (null , null , null , getConfig ().getUserName ());
140
147
}
@@ -241,6 +248,18 @@ public Bot(CommandLine cmdLine) {
241
248
sp .ping ();
242
249
}
243
250
251
+ public FishingModule getFishingModule () {
252
+ return (FishingModule ) getModuleManager ().getLoadedModule (FishingModule .class ).orElse (null );
253
+ }
254
+
255
+ public EjectionModule getEjectModule () {
256
+ return (EjectionModule ) getModuleManager ().getLoadedModule (EjectionModule .class ).orElse (null );
257
+ }
258
+
259
+ public DiscordModule getDiscordModule () {
260
+ return (DiscordModule ) getModuleManager ().getLoadedModule (DiscordModule .class ).orElse (null );
261
+ }
262
+
244
263
public void start () {
245
264
if (isRunning () || isPreventStartup ()) {
246
265
FishingBot .getInstance ().setCurrentBot (null );
@@ -253,14 +272,14 @@ public void start() {
253
272
254
273
private boolean authenticate (File accountFile ) {
255
274
Authenticator authenticator = new Authenticator (accountFile );
256
- AuthData authData = authenticator .authenticate ();
275
+ Optional < AuthData > authData = authenticator .authenticate (getConfig (). getAuthService () );
257
276
258
- if (authData == null ) {
277
+ if (! authData . isPresent () ) {
259
278
setAuthData (new AuthData (null , null , null , getConfig ().getUserName ()));
260
279
return false ;
261
280
}
262
281
263
- setAuthData (authData );
282
+ setAuthData (authData . get () );
264
283
return true ;
265
284
}
266
285
@@ -273,12 +292,16 @@ private void registerCommands() {
273
292
getCommandRegistry ().registerCommand (new StuckCommand ());
274
293
getCommandRegistry ().registerCommand (new DropRodCommand ());
275
294
getCommandRegistry ().registerCommand (new LookCommand ());
295
+ getCommandRegistry ().registerCommand (new SummaryCommand ());
296
+ getCommandRegistry ().registerCommand (new RightClickCommand ());
276
297
}
277
298
278
299
private void connect () {
279
300
String serverName = getServerHost ();
280
301
int port = getServerPort ();
281
302
303
+ LootHistory savedLootHistory = new LootHistory ();
304
+
282
305
do {
283
306
try {
284
307
setRunning (true );
@@ -300,27 +323,39 @@ private void connect() {
300
323
this .net = new NetworkHandler ();
301
324
302
325
registerCommands ();
326
+ if (FishingBot .getInstance ().getMainGUIController () != null )
327
+ getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
328
+
329
+ if (FishingBot .getInstance ().getMainGUIController () != null && !getEventManager ().isRegistered (FishingBot .getInstance ().getMainGUIController ()))
330
+ getEventManager ().registerListener (FishingBot .getInstance ().getMainGUIController ());
303
331
304
- this .fishingModule = new FishingModule ();
305
- getFishingModule ().enable ();
332
+ // enable required modules
333
+
334
+ getModuleManager ().enableModule (new HandshakeModule (serverName , port ));
335
+ getModuleManager ().enableModule (new LoginModule (getAuthData ().getUsername ()));
336
+ getModuleManager ().enableModule (new ClientDefaultsModule ());
337
+ getModuleManager ().enableModule (new FishingModule (savedLootHistory ));
338
+ getModuleManager ().enableModule (new ChatProxyModule ());
306
339
307
- new HandshakeModule (serverName , port ).enable ();
308
- new LoginModule (getAuthData ().getUsername ()).enable ();
309
- this .chatProxyModule = new ChatProxyModule ();
310
- getChatProxyModule ().enable ();
311
340
if (getConfig ().isStartTextEnabled ())
312
- new ChatCommandModule ().enable ();
313
- this .clientModule = new ClientDefaultsModule ();
314
- getClientModule ().enable ();
341
+ getModuleManager ().enableModule (new ChatCommandModule ());
342
+
315
343
if (getConfig ().isWebHookEnabled ())
316
- new DiscordModule ().enable ();
317
- if (getConfig ().isAutoLootEjectionEnabled ()) {
318
- this .ejectModule = new EjectionModule ();
319
- getEjectModule ().enable ();
320
- }
344
+ getModuleManager ().enableModule (new DiscordModule ());
345
+
346
+ if (getConfig ().isAutoLootEjectionEnabled ())
347
+ getModuleManager ().enableModule (new EjectionModule ());
348
+
349
+ if (getConfig ().isTimerEnabled ())
350
+ getModuleManager ().enableModule (new TimerModule ());
351
+
352
+ // init item handler & player
353
+
321
354
new ItemHandler (getServerProtocol ());
322
355
this .player = new Player ();
323
356
357
+ // add shutdown hook
358
+
324
359
Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
325
360
try {
326
361
if (socket != null && !socket .isClosed ())
@@ -330,6 +365,8 @@ private void connect() {
330
365
}
331
366
}));
332
367
368
+ // game loop (for receiving packets)
369
+
333
370
while (running ) {
334
371
try {
335
372
net .readData ();
@@ -350,20 +387,14 @@ private void connect() {
350
387
e .printStackTrace ();
351
388
}
352
389
353
- if (getClientModule () != null )
354
- getClientModule ().disable ();
355
- if (getFishingModule () != null )
356
- getFishingModule ().disable ();
357
- if (getChatProxyModule () != null )
358
- getChatProxyModule ().disable ();
359
- if (getEjectModule () != null )
360
- getEjectModule ().disable ();
361
390
if (getPlayer () != null )
362
391
getEventManager ().unregisterListener (getPlayer ());
363
392
getEventManager ().getRegisteredListener ().clear ();
364
393
getEventManager ().getClassToInstanceMapping ().clear ();
394
+ if (getFishingModule () != null )
395
+ savedLootHistory = getFishingModule ().getLootHistory ();
396
+ getModuleManager ().disableAll ();
365
397
this .socket = null ;
366
- this .fishingModule = null ;
367
398
this .net = null ;
368
399
this .player = null ;
369
400
}
0 commit comments