From db2023f5b9a44c04db56f8cdfc670c769a196e01 Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 26 Jun 2023 18:41:24 +0200 Subject: [PATCH] Move app store validator to `CarpetSettings` --- src/main/java/carpet/CarpetSettings.java | 30 +++++++++++++++-- .../java/carpet/script/external/Carpet.java | 33 ------------------- .../carpet/script/utils/AppStoreManager.java | 6 +++- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/main/java/carpet/CarpetSettings.java b/src/main/java/carpet/CarpetSettings.java index 270bae34..4caf08bf 100644 --- a/src/main/java/carpet/CarpetSettings.java +++ b/src/main/java/carpet/CarpetSettings.java @@ -4,7 +4,7 @@ import carpet.api.settings.CarpetRule; import carpet.api.settings.RuleCategory; import carpet.api.settings.Validators; import carpet.api.settings.Validator; -import carpet.script.external.Carpet; +import carpet.script.utils.AppStoreManager; import carpet.settings.Rule; import carpet.utils.Translations; import carpet.utils.CommandHelper; @@ -551,6 +551,32 @@ public class CarpetSettings ) public static boolean scriptsOptimization = true; + private static class ScarpetAppStore extends Validator { + @Override + public String validate(CommandSourceStack source, CarpetRule currentRule, String newValue, String stringInput) { + if (newValue.equals(currentRule.value())) { + // Don't refresh the local repo if it's the same (world change), helps preventing hitting rate limits from github when + // getting suggestions. Pending is a way to invalidate the cache when it gets old, and investigating api usage further + return newValue; + } + if (newValue.equals("none")) { + AppStoreManager.setScarpetRepoLink(null); + } else { + if (newValue.endsWith("/")) + newValue = newValue.substring(0, newValue.length() - 1); + AppStoreManager.setScarpetRepoLink("https://api.github.com/repos/" + newValue + "/"); + } + if (source != null) + CommandHelper.notifyPlayersCommandsChanged(source.getServer()); + return newValue; + } + + @Override + public String description() { + return "Appstore link should point to a valid github repository"; + } + } + @Rule( desc = "Location of the online repository of scarpet apps", extra = { @@ -560,7 +586,7 @@ public class CarpetSettings }, category = SCARPET, strict = false, - validate= Carpet.ScarpetAppStoreValidator.class + validate = ScarpetAppStore.class ) public static String scriptsAppStore = "gnembon/scarpet/contents/programs"; diff --git a/src/main/java/carpet/script/external/Carpet.java b/src/main/java/carpet/script/external/Carpet.java index 21182959..d4f92f2d 100644 --- a/src/main/java/carpet/script/external/Carpet.java +++ b/src/main/java/carpet/script/external/Carpet.java @@ -5,7 +5,6 @@ import carpet.CarpetSettings; import carpet.api.settings.CarpetRule; import carpet.api.settings.RuleHelper; import carpet.api.settings.SettingsManager; -import carpet.api.settings.Validator; import carpet.fakes.MinecraftServerInterface; import carpet.logging.HUDController; import carpet.network.ServerNetworkHandler; @@ -17,7 +16,6 @@ import carpet.script.CarpetScriptServer; import carpet.script.Module; import carpet.script.exception.InternalExpressionException; import carpet.script.exception.LoadException; -import carpet.script.utils.AppStoreManager; import carpet.script.value.MapValue; import carpet.script.value.StringValue; import carpet.utils.CarpetProfiler; @@ -241,35 +239,4 @@ public class Carpet }; SettingsManager.registerGlobalRuleObserver((source, changedRule, userInput) -> carpetRuleChanges.handleAny(changedRule, source)); } - - public static class ScarpetAppStoreValidator extends Validator - { - @Override - public String validate(@Nullable CommandSourceStack source, CarpetRule currentRule, String newValue, String stringInput) - { - if (newValue.equals(currentRule.value())) - { - // Don't refresh the local repo if it's the same (world change), helps preventing hitting rate limits from github when - // getting suggestions. Pending is a way to invalidate the cache when it gets old, and investigating api usage further - return newValue; - } - if (newValue.equalsIgnoreCase("none")) - { - AppStoreManager.setScarpetRepoLink(null); - return newValue; - } - if (newValue.endsWith("/")) - { - newValue = newValue.substring(0, newValue.length() - 1); - } - AppStoreManager.setScarpetRepoLink("https://api.github.com/repos/" + newValue + "/"); - return newValue; - } - - @Override - public String description() - { - return "Appstore link should point to a valid github repository"; - } - } } diff --git a/src/main/java/carpet/script/utils/AppStoreManager.java b/src/main/java/carpet/script/utils/AppStoreManager.java index 76ebccf8..505e73bc 100644 --- a/src/main/java/carpet/script/utils/AppStoreManager.java +++ b/src/main/java/carpet/script/utils/AppStoreManager.java @@ -58,6 +58,10 @@ public class AppStoreManager scarpetRepoLink = link; } + public static boolean enabled() + { + return scarpetRepoLink != null; + } private record AppInfo(String name, String url, StoreNode source) { @@ -123,7 +127,7 @@ public class AppStoreManager { return; } - if (scarpetRepoLink == null) + if (!enabled()) { throw new IOException("Accessing scarpet app repo is disabled"); }