more flexible dealing with custom argument types, allowing _ in custom types. Fixes #936

This commit is contained in:
gnembon committed 2021-06-23 13:36:14 -03:00
1 parent 979032688d
commit 85d60b283f
2 files changed
+19 -7

No files matched your search

+9 -3
View File
@@ -188,7 +188,7 @@ baz(entities) -> // same thing
``` ```
It works similarly to the auto command, but arguments get their inferred types based on the argument It works similarly to the auto command, but arguments get their inferred types based on the argument
names, looking at the full name, or the suffix after the last `_` that indicates the variable type. For instance, variable named `float` will names, looking at the full name, or any suffix when splitting on `_` that indicates the variable type. For instance, variable named `float` will
be parsed as a floating point number, but it can be named `'first_float'` or `'other_float'` as well. Any variable that is not be parsed as a floating point number, but it can be named `'first_float'` or `'other_float'` as well. Any variable that is not
supported, will be parsed as a `'string'` type. supported, will be parsed as a `'string'` type.
@@ -253,8 +253,14 @@ and less frequently used features, like forks and redirects, used pretty much on
### Command argument types ### Command argument types
There are several default argument types that can be used directly without specifying custom types. Each argument can be Argument types differ from actual argument names that the types are the suffixes of the used argument names, when separated with
customized in the `'arguments'` section of the app config, specifying its base type, via `'type'` that needs `'_'` symbol. For example argument name `'from_pos'` will be interpreted as a built-in type `'int'` and provided to the command system
as a name `'from_pos'`, however if you define a custom type `'from_pos'`, your custom type will be used instead.
Longer suffixes take priority over shorter prefixes, then user defined suffixes mask build-in prefixes.
There are several default argument types that can be used directly without specifying custom types.
Each argument can be customized in the `'arguments'` section of the app config, specifying its base type, via `'type'` that needs
to match any of the built-in types, with a series of optional modifiers. Shared modifiers include: to match any of the built-in types, with a series of optional modifiers. Shared modifiers include:
* `suggest` - static list of suggestions to show above the command while typing * `suggest` - static list of suggestions to show above the command while typing
* `suggester` - function taking one map argument, indicating current state of attributes in the parsed command * `suggester` - function taking one map argument, indicating current state of attributes in the parsed command
@@ -269,10 +269,16 @@ public abstract class CommandArgument
public static CommandArgument getTypeForArgument(String argument, CarpetScriptHost host) public static CommandArgument getTypeForArgument(String argument, CarpetScriptHost host)
{ {
String[] components = argument.split("_"); String[] components = argument.split("_");
String suffix = components[components.length-1]; CommandArgument arg;
CommandArgument arg = host.appArgTypes.get(suffix); for (int i = 0; i < components.length; i++)
if (arg != null) return arg; {
return builtIns.getOrDefault(suffix, DEFAULT); String candidate = String.join("_", Arrays.asList(components).subList(i, components.length));
arg = host.appArgTypes.get(candidate);
if (arg != null) return arg;
arg = builtIns.get(candidate);
if (arg != null) return arg;
}
return DEFAULT;
} }
public static RequiredArgumentBuilder<ServerCommandSource, ?> argumentNode(String param, CarpetScriptHost host) throws CommandSyntaxException public static RequiredArgumentBuilder<ServerCommandSource, ?> argumentNode(String param, CarpetScriptHost host) throws CommandSyntaxException