32
32
import org .bukkit .configuration .ConfigurationSection ;
33
33
import org .bukkit .configuration .InvalidConfigurationException ;
34
34
import org .bukkit .configuration .file .YamlConfiguration ;
35
+ import org .bukkit .entity .Player ;
35
36
import org .bukkit .event .HandlerList ;
36
37
import org .bukkit .event .Listener ;
37
38
import org .bukkit .generator .ChunkGenerator ;
38
39
import org .bukkit .permissions .PermissionDefault ;
39
40
import org .bukkit .plugin .InvalidDescriptionException ;
40
41
import org .bukkit .plugin .Plugin ;
41
- import org .bukkit .plugin .PluginLoader ;
42
42
import org .bukkit .plugin .java .JavaPlugin ;
43
43
import org .bukkit .util .permissions .DefaultPermissions ;
44
44
import org .eclipse .jdt .annotation .NonNull ;
45
45
import org .eclipse .jdt .annotation .Nullable ;
46
+ import org .jetbrains .annotations .NotNull ;
46
47
47
48
import world .bentobox .bentobox .BentoBox ;
48
49
import world .bentobox .bentobox .api .addons .Addon ;
@@ -68,6 +69,8 @@ public class AddonsManager {
68
69
69
70
private static final String GAMEMODE = "[gamemode]." ;
70
71
72
+ private static final @ NotNull String BENTOBOX = "/bentobox" ;
73
+
71
74
@ NonNull
72
75
private final List <Addon > addons ;
73
76
@ NonNull
@@ -82,8 +85,6 @@ public class AddonsManager {
82
85
@ NonNull
83
86
private final Map <@ NonNull Addon , @ NonNull List <Listener >> listeners ;
84
87
85
- private final PluginLoader pluginLoader ;
86
-
87
88
public AddonsManager (@ NonNull BentoBox plugin ) {
88
89
this .plugin = plugin ;
89
90
addons = new ArrayList <>();
@@ -92,7 +93,6 @@ public AddonsManager(@NonNull BentoBox plugin) {
92
93
classes = new HashMap <>();
93
94
listeners = new HashMap <>();
94
95
worldNames = new HashMap <>();
95
- pluginLoader = plugin .getPluginLoader ();
96
96
}
97
97
98
98
/**
@@ -380,9 +380,68 @@ private void createSeedWorlds(GameModeAddon gameMode) {
380
380
}
381
381
}
382
382
383
+ /**
384
+ * Removes the temporary seed worlds on shutdown
385
+ */
386
+ public void removeSeedWorlds () {
387
+ plugin .log ("Removing temporary seed worlds..." );
388
+ this .getGameModeAddons ().stream ().filter (gm -> gm .isUsesNewChunkGeneration ()).forEach (gameMode -> {
389
+ plugin .log ("Removing " + gameMode .getDescription ().getName ());
390
+ if (gameMode .getOverWorld () != null ) {
391
+ plugin .log ("Removing " + gameMode .getOverWorld ().getName () + BENTOBOX );
392
+ removeSeedWorld (gameMode .getOverWorld ().getName () + BENTOBOX );
393
+ }
394
+ if (gameMode .getNetherWorld () != null ) {
395
+ removeSeedWorld (gameMode .getNetherWorld ().getName () + BENTOBOX );
396
+ }
397
+ if (gameMode .getEndWorld () != null ) {
398
+ removeSeedWorld (gameMode .getEndWorld ().getName () + BENTOBOX );
399
+ }
400
+ });
401
+ plugin .log ("Removed temporary seed worlds." );
402
+ }
403
+
404
+ private void removeSeedWorld (String name ) {
405
+ World world = Bukkit .getWorld (name );
406
+ if (world == null || !world .getName ().endsWith (BENTOBOX )) {
407
+ return ;
408
+ }
409
+ // Teleport any players out of the world, just in case
410
+ for (Player player : world .getPlayers ()) {
411
+ player .teleport (Bukkit .getWorlds ().get (0 ).getSpawnLocation ());
412
+ }
413
+
414
+ // Unload the world
415
+ boolean success = Bukkit .unloadWorld (world , false ); // false = do not save
416
+ if (!success ) {
417
+ plugin .logWarning ("Failed to unload seed world: " + world .getName ());
418
+ }
419
+ // Delete the world folder and everything in it
420
+ File path = new File (Bukkit .getWorldContainer (), world .getName ());
421
+ this .deleteWorldFolder (path );
422
+ }
423
+
424
+ /**
425
+ * Recursive delete
426
+ * @param path path to delete
427
+ */
428
+ private void deleteWorldFolder (File path ) {
429
+ if (path .exists ()) {
430
+ File [] files = path .listFiles ();
431
+ if (files != null ) {
432
+ for (File file : files ) {
433
+ deleteWorldFolder (file );
434
+ }
435
+ }
436
+ if (!path .delete ()) {
437
+ plugin .logWarning ("Failed to delete: " + path .getAbsolutePath ());
438
+ }
439
+ }
440
+ }
441
+
383
442
private void seedWorld (GameModeAddon gameMode , @ NonNull World world ) {
384
443
// Use the Flat type of world because this is a copy and no vanilla creation is required
385
- WorldCreator wc = WorldCreator .name (world .getName () + "/bentobox" ).type (WorldType .FLAT )
444
+ WorldCreator wc = WorldCreator .name (world .getName () + BENTOBOX ).type (WorldType .FLAT )
386
445
.environment (world .getEnvironment ()).seed (world .getSeed ());
387
446
World w = gameMode .getWorldSettings ().isUseOwnGenerator () ? wc .createWorld ()
388
447
: wc .generator (world .getGenerator ()).createWorld ();
@@ -410,8 +469,11 @@ private void handleAddonIncompatibility(@NonNull Addon addon, LinkageError e) {
410
469
plugin .logStacktrace (e );
411
470
}
412
471
472
+
413
473
private boolean isAddonCompatibleWithBentoBox (@ NonNull Addon addon ) {
414
- return isAddonCompatibleWithBentoBox (addon , plugin .getDescription ().getVersion ());
474
+ @ SuppressWarnings ("deprecation" )
475
+ String v = plugin .getDescription ().getVersion ();
476
+ return isAddonCompatibleWithBentoBox (addon , v );
415
477
}
416
478
417
479
/**
@@ -481,6 +543,8 @@ public void disableAddons() {
481
543
}
482
544
// Unregister all commands
483
545
plugin .getCommandsManager ().unregisterCommands ();
546
+ // Delete seed worlds
547
+ removeSeedWorlds ();
484
548
// Clear all maps
485
549
listeners .clear ();
486
550
pladdons .clear ();
@@ -657,7 +721,7 @@ private void sortAddons() {
657
721
@ Nullable
658
722
public ChunkGenerator getDefaultWorldGenerator (String worldName , String id ) {
659
723
// Clean up world name
660
- String w = worldName .replace ("_nether" , "" ).replace ("_the_end" , "" ).replace ("/bentobox" , "" )
724
+ String w = worldName .replace ("_nether" , "" ).replace ("_the_end" , "" ).replace (BENTOBOX , "" )
661
725
.toLowerCase (Locale .ENGLISH );
662
726
if (worldNames .containsKey (w ) && worldNames .get (w ) != null ) {
663
727
return worldNames .get (w ).getDefaultWorldGenerator (worldName , id );
@@ -710,10 +774,12 @@ private void disable(@NonNull Addon addon) {
710
774
loaders .remove (addon );
711
775
}
712
776
// Disable pladdons
777
+ /*
713
778
if (pladdons.containsKey(addon)) {
714
779
this.pluginLoader.disablePlugin(Objects.requireNonNull(this.pladdons.get(addon)));
715
780
pladdons.remove(addon);
716
781
}
782
+ */
717
783
// Remove it from the addons list
718
784
addons .remove (addon );
719
785
}
0 commit comments