19
19
20
20
package net .william278 .huskhomes ;
21
21
22
+ import com .google .common .collect .Maps ;
23
+ import com .google .common .collect .Sets ;
24
+ import lombok .AccessLevel ;
25
+ import lombok .Getter ;
26
+ import lombok .NoArgsConstructor ;
27
+ import lombok .Setter ;
22
28
import net .kyori .adventure .audience .Audience ;
23
- import net .kyori .adventure .platform .AudienceProvider ;
24
29
import net .kyori .adventure .platform .bukkit .BukkitAudiences ;
25
- import net .william278 .annotaml .Annotaml ;
26
30
import net .william278 .desertwell .util .Version ;
27
31
import net .william278 .huskhomes .api .HuskHomesAPI ;
28
32
import net .william278 .huskhomes .command .BukkitCommand ;
46
50
import net .william278 .huskhomes .network .Broker ;
47
51
import net .william278 .huskhomes .network .PluginMessageBroker ;
48
52
import net .william278 .huskhomes .network .RedisBroker ;
49
- import net .william278 .huskhomes .position .Location ;
50
53
import net .william278 .huskhomes .position .World ;
51
54
import net .william278 .huskhomes .random .NormalDistributionEngine ;
52
55
import net .william278 .huskhomes .random .RandomTeleportEngine ;
71
74
import space .arim .morepaperlib .scheduling .GracefulScheduling ;
72
75
73
76
import java .io .File ;
74
- import java .io .IOException ;
75
- import java .lang .reflect .InvocationTargetException ;
77
+ import java .nio .file .Path ;
76
78
import java .util .*;
77
79
import java .util .logging .Level ;
78
80
import java .util .stream .Collectors ;
79
81
82
+ @ Getter
83
+ @ Setter
84
+ @ NoArgsConstructor
80
85
public class BukkitHuskHomes extends JavaPlugin implements HuskHomes , BukkitTask .Supplier , BukkitEventDispatcher ,
81
86
PluginMessageListener , BukkitSafetyResolver {
82
87
@@ -85,7 +90,10 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
85
90
*/
86
91
private static final int METRICS_ID = 8430 ;
87
92
88
- private Set <SavedUser > savedUsers ;
93
+ private final Set <SavedUser > savedUsers = Sets .newHashSet ();
94
+ private final Map <String , List <String >> globalPlayerList = Maps .newConcurrentMap ();
95
+ private final Set <UUID > currentlyOnWarmup = Sets .newConcurrentHashSet ();
96
+
89
97
private Settings settings ;
90
98
private Locales locales ;
91
99
private Database database ;
@@ -97,19 +105,15 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
97
105
private UnsafeBlocks unsafeBlocks ;
98
106
private List <Hook > hooks ;
99
107
private List <Command > commands ;
100
- private Map <String , List <String >> globalPlayerList ;
101
- private Set <UUID > currentlyOnWarmup ;
108
+ @ Getter (AccessLevel .NONE )
102
109
private Server server ;
110
+ @ Getter (AccessLevel .NONE )
103
111
private BukkitAudiences audiences ;
112
+ @ Getter (AccessLevel .NONE )
104
113
private MorePaperLib paperLib ;
105
114
@ Nullable
106
115
private Broker broker ;
107
116
108
- // Default public constructor
109
- public BukkitHuskHomes () {
110
- super ();
111
- }
112
-
113
117
// Super constructor for unit testing
114
118
@ TestOnly
115
119
protected BukkitHuskHomes (@ NotNull JavaPluginLoader loader , @ NotNull PluginDescriptionFile description ,
@@ -122,21 +126,15 @@ public void onEnable() {
122
126
// Create adventure audience
123
127
this .audiences = BukkitAudiences .create (this );
124
128
this .paperLib = new MorePaperLib (this );
125
- this .savedUsers = new HashSet <>();
126
- this .globalPlayerList = new HashMap <>();
127
- this .currentlyOnWarmup = new HashSet <>();
128
129
this .validator = new Validator (this );
129
130
130
131
// Load settings and locales
131
- initialize ("plugin config & locale files" , (plugin ) -> {
132
- if (!loadConfigs ()) {
133
- throw new IllegalStateException ("Failed to load config files. Please check the console for errors" );
134
- }
135
- });
132
+ initialize ("plugin config & locale files" , (plugin ) -> loadConfigs ());
136
133
137
134
// Initialize the database
138
- initialize (getSettings ().getDatabaseType ().getDisplayName () + " database connection" , (plugin ) -> {
139
- this .database = switch (getSettings ().getDatabaseType ()) {
135
+ final Database .Type type = getSettings ().getDatabase ().getType ();
136
+ initialize (type .getDisplayName () + " database connection" , (plugin ) -> {
137
+ this .database = switch (type ) {
140
138
case MYSQL , MARIADB -> new MySqlDatabase (this );
141
139
case SQLITE -> new SqLiteDatabase (this );
142
140
case H2 -> new H2Database (this );
@@ -149,9 +147,10 @@ public void onEnable() {
149
147
this .manager = new Manager (this );
150
148
151
149
// Initialize the network messenger if proxy mode is enabled
152
- if (getSettings ().doCrossServer ()) {
153
- initialize (settings .getBrokerType ().getDisplayName () + " message broker" , (plugin ) -> {
154
- broker = switch (settings .getBrokerType ()) {
150
+ final Settings .CrossServerSettings crossServer = getSettings ().getCrossServer ();
151
+ if (crossServer .isEnabled ()) {
152
+ initialize (crossServer .getBrokerType ().getDisplayName () + " message broker" , (plugin ) -> {
153
+ broker = switch (crossServer .getBrokerType ()) {
155
154
case PLUGIN_MESSAGE -> new PluginMessageBroker (this );
156
155
case REDIS -> new RedisBroker (this );
157
156
};
@@ -205,7 +204,7 @@ public void registerHooks() {
205
204
HuskHomes .super .registerHooks ();
206
205
207
206
// Hooks
208
- if (getSettings ().doEconomy () && isDependencyLoaded ("Vault" )) {
207
+ if (getSettings ().getEconomy (). isEnabled () && isDependencyLoaded ("Vault" )) {
209
208
getHooks ().add (new VaultEconomyHook (this ));
210
209
}
211
210
if (isDependencyLoaded ("PlaceholderAPI" )) {
@@ -267,52 +266,6 @@ public Audience getAudience(@NotNull UUID user) {
267
266
return audiences .player (user );
268
267
}
269
268
270
- @ NotNull
271
- @ Override
272
- public Set <SavedUser > getSavedUsers () {
273
- return savedUsers ;
274
- }
275
-
276
- @ NotNull
277
- @ Override
278
- public Settings getSettings () {
279
- return settings ;
280
- }
281
-
282
- @ Override
283
- public void setSettings (@ NotNull Settings settings ) {
284
- this .settings = settings ;
285
- }
286
-
287
- @ NotNull
288
- @ Override
289
- public Locales getLocales () {
290
- return locales ;
291
- }
292
-
293
- @ Override
294
- public void setLocales (@ NotNull Locales locales ) {
295
- this .locales = locales ;
296
- }
297
-
298
- @ Override
299
- @ NotNull
300
- public Database getDatabase () {
301
- return database ;
302
- }
303
-
304
- @ Override
305
- @ NotNull
306
- public Validator getValidator () {
307
- return validator ;
308
- }
309
-
310
- @ NotNull
311
- @ Override
312
- public Manager getManager () {
313
- return manager ;
314
- }
315
-
316
269
@ NotNull
317
270
@ Override
318
271
public Broker getMessenger () {
@@ -322,17 +275,6 @@ public Broker getMessenger() {
322
275
return broker ;
323
276
}
324
277
325
- @ NotNull
326
- @ Override
327
- public RandomTeleportEngine getRandomTeleportEngine () {
328
- return randomTeleportEngine ;
329
- }
330
-
331
- @ Override
332
- public void setRandomTeleportEngine (@ NotNull RandomTeleportEngine randomTeleportEngine ) {
333
- this .randomTeleportEngine = randomTeleportEngine ;
334
- }
335
-
336
278
@ Override
337
279
public Optional <Spawn > getServerSpawn () {
338
280
return Optional .ofNullable (serverSpawn );
@@ -343,81 +285,27 @@ public void setServerSpawn(@NotNull Spawn spawn) {
343
285
this .serverSpawn = spawn ;
344
286
}
345
287
346
- @ Override
347
- public void setServerSpawn (@ NotNull Location location ) {
348
- try {
349
- // Create or update the spawn.yml file
350
- final File spawnFile = new File (getDataFolder (), "spawn.yml" );
351
- if (spawnFile .exists () && !spawnFile .delete ()) {
352
- log (Level .WARNING , "Failed to delete the existing spawn.yml file" );
353
- }
354
- this .serverSpawn = Annotaml .create (spawnFile , new Spawn (location )).get ();
355
-
356
- // Update the world spawn location, too
357
- BukkitAdapter .adaptLocation (location ).ifPresent (bukkitLocation -> {
358
- assert bukkitLocation .getWorld () != null : "World was null when setting server spawn" ;
359
- bukkitLocation .getWorld ().setSpawnLocation (bukkitLocation );
360
- });
361
- } catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e ) {
362
- log (Level .WARNING , "Failed to save the server spawn.yml file" , e );
363
- }
364
- }
365
-
366
- @ Override
367
- @ NotNull
368
- public List <Hook > getHooks () {
369
- return hooks ;
370
- }
371
-
372
- @ Override
373
- public void setHooks (@ NotNull List <Hook > hooks ) {
374
- this .hooks = hooks ;
375
- }
376
-
377
288
@ Override
378
289
@ NotNull
379
290
public Version getVersion () {
380
291
return Version .fromString (getDescription ().getVersion (), "-" );
381
292
}
382
293
383
- @ Override
384
- @ NotNull
385
- public List <Command > getCommands () {
386
- return commands ;
387
- }
388
-
389
- @ Override
390
- @ NotNull
391
- public Map <String , List <String >> getGlobalPlayerList () {
392
- return globalPlayerList ;
393
- }
394
-
395
- @ Override
396
- @ NotNull
397
- public Set <UUID > getCurrentlyOnWarmup () {
398
- return currentlyOnWarmup ;
399
- }
400
-
401
294
@ Override
402
295
@ NotNull
403
296
public String getServerName () {
404
- return server .getName ();
297
+ return server != null ? server .getName () : "server" ;
405
298
}
406
299
407
300
@ Override
408
- public void setServer (@ NotNull Server server ) {
301
+ public void setServerName (@ NotNull Server server ) {
409
302
this .server = server ;
410
303
}
411
304
412
- @ Override
413
- public void setUnsafeBlocks (@ NotNull UnsafeBlocks unsafeBlocks ) {
414
- this .unsafeBlocks = unsafeBlocks ;
415
- }
416
-
417
305
@ Override
418
306
@ NotNull
419
- public UnsafeBlocks getUnsafeBlocks () {
420
- return unsafeBlocks ;
307
+ public Path getConfigDirectory () {
308
+ return getDataFolder (). toPath () ;
421
309
}
422
310
423
311
@ Override
@@ -438,21 +326,23 @@ public void registerMetrics(int metricsId) {
438
326
try {
439
327
final Metrics metrics = new Metrics (this , metricsId );
440
328
metrics .addCustomChart (new SimplePie ("bungee_mode" ,
441
- () -> Boolean .toString (getSettings ().doCrossServer ())));
329
+ () -> Boolean .toString (getSettings ().getCrossServer ().isEnabled ())
330
+ ));
442
331
443
- if (getSettings ().doCrossServer ()) {
332
+ if (getSettings ().getCrossServer (). isEnabled ()) {
444
333
metrics .addCustomChart (new SimplePie ("messenger_type" ,
445
- () -> getSettings ().getBrokerType ().getDisplayName ()));
334
+ () -> getSettings ().getCrossServer ().getBrokerType ().getDisplayName ())
335
+ );
446
336
}
447
337
448
338
metrics .addCustomChart (new SimplePie ("language" ,
449
339
() -> getSettings ().getLanguage ().toLowerCase ()));
450
340
metrics .addCustomChart (new SimplePie ("database_type" ,
451
- () -> getSettings ().getDatabaseType ().getDisplayName ()));
341
+ () -> getSettings ().getDatabase (). getType ().getDisplayName ()));
452
342
metrics .addCustomChart (new SimplePie ("using_economy" ,
453
- () -> Boolean .toString (getSettings ().doEconomy ())));
343
+ () -> Boolean .toString (getSettings ().getEconomy (). isEnabled ())));
454
344
metrics .addCustomChart (new SimplePie ("using_map" ,
455
- () -> Boolean .toString (getSettings ().doMapHook ())));
345
+ () -> Boolean .toString (getSettings ().getMapHook (). isEnabled ())));
456
346
457
347
getMapHook ().ifPresent (hook -> metrics .addCustomChart (new SimplePie ("map_type" , hook ::getName )));
458
348
} catch (Throwable e ) {
@@ -479,7 +369,7 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
479
369
@ Override
480
370
public void onPluginMessageReceived (@ NotNull String channel , @ NotNull Player player , byte [] message ) {
481
371
if (broker != null && broker instanceof PluginMessageBroker pluginMessenger
482
- && getSettings ().getBrokerType () == Broker .Type .PLUGIN_MESSAGE ) {
372
+ && getSettings ().getCrossServer (). getBrokerType () == Broker .Type .PLUGIN_MESSAGE ) {
483
373
pluginMessenger .onReceive (channel , BukkitUser .adapt (player , this ), message );
484
374
}
485
375
}
0 commit comments