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

Added backupConfigs #743

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Changes from all 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
32 changes: 29 additions & 3 deletions src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPluginLoader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -146,6 +144,7 @@ public void registerListeners() {

@Override
public void loadConfigs() {
backupConfigs("backup");
this.configuration = getPersist().load(Configuration.class);
this.messages = getPersist().load(Messages.class);
this.commands = getPersist().load(Commands.class);
Expand Down Expand Up @@ -275,6 +274,33 @@ private void saveFile(File parent, String name) {
}
}

private void backupConfigs(String backupFolderName) {

getLogger().info("Attempting to create backup of configuration files...");

File pluginFolder = new File(getDataFolder().getPath());
File backupFolder = new File(pluginFolder.getPath() + File.separator + backupFolderName);
if (!backupFolder.exists()) backupFolder.mkdir();

File[] configFiles = pluginFolder.listFiles((dir, name) -> name.endsWith(".yml") || name.endsWith(".db"));

if(configFiles == null) {
getLogger().info("No files found.");
return;
}

for (File configFile : configFiles) {
File backupFile = new File(backupFolder, configFile.getName());

try {
Files.copy(configFile.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException exception) {
getLogger().warning("Could not copy " + configFile.getName() + " to " + backupFile.getAbsolutePath());
}
}
getLogger().info("Backup successful, check " + backupFolder.getPath() + ".");
}

@Override
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
return this.chunkGenerator;
Expand Down
Loading