Move app store validator to CarpetSettings

This commit is contained in:
altrisi committed 2023-06-26 18:44:15 +02:00
1 parent 177179b70a
commit db2023f5b9
3 files changed
+33 -36

No files matched your search

+28 -2
View File
@@ -4,7 +4,7 @@ import carpet.api.settings.CarpetRule;
import carpet.api.settings.RuleCategory; import carpet.api.settings.RuleCategory;
import carpet.api.settings.Validators; import carpet.api.settings.Validators;
import carpet.api.settings.Validator; import carpet.api.settings.Validator;
import carpet.script.external.Carpet; import carpet.script.utils.AppStoreManager;
import carpet.settings.Rule; import carpet.settings.Rule;
import carpet.utils.Translations; import carpet.utils.Translations;
import carpet.utils.CommandHelper; import carpet.utils.CommandHelper;
@@ -551,6 +551,32 @@ public class CarpetSettings
) )
public static boolean scriptsOptimization = true; public static boolean scriptsOptimization = true;
private static class ScarpetAppStore extends Validator<String> {
@Override
public String validate(CommandSourceStack source, CarpetRule<String> 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( @Rule(
desc = "Location of the online repository of scarpet apps", desc = "Location of the online repository of scarpet apps",
extra = { extra = {
@@ -560,7 +586,7 @@ public class CarpetSettings
}, },
category = SCARPET, category = SCARPET,
strict = false, strict = false,
validate= Carpet.ScarpetAppStoreValidator.class validate = ScarpetAppStore.class
) )
public static String scriptsAppStore = "gnembon/scarpet/contents/programs"; public static String scriptsAppStore = "gnembon/scarpet/contents/programs";
-33
View File
@@ -5,7 +5,6 @@ import carpet.CarpetSettings;
import carpet.api.settings.CarpetRule; import carpet.api.settings.CarpetRule;
import carpet.api.settings.RuleHelper; import carpet.api.settings.RuleHelper;
import carpet.api.settings.SettingsManager; import carpet.api.settings.SettingsManager;
import carpet.api.settings.Validator;
import carpet.fakes.MinecraftServerInterface; import carpet.fakes.MinecraftServerInterface;
import carpet.logging.HUDController; import carpet.logging.HUDController;
import carpet.network.ServerNetworkHandler; import carpet.network.ServerNetworkHandler;
@@ -17,7 +16,6 @@ import carpet.script.CarpetScriptServer;
import carpet.script.Module; import carpet.script.Module;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.LoadException; import carpet.script.exception.LoadException;
import carpet.script.utils.AppStoreManager;
import carpet.script.value.MapValue; import carpet.script.value.MapValue;
import carpet.script.value.StringValue; import carpet.script.value.StringValue;
import carpet.utils.CarpetProfiler; import carpet.utils.CarpetProfiler;
@@ -241,35 +239,4 @@ public class Carpet
}; };
SettingsManager.registerGlobalRuleObserver((source, changedRule, userInput) -> carpetRuleChanges.handleAny(changedRule, source)); SettingsManager.registerGlobalRuleObserver((source, changedRule, userInput) -> carpetRuleChanges.handleAny(changedRule, source));
} }
public static class ScarpetAppStoreValidator extends Validator<String>
{
@Override
public String validate(@Nullable CommandSourceStack source, CarpetRule<String> 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";
}
}
} }
@@ -58,6 +58,10 @@ public class AppStoreManager
scarpetRepoLink = link; scarpetRepoLink = link;
} }
public static boolean enabled()
{
return scarpetRepoLink != null;
}
private record AppInfo(String name, String url, StoreNode source) private record AppInfo(String name, String url, StoreNode source)
{ {
@@ -123,7 +127,7 @@ public class AppStoreManager
{ {
return; return;
} }
if (scarpetRepoLink == null) if (!enabled())
{ {
throw new IOException("Accessing scarpet app repo is disabled"); throw new IOException("Accessing scarpet app repo is disabled");
} }