Improved: UpdateChecker

This commit is contained in:
Bram 2024-07-07 15:50:13 +02:00
parent b58c4c7909
commit 7ff17301fd
No known key found for this signature in database
GPG key ID: 13E608068F40E3CC
3 changed files with 24 additions and 34 deletions

View file

@ -1,17 +1,8 @@
package me.tofaa.entitylib; package me.tofaa.entitylib;
import com.github.retrooper.packetevents.PacketEventsAPI; 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 org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public final class APIConfig { public final class APIConfig {
private final PacketEventsAPI<?> packetEvents; private final PacketEventsAPI<?> packetEvents;
@ -27,13 +18,6 @@ public final class APIConfig {
this.packetEvents = packetEvents; 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() { public @NotNull APIConfig useBstats() {
this.bstats = true; this.bstats = true;
return this; return this;

View file

@ -2,8 +2,8 @@ package me.tofaa.entitylib;
import me.tofaa.entitylib.utils.ELVersion; import me.tofaa.entitylib.utils.ELVersion;
import me.tofaa.entitylib.utils.ELVersions; import me.tofaa.entitylib.utils.ELVersions;
import me.tofaa.entitylib.utils.GithubUpdater;
import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Level; import java.util.logging.Level;
@ -20,20 +20,11 @@ public final class EntityLib {
api = platform.getAPI(); api = platform.getAPI();
if (api.getSettings().shouldCheckForUpdate()) { if (api.getSettings().shouldCheckForUpdate()) {
try {
if (api.getSettings().isDebugMode()) { if (api.getSettings().isDebugMode()) {
platform.getLogger().log(Level.INFO, "Checking for updates..."); 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) { new GithubUpdater("Tofaa2", "EntityLib").checkForUpdates();
platform.getLogger().log(Level.WARNING, e, () -> "EntityLib failed to check for updates.");
}
} }
} }

View file

@ -2,6 +2,7 @@ package me.tofaa.entitylib.utils;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import me.tofaa.entitylib.EntityLib;
import org.jetbrains.annotations.Blocking; import org.jetbrains.annotations.Blocking;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -9,23 +10,37 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class GithubUpdater { public final class GithubUpdater {
private final String org; private final String org;
private final String repo; private final String repo;
private final ELVersion currentVersion; private final ELVersion currentVersion;
private final Logger logger;
public GithubUpdater(String org, String repo) { public GithubUpdater(String org, String repo) {
this.org = org; this.org = org;
this.repo = repo; this.repo = repo;
this.currentVersion = ELVersions.CURRENT; this.currentVersion = ELVersions.CURRENT;
this.logger = EntityLib.getPlatform().getLogger();
} }
@Blocking public void checkForUpdates() {
public boolean isLatestVersion() throws IOException { CompletableFuture.runAsync(() -> {
ELVersion latest = getLatestVersion(); try {
return currentVersion.isNewerThan(latest); 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 @Blocking