1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Fixed an exception when disabled in an invalid environment.

This commit is contained in:
TheBusyBiscuit 2021-02-06 11:18:09 +01:00
parent dec868284d
commit 47743b632e
3 changed files with 76 additions and 23 deletions

View File

@ -33,6 +33,7 @@
#### Fixes
* Fixed #2794
* Fixed #2793
* Fixed a small exception which gets thrown when Slimefun is disabled due to an invalid environment
## Release Candidate 20 (30 Jan 2021)

View File

@ -29,38 +29,51 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
*/
public class BackupService implements Runnable {
/**
* The maximum amount of backups to maintain
*/
private static final int MAX_BACKUPS = 20;
/**
* Our {@link DateTimeFormatter} for formatting file names.
*/
private final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm", Locale.ROOT);
/**
* The directory in which to create the backups
*/
private final File directory = new File("data-storage/Slimefun/block-backups");
@Override
public void run() {
List<File> backups = Arrays.asList(directory.listFiles());
// Make sure that the directory exists.
if (directory.exists()) {
List<File> backups = Arrays.asList(directory.listFiles());
if (backups.size() > MAX_BACKUPS) {
try {
purgeBackups(backups);
} catch (IOException e) {
SlimefunPlugin.logger().log(Level.WARNING, "Could not delete an old backup", e);
}
}
File file = new File(directory, format.format(LocalDateTime.now()) + ".zip");
if (!file.exists()) {
try {
if (file.createNewFile()) {
try (ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file))) {
createBackup(output);
}
SlimefunPlugin.logger().log(Level.INFO, "Backed up Slimefun data to: {0}", file.getName());
} else {
SlimefunPlugin.logger().log(Level.WARNING, "Could not create backup-file: {0}", file.getName());
if (backups.size() > MAX_BACKUPS) {
try {
purgeBackups(backups);
} catch (IOException e) {
SlimefunPlugin.logger().log(Level.WARNING, "Could not delete an old backup", e);
}
}
File file = new File(directory, format.format(LocalDateTime.now()) + ".zip");
if (!file.exists()) {
try {
if (file.createNewFile()) {
try (ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file))) {
createBackup(output);
}
SlimefunPlugin.logger().log(Level.INFO, "Backed up Slimefun data to: {0}", file.getName());
} else {
SlimefunPlugin.logger().log(Level.WARNING, "Could not create backup-file: {0}", file.getName());
}
} catch (IOException x) {
SlimefunPlugin.logger().log(Level.SEVERE, x, () -> "An Exception occurred while creating a backup for Slimefun " + SlimefunPlugin.getVersion());
}
} catch (IOException x) {
SlimefunPlugin.logger().log(Level.SEVERE, x, () -> "An Error occurred while creating a backup for Slimefun " + SlimefunPlugin.getVersion());
}
}
}

View File

@ -1,6 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.services;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@ -10,6 +11,9 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Server;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
@ -34,9 +38,19 @@ import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunG
*/
public class MinecraftRecipeService {
/**
* Our {@link Plugin} instance
*/
private final Plugin plugin;
/**
* The subscriber list for the {@link RecipeSnapshot}.
*/
private final List<Consumer<RecipeSnapshot>> subscriptions = new LinkedList<>();
/**
* Our {@link RecipeSnapshot} - The centerpiece of this class.
*/
private RecipeSnapshot snapshot;
/**
@ -115,6 +129,7 @@ public class MinecraftRecipeService {
*
* @param recipe
* The {@link Recipe} to get the shape from
*
* @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe}
*/
@Nonnull
@ -149,6 +164,7 @@ public class MinecraftRecipeService {
*
* @param item
* The {@link ItemStack} for which to get the recipes
*
* @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack}
*/
@Nonnull
@ -160,4 +176,27 @@ public class MinecraftRecipeService {
}
}
/**
* This returns the corresponding {@link Keyed} {@link Recipe} for the given {@link NamespacedKey}.
* If no {@link Recipe} was found, null will be returned.
* This is a significantly faster method over {@link Bukkit#getRecipe(NamespacedKey)} since we
* operate on a cached {@link HashMap}
*
* @param key
* The {@link NamespacedKey}
*
* @return The corresponding {@link Recipe} or null
*/
@Nullable
public Recipe getRecipe(@Nonnull NamespacedKey key) {
Validate.notNull(key, "The NamespacedKey should not be null");
if (snapshot != null) {
// We operate on a cached HashMap which is much faster than Bukkit's method.
return snapshot.getRecipe(key);
} else {
return Bukkit.getRecipe(key);
}
}
}