From 7ff17301fdfec47ec6ecee36a665dabc17b91781 Mon Sep 17 00:00:00 2001
From: Bram <bram@bramdekker.com>
Date: Sun, 7 Jul 2024 15:50:13 +0200
Subject: [PATCH] Improved: UpdateChecker

---
 .../java/me/tofaa/entitylib/APIConfig.java    | 16 -------------
 .../java/me/tofaa/entitylib/EntityLib.java    | 19 ++++-----------
 .../tofaa/entitylib/utils/GithubUpdater.java  | 23 +++++++++++++++----
 3 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/api/src/main/java/me/tofaa/entitylib/APIConfig.java b/api/src/main/java/me/tofaa/entitylib/APIConfig.java
index 9631dd4..3cba64a 100644
--- a/api/src/main/java/me/tofaa/entitylib/APIConfig.java
+++ b/api/src/main/java/me/tofaa/entitylib/APIConfig.java
@@ -1,17 +1,8 @@
 package me.tofaa.entitylib;
 
 import com.github.retrooper.packetevents.PacketEventsAPI;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonParser;
-import me.tofaa.entitylib.utils.GithubUpdater;
-import org.jetbrains.annotations.Blocking;
 import org.jetbrains.annotations.NotNull;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
-
 public final class APIConfig {
 
     private final PacketEventsAPI<?> packetEvents;
@@ -27,13 +18,6 @@ public final class APIConfig {
         this.packetEvents = packetEvents;
     }
 
-    @Blocking
-    public boolean requiresUpdate() throws IOException {
-        if (!checkForUpdates) return false;
-        GithubUpdater updater = new GithubUpdater("Tofaa2", "EntityLib");
-        return !updater.isLatestVersion();
-    }
-
     public @NotNull APIConfig useBstats() {
         this.bstats = true;
         return this;
diff --git a/api/src/main/java/me/tofaa/entitylib/EntityLib.java b/api/src/main/java/me/tofaa/entitylib/EntityLib.java
index 85d97a9..d966467 100644
--- a/api/src/main/java/me/tofaa/entitylib/EntityLib.java
+++ b/api/src/main/java/me/tofaa/entitylib/EntityLib.java
@@ -2,8 +2,8 @@ package me.tofaa.entitylib;
 
 import me.tofaa.entitylib.utils.ELVersion;
 import me.tofaa.entitylib.utils.ELVersions;
+import me.tofaa.entitylib.utils.GithubUpdater;
 
-import java.io.IOException;
 import java.util.Optional;
 import java.util.logging.Level;
 
@@ -20,20 +20,11 @@ public final class EntityLib {
         api = platform.getAPI();
 
         if (api.getSettings().shouldCheckForUpdate()) {
-            try {
-                if (api.getSettings().isDebugMode()) {
-                    platform.getLogger().log(Level.INFO, "Checking for updates...");
-                }
-                if (api.getSettings().requiresUpdate()) {
-                    platform.getLogger().log(Level.WARNING, "You are using an outdated version of EntityLib. Please take a look at the Github releases page.");
-                }
-                else {
-                    platform.getLogger().log(Level.INFO, "No EntityLib updates found.");
-                }
-
-            } catch (IOException e) {
-                platform.getLogger().log(Level.WARNING, e, () -> "EntityLib failed to check for updates.");
+            if (api.getSettings().isDebugMode()) {
+                platform.getLogger().log(Level.INFO, "Checking for updates...");
             }
+
+            new GithubUpdater("Tofaa2", "EntityLib").checkForUpdates();
         }
     }
 
diff --git a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
index 72131c4..6ee88b7 100644
--- a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
+++ b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
@@ -2,6 +2,7 @@ package me.tofaa.entitylib.utils;
 
 import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
 import com.google.gson.JsonObject;
+import me.tofaa.entitylib.EntityLib;
 import org.jetbrains.annotations.Blocking;
 
 import java.io.BufferedReader;
@@ -9,23 +10,37 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.concurrent.CompletableFuture;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 public final class GithubUpdater {
 
     private final String org;
     private final String repo;
     private final ELVersion currentVersion;
+    private final Logger logger;
 
     public GithubUpdater(String org, String repo) {
         this.org = org;
         this.repo = repo;
         this.currentVersion = ELVersions.CURRENT;
+        this.logger = EntityLib.getPlatform().getLogger();
     }
 
-    @Blocking
-    public boolean isLatestVersion() throws IOException {
-        ELVersion latest = getLatestVersion();
-        return currentVersion.isNewerThan(latest);
+    public void checkForUpdates() {
+        CompletableFuture.runAsync(() -> {
+            try {
+                ELVersion latestVersion = getLatestVersion();
+                if (currentVersion.isOlderThan(latestVersion)) {
+                    logger.log(Level.WARNING, "You are using an outdated version of EntityLib. Please take a look at the Github releases page.");
+                } else if (currentVersion.isNewerThan(latestVersion)) {
+                    logger.log(Level.INFO, "No EntityLib updates found.");
+                }
+            } catch (Exception ex) {
+                logger.warning("Failed to check for updates: " + ex.getMessage());
+            }
+        });
     }
 
     @Blocking