Only delete world folder contents on mv regen, fixes #767

This commit is contained in:
Ammar Askar 2012-10-22 21:41:36 +05:00 committed by main()
parent 5a1fe2a9e5
commit 895360ce8b
3 changed files with 45 additions and 5 deletions

View File

@ -90,6 +90,17 @@ public interface MVWorldManager {
*/ */
boolean deleteWorld(String name, boolean removeConfig); boolean deleteWorld(String name, boolean removeConfig);
/**
*
* @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
* @return True if success, false if failure.
*/
boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder);
/** /**
* Unload a world from Multiverse. * Unload a world from Multiverse.
* *

View File

@ -45,6 +45,27 @@ public class FileUtils {
} }
} }
/**
* Used to delete the contents of a folder, without deleting the folder itself
*
* @param file The folder whose contents to delete.
* @return true if the contents were successfully deleted
*/
public static boolean deleteFolderContents(File file) {
if (file.exists()) {
boolean ret = true;
// If the file exists, and it has more than one file in it.
if (file.isDirectory()) {
for (File f : file.listFiles()) {
ret = ret && deleteFolder(f);
}
}
return ret;
} else {
return false;
}
}
private static final int COPY_BLOCK_SIZE = 1024; private static final int COPY_BLOCK_SIZE = 1024;
/** /**

View File

@ -405,7 +405,7 @@ public class WorldManager implements MVWorldManager {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean deleteWorld(String name, boolean removeFromConfig) { public boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder) {
World world = this.plugin.getServer().getWorld(name); World world = this.plugin.getServer().getWorld(name);
if (world == null) { if (world == null) {
// We can only delete loaded worlds // We can only delete loaded worlds
@ -433,15 +433,15 @@ public class WorldManager implements MVWorldManager {
try { try {
File worldFile = world.getWorldFolder(); File worldFile = world.getWorldFolder();
plugin.log(Level.FINER, "deleteWorld(): worldFile: " + worldFile.getAbsolutePath()); plugin.log(Level.FINER, "deleteWorld(): worldFile: " + worldFile.getAbsolutePath());
boolean deletedWorld = FileUtils.deleteFolder(worldFile); if (deleteWorldFolder ? FileUtils.deleteFolder(worldFile) : FileUtils.deleteFolderContents(worldFile)) {
if (deletedWorld) {
this.plugin.log(Level.INFO, "World " + name + " was DELETED."); this.plugin.log(Level.INFO, "World " + name + " was DELETED.");
return true;
} else { } else {
this.plugin.log(Level.SEVERE, "World " + name + " was NOT deleted."); this.plugin.log(Level.SEVERE, "World " + name + " was NOT deleted.");
this.plugin.log(Level.SEVERE, "Are you sure the folder " + name + " exists?"); this.plugin.log(Level.SEVERE, "Are you sure the folder " + name + " exists?");
this.plugin.log(Level.SEVERE, "Please check your file permissions on " + name); this.plugin.log(Level.SEVERE, "Please check your file permissions on " + name);
return false;
} }
return deletedWorld;
} catch (Throwable e) { } catch (Throwable e) {
this.plugin.log(Level.SEVERE, "Hrm, something didn't go as planned. Here's an exception for ya."); this.plugin.log(Level.SEVERE, "Hrm, something didn't go as planned. Here's an exception for ya.");
this.plugin.log(Level.SEVERE, "You can go politely explain your situation in #multiverse on esper.net"); this.plugin.log(Level.SEVERE, "You can go politely explain your situation in #multiverse on esper.net");
@ -452,6 +452,14 @@ public class WorldManager implements MVWorldManager {
} }
} }
/**
* {@inheritDoc}
*/
@Override
public boolean deleteWorld(String name, boolean removeFromConfig) {
return this.deleteWorld(name, removeFromConfig, true);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -791,7 +799,7 @@ public class WorldManager implements MVWorldManager {
world.setSeed(theSeed); world.setSeed(theSeed);
} }
if (this.deleteWorld(name, false)) { if (this.deleteWorld(name, false, false)) {
this.doLoad(name, true); this.doLoad(name, true);
SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter(); SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter();
Location newSpawn = world.getSpawnLocation(); Location newSpawn = world.getSpawnLocation();