Skip to content

Commit

Permalink
Fixed backup system, boss (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: SlashRemix <[email protected]>
  • Loading branch information
sh0inx and MertUnverdi authored Oct 27, 2023
1 parent ac1b889 commit f4aad2e
Showing 1 changed file with 20 additions and 10 deletions.
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);

This comment has been minimized.

Copy link
@MertUnverdi

MertUnverdi Oct 27, 2023

Author Member

U have to use child param of new File constructor i hate separators lol
new File(pluginFolder.getPath(), 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) {

This comment has been minimized.

Copy link
@MertUnverdi

MertUnverdi Oct 27, 2023

Author Member

which ide is added this blanks?

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;
}

}

}

0 comments on commit f4aad2e

Please sign in to comment.