Cannot use protected Java keywords as arguments in command
marcbeat opened this issue ยท 2 comments
When using protected Java keywords (e. g. new
), CommandDispatcher throws an error message.
Possible solution (ChatGPT):
The issue you're encountering with Java keywords in arguments can be addressed by properly managing and escaping these keywords. Since Java keywords can cause parsing issues, we need to handle them carefully. However, the Minecraft Brigadier command framework should be able to manage string arguments without directly conflicting with Java keywords.
Here are a few approaches to ensure that your commands work correctly even with Java keywords:
-
Argument Type Validation:
Ensure your command arguments do not directly conflict with Java keywords by validating the input argument. TheStringArgumentType
should generally handle keywords unless there are additional restrictions. -
Escape Keywords:
Escape keywords by wrapping them in quotes when they are inputted in the command line. This method ensures that the input is treated as a literal string, not a keyword.
Here's an example to illustrate how you can handle this:
private void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("pos")
.then(Commands.argument("action", StringArgumentType.word())
.suggests((context, builder) -> {
builder.suggest("get");
builder.suggest("set");
builder.suggest("rem");
builder.suggest("update");
builder.suggest("lists");
builder.suggest("help");
return builder.buildFuture();
})
.executes(context -> {
String action = StringArgumentType.getString(context, "action");
return executeBaseCommand(context.getSource(), action, null, null);
})
.then(Commands.argument("query", StringArgumentType.string())
.executes(context -> {
String action = StringArgumentType.getString(context, "action");
String query = StringArgumentType.getString(context, "query");
return executeBaseCommand(context.getSource(), action, query, null);
})
.then(Commands.argument("target", StringArgumentType.string())
.executes(context -> {
String action = StringArgumentType.getString(context, "action");
String query = StringArgumentType.getString(context, "query");
String target = StringArgumentType.getString(context, "target");
return executeBaseCommand(context.getSource(), action, query, target);
})
)
)
)
);
}
private int executeBaseCommand(CommandSourceStack source, String action, String query, String target) {
// Implementation of the command
return 1;
}
Explanation:
-
StringArgumentType.string(): Instead of
StringArgumentType.word()
, useStringArgumentType.string()
. This allows input strings to be enclosed in quotes, ensuring that even if they are Java keywords, they will be handled correctly. -
Quoting Arguments: When entering the command in-game, wrap arguments that may be Java keywords in quotes, e.g.,
/pos set "new" "target"
. This ensures the input is parsed correctly as a string.
Handling Quoting in Input:
Ensure users are aware of the need to quote arguments that might be Java keywords. This can be part of your command help documentation.
By following these steps, you should be able to use Java keywords as command arguments without causing exceptions.
Fixed with 56ebf8a