Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2021-06-23 13:37:29 -03:00
commit 8314072f9d
3 files changed
+28 -8

No files matched your search

+10 -4
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
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
supported, will be parsed as a `'string'` type.
@@ -240,7 +240,7 @@ paths with functions to execute, and optionally, custom argument types. Commands
the key (can be empty) consists of
the execution path with the command syntax, which consists of literals (as is) and arguments (wrapped with `<>`), with the name / suffix
of the name of the attribute indicating its type, and the value represent function to call, either function values,
defined function names, or functions with some default arguments. Values extracted from commands will be passed to the
defined function names, or functions with some default arguments. Argument names need to be unique for each command. Values extracted from commands will be passed to the
functions and executed. By default, command list will be checked for ambiguities (commands with the same path up to some point
that further use different attributes), causing app loading error if that happens, however this can be suppressed by specifying
`'allow_command_conflicts'`.
@@ -253,8 +253,14 @@ and less frequently used features, like forks and redirects, used pretty much on
### Command argument types
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
Argument types differ from actual argument names that the types are the suffixes of the used argument names, when separated with
`'_'` 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:
* `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
@@ -269,10 +269,16 @@ public abstract class CommandArgument
public static CommandArgument getTypeForArgument(String argument, CarpetScriptHost host)
{
String[] components = argument.split("_");
String suffix = components[components.length-1];
CommandArgument arg = host.appArgTypes.get(suffix);
if (arg != null) return arg;
return builtIns.getOrDefault(suffix, DEFAULT);
CommandArgument arg;
for (int i = 0; i < components.length; i++)
{
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
@@ -9,6 +9,7 @@ import net.minecraft.server.command.ServerCommandSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
@@ -46,10 +47,17 @@ public class CommandToken implements Comparable<CommandToken>
spec = spec.trim();
if (spec.isEmpty()) return Collections.emptyList();
List<CommandToken> elements = new ArrayList<>();
HashSet<String> seenArgs = new HashSet<>();
for (String el: spec.split("\\s+"))
{
CommandToken tok = CommandToken.getToken(el, host);
if (tok == null) throw CommandArgument.error("Unrecognized command token: "+ el);
if (tok.isArgument)
{
if (seenArgs.contains(tok.surface))
throw CommandArgument.error("Repeated command argument: "+tok.surface+", for '"+spec+"'. Argument names have to be unique");
seenArgs.add(tok.surface);
}
elements.add(tok);
}
return elements;