Skip to content

Commit

Permalink
add save and restore recipe backups
Browse files Browse the repository at this point in the history
  • Loading branch information
SchnTgaiSpock committed Nov 10, 2024
1 parent 7b632d5 commit 77e4c22
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ public void onExecute(CommandSender sender, String[] args) {
}
Slimefun.getRecipeService().saveAllRecipes();
break;
case "backup":
if (args.length != 2) {
Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf recipe backup"));
break;
}
Slimefun.getRecipeService().saveAllRecipes();
Slimefun.getRecipeService().backUpRecipeFiles();
break;
case "restore_backup":
if (args.length != 2) {
Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf recipe restore_backup"));
break;
}
Slimefun.getRecipeService().restoreBackupRecipeFiles();
Slimefun.getRecipeService().loadAllRecipes();
break;
case "clear":
if (args.length != 2) {
Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf recipe clear"));
break;
}
Slimefun.getRecipeService().clear();
break;
case "delete":
if (args.length != 2) {
Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf recipe delete"));
break;
}
Slimefun.getRecipeService().deleteRecipeFiles();
break;
default:
Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf recipe <subcommand>"));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;

Expand Down Expand Up @@ -383,12 +382,8 @@ public Recipe parseRecipeString(String s) {
return gson.fromJson(s, Recipe.class);
}

/**
* @return The list of all recipe files in <code>/plugins/Slimefun/recipes/[subdirectory]</code>
* with the .json removed
*/
public Set<String> getAllRecipeFilenames(String subdirectory) {
Path dir = Path.of(SAVED_RECIPE_DIR, subdirectory);
private Set<String> getAllRecipeFilenames(String directory, String subdirectory) {
Path dir = Path.of(directory, subdirectory);
if (!dir.toFile().exists()) {
return Collections.emptySet();
}
Expand All @@ -405,6 +400,14 @@ public Set<String> getAllRecipeFilenames(String subdirectory) {
}
}

/**
* @return The list of all recipe files in <code>/plugins/Slimefun/recipes/[subdirectory]</code>
* with the .json removed
*/
public Set<String> getAllRecipeFilenames(String subdirectory) {
return getAllRecipeFilenames(SAVED_RECIPE_DIR, subdirectory);
}

/**
* @return The list of all recipe files in <code>/plugins/Slimefun/recipes</code>
* directory, with the .json removed
Expand Down Expand Up @@ -487,36 +490,59 @@ public void saveAllRecipes() {
} else {
gson.toJson(recipes, List.class, jsonWriter);
}
} catch (IOException e) {
plugin.getLogger().warning("Couldn't save recipe to '" + filename + "': " + e.getLocalizedMessage());
} catch (JsonIOException e) {
} catch (Exception e) {
plugin.getLogger().warning("Couldn't save recipe to '" + filename + "': " + e.getLocalizedMessage());
}
}
}

public void backUpRecipeFiles() {
// Delete old backups
try (Stream<Path> backups = Files.list(Path.of(BACKUP_RECIPE_DIR))) {
backups.forEach(p -> p.toFile().delete());

// Back up recipe files
getAllRecipeFilenames().forEach(source -> {
Path destination = Paths.get(BACKUP_RECIPE_DIR, source + ".json");
System.out.println(source);
System.out.println(destination);
private void copyRecipeFiles(String sourceDir, String targetDir, boolean clean) {
try (Stream<Path> target = Files.list(Path.of(targetDir))) {
Set<String> filenames = getAllRecipeFilenames(sourceDir, "");
target.forEach(p -> {
if (clean || filenames.contains(Path.of(targetDir).relativize(p).toString())) {
p.toFile().delete();
}
});

getAllRecipeFilenames(sourceDir, "").forEach(source -> {
Path destination = Paths.get(targetDir, source + ".json");
Path parent = destination.getParent();
if (parent != null && !parent.toFile().exists()) {
parent.toFile().mkdirs();
}
try {
Files.copy(Path.of(SAVED_RECIPE_DIR, source + ".json"), destination);
Files.copy(Path.of(sourceDir, source + ".json"), destination);
} catch (IOException e) {
plugin.getLogger().warning("Couldn't backup recipe '" + source + "'");
plugin.getLogger().warning("Couldn't copy recipe from '" + source + "' to '" + targetDir + "'");
}
});
} catch (Exception e) {
plugin.getLogger().warning("Couldn't clear old backups");
plugin.getLogger().warning("Couldn't copy recipes from '" + sourceDir + "' to '" + targetDir + "'");
}
}

public void backUpRecipeFiles() {
copyRecipeFiles(SAVED_RECIPE_DIR, BACKUP_RECIPE_DIR, true);
}

public void restoreBackupRecipeFiles() {
copyRecipeFiles(BACKUP_RECIPE_DIR, SAVED_RECIPE_DIR, false);
}

public void clear() {
recipesByFilename.clear();
recipesById.clear();
recipesByType.clear();
filesRead.clear();
recipeCache.clear();
}

public void deleteRecipeFiles() {
try (Stream<Path> target = Files.list(Path.of(SAVED_RECIPE_DIR))) {
target.forEach(p -> p.toFile().delete());
} catch (Exception e) {
plugin.getLogger().warning("Couldn't delete recipe files");
}
}

Expand Down

0 comments on commit 77e4c22

Please sign in to comment.