Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve world regeneration command. #2799

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.onarandombox.MultiverseCore.api;

import com.onarandombox.MultiverseCore.enums.KeepWorld;
import com.onarandombox.MultiverseCore.utils.PurgeWorlds;
import com.onarandombox.MultiverseCore.utils.SimpleWorldPurger;
import org.bukkit.World;
Expand Down Expand Up @@ -108,11 +109,10 @@ boolean addWorld(String name, Environment env, String seedString, WorldType type
* @param name The name of the world to remove
* @param removeFromConfig If true(default), we'll remove the entries from the
* config. If false, they'll stay and the world may come back.
* @param deleteWorldFolder If true the world folder will be completely deleted. If false
* only the contents of the world folder will be deleted
* @param keepContents The files you want to keep. (Paper configuration, World directory, or nothing)
* @return True if success, false if failure.
*/
boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder);
boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents);
TraceLTRC marked this conversation as resolved.
Show resolved Hide resolved

/**
* Unload a world from Multiverse.
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.onarandombox.MultiverseCore.enums;

/**
* Custom enum for method argument in deciding what to keep when deleting world files.
*/
public enum KeepWorld {
TraceLTRC marked this conversation as resolved.
Show resolved Hide resolved
/**
* Keep paper's config file if it exists.
*/
CONFIG,
/**
* Keeps the directory of the world folder, delete all the contents.
*/
FOLDER,
/**
* Delete everything, including the directory.
*/
NONE
}
13 changes: 13 additions & 0 deletions src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public static boolean deleteFolderContents(File file) {
}
}

public static boolean deleteWorldContents(File file) {
try (Stream<Path> files = Files.walk(file.toPath())){
files.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.filter(f -> !f.equals(file) && !f.getName().equals("paper-world.yml"))
.forEach(File::delete);
return true;
} catch (IOException e) {
Logging.warning(e.getMessage());
return false;
}
}

/**
* Helper method to copy the world-folder.
* @param source Source-File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.api.WorldPurger;
import com.onarandombox.MultiverseCore.enums.KeepWorld;
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
Expand Down Expand Up @@ -505,7 +506,7 @@ private boolean doLoad(WorldCreator creator, boolean ignoreExists) {
* {@inheritDoc}
*/
@Override
public boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder) {
public boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents) {
if (this.hasUnloadedWorld(name, false)) {
// Attempt to load if unloaded so we can actually delete the world
if (!this.doLoad(name)) {
Expand Down Expand Up @@ -540,7 +541,17 @@ public boolean deleteWorld(String name, boolean removeFromConfig, boolean delete
try {
File worldFile = world.getWorldFolder();
Logging.finer("deleteWorld(): worldFile: " + worldFile.getAbsolutePath());
if (deleteWorldFolder ? FileUtils.deleteFolder(worldFile) : FileUtils.deleteFolderContents(worldFile)) {
boolean success;

if (keepContents.equals(KeepWorld.FOLDER)) {
success = FileUtils.deleteFolderContents(worldFile);
} else if (keepContents.equals(KeepWorld.CONFIG)) {
success = FileUtils.deleteWorldContents(worldFile);
} else {
success = FileUtils.deleteFolder(worldFile);
}

if (success) {
Logging.info("World '%s' was DELETED.", name);
return true;
} else {
Expand All @@ -564,7 +575,7 @@ public boolean deleteWorld(String name, boolean removeFromConfig, boolean delete
*/
@Override
public boolean deleteWorld(String name, boolean removeFromConfig) {
return this.deleteWorld(name, removeFromConfig, true);
return this.deleteWorld(name, removeFromConfig, KeepWorld.NONE);
}

/**
Expand Down Expand Up @@ -947,7 +958,7 @@ public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, S
}

// Do the regen.
if (!this.deleteWorld(name, false, false)) {
if (!this.deleteWorld(name, false, KeepWorld.CONFIG)) {
Logging.severe("Unable to regen world as world cannot be deleted.");
return false;
}
Expand All @@ -971,6 +982,11 @@ public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, S
}
}

// If using a new seed, you will most definitely want the same spawn the world uses.
if (useNewSeed && !world.getSpawnLocation().equals(world.getCBWorld().getSpawnLocation())) {
world.setSpawnLocation(world.getCBWorld().getSpawnLocation());
}

// Send all players that were in the old world, BACK to it!
SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter();
Location newSpawn = world.getSpawnLocation();
Expand Down