diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index 63631629..29001c8c 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -256,9 +256,10 @@ public class DisguiseConfig { @Setter private static boolean saveUserPreferences; @Getter - @Setter private static long lastUpdateRequest; @Getter + private static boolean hittingRateLimit; + @Getter @Setter private static boolean copyPlayerTeamInfo; @Getter @@ -294,6 +295,15 @@ public class DisguiseConfig { doUpdaterTask(); } + public static void setLastUpdateRequest(long lastRequest) { + if (lastRequest <= getLastUpdateRequest()) { + return; + } + + lastUpdateRequest = lastRequest; + saveInternalConfig(); + } + private static void doUpdaterTask() { boolean startTask = isAutoUpdate() || isNotifyUpdate() || "1592".equals( (LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() : @@ -304,6 +314,8 @@ public class DisguiseConfig { return; } + int timer = (int) (TimeUnit.HOURS.toSeconds(isHittingRateLimit() ? 36 : 6) * 20); + if (!LibsDisguises.getInstance().getConfig().getDefaults().getBoolean("AutoUpdate")) { updaterTask = Bukkit.getScheduler().runTaskTimer(LibsDisguises.getInstance(), new Runnable() { @Override @@ -316,6 +328,8 @@ public class DisguiseConfig { } } }, TimeUnit.HOURS.toSeconds(1) * 20, (20 * TimeUnit.MINUTES.toSeconds(10))); + + return; } if (updaterTask == null != startTask) { @@ -328,25 +342,18 @@ public class DisguiseConfig { return; } - long timeSinceLast = System.currentTimeMillis() - (getLastUpdateRequest() + TimeUnit.HOURS.toMillis(6)); + // Get the ticks since last update + long timeSinceLast = (System.currentTimeMillis() - getLastUpdateRequest()) / 50; - // Change timer to 30 min if longer than that - if (timeSinceLast > TimeUnit.MINUTES.toMillis(30)) { - timeSinceLast = TimeUnit.MINUTES.toMillis(30); - } - - if (timeSinceLast > 0) { - timeSinceLast /= 50; - } else { - timeSinceLast = 0; - } + // Next update check will be in 30 seconds, or the timer - elapsed time. Whatever is greater + timeSinceLast = Math.max(30 * 20, timer - timeSinceLast); updaterTask = Bukkit.getScheduler().runTaskTimerAsynchronously(LibsDisguises.getInstance(), new Runnable() { @Override public void run() { LibsDisguises.getInstance().getUpdateChecker().doAutoUpdateCheck(); } - }, timeSinceLast, (20 * TimeUnit.HOURS.toSeconds(6))); // Check every 6 hours + }, timeSinceLast, timer); } public static void setUsingReleaseBuilds(boolean useReleaseBuilds) { @@ -358,6 +365,16 @@ public class DisguiseConfig { saveInternalConfig(); } + public static void setHittingRateLimit(boolean hitRateLimit) { + if (hitRateLimit == isHittingRateLimit()) { + return; + } + + hittingRateLimit = hitRateLimit; + saveInternalConfig(); + doUpdaterTask(); + } + public static void setBisectHosted(boolean isBisectHosted, String serverIP) { if (isBisectHosted() == isBisectHosted && getSavedServerIp().equals(serverIP)) { return; @@ -381,6 +398,7 @@ public class DisguiseConfig { savedServerIp = configuration.getString("Server-IP", getSavedServerIp()); usingReleaseBuild = configuration.getBoolean("ReleaseBuild", isUsingReleaseBuild()); lastUpdateRequest = configuration.getLong("LastUpdateRequest", 0L); + hittingRateLimit = configuration.getBoolean("HittingRateLimit", false); if (!configuration.contains("Bisect-Hosted") || !configuration.contains("Server-IP") || !configuration.contains("ReleaseBuild")) { @@ -396,7 +414,7 @@ public class DisguiseConfig { // Bisect hosted, server ip, release builds for (Object s : new Object[]{isBisectHosted(), getSavedServerIp(), isUsingReleaseBuild(), - getLastUpdateRequest()}) { + getLastUpdateRequest(), isHittingRateLimit()}) { internalConfig = internalConfig.replaceFirst("%data%", "" + s); } @@ -747,8 +765,8 @@ public class DisguiseConfig { } try { - String option = - config.getString("SelfDisguisesScoreboard", DisguisePushing.MODIFY_SCOREBOARD.name()).toUpperCase(Locale.ENGLISH); + String option = config.getString("SelfDisguisesScoreboard", DisguisePushing.MODIFY_SCOREBOARD.name()) + .toUpperCase(Locale.ENGLISH); if (!option.endsWith("_SCOREBOARD")) { option += "_SCOREBOARD"; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java index fb20e222..4641da73 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java @@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities.updates; import com.google.gson.Gson; import lombok.AllArgsConstructor; import lombok.Getter; +import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsPremium; @@ -83,8 +84,7 @@ public class LDGithub { } return ((String) map.get("body")).split("(\\r|\\n)+"); - } - catch (Exception ignored) { + } catch (Exception ignored) { } return new String[0]; @@ -129,15 +129,18 @@ public class LDGithub { .collect(Collectors.joining("\n")); gitData = new Gson().fromJson(json, GithubData.class); - } - catch (IOException ex) { + } catch (IOException ex) { try (InputStream error = con.getErrorStream()) { String line = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8)).lines() .collect(Collectors.joining("\n")); DisguiseUtilities.getLogger().severe("Error with Github! " + line); - } - catch (Exception ex1) { + + if (line.contains("rate limit") && !DisguiseConfig.isHittingRateLimit()) { + DisguiseConfig.setHittingRateLimit(true); + DisguiseUtilities.getLogger().severe("Changed update checker to be every 36 hours due to rate limiting from this IP"); + } + } catch (Exception ex1) { DisguiseUtilities.getLogger().severe("Error when trying to read error stream! Inception!"); ex1.printStackTrace(); } @@ -162,8 +165,7 @@ public class LDGithub { return new GithubUpdate(gitData.getTag_name().replace("v", ""), gitData.getBody().split("(\\r|\\n)+"), download); - } - catch (Exception ex) { + } catch (Exception ex) { DisguiseUtilities.getLogger().warning("Failed to check for a release on Github"); ex.printStackTrace(); } diff --git a/src/main/resources/internal.yml b/src/main/resources/internal.yml index 612ad827..ae043390 100644 --- a/src/main/resources/internal.yml +++ b/src/main/resources/internal.yml @@ -7,4 +7,5 @@ Server-IP: %data% # Should the plugin be doing release or dev builds updating? ReleaseBuild: %data% # Make sure the plugin aint spamming update requests -LastUpdateCheck: %data% \ No newline at end of file +LastUpdateCheck: %data% +HittingRateLimit: %data% \ No newline at end of file