custom command trees, no validation so far
This commit is contained in:
1 parent
c7a96aa219
commit
9f6771123f
5 files changed
+185
-66
No files matched your search
@@ -89,6 +89,11 @@ import static net.minecraft.server.command.CommandManager.argument;
|
||||
|
||||
public abstract class CommandArgument
|
||||
{
|
||||
public static CommandSyntaxException error(String text)
|
||||
{
|
||||
return new SimpleCommandExceptionType(new LiteralText(text)).create();
|
||||
}
|
||||
|
||||
private static final List<? extends CommandArgument> baseTypes = Lists.newArrayList(
|
||||
// default
|
||||
new StringArgument(),
|
||||
@@ -241,7 +246,7 @@ public abstract class CommandArgument
|
||||
|
||||
public static final CommandArgument DEFAULT = baseTypes.get(0);
|
||||
|
||||
private static CommandArgument getTypeForArgument(String argument, CarpetScriptHost host)
|
||||
public static CommandArgument getTypeForArgument(String argument, CarpetScriptHost host)
|
||||
{
|
||||
String[] components = argument.split("_");
|
||||
String suffix = components[components.length-1].toLowerCase(Locale.ROOT);
|
||||
@@ -290,7 +295,7 @@ public abstract class CommandArgument
|
||||
public static CommandArgument buildFromConfig(String suffix, Map<String, Value> config)
|
||||
{
|
||||
if (!config.containsKey("type"))
|
||||
throw new InternalExpressionException("Custom types should at least specify the base type");
|
||||
throw new InternalExpressionException("Custom types should at least specify the type");
|
||||
String baseType = config.get("type").getString();
|
||||
if (!builtIns.containsKey(baseType))
|
||||
throw new InternalExpressionException("Unknown base type: "+baseType);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package carpet.script.command;
|
||||
|
||||
import carpet.script.CarpetScriptHost;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
|
||||
public class CommandToken
|
||||
{
|
||||
public String surface;
|
||||
public boolean isArgument;
|
||||
|
||||
private CommandToken(String surface, CommandArgument type )
|
||||
{
|
||||
this.surface = surface;
|
||||
isArgument = type != null;
|
||||
}
|
||||
|
||||
public static CommandToken getToken(String source, CarpetScriptHost host)
|
||||
{
|
||||
// todo add more type checking and return null
|
||||
if (!source.startsWith("<"))
|
||||
{
|
||||
return new CommandToken(source, null);
|
||||
}
|
||||
source = source.substring(1, source.length()-1);
|
||||
CommandArgument arg = CommandArgument.getTypeForArgument(source, host);
|
||||
return new CommandToken(source, arg);
|
||||
}
|
||||
|
||||
public static List<CommandToken> parseSpec(String spec, CarpetScriptHost host) throws CommandSyntaxException
|
||||
{
|
||||
spec = spec.trim();
|
||||
if (spec.isEmpty()) return Collections.emptyList();
|
||||
List<CommandToken> elements = new ArrayList<>();
|
||||
for (String el: spec.split("\\s+"))
|
||||
{
|
||||
CommandToken tok = CommandToken.getToken(el, host);
|
||||
if (tok == null) throw CommandArgument.error("Unrecognized command token: "+ el);
|
||||
elements.add(tok);
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
public static String specFromSignature(FunctionValue function)
|
||||
{
|
||||
List<String> tokens = Lists.newArrayList(function.getString());
|
||||
for (String arg : function.getArguments()) tokens.add("<"+arg+">");
|
||||
return String.join(" ", tokens);
|
||||
}
|
||||
|
||||
|
||||
public ArgumentBuilder<ServerCommandSource, ?> getCommandNode(CarpetScriptHost host) throws CommandSyntaxException
|
||||
{
|
||||
if (isArgument)
|
||||
return CommandArgument.argumentNode(surface, host);
|
||||
return literal(surface);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user