diff --git a/src/main/java/net/onelitefeather/antiredstoneclockremastered/service/UpdateService.java b/src/main/java/net/onelitefeather/antiredstoneclockremastered/service/UpdateService.java index ab94fdc..ffcb79e 100644 --- a/src/main/java/net/onelitefeather/antiredstoneclockremastered/service/UpdateService.java +++ b/src/main/java/net/onelitefeather/antiredstoneclockremastered/service/UpdateService.java @@ -22,12 +22,15 @@ @Singleton public final class UpdateService implements Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(UpdateService.class); + private static final long UPDATE_CHECK_INTERVAL_TICKS = Long.getLong("ARCR_UPDATE_SERVICE_SCHEDULE", 20 * 60 * 60 * 3L); // 3 hours + private static final int MAX_RETRY_COUNT = Integer.getInteger("ARCR_UPDATE_SERVICE_MAX_RETRY", 5); private static final String DOWNLOAD_URL = "https://hangar.papermc.io/OneLiteFeather/AntiRedstoneClock-Remastered/versions/%s"; private final HttpClient hangarClient = HttpClient.newBuilder().build(); private final Version localVersion; private final SchedulerService schedulerService; private Version remoteVersion; private ScheduledTask scheduler; + private int retryCount = 0; @Inject public UpdateService(AntiRedstoneClockRemastered antiRedstoneClockRemastered, SchedulerService schedulerService) { @@ -38,6 +41,12 @@ public UpdateService(AntiRedstoneClockRemastered antiRedstoneClockRemastered, Sc @Override public void run() { + if (retryCount >= MAX_RETRY_COUNT) { + LOGGER.error("Max retry count reached for update check, stopping further attempts"); + LOGGER.error("Please check your internet connection or https://hangar.papermc.io/ status."); + + return; + } var remoteVersion = getNewerVersion(); if (remoteVersion != null) { this.remoteVersion = remoteVersion; @@ -76,7 +85,7 @@ private void notifyPlayer(Version localVersion, Version remoteVersion, Player pl } public void schedule() { - this.scheduler = schedulerService.runTaskTimerAsynchronously(scheduledTask -> this.run(), 0, 20 * 60 * 60 * 3); + this.scheduler = schedulerService.runTaskTimerAsynchronously(scheduledTask -> this.run(), 0, UPDATE_CHECK_INTERVAL_TICKS); } @@ -84,6 +93,11 @@ public void schedule() { private Version getNewerVersion() { try { HttpResponse httpResponse = hangarClient.send(Constants.LATEST_RELEASE_VERSION_REQUEST, HttpResponse.BodyHandlers.ofString()); + if (httpResponse.statusCode() != 200) { + LOGGER.error("Failed to check for updates, status code: {}", httpResponse.statusCode()); + retryCount++; + return null; + } Version remoteVersion = Version.parse(httpResponse.body()); if (remoteVersion.isHigherThan(this.localVersion)) { return remoteVersion;