From 91903894d29c1cfab45ae77481cd111895f38058 Mon Sep 17 00:00:00 2001 From: Fix3d Date: Wed, 31 May 2023 14:15:49 +0300 Subject: [PATCH] fix repository update --- .../skyblock/api/RepositoryUpdate.java | 38 +++++++++++-------- .../skyblock/itemlist/ItemRegistry.java | 14 ++++++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/api/RepositoryUpdate.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/api/RepositoryUpdate.java index 3854dd88fb..e08cb1c01a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/api/RepositoryUpdate.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/api/RepositoryUpdate.java @@ -3,6 +3,7 @@ import me.xmrvizzy.skyblocker.skyblock.itemlist.ItemRegistry; 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.text.Text; import java.io.File; @@ -10,6 +11,7 @@ import java.util.concurrent.CompletableFuture; public class RepositoryUpdate { + public static final MinecraftClient client = MinecraftClient.getInstance(); /** * Adds command to update repository manually from ingame. @@ -21,22 +23,7 @@ public static void init(){ ClientCommandManager.literal("skyblocker") .then(ClientCommandManager.literal("updaterepository") .executes(context -> { - CompletableFuture.runAsync(() -> { - try { - ItemRegistry.filesImported = false; - File dir = ItemRegistry.LOCAL_ITEM_REPO_DIR.toFile(); - recursiveDelete(dir); - } catch (Exception ex) { - ItemRegistry.client.player.sendMessage( - Text.translatable("skyblocker.updaterepository.failed") - , false - ); - return; - } - - ItemRegistry.init(); - }); - + updateRepository(); return 1; }) ) @@ -45,6 +32,25 @@ public static void init(){ } + public static void updateRepository() { + CompletableFuture.runAsync(() -> { + try { + ItemRegistry.filesImported = false; + File dir = ItemRegistry.LOCAL_ITEM_REPO_DIR.toFile(); + recursiveDelete(dir); + } catch (Exception ex) { + if (client.player != null) + client.player.sendMessage( + Text.translatable("skyblocker.updaterepository.failed") + , false + ); + return; + } + + ItemRegistry.init(); + }); + } + private static void recursiveDelete(File dir) { if (dir.isDirectory() && !Files.isSymbolicLink(dir.toPath())) { for (File child : dir.listFiles()) { diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java index a1507b95bf..d9f3b47386 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java @@ -3,6 +3,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import me.xmrvizzy.skyblocker.skyblock.api.RepositoryUpdate; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.item.ItemStack; @@ -10,6 +11,7 @@ import net.minecraft.text.Text; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullResult; +import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,20 +49,26 @@ public static void init() { } private static void updateItemRepo() { + Git git; if (!Files.isDirectory(LOCAL_ITEM_REPO_DIR)) { try { - Git.cloneRepository() + git = Git.cloneRepository() .setURI(REMOTE_ITEM_REPO) .setDirectory(LOCAL_ITEM_REPO_DIR.toFile()) .setBranchesToClone(List.of("refs/heads/master")) .setBranch("refs/heads/master") .call(); + git.close(); + LOGGER.info("[Skyblocker Repository Update] Repository updated."); } catch (Exception e) { e.printStackTrace(); } } else { try { - PullResult pull = Git.open(LOCAL_ITEM_REPO_DIR.toFile()).pull().setRebase(true).call(); + git = Git.open(LOCAL_ITEM_REPO_DIR.toFile()); + PullResult pull = git.pull().setRebase(true).call(); + git.close(); + if (pull.getRebaseResult() == null) { LOGGER.info("[Skyblocker Repository Update] No update result"); } else if (pull.getRebaseResult().getStatus().isSuccessful()) { @@ -68,6 +76,8 @@ private static void updateItemRepo() { } else if (!pull.getRebaseResult().getStatus().isSuccessful()) { LOGGER.warn("[Skyblocker Repository Update] Status: " + pull.getRebaseResult().getStatus().name()); } + } catch (RepositoryNotFoundException e) { + RepositoryUpdate.updateRepository(); } catch (Exception e) { e.printStackTrace(); }