From 027426aacac048b85cd310e9e3d4101e0156a917 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Mon, 22 May 2023 20:30:12 -0400 Subject: Add Fairy Souls Helper --- .../java/me/xmrvizzy/skyblocker/utils/NEURepo.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java new file mode 100644 index 00000000..027cfa7a --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java @@ -0,0 +1,90 @@ +package me.xmrvizzy.skyblocker.utils; + +import me.xmrvizzy.skyblocker.SkyblockerMod; +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 org.eclipse.jgit.api.Git; +import org.eclipse.jgit.errors.RepositoryNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class NEURepo { + private static final Logger LOGGER = LoggerFactory.getLogger(NEURepo.class); + public static final String REMOTE_REPO_URL = "https://github.com/NotEnoughUpdates/NotEnoughUpdates-REPO.git"; + public static final Path LOCAL_REPO_DIR = SkyblockerMod.CONFIG_DIR.resolve("item-repo"); + private static final CompletableFuture REPO_INITIALIZED = initRepository(); + + /** + * Adds command to update repository manually from ingame. + *

+ * TODO A button could be added to the settings menu that will trigger this command. + */ + public static void init() { + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> + dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE) + .then(ClientCommandManager.literal("updaterepository").executes(context -> { + deleteAndDownloadRepository(); + return 1; + })))); + } + + public static CompletableFuture initRepository() { + return CompletableFuture.runAsync(() -> { + try { + if (Files.isDirectory(NEURepo.LOCAL_REPO_DIR)) { + try (Git localRepo = Git.open(NEURepo.LOCAL_REPO_DIR.toFile())) { + localRepo.pull().setRebase(true).call(); + LOGGER.info("[Skyblocker] NEU Repository Updated"); + } + } else { + Git.cloneRepository().setURI(REMOTE_REPO_URL).setDirectory(NEURepo.LOCAL_REPO_DIR.toFile()).setBranchesToClone(List.of("refs/heads/master")).setBranch("refs/heads/master").call().close(); + LOGGER.info("[Skyblocker] NEU Repository Downloaded"); + } + } catch (RepositoryNotFoundException e) { + LOGGER.warn("Local NEU Repository not found or corrupted, downloading new one", e); + deleteAndDownloadRepository(); + } catch (Exception e) { + LOGGER.error("Encountered unknown exception while initializing NEU Repository", e); + } + }); + } + + public static void deleteAndDownloadRepository() { + CompletableFuture.runAsync(() -> { + try { + ItemRegistry.filesImported = false; + File dir = NEURepo.LOCAL_REPO_DIR.toFile(); + recursiveDelete(dir); + } catch (Exception ex) { + if (MinecraftClient.getInstance().player != null) + MinecraftClient.getInstance().player.sendMessage(Text.translatable("skyblocker.updaterepository.failed"), false); + return; + } + initRepository(); + }); + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + private static void recursiveDelete(File dir) { + File[] children; + if (dir.isDirectory() && !Files.isSymbolicLink(dir.toPath()) && (children = dir.listFiles()) != null) { + for (File child : children) { + recursiveDelete(child); + } + } + dir.delete(); + } + + public static CompletableFuture runAsyncAfterLoad(Runnable runnable) { + return REPO_INITIALIZED.thenRunAsync(runnable); + } +} -- cgit From 08fa8669d4ff3ec098688a3e85b58ee8e9e0eea4 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Thu, 25 May 2023 18:08:02 -0400 Subject: Add docs to Utils and NEURepo --- .../java/me/xmrvizzy/skyblocker/utils/NEURepo.java | 8 ++++++ .../java/me/xmrvizzy/skyblocker/utils/Utils.java | 33 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java index 027cfa7a..0de6cecd 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java @@ -17,6 +17,9 @@ import java.nio.file.Path; import java.util.List; import java.util.concurrent.CompletableFuture; +/** + * Initializes the NEU repo, which contains item metadata and fairy souls location data. Clones the repo if it does not exist and checks for updates. Use {@link #runAsyncAfterLoad(Runnable)} to run code after the repo is initialized. + */ public class NEURepo { private static final Logger LOGGER = LoggerFactory.getLogger(NEURepo.class); public static final String REMOTE_REPO_URL = "https://github.com/NotEnoughUpdates/NotEnoughUpdates-REPO.git"; @@ -84,6 +87,11 @@ public class NEURepo { dir.delete(); } + /** + * Runs the given runnable after the NEU repo is initialized. + * @param runnable the runnable to run + * @return a completable future of the given runnable + */ public static CompletableFuture runAsyncAfterLoad(Runnable runnable) { return REPO_INITIALIZED.thenRunAsync(runnable); } diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Utils.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Utils.java index 887476f2..1b5019a7 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Utils.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Utils.java @@ -31,6 +31,10 @@ public class Utils { private static boolean isOnSkyblock = false; private static boolean isInDungeons = false; private static boolean isInjected = false; + /** + * The following fields store data returned from /locraw: {@link #profile}, {@link #server}, {@link #gameType}, {@link #locationRaw}, and {@link #map}. + */ + @SuppressWarnings("JavadocDeclaration") private static String profile = ""; private static String server = ""; private static String gameType = ""; @@ -52,22 +56,37 @@ public class Utils { return isInjected; } + /** + * @return the profile parsed from the player list. + */ public static String getProfile() { return profile; } + /** + * @return the server parsed from /locraw. + */ public static String getServer() { return server; } + /** + * @return the game type parsed from /locraw. + */ public static String getGameType() { return gameType; } + /** + * @return the location raw parsed from /locraw. + */ public static String getLocationRaw() { return locationRaw; } + /** + * @return the map parsed from /locraw. + */ public static String getMap() { return map; } @@ -77,6 +96,9 @@ public class Utils { ClientReceiveMessageEvents.ALLOW_GAME.register(Utils::onChatMessage); } + /** + * Updates all the fields stored in this class from the sidebar, player list, and /locraw. + */ public static void update() { MinecraftClient client = MinecraftClient.getInstance(); updateFromScoreboard(client); @@ -84,6 +106,9 @@ public class Utils { updateLocRaw(); } + /** + * Updates {@link #isOnSkyblock}, {@link #isInDungeons}, and {@link #isInjected} from the scoreboard. + */ public static void updateFromScoreboard(MinecraftClient client) { List sidebar; @@ -219,6 +244,9 @@ public class Utils { resetLocRawInfo(); } + /** + * Sends /locraw to the server if the player is on skyblock and on a new island. + */ private static void updateLocRaw() { if (isOnSkyblock) { long currentTime = System.currentTimeMillis(); @@ -232,6 +260,11 @@ public class Utils { } } + /** + * Parses the /locraw reply from the server + * + * @return not display the message in chat is the command is sent by the mod + */ public static boolean onChatMessage(Text text, boolean overlay) { String message = text.getString(); if (message.startsWith("{\"server\":") && message.endsWith("}")) { -- cgit From a0d504d9e2ffa95bbd2d9d99e546a44572417de9 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Wed, 31 May 2023 08:31:57 +0800 Subject: Updated NEU repo error handling --- src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java | 5 ++--- .../me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java | 4 +++- src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java | 7 +++++-- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java index ff3c7fae..838b2a55 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java @@ -63,10 +63,9 @@ public class FairySouls { } reader.close(); } catch (IOException e) { - e.printStackTrace(); - LOGGER.error("Failed to load found fairy souls."); + LOGGER.error("Failed to load found fairy souls", e); } catch (Exception e) { - e.printStackTrace(); + LOGGER.error("Encountered unknown exception loading fairy souls", e); } }); ClientLifecycleEvents.CLIENT_STOPPING.register(FairySouls::saveFoundFairySouls); 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 13ca356a..dc63e351 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java @@ -36,7 +36,9 @@ public class ItemRegistry { File dir = ITEM_LIST_DIR.toFile(); File[] files = dir.listFiles(); - assert files != null; + if (files == null) { + return; + } for (File file : files) { Path path = ITEM_LIST_DIR.resolve(file.getName()); try { diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java index 0de6cecd..5254feb1 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java @@ -7,6 +7,7 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallba import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,11 +53,13 @@ public class NEURepo { Git.cloneRepository().setURI(REMOTE_REPO_URL).setDirectory(NEURepo.LOCAL_REPO_DIR.toFile()).setBranchesToClone(List.of("refs/heads/master")).setBranch("refs/heads/master").call().close(); LOGGER.info("[Skyblocker] NEU Repository Downloaded"); } + } 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("Local NEU Repository not found or corrupted, downloading new one", e); + LOGGER.warn("[Skyblocker] Local NEU Repository not found or corrupted, downloading new one", e); deleteAndDownloadRepository(); } catch (Exception e) { - LOGGER.error("Encountered unknown exception while initializing NEU Repository", e); + LOGGER.error("[Skyblocker] Encountered unknown exception while initializing NEU Repository", e); } }); } -- cgit From 5f8683ab480d9efadbe1b39c006bcc33ae68458e Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 2 Jun 2023 15:51:19 +0800 Subject: Narrowed access to methods --- src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java index 5254feb1..29b39aa3 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java @@ -41,7 +41,7 @@ public class NEURepo { })))); } - public static CompletableFuture initRepository() { + private static CompletableFuture initRepository() { return CompletableFuture.runAsync(() -> { try { if (Files.isDirectory(NEURepo.LOCAL_REPO_DIR)) { @@ -64,7 +64,7 @@ public class NEURepo { }); } - public static void deleteAndDownloadRepository() { + private static void deleteAndDownloadRepository() { CompletableFuture.runAsync(() -> { try { ItemRegistry.filesImported = false; -- cgit