aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorKevin <92656833+kevinthegreat1@users.noreply.github.com>2024-04-22 00:30:35 -0400
committerGitHub <noreply@github.com>2024-04-22 00:30:35 -0400
commitabf64d39fd9bf39da3fb1373efc606942ff62400 (patch)
tree716f63eb18cda2d28c1896cae245555a9911f086 /src/main/java
parent9e1fb6e741bd9938365a8751cce274af54f2d463 (diff)
downloadSkyblocker-abf64d39fd9bf39da3fb1373efc606942ff62400.tar.gz
Skyblocker-abf64d39fd9bf39da3fb1373efc606942ff62400.tar.bz2
Skyblocker-abf64d39fd9bf39da3fb1373efc606942ff62400.zip
Fix update repository (#663)
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/FileUtils.java34
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/NEURepoManager.java48
2 files changed, 51 insertions, 31 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/FileUtils.java b/src/main/java/de/hysky/skyblocker/utils/FileUtils.java
index 9bec2745..59b706c4 100644
--- a/src/main/java/de/hysky/skyblocker/utils/FileUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/FileUtils.java
@@ -1,29 +1,35 @@
package de.hysky.skyblocker.utils;
+import com.mojang.logging.LogUtils;
+import org.slf4j.Logger;
+
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-
-import org.slf4j.Logger;
-
-import com.mojang.logging.LogUtils;
+import java.util.stream.Stream;
public class FileUtils {
private static final Logger LOGGER = LogUtils.getLogger();
public static void recursiveDelete(Path dir) throws IOException {
+ if (!Files.exists(dir)) {
+ return;
+ }
+
if (Files.isDirectory(dir) && !Files.isSymbolicLink(dir)) {
- Files.list(dir).forEach(child -> {
- try {
- recursiveDelete(child);
- } catch (Exception e) {
- LOGGER.error("[Skyblocker] Encountered an exception while deleting a file! Path: {}", child.toAbsolutePath(), e);
- }
- });
+ try (Stream<Path> stream = Files.list(dir)) {
+ stream.forEach(child -> {
+ try {
+ recursiveDelete(child);
+ } catch (Exception e) {
+ LOGGER.error("[Skyblocker] Encountered an exception while deleting a file. Path: {}", child.toAbsolutePath(), e);
+ }
+ });
+ }
}
- if (!Files.isWritable(dir)) {
- dir.toFile().setWritable(true);
+ if (!Files.isWritable(dir) && !dir.toFile().setWritable(true)) {
+ LOGGER.error("[Skyblocker] Failed to make file writable. Path: {}", dir.toAbsolutePath());
}
Files.delete(dir);
@@ -31,7 +37,7 @@ public class FileUtils {
/**
* Replaces any characters that do not match the regex: [^a-z0-9_.-]
- *
+ *
* @implNote Designed to convert a file path to an {@link net.minecraft.util.Identifier}
*/
public static String normalizePath(Path path) {
diff --git a/src/main/java/de/hysky/skyblocker/utils/NEURepoManager.java b/src/main/java/de/hysky/skyblocker/utils/NEURepoManager.java
index c779d666..caa5c48c 100644
--- a/src/main/java/de/hysky/skyblocker/utils/NEURepoManager.java
+++ b/src/main/java/de/hysky/skyblocker/utils/NEURepoManager.java
@@ -1,12 +1,16 @@
package de.hysky.skyblocker.utils;
+import com.mojang.brigadier.Command;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
+import de.hysky.skyblocker.utils.scheduler.Scheduler;
import io.github.moulberry.repo.NEURepository;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.client.MinecraftClient;
+import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
+import org.apache.commons.lang3.function.Consumers;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
@@ -28,7 +32,7 @@ public class NEURepoManager {
* Use {@link #NEU_REPO}.
*/
private static final Path LOCAL_REPO_DIR = SkyblockerMod.CONFIG_DIR.resolve("item-repo"); // TODO rename to NotEnoughUpdates-REPO
- private static final CompletableFuture<Void> REPO_INITIALIZED = loadRepository();
+ private static CompletableFuture<Void> REPO_LOADING = loadRepository().thenAccept(Consumers.nop());
public static final NEURepository NEU_REPO = NEURepository.of(LOCAL_REPO_DIR);
/**
@@ -39,14 +43,16 @@ public class NEURepoManager {
public static void init() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE)
- .then(ClientCommandManager.literal("updaterepository").executes(context -> {
- deleteAndDownloadRepository();
- return 1;
- }))));
+ .then(ClientCommandManager.literal("updateRepository").executes(context -> {
+ deleteAndDownloadRepository(context.getSource().getPlayer());
+ return Command.SINGLE_SUCCESS;
+ }))
+ )
+ );
}
- private static CompletableFuture<Void> loadRepository() {
- return CompletableFuture.runAsync(() -> {
+ private static CompletableFuture<Boolean> loadRepository() {
+ return CompletableFuture.supplyAsync(() -> {
try {
if (Files.isDirectory(NEURepoManager.LOCAL_REPO_DIR)) {
try (Git localRepo = Git.open(NEURepoManager.LOCAL_REPO_DIR.toFile())) {
@@ -58,28 +64,36 @@ public class NEURepoManager {
LOGGER.info("[Skyblocker] NEU Repository Downloaded");
}
NEU_REPO.reload();
- } catch (TransportException e){
+ return true;
+ } catch (TransportException e) {
LOGGER.error("[Skyblocker] Transport operation failed. Most likely unable to connect to the remote NEU repo on github", e);
} catch (RepositoryNotFoundException e) {
LOGGER.warn("[Skyblocker] Local NEU Repository not found or corrupted, downloading new one", e);
- deleteAndDownloadRepository();
+ Scheduler.INSTANCE.schedule(() -> deleteAndDownloadRepository(MinecraftClient.getInstance().player), 1);
} catch (Exception e) {
LOGGER.error("[Skyblocker] Encountered unknown exception while initializing NEU Repository", e);
}
+ return false;
});
}
- private static void deleteAndDownloadRepository() {
- CompletableFuture.runAsync(() -> {
+ private static void deleteAndDownloadRepository(PlayerEntity player) {
+ if (REPO_LOADING != null && !REPO_LOADING.isDone()) {
+ player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.updateRepository.loading")), false);
+ return;
+ }
+ player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.updateRepository.start")), false);
+
+ REPO_LOADING = CompletableFuture.runAsync(() -> {
try {
ItemRepository.setFilesImported(false);
FileUtils.recursiveDelete(NEURepoManager.LOCAL_REPO_DIR);
- } catch (Exception ex) {
- if (MinecraftClient.getInstance().player != null)
- MinecraftClient.getInstance().player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.updaterepository.failed")), false);
- return;
+ player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.updateRepository.deleted")), false);
+ player.sendMessage(Constants.PREFIX.get().append(Text.translatable(loadRepository().join() ? "skyblocker.updateRepository.success" : "skyblocker.updateRepository.failed")), false);
+ } catch (Exception e) {
+ LOGGER.error("[Skyblocker] Encountered unknown exception while deleting the NEU repo", e);
+ player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.updateRepository.error")), false);
}
- loadRepository();
});
}
@@ -89,6 +103,6 @@ public class NEURepoManager {
* @return a completable future of the given runnable
*/
public static CompletableFuture<Void> runAsyncAfterLoad(Runnable runnable) {
- return REPO_INITIALIZED.thenRunAsync(runnable);
+ return REPO_LOADING.thenRunAsync(runnable);
}
}