From 40869ef9bf54a7fb671bdccdce2551fc62ff3367 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 12 Apr 2021 14:24:10 -0300 Subject: [PATCH] moved custom arguments to before the commands and handled missing functions nicely. --- .../java/carpet/script/CarpetScriptHost.java | 51 ++++++++++++------- .../script/argument/FunctionArgument.java | 10 ++-- .../script/command/CommandArgument.java | 44 ++++++++-------- 3 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index 36bb7b23..2f2e9a57 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -283,21 +283,7 @@ public class CarpetScriptHost extends ScriptHost Map config = ((MapValue) ret).getMap(); setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player")); persistenceRequired = config.getOrDefault(new StringValue("stay_loaded"), Value.TRUE).getBoolean(); - // read custom arguments - Value arguments = config.get(StringValue.of("arguments")); - if (arguments != null) - { - if (!(arguments instanceof MapValue)) - throw new InternalExpressionException("'arguments' element in config should be a map"); - for (Map.Entry typeData : ((MapValue)arguments).getMap().entrySet()) - { - String argument = typeData.getKey().getString(); - Value spec = typeData.getValue(); - if (!(spec instanceof MapValue)) throw new InternalExpressionException("Spec for '"+argument+"' should be a map"); - Map specData = ((MapValue) spec).getMap().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().getString(), Map.Entry::getValue)); - appArgTypes.put(argument, CommandArgument.buildFromConfig(argument, specData, this)); - } - } + appConfig = config; } catch (NullPointerException ignored) @@ -323,8 +309,37 @@ public class CarpetScriptHost extends ScriptHost } } + public void readCustomArgumentTypes() throws CommandSyntaxException + { + // read custom arguments + Value arguments = appConfig.get(StringValue.of("arguments")); + if (arguments != null) + { + if (!(arguments instanceof MapValue)) + throw CommandArgument.error("'arguments' element in config should be a map"); + appArgTypes.clear(); + for (Map.Entry typeData : ((MapValue)arguments).getMap().entrySet()) + { + String argument = typeData.getKey().getString(); + Value spec = typeData.getValue(); + if (!(spec instanceof MapValue)) throw CommandArgument.error("Spec for '"+argument+"' should be a map"); + Map specData = ((MapValue) spec).getMap().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().getString(), Map.Entry::getValue)); + appArgTypes.put(argument, CommandArgument.buildFromConfig(argument, specData, this)); + } + } + } + public Boolean addAppCommands(Consumer notifier) { + try + { + readCustomArgumentTypes(); + } + catch (CommandSyntaxException e) + { + notifier.accept(Messenger.c("Error when handling of setting up custom argument types: "+e.getMessage())); + return false; + } if (appConfig.get(StringValue.of("commands")) != null) { try @@ -342,7 +357,7 @@ public class CarpetScriptHost extends ScriptHost catch (CommandSyntaxException cse) { // failed - notifier.accept(Messenger.c("r Failed to build command system for "+getName()+" thus failed to load the app ", cse.getRawMessage())); + notifier.accept(Messenger.c("r Failed to build command system for "+getName()+" thus failed to load the app: ", cse.getRawMessage())); return null; } @@ -382,9 +397,11 @@ public class CarpetScriptHost extends ScriptHost return (int)response.readInteger(); }); + boolean hasTypeSupport = appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean(); + for (String function : globaFunctionNames(main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList())) { - if (appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean()) + if (hasTypeSupport) { try { diff --git a/src/main/java/carpet/script/argument/FunctionArgument.java b/src/main/java/carpet/script/argument/FunctionArgument.java index b942e7f7..8c52c8d5 100644 --- a/src/main/java/carpet/script/argument/FunctionArgument.java +++ b/src/main/java/carpet/script/argument/FunctionArgument.java @@ -4,10 +4,12 @@ import carpet.script.Context; import carpet.script.LazyValue; import carpet.script.ScriptHost; import carpet.script.bundled.Module; +import carpet.script.command.CommandArgument; import carpet.script.exception.InternalExpressionException; import carpet.script.value.FunctionValue; import carpet.script.value.ListValue; import carpet.script.value.Value; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import java.util.ArrayList; import java.util.Collections; @@ -70,14 +72,13 @@ public class FunctionArgument extends Argument return new FunctionArgument<>(fun, offset + 1 + argsize, lvargs); } - public static FunctionArgument fromCommandSpec(ScriptHost host, Value funSpec) - { + public static FunctionArgument fromCommandSpec(ScriptHost host, Value funSpec) throws CommandSyntaxException { FunctionValue function; List args = Collections.emptyList(); if (!(funSpec instanceof ListValue)) funSpec = ListValue.of(funSpec); List params = ((ListValue) funSpec).getItems(); - if (params.isEmpty()) throw new InternalExpressionException("Function has empty spec"); + if (params.isEmpty()) throw CommandArgument.error("Function has empty spec"); Value first = params.get(0); if (first instanceof FunctionValue) { @@ -86,7 +87,8 @@ public class FunctionArgument extends Argument else { String name = first.getString(); - function = host.getAssertFunction(host.main, name); + function = host.getFunction(name); + if (function == null) throw CommandArgument.error("Function "+name+" is not defined yet"); } if (params.size() > 1) args = params.subList(1,params.size()); diff --git a/src/main/java/carpet/script/command/CommandArgument.java b/src/main/java/carpet/script/command/CommandArgument.java index 1285a838..5ca2f191 100644 --- a/src/main/java/carpet/script/command/CommandArgument.java +++ b/src/main/java/carpet/script/command/CommandArgument.java @@ -319,20 +319,20 @@ public abstract class CommandArgument return suffix; } - public static CommandArgument buildFromConfig(String suffix, Map config, CarpetScriptHost host) + public static CommandArgument buildFromConfig(String suffix, Map config, CarpetScriptHost host) throws CommandSyntaxException { if (!config.containsKey("type")) - throw new InternalExpressionException("Custom type "+suffix+" should at least specify the type"); + throw CommandArgument.error("Custom type "+suffix+" should at least specify the type"); String baseType = config.get("type").getString(); if (!builtIns.containsKey(baseType)) - throw new InternalExpressionException("Unknown base type "+baseType+" for custom type "+suffix); + throw CommandArgument.error("Unknown base type "+baseType+" for custom type "+suffix); CommandArgument variant = builtIns.get(baseType).factory().get(); variant.suffix = suffix; variant.configure(config, host); return variant; } - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { caseSensitive = config.getOrDefault("case_sensitive", Value.TRUE).getBoolean(); if (config.containsKey("suggester")) @@ -341,9 +341,9 @@ public abstract class CommandArgument } if (config.containsKey("suggest")) { - if (config.containsKey("suggester")) throw new InternalExpressionException("Attempted to provide 'suggest' list while 'suggester' is present"+" for custom type "+suffix); + if (config.containsKey("suggester")) throw error("Attempted to provide 'suggest' list while 'suggester' is present"+" for custom type "+suffix); Value suggestionValue = config.get("suggest"); - if (!(suggestionValue instanceof ListValue)) throw new InternalExpressionException("Argument suggestions needs to be a list"+" for custom type "+suffix); + if (!(suggestionValue instanceof ListValue)) throw error("Argument suggestions needs to be a list"+" for custom type "+suffix); examples = ((ListValue) suggestionValue).getItems().stream() .map(Value::getString) .collect(Collectors.toSet()); @@ -442,13 +442,13 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); if (config.containsKey("options")) { Value optionsValue = config.get("options"); - if (!(optionsValue instanceof ListValue)) throw new InternalExpressionException("Custom string type requires options passed as a list"+" for custom type "+suffix); + if (!(optionsValue instanceof ListValue)) throw error("Custom string type requires options passed as a list"+" for custom type "+suffix); validOptions = ((ListValue) optionsValue).getItems().stream() .map(v -> caseSensitive?v.getString():(v.getString().toLowerCase(Locale.ROOT))) .collect(Collectors.toSet()); @@ -508,7 +508,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); mustBeLoaded = config.getOrDefault("loaded", Value.FALSE).getBoolean(); @@ -543,7 +543,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); blockCentered = config.getOrDefault("block_centered", Value.TRUE).getBoolean(); @@ -591,7 +591,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); onlyFans = config.getOrDefault("players", Value.FALSE).getBoolean(); @@ -632,7 +632,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); single = config.getOrDefault("single", Value.FALSE).getBoolean(); @@ -673,7 +673,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); single = config.getOrDefault("single", Value.FALSE).getBoolean(); @@ -710,7 +710,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); mapRequired = !config.getOrDefault("allow_element", Value.FALSE).getBoolean(); @@ -756,13 +756,13 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); if (config.containsKey("options")) { Value optionsValue = config.get("options"); - if (!(optionsValue instanceof ListValue)) throw new InternalExpressionException("Custom sting type requires options passed as a list"+" for custom type "+suffix); + if (!(optionsValue instanceof ListValue)) throw error("Custom sting type requires options passed as a list"+" for custom type "+suffix); validOptions = ((ListValue) optionsValue).getItems().stream().map(v -> new Identifier(v.getString())).collect(Collectors.toSet()); } } @@ -798,7 +798,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); if (config.containsKey("min")) @@ -809,7 +809,7 @@ public abstract class CommandArgument { max = NumericValue.asNumber(config.get("max"), "max").getDouble(); } - if (max != null && min == null) throw new InternalExpressionException("Double types cannot be only upper-bounded"+" for custom type "+suffix); + if (max != null && min == null) throw error("Double types cannot be only upper-bounded"+" for custom type "+suffix); } @Override @@ -849,7 +849,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); if (config.containsKey("min")) @@ -860,7 +860,7 @@ public abstract class CommandArgument { max = NumericValue.asNumber(config.get("max"), "max").getLong(); } - if (max != null && min == null) throw new InternalExpressionException("Double types cannot be only upper-bounded"+" for custom type "+suffix); + if (max != null && min == null) throw error("Double types cannot be only upper-bounded"+" for custom type "+suffix); } @Override @@ -939,7 +939,7 @@ public abstract class CommandArgument } @Override - protected void configure(Map config, CarpetScriptHost host) + protected void configure(Map config, CarpetScriptHost host) throws CommandSyntaxException { super.configure(config, host); if (config.containsKey("restrict")) @@ -947,7 +947,7 @@ public abstract class CommandArgument restrict = config.get("restrict").getString().toLowerCase(Locale.ROOT); needsMatching = true; if (!RESTRICTED_CONTAINERS.containsKey(restrict)) - throw new InternalExpressionException("Incorrect slot restriction "+restrict+" for custom type "+suffix); + throw error("Incorrect slot restriction "+restrict+" for custom type "+suffix); } }