From 348ef9e25d594d3bc36db65005eef82edca176d8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 24 Jan 2021 00:13:26 +1300 Subject: [PATCH] Lower the amount of github rate limit calls --- .../disguise/DisguiseConfig.java | 114 ++++++++---------- .../disguise/utilities/updates/LDGithub.java | 43 +++++-- src/main/resources/internal.yml | 4 +- 3 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index 41d49e74..626c3b8f 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -271,6 +271,12 @@ public class DisguiseConfig { @Getter @Setter private static boolean viewSelfDisguisesDefault; + @Getter + @Setter + private static String lastGithubUpdateETag; + @Getter + @Setter + private static String lastPluginUpdateVersion; public static boolean isArmorstandsName() { return getPlayerNameType() == PlayerNameType.ARMORSTANDS; @@ -308,9 +314,8 @@ public class DisguiseConfig { } private static void doUpdaterTask() { - boolean startTask = isAutoUpdate() || isNotifyUpdate() || "1592".equals( - (LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() : - LibsPremium.getPaidInformation()).getUserID()); + boolean startTask = isAutoUpdate() || isNotifyUpdate() || + "1592".equals((LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() : LibsPremium.getPaidInformation()).getUserID()); // Don't ever run the auto updater on a custom build.. if (!LibsDisguises.getInstance().isNumberedBuild()) { @@ -402,9 +407,10 @@ public class DisguiseConfig { usingReleaseBuild = configuration.getBoolean("ReleaseBuild", isUsingReleaseBuild()); lastUpdateRequest = configuration.getLong("LastUpdateRequest", 0L); hittingRateLimit = configuration.getBoolean("HittingRateLimit", false); + lastGithubUpdateETag = configuration.getString("LastGithubETag", null); + lastPluginUpdateVersion = configuration.getString("LastPluginVersion", null); - if (!configuration.contains("Bisect-Hosted") || !configuration.contains("Server-IP") || - !configuration.contains("ReleaseBuild")) { + if (!configuration.contains("Bisect-Hosted") || !configuration.contains("Server-IP") || !configuration.contains("ReleaseBuild")) { saveInternalConfig(); } } @@ -412,12 +418,11 @@ public class DisguiseConfig { public static void saveInternalConfig() { File internalFile = new File(LibsDisguises.getInstance().getDataFolder(), "internal.yml"); - String internalConfig = - ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), "internal.yml"); + String internalConfig = ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), "internal.yml"); // Bisect hosted, server ip, release builds - for (Object s : new Object[]{isBisectHosted(), getSavedServerIp(), isUsingReleaseBuild(), - getLastUpdateRequest(), isHittingRateLimit()}) { + for (Object s : new Object[]{isBisectHosted(), getSavedServerIp(), isUsingReleaseBuild(), getLastUpdateRequest(), isHittingRateLimit(), + getLastGithubUpdateETag(), getLastPluginUpdateVersion()}) { internalConfig = internalConfig.replaceFirst("%data%", "" + s); } @@ -500,8 +505,7 @@ public class DisguiseConfig { return null; } - return new HashMap.SimpleEntry(entry.getKey(), - DisguiseParser.parseDisguise(Bukkit.getConsoleSender(), target, entry.getValue())); + return new HashMap.SimpleEntry(entry.getKey(), DisguiseParser.parseDisguise(Bukkit.getConsoleSender(), target, entry.getValue())); } public static Entry getCustomDisguise(CommandSender invoker, Entity target, String disguise) @@ -558,8 +562,7 @@ public class DisguiseConfig { public static void saveDefaultConfig() { DisguiseUtilities.getLogger().info("Config is out of date! Now updating it!"); - String[] string = - ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), "config.yml").split("\n"); + String[] string = ReflectionManager.getResourceAsString(LibsDisguises.getInstance().getFile(), "config.yml").split("\n"); FileConfiguration savedConfig = LibsDisguises.getInstance().getConfig(); StringBuilder section = new StringBuilder(); @@ -594,8 +597,7 @@ public class DisguiseConfig { Object val = savedConfig.get(key); if (savedConfig.isString(key) && !rawVal.equals("true") && !rawVal.equals("false")) { - val = "'" + StringEscapeUtils.escapeJava(val.toString().replace(ChatColor.COLOR_CHAR + "", "&")) + - "'"; + val = "'" + StringEscapeUtils.escapeJava(val.toString().replace(ChatColor.COLOR_CHAR + "", "&")) + "'"; } string[i] = rawKey + ": " + val; @@ -641,8 +643,7 @@ public class DisguiseConfig { explain.createNewFile(); try (PrintWriter out = new PrintWriter(explain)) { - out.println("This folder is used to store .png files for uploading with the /savedisguise or " + - "/grabskin " + "commands"); + out.println("This folder is used to store .png files for uploading with the /savedisguise or " + "/grabskin " + "commands"); } } catch (IOException e) { e.printStackTrace(); @@ -663,10 +664,8 @@ public class DisguiseConfig { setDisablePvP(config.getBoolean("DisablePvP")); setDisablePvE(config.getBoolean("DisablePvE")); setPvPTimer(config.getDouble("PvPTimer")); - setDisguiseBlownWhenAttacked( - config.getBoolean("BlowDisguises", config.getBoolean("BlowDisguisesWhenAttacked"))); - setDisguiseBlownWhenAttacking( - config.getBoolean("BlowDisguises", config.getBoolean("BlowDisguisesWhenAttacking"))); + setDisguiseBlownWhenAttacked(config.getBoolean("BlowDisguises", config.getBoolean("BlowDisguisesWhenAttacked"))); + setDisguiseBlownWhenAttacking(config.getBoolean("BlowDisguises", config.getBoolean("BlowDisguisesWhenAttacking"))); setDisguiseCloneExpire(config.getInt("DisguiseCloneExpire")); setDisguiseEntityExpire(config.getInt("DisguiseEntityExpire")); setDynamicExpiry(config.getBoolean("DynamicExpiry")); @@ -728,49 +727,42 @@ public class DisguiseConfig { try { setPlayerNameType(PlayerNameType.valueOf(config.getString("PlayerNames").toUpperCase(Locale.ENGLISH))); } catch (Exception ex) { - DisguiseUtilities.getLogger().warning( - "Cannot parse '" + config.getString("PlayerNames") + "' to a valid option for PlayerNames"); + DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("PlayerNames") + "' to a valid option for PlayerNames"); } try { setNotifyBar(NotifyBar.valueOf(config.getString("NotifyBar").toUpperCase(Locale.ENGLISH))); if (getNotifyBar() == NotifyBar.BOSS_BAR && !NmsVersion.v1_13.isSupported()) { - DisguiseUtilities.getLogger().warning( - "BossBars hasn't been implemented properly in 1.12 due to api restrictions, falling back to " + - "ACTION_BAR"); + DisguiseUtilities.getLogger() + .warning("BossBars hasn't been implemented properly in 1.12 due to api restrictions, falling back to " + "ACTION_BAR"); setNotifyBar(NotifyBar.ACTION_BAR); } } catch (Exception ex) { - DisguiseUtilities.getLogger() - .warning("Cannot parse '" + config.getString("NotifyBar") + "' to a valid option for NotifyBar"); + DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("NotifyBar") + "' to a valid option for NotifyBar"); } try { setBossBarColor(BarColor.valueOf(config.getString("BossBarColor").toUpperCase(Locale.ENGLISH))); } catch (Exception ex) { - DisguiseUtilities.getLogger().warning( - "Cannot parse '" + config.getString("BossBarColor") + "' to a valid option for BossBarColor"); + DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("BossBarColor") + "' to a valid option for BossBarColor"); } try { setBossBarStyle(BarStyle.valueOf(config.getString("BossBarStyle").toUpperCase(Locale.ENGLISH))); } catch (Exception ex) { - DisguiseUtilities.getLogger().warning( - "Cannot parse '" + config.getString("BossBarStyle") + "' to a valid option for BossBarStyle"); + DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("BossBarStyle") + "' to a valid option for BossBarStyle"); } try { setUpdatesBranch(UpdatesBranch.valueOf(config.getString("UpdatesBranch").toUpperCase(Locale.ENGLISH))); } catch (Exception ex) { - DisguiseUtilities.getLogger().warning( - "Cannot parse '" + config.getString("UpdatesBranch") + "' to a valid option for UpdatesBranch"); + DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("UpdatesBranch") + "' to a valid option for UpdatesBranch"); } 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"; @@ -778,15 +770,15 @@ public class DisguiseConfig { pushingOption = DisguisePushing.valueOf(option); } catch (Exception ex) { - DisguiseUtilities.getLogger().warning("Cannot parse '" + config.getString("SelfDisguisesScoreboard") + - "' to a valid option for SelfDisguisesScoreboard"); + DisguiseUtilities.getLogger() + .warning("Cannot parse '" + config.getString("SelfDisguisesScoreboard") + "' to a valid option for SelfDisguisesScoreboard"); } PermissionDefault commandVisibility = PermissionDefault.getByName(config.getString("Permissions.SeeCommands")); if (commandVisibility == null) { - DisguiseUtilities.getLogger().warning("Invalid option '" + config.getString("Permissions.SeeCommands") + - "' for Permissions.SeeCommands when loading config!"); + DisguiseUtilities.getLogger() + .warning("Invalid option '" + config.getString("Permissions.SeeCommands") + "' for Permissions.SeeCommands when loading config!"); } else { setCommandVisibility(commandVisibility); } @@ -815,9 +807,7 @@ public class DisguiseConfig { if (config.contains("VerboseConfig")) { verbose = config.getBoolean("VerboseConfig"); } else { - DisguiseUtilities.getLogger() - .info("As 'VerboseConfig' hasn't been set, it is assumed true. Set it in your config to remove " + - "these messages!"); + DisguiseUtilities.getLogger().info("As 'VerboseConfig' hasn't been set, it is assumed true. Set it in your config to remove " + "these messages!"); verbose = true; } @@ -827,9 +817,8 @@ public class DisguiseConfig { ArrayList returns = doOutput(config, changed, verbose); if (!returns.isEmpty()) { - DisguiseUtilities.getLogger() - .info("This is not an error! Now outputting " + (verbose ? "missing " : "") + - (changed ? (verbose ? "and " : "") + "changed/invalid " : "") + "config values"); + DisguiseUtilities.getLogger().info("This is not an error! Now outputting " + (verbose ? "missing " : "") + + (changed ? (verbose ? "and " : "") + "changed/invalid " : "") + "config values"); for (String v : returns) { DisguiseUtilities.getLogger().info(v); @@ -852,10 +841,8 @@ public class DisguiseConfig { saveDefaultConfig(); DisguiseUtilities.getLogger().info("Config has been auto-updated!"); } else if (!verbose) { - DisguiseUtilities.getLogger().warning("Your config is missing " + missingConfigs + - " options! Please consider regenerating your config!"); - DisguiseUtilities.getLogger() - .info("You can also add the missing entries yourself! Try '/libsdisguises config'"); + DisguiseUtilities.getLogger().warning("Your config is missing " + missingConfigs + " options! Please consider regenerating your config!"); + DisguiseUtilities.getLogger().info("You can also add the missing entries yourself! Try '/libsdisguises config'"); } } else { DisguiseUtilities.getLogger().info("Config is up to date!"); @@ -904,8 +891,7 @@ public class DisguiseConfig { boolean living = section.getString("Type", "LIVING").equalsIgnoreCase("LIVING"); String type = section.getString("Type"); String mod = section.getString("Mod"); - String[] version = - mod == null || !section.contains("Version") ? null : section.getString("Version").split(","); + String[] version = mod == null || !section.contains("Version") ? null : section.getString("Version").split(","); String requireMessage = mod == null ? null : section.getString("Required"); if (section.contains("Channels")) { @@ -926,17 +912,14 @@ public class DisguiseConfig { ModdedEntity entity = new ModdedEntity(null, name, living, mod, version, requireMessage, 0); if (ModdedManager.getModdedEntity(name) != null) { - DisguiseUtilities.getLogger() - .info("Modded entity " + name + " has already been " + (register ? "registered" : "added")); + DisguiseUtilities.getLogger().info("Modded entity " + name + " has already been " + (register ? "registered" : "added")); continue; } - ModdedManager.registerModdedEntity( - new NamespacedKey(key.substring(0, key.indexOf(":")), key.substring(key.indexOf(":") + 1)), - entity, register); + ModdedManager + .registerModdedEntity(new NamespacedKey(key.substring(0, key.indexOf(":")), key.substring(key.indexOf(":") + 1)), entity, register); - DisguiseUtilities.getLogger() - .info("Modded entity " + name + " has been " + (register ? "registered" : "added")); + DisguiseUtilities.getLogger().info("Modded entity " + name + " has been " + (register ? "registered" : "added")); } catch (Exception ex) { DisguiseUtilities.getLogger().severe("Error while trying to register modded entity " + name); ex.printStackTrace(); @@ -946,8 +929,7 @@ public class DisguiseConfig { new ModdedManager(channels); } - public static ArrayList doOutput(ConfigurationSection config, boolean informChangedUnknown, - boolean informMissing) { + public static ArrayList doOutput(ConfigurationSection config, boolean informChangedUnknown, boolean informMissing) { HashMap configs = new HashMap<>(); ConfigurationSection defaultSection = config.getDefaultSection(); ArrayList returns = new ArrayList<>(); @@ -1013,8 +995,8 @@ public class DisguiseConfig { String toParse = section.getString(key); if (!NmsVersion.v1_13.isSupported() && key.equals("libraryaddict")) { - toParse = toParse.replace("GOLDEN_BOOTS,GOLDEN_LEGGINGS,GOLDEN_CHESTPLATE,GOLDEN_HELMET", - "GOLD_BOOTS,GOLD_LEGGINGS,GOLD_CHESTPLATE,GOLD_HELMET"); + toParse = + toParse.replace("GOLDEN_BOOTS,GOLDEN_LEGGINGS,GOLDEN_CHESTPLATE,GOLDEN_HELMET", "GOLD_BOOTS,GOLD_LEGGINGS,GOLD_CHESTPLATE,GOLD_HELMET"); } try { @@ -1040,8 +1022,7 @@ public class DisguiseConfig { DisguiseUtilities.getLogger().warning("Failed to load " + failedCustomDisguises + " custom disguises"); } - DisguiseUtilities.getLogger().info("Loaded " + customDisguises.size() + " custom disguise" + - (customDisguises.size() == 1 ? "" : "s")); + DisguiseUtilities.getLogger().info("Loaded " + customDisguises.size() + " custom disguise" + (customDisguises.size() == 1 ? "" : "s")); } public static void addCustomDisguise(String disguiseName, String toParse) throws DisguiseParseException { @@ -1065,8 +1046,7 @@ public class DisguiseConfig { DisguiseUtilities.getLogger().info("Loaded custom disguise " + disguiseName); } catch (DisguiseParseException e) { - throw new DisguiseParseException(LibsMsg.ERROR_LOADING_CUSTOM_DISGUISE, disguiseName, - (e.getMessage() == null ? "" : ": " + e.getMessage())); + throw new DisguiseParseException(LibsMsg.ERROR_LOADING_CUSTOM_DISGUISE, disguiseName, (e.getMessage() == null ? "" : ": " + e.getMessage())); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); throw new DisguiseParseException(LibsMsg.ERROR_LOADING_CUSTOM_DISGUISE, disguiseName, ""); 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 d9bbbcc0..8c277880 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java @@ -17,6 +17,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.HashMap; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -58,6 +59,10 @@ public class LDGithub { private UpdateChecker checker; private String[] getBadUsers() { + if (!LibsPremium.isPremium() || (LibsPremium.isBisectHosted() && (LibsPremium.getPaidInformation() == null || LibsPremium.getUserID().contains("%")))) { + return new String[0]; + } + // List of bad users that need to redownload Libs Disguises try { @@ -73,8 +78,7 @@ public class LDGithub { // Get the input stream, what we receive try (InputStream input = con.getInputStream()) { // Read it to string - String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n")); + String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); map = new Gson().fromJson(json, HashMap.class); } @@ -96,12 +100,10 @@ public class LDGithub { for (String s : users) { if (LibsPremium.getPaidInformation() != null && - (s.equals(LibsPremium.getPaidInformation().getDownloadID()) || - s.equals(LibsPremium.getPaidInformation().getUserID()))) { + (s.equals(LibsPremium.getPaidInformation().getDownloadID()) || s.equals(LibsPremium.getPaidInformation().getUserID()))) { LibsDisguises.getInstance().unregisterCommands(true); } else { - if (LibsPremium.getUserID() == null || - (!s.equals(LibsPremium.getUserID()) && !s.equals(LibsPremium.getDownloadID()))) { + if (LibsPremium.getUserID() == null || (!s.equals(LibsPremium.getUserID()) && !s.equals(LibsPremium.getDownloadID()))) { continue; } @@ -109,6 +111,8 @@ public class LDGithub { } } + String ourVersion = LibsDisguises.getInstance().getDescription().getVersion(); + if (!getChecker().isGoSilent()) { DisguiseUtilities.getLogger().info("Now looking for update on Github.."); } @@ -120,19 +124,27 @@ public class LDGithub { con.setRequestProperty("User-Agent", "libraryaddict/LibsDisguises"); con.setRequestProperty("Accept", "application/vnd.github.v3+json"); + // We believe we're on the latest version and know what the last etag was + if (Objects.equals(ourVersion, DisguiseConfig.getLastPluginUpdateVersion()) && DisguiseConfig.getLastGithubUpdateETag() != null) { + con.setRequestProperty("If-None-Match", DisguiseConfig.getLastGithubUpdateETag()); + } + + if (con.getResponseCode() == 304) { + // Its the same as the last one we checked + return null; + } + GithubData gitData; // Get the input stream, what we receive try (InputStream input = con.getInputStream()) { // Read it to string - String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n")); + String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); gitData = new Gson().fromJson(json, GithubData.class); } catch (IOException ex) { try (InputStream error = con.getErrorStream()) { - String line = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n")); + String line = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); DisguiseUtilities.getLogger().severe("Error with Github! " + line); @@ -164,8 +176,15 @@ public class LDGithub { throw new IllegalStateException("Download url is missing"); } - return new GithubUpdate(gitData.getTag_name().replace("v", ""), gitData.getBody().split("(\\r|\\n)+"), - download); + GithubUpdate update = new GithubUpdate(gitData.getTag_name().replace("v", ""), gitData.getBody().split("(\\r|\\n)+"), download); + + if (Objects.equals(update.getVersion(), ourVersion)) { + DisguiseConfig.setLastGithubUpdateETag(con.getHeaderField("ETag")); + DisguiseConfig.setLastPluginUpdateVersion(ourVersion); + DisguiseConfig.saveInternalConfig(); + } + + return update; } 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 ae043390..5bb5187e 100644 --- a/src/main/resources/internal.yml +++ b/src/main/resources/internal.yml @@ -8,4 +8,6 @@ Server-IP: %data% ReleaseBuild: %data% # Make sure the plugin aint spamming update requests LastUpdateCheck: %data% -HittingRateLimit: %data% \ No newline at end of file +HittingRateLimit: %data% +LastGithubETag: %data% +LastPluginVersion: %data% \ No newline at end of file