From fbc43b4560c8ed5f0153c5d613066c92767fe1a4 Mon Sep 17 00:00:00 2001 From: Max Henkel Date: Tue, 2 Apr 2024 09:59:18 +0200 Subject: [PATCH] Use a separate thread for every custom update checker --- .../modmenu/api/UpdateChecker.java | 6 ++++++ .../modmenu/util/UpdateCheckerThread.java | 17 +++++++++++++++++ .../modmenu/util/UpdateCheckerUtil.java | 10 ++++------ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerThread.java diff --git a/src/main/java/com/terraformersmc/modmenu/api/UpdateChecker.java b/src/main/java/com/terraformersmc/modmenu/api/UpdateChecker.java index 87ce1886..1c2aa231 100644 --- a/src/main/java/com/terraformersmc/modmenu/api/UpdateChecker.java +++ b/src/main/java/com/terraformersmc/modmenu/api/UpdateChecker.java @@ -2,6 +2,12 @@ public interface UpdateChecker { + /** + * Gets called when ModMenu is checking for updates. + * This is done in a separate thread, so this call can/should be blocking. + * + * @return The update info + */ UpdateInfo checkForUpdates(); } diff --git a/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerThread.java b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerThread.java new file mode 100644 index 00000000..67b02b72 --- /dev/null +++ b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerThread.java @@ -0,0 +1,17 @@ +package com.terraformersmc.modmenu.util; + +import com.terraformersmc.modmenu.util.mod.Mod; + +public class UpdateCheckerThread extends Thread { + + protected UpdateCheckerThread(Mod mod, Runnable runnable) { + super(runnable); + setDaemon(true); + setName("Update Checker/%s".formatted(mod.getName())); + } + + public static void run(Mod mod, Runnable runnable) { + new UpdateCheckerThread(mod, runnable).start(); + } + +} diff --git a/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java index 6870574e..509c2060 100644 --- a/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java +++ b/src/main/java/com/terraformersmc/modmenu/util/UpdateCheckerUtil.java @@ -39,11 +39,9 @@ public static void checkForUpdates() { return; } - Util.getMainWorkerExecutor().execute(() -> { - LOGGER.info("Checking mod updates..."); - checkForModrinthUpdates(); - checkForCustomUpdates(); - }); + LOGGER.info("Checking mod updates..."); + Util.getMainWorkerExecutor().execute(UpdateCheckerUtil::checkForModrinthUpdates); + checkForCustomUpdates(); } public static void checkForCustomUpdates() { @@ -52,7 +50,7 @@ public static void checkForCustomUpdates() { if (updateChecker == null) { return; } - mod.setUpdateInfo(updateChecker.checkForUpdates()); + UpdateCheckerThread.run(mod, () -> mod.setUpdateInfo(updateChecker.checkForUpdates())); }); }