Skip to content

Commit 385a7da

Browse files
authored
Merge pull request #2682 from BentoBoxWorld/2678_make_seed_worlds_temp
Delete temporary seed worlds on shutdown #2678
2 parents a1ab87a + 1f0b891 commit 385a7da

File tree

3 files changed

+74
-69
lines changed

3 files changed

+74
-69
lines changed

src/main/java/world/bentobox/bentobox/BentoBox.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import world.bentobox.bentobox.listeners.JoinLeaveListener;
4242
import world.bentobox.bentobox.listeners.PanelListenerManager;
4343
import world.bentobox.bentobox.listeners.PrimaryIslandListener;
44-
import world.bentobox.bentobox.listeners.SeedWorldMakerListener;
4544
import world.bentobox.bentobox.listeners.StandardSpawnProtectionListener;
4645
import world.bentobox.bentobox.listeners.teleports.EntityTeleportListener;
4746
import world.bentobox.bentobox.listeners.teleports.PlayerTeleportListener;
@@ -337,8 +336,6 @@ private void registerListeners() {
337336
manager.registerEvents(islandDeletionManager, this);
338337
// Primary Island Listener
339338
manager.registerEvents(new PrimaryIslandListener(this), this);
340-
// Seed world chunk generator
341-
manager.registerEvents(new SeedWorldMakerListener(this), this);
342339
}
343340

344341
@Override
@@ -359,6 +356,7 @@ public void onDisable() {
359356
islandsManager.shutdown();
360357
}
361358

359+
362360
}
363361

364362
@EventHandler

src/main/java/world/bentobox/bentobox/listeners/SeedWorldMakerListener.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main/java/world/bentobox/bentobox/managers/AddonsManager.java

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@
3232
import org.bukkit.configuration.ConfigurationSection;
3333
import org.bukkit.configuration.InvalidConfigurationException;
3434
import org.bukkit.configuration.file.YamlConfiguration;
35+
import org.bukkit.entity.Player;
3536
import org.bukkit.event.HandlerList;
3637
import org.bukkit.event.Listener;
3738
import org.bukkit.generator.ChunkGenerator;
3839
import org.bukkit.permissions.PermissionDefault;
3940
import org.bukkit.plugin.InvalidDescriptionException;
4041
import org.bukkit.plugin.Plugin;
41-
import org.bukkit.plugin.PluginLoader;
4242
import org.bukkit.plugin.java.JavaPlugin;
4343
import org.bukkit.util.permissions.DefaultPermissions;
4444
import org.eclipse.jdt.annotation.NonNull;
4545
import org.eclipse.jdt.annotation.Nullable;
46+
import org.jetbrains.annotations.NotNull;
4647

4748
import world.bentobox.bentobox.BentoBox;
4849
import world.bentobox.bentobox.api.addons.Addon;
@@ -68,6 +69,8 @@ public class AddonsManager {
6869

6970
private static final String GAMEMODE = "[gamemode].";
7071

72+
private static final @NotNull String BENTOBOX = "/bentobox";
73+
7174
@NonNull
7275
private final List<Addon> addons;
7376
@NonNull
@@ -82,8 +85,6 @@ public class AddonsManager {
8285
@NonNull
8386
private final Map<@NonNull Addon, @NonNull List<Listener>> listeners;
8487

85-
private final PluginLoader pluginLoader;
86-
8788
public AddonsManager(@NonNull BentoBox plugin) {
8889
this.plugin = plugin;
8990
addons = new ArrayList<>();
@@ -92,7 +93,6 @@ public AddonsManager(@NonNull BentoBox plugin) {
9293
classes = new HashMap<>();
9394
listeners = new HashMap<>();
9495
worldNames = new HashMap<>();
95-
pluginLoader = plugin.getPluginLoader();
9696
}
9797

9898
/**
@@ -380,9 +380,68 @@ private void createSeedWorlds(GameModeAddon gameMode) {
380380
}
381381
}
382382

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+
383442
private void seedWorld(GameModeAddon gameMode, @NonNull World world) {
384443
// 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)
386445
.environment(world.getEnvironment()).seed(world.getSeed());
387446
World w = gameMode.getWorldSettings().isUseOwnGenerator() ? wc.createWorld()
388447
: wc.generator(world.getGenerator()).createWorld();
@@ -410,8 +469,11 @@ private void handleAddonIncompatibility(@NonNull Addon addon, LinkageError e) {
410469
plugin.logStacktrace(e);
411470
}
412471

472+
413473
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);
415477
}
416478

417479
/**
@@ -481,6 +543,8 @@ public void disableAddons() {
481543
}
482544
// Unregister all commands
483545
plugin.getCommandsManager().unregisterCommands();
546+
// Delete seed worlds
547+
removeSeedWorlds();
484548
// Clear all maps
485549
listeners.clear();
486550
pladdons.clear();
@@ -657,7 +721,7 @@ private void sortAddons() {
657721
@Nullable
658722
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
659723
// 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, "")
661725
.toLowerCase(Locale.ENGLISH);
662726
if (worldNames.containsKey(w) && worldNames.get(w) != null) {
663727
return worldNames.get(w).getDefaultWorldGenerator(worldName, id);
@@ -710,10 +774,12 @@ private void disable(@NonNull Addon addon) {
710774
loaders.remove(addon);
711775
}
712776
// Disable pladdons
777+
/*
713778
if (pladdons.containsKey(addon)) {
714779
this.pluginLoader.disablePlugin(Objects.requireNonNull(this.pladdons.get(addon)));
715780
pladdons.remove(addon);
716781
}
782+
*/
717783
// Remove it from the addons list
718784
addons.remove(addon);
719785
}

0 commit comments

Comments
 (0)