Skip to content

Instantly share code, notes, and snippets.

@Fabricio20
Last active February 16, 2021 16:35
Show Gist options
  • Save Fabricio20/b965c79377f87ef9cdf3 to your computer and use it in GitHub Desktop.
Save Fabricio20/b965c79377f87ef9cdf3 to your computer and use it in GitHub Desktop.
Resetting Worlds Spigot - Fabricio20
// This Reset one is hand-made and may not work, but these are the methods and the sequence you need to follow.
public void reset(World world) { // Please modify to your needs
Bukkit.getServer().unloadWorld(world, false); // False = Not Save
Bukkit.getServer().runTaskLater(Main.getInstance(), new Runnable(){ // Run It Later to make sure its unloaded.
@Override
public void run() {
final File srcWorldFolder = new File("plugins/YourPluginNameCaseSensitive/" + world.getName() + "/" + world.getName()); // Backup world folder location
final File worldFolder = new File(world.getName()); // World folder name
deleteFolder(worldFolder); // Delete old folder
copyWorldFolder(srcWorldFolder, worldFolder); // Copy backup folder
final WorldCreator w = new WorldCreator(world.getName()); // This starts the world load
}
}, 60);
}
public void saveWorld(World world) {
if(world != null) {
File worldFolder = new File("plugins/YourPluginNameCaseSensitive/" + world.getName() +"/" + world.getName()); // I Save in /plugins/minigame/WorldName/WorldName ( the second world name is the actual folder )
File srcWorldFolder = new File(world.getName());
if(worldFolder.exists()) {
deleteFolder(worldFolder);
}
copyWorldFolder(srcWorldFolder, worldFolder);
}
}
private void deleteFolder(File folder) {
File[] files = folder.listFiles();
if(files != null) {
for(File file : files) {
if(file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
}
}
folder.delete();
}
private void copyWorldFolder(File from, File to) {
try {
ArrayList<String> ignore = new ArrayList<String>(Arrays.asList(new String[] { "uid.dat", "session.dat" }));
if(!ignore.contains(from.getName())) {
if(from.isDirectory()) {
if(!to.exists()) {
to.mkdirs();
}
String[] files = from.list();
for(String file : files) {
File srcFile = new File(from, file);
File destFile = new File(to, file);
copyWorldFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment