moved custom arguments to before the commands and handled missing functions nicely.
This commit is contained in:
1 parent
92cc545896
commit
40869ef9bf
3 files changed
+62
-43
No files matched your search
@@ -283,21 +283,7 @@ public class CarpetScriptHost extends ScriptHost
|
|||||||
Map<Value, Value> config = ((MapValue) ret).getMap();
|
Map<Value, Value> config = ((MapValue) ret).getMap();
|
||||||
setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
|
setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
|
||||||
persistenceRequired = config.getOrDefault(new StringValue("stay_loaded"), Value.TRUE).getBoolean();
|
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<Value, Value> 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<String, Value> 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;
|
appConfig = config;
|
||||||
}
|
}
|
||||||
catch (NullPointerException ignored)
|
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<Value, Value> 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<String, Value> 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<Text> notifier)
|
public Boolean addAppCommands(Consumer<Text> 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)
|
if (appConfig.get(StringValue.of("commands")) != null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -342,7 +357,7 @@ public class CarpetScriptHost extends ScriptHost
|
|||||||
catch (CommandSyntaxException cse)
|
catch (CommandSyntaxException cse)
|
||||||
{
|
{
|
||||||
// failed
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -382,9 +397,11 @@ public class CarpetScriptHost extends ScriptHost
|
|||||||
return (int)response.readInteger();
|
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()))
|
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
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import carpet.script.Context;
|
|||||||
import carpet.script.LazyValue;
|
import carpet.script.LazyValue;
|
||||||
import carpet.script.ScriptHost;
|
import carpet.script.ScriptHost;
|
||||||
import carpet.script.bundled.Module;
|
import carpet.script.bundled.Module;
|
||||||
|
import carpet.script.command.CommandArgument;
|
||||||
import carpet.script.exception.InternalExpressionException;
|
import carpet.script.exception.InternalExpressionException;
|
||||||
import carpet.script.value.FunctionValue;
|
import carpet.script.value.FunctionValue;
|
||||||
import carpet.script.value.ListValue;
|
import carpet.script.value.ListValue;
|
||||||
import carpet.script.value.Value;
|
import carpet.script.value.Value;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -70,14 +72,13 @@ public class FunctionArgument<T> extends Argument
|
|||||||
return new FunctionArgument<>(fun, offset + 1 + argsize, lvargs);
|
return new FunctionArgument<>(fun, offset + 1 + argsize, lvargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FunctionArgument<Value> fromCommandSpec(ScriptHost host, Value funSpec)
|
public static FunctionArgument<Value> fromCommandSpec(ScriptHost host, Value funSpec) throws CommandSyntaxException {
|
||||||
{
|
|
||||||
FunctionValue function;
|
FunctionValue function;
|
||||||
List<Value> args = Collections.emptyList();
|
List<Value> args = Collections.emptyList();
|
||||||
if (!(funSpec instanceof ListValue))
|
if (!(funSpec instanceof ListValue))
|
||||||
funSpec = ListValue.of(funSpec);
|
funSpec = ListValue.of(funSpec);
|
||||||
List<Value> params = ((ListValue) funSpec).getItems();
|
List<Value> 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);
|
Value first = params.get(0);
|
||||||
if (first instanceof FunctionValue)
|
if (first instanceof FunctionValue)
|
||||||
{
|
{
|
||||||
@@ -86,7 +87,8 @@ public class FunctionArgument<T> extends Argument
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
String name = first.getString();
|
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());
|
if (params.size() > 1) args = params.subList(1,params.size());
|
||||||
|
|
||||||
|
|||||||
@@ -319,20 +319,20 @@ public abstract class CommandArgument
|
|||||||
return suffix;
|
return suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CommandArgument buildFromConfig(String suffix, Map<String, Value> config, CarpetScriptHost host)
|
public static CommandArgument buildFromConfig(String suffix, Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
if (!config.containsKey("type"))
|
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();
|
String baseType = config.get("type").getString();
|
||||||
if (!builtIns.containsKey(baseType))
|
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();
|
CommandArgument variant = builtIns.get(baseType).factory().get();
|
||||||
variant.suffix = suffix;
|
variant.suffix = suffix;
|
||||||
variant.configure(config, host);
|
variant.configure(config, host);
|
||||||
return variant;
|
return variant;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
caseSensitive = config.getOrDefault("case_sensitive", Value.TRUE).getBoolean();
|
caseSensitive = config.getOrDefault("case_sensitive", Value.TRUE).getBoolean();
|
||||||
if (config.containsKey("suggester"))
|
if (config.containsKey("suggester"))
|
||||||
@@ -341,9 +341,9 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
if (config.containsKey("suggest"))
|
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");
|
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()
|
examples = ((ListValue) suggestionValue).getItems().stream()
|
||||||
.map(Value::getString)
|
.map(Value::getString)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
@@ -442,13 +442,13 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
if (config.containsKey("options"))
|
if (config.containsKey("options"))
|
||||||
{
|
{
|
||||||
Value optionsValue = config.get("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()
|
validOptions = ((ListValue) optionsValue).getItems().stream()
|
||||||
.map(v -> caseSensitive?v.getString():(v.getString().toLowerCase(Locale.ROOT)))
|
.map(v -> caseSensitive?v.getString():(v.getString().toLowerCase(Locale.ROOT)))
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
@@ -508,7 +508,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
mustBeLoaded = config.getOrDefault("loaded", Value.FALSE).getBoolean();
|
mustBeLoaded = config.getOrDefault("loaded", Value.FALSE).getBoolean();
|
||||||
@@ -543,7 +543,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
blockCentered = config.getOrDefault("block_centered", Value.TRUE).getBoolean();
|
blockCentered = config.getOrDefault("block_centered", Value.TRUE).getBoolean();
|
||||||
@@ -591,7 +591,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
onlyFans = config.getOrDefault("players", Value.FALSE).getBoolean();
|
onlyFans = config.getOrDefault("players", Value.FALSE).getBoolean();
|
||||||
@@ -632,7 +632,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
single = config.getOrDefault("single", Value.FALSE).getBoolean();
|
single = config.getOrDefault("single", Value.FALSE).getBoolean();
|
||||||
@@ -673,7 +673,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
single = config.getOrDefault("single", Value.FALSE).getBoolean();
|
single = config.getOrDefault("single", Value.FALSE).getBoolean();
|
||||||
@@ -710,7 +710,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
mapRequired = !config.getOrDefault("allow_element", Value.FALSE).getBoolean();
|
mapRequired = !config.getOrDefault("allow_element", Value.FALSE).getBoolean();
|
||||||
@@ -756,13 +756,13 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
if (config.containsKey("options"))
|
if (config.containsKey("options"))
|
||||||
{
|
{
|
||||||
Value optionsValue = config.get("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());
|
validOptions = ((ListValue) optionsValue).getItems().stream().map(v -> new Identifier(v.getString())).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -798,7 +798,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
if (config.containsKey("min"))
|
if (config.containsKey("min"))
|
||||||
@@ -809,7 +809,7 @@ public abstract class CommandArgument
|
|||||||
{
|
{
|
||||||
max = NumericValue.asNumber(config.get("max"), "max").getDouble();
|
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
|
@Override
|
||||||
@@ -849,7 +849,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
if (config.containsKey("min"))
|
if (config.containsKey("min"))
|
||||||
@@ -860,7 +860,7 @@ public abstract class CommandArgument
|
|||||||
{
|
{
|
||||||
max = NumericValue.asNumber(config.get("max"), "max").getLong();
|
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
|
@Override
|
||||||
@@ -939,7 +939,7 @@ public abstract class CommandArgument
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(Map<String, Value> config, CarpetScriptHost host)
|
protected void configure(Map<String, Value> config, CarpetScriptHost host) throws CommandSyntaxException
|
||||||
{
|
{
|
||||||
super.configure(config, host);
|
super.configure(config, host);
|
||||||
if (config.containsKey("restrict"))
|
if (config.containsKey("restrict"))
|
||||||
@@ -947,7 +947,7 @@ public abstract class CommandArgument
|
|||||||
restrict = config.get("restrict").getString().toLowerCase(Locale.ROOT);
|
restrict = config.get("restrict").getString().toLowerCase(Locale.ROOT);
|
||||||
needsMatching = true;
|
needsMatching = true;
|
||||||
if (!RESTRICTED_CONTAINERS.containsKey(restrict))
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user