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

Fixed backup system, boss #50

Merged
merged 6 commits into from
Oct 27, 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
30 changes: 20 additions & 10 deletions plugin/src/main/java/com/iridium/iridiumcore/Persist.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -12,7 +13,6 @@
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.util.Locale;

/**
Expand Down Expand Up @@ -185,21 +185,33 @@ public <T> T load(Class<T> clazz, File file) {
return objectMapper.readValue(file, clazz);
} catch (IOException e) {
javaPlugin.getLogger().severe("Failed to parse " + file + ": " + e.getMessage());
javaPlugin.getLogger().severe("Getting a backup for " + file + " into backups folder");
javaPlugin.getLogger().severe("Creating a backup for " + file.getName() + " in \"backups\" folder...");

String backupFolderName = "backups";
File pluginFolder = new File(javaPlugin.getDataFolder().getPath());
File backupFolder = new File(pluginFolder.getPath() + File.separator + backupFolderName);
File backupConfigFile = new File(backupFolder + File.separator + file.getName());

try {
file.renameTo(new File(javaPlugin.getDataFolder(), "broken_" + file.getName() + "_" + LocalDateTime.now() + persistType.getExtension()));
File backupFolder = new File(javaPlugin.getDataFolder().getPath(), "backups");
if (!backupFolder.exists()) backupFolder.mkdirs();
Files.move(file.toPath(), backupFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (!backupFolder.exists()) backupFolder.mkdir();
Files.copy(file.toPath(), backupConfigFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
file.renameTo(new File(javaPlugin.getDataFolder(), file.getName() + "_BROKEN"));

} catch (IOException exception) {
javaPlugin.getLogger().severe(
"Failed to move " + file.getName() + " to "
"Failed to move " + file + " to "

+ javaPlugin.getDataFolder().getName() + File.separator + "backups: "
+ exception.getMessage());
Bukkit.getPluginManager().disablePlugin(javaPlugin);
}

javaPlugin.getLogger().info("Success! Backup \"" + file.getName() + "\" created, check \"" + backupFolder.getPath() + "\".");
load(clazz, file);

}
}

try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
Expand All @@ -217,7 +229,7 @@ public <T> T load(Class<T> clazz, File file) {
* @param <T> The type of class
* @return The loaded class. Might be null
*/
public <T> T load(Class<T> clazz, String content) {
public <T> T load(Class < T > clazz, String content) {
try {
return objectMapper.readValue(content, clazz);
} catch (IOException e) {
Expand Down Expand Up @@ -269,7 +281,5 @@ public String getExtension() {
public JsonFactory getFactory() {
return factory;
}

}

}