Added raw arg in CommandContext + cleanup

This commit is contained in:
themode 2021-03-10 20:03:36 +01:00
parent cbe57f9cac
commit 7bf2deecf2
5 changed files with 34 additions and 5 deletions

View File

@ -28,13 +28,13 @@ import java.util.Map;
@Deprecated
public class Arguments {
private Map<String, Object> args = new HashMap<>();
protected Map<String, Object> args = new HashMap<>();
private CommandData returnData;
@NotNull
public <T> T get(@NotNull Argument<T> argument) {
return (T) getObject(argument.getId());
return get(argument.getId());
}
public <T> T get(@NotNull String identifier) {
@ -280,6 +280,10 @@ public class Arguments {
return args;
}
/**
* @deprecated use {@link CommandContext#setArg(String, Object, String)}
*/
@Deprecated
public void setArg(@NotNull String id, Object value) {
this.args.put(id, value);
}

View File

@ -3,6 +3,9 @@ package net.minestom.server.command.builder;
import net.minestom.server.command.builder.arguments.Argument;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* Class used to retrieve argument data in a {@link CommandExecutor}.
* <p>
@ -15,6 +18,7 @@ import org.jetbrains.annotations.NotNull;
public class CommandContext extends Arguments {
private final String input;
private Map<String, String> rawArgs = new HashMap<>();
public CommandContext(@NotNull String input) {
this.input = input;
@ -24,4 +28,23 @@ public class CommandContext extends Arguments {
public String getInput() {
return input;
}
@NotNull
public <T> T getRaw(@NotNull Argument<T> argument) {
return get(argument.getId());
}
public <T> T getRaw(@NotNull String identifier) {
return (T) rawArgs.computeIfAbsent(identifier, s -> {
throw new NullPointerException(
"The argument with the id '" + identifier + "' has no value assigned, be sure to check your arguments id, your syntax, and that you do not change the argument id dynamically.");
});
}
public void setArg(@NotNull String id, Object value, String rawInput) {
this.args.put(id, value);
this.rawArgs.put(id, rawInput);
}
}

View File

@ -1,9 +1,11 @@
package net.minestom.server.command.builder.parser;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.CommandSyntax;
import net.minestom.server.command.builder.arguments.Argument;
public class ArgumentQueryResult {
public CommandSyntax syntax;
public Argument<?> argument;
public CommandContext context;
public String input;

View File

@ -154,7 +154,6 @@ public class CommandParser {
public static ArgumentQueryResult findSuggestibleArgument(@NotNull Command command, String[] args, String commandString) {
final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
Map<CommandSyntax, CommandContext> contextMap = new HashMap<>();
Int2ObjectRBTreeMap<ArgumentQueryResult> suggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder());
for (CommandSyntax syntax : syntaxes) {
@ -163,7 +162,6 @@ public class CommandParser {
}
final CommandContext context = new CommandContext(commandString);
contextMap.put(syntax, context);
final Argument<?>[] commandArguments = syntax.getArguments();
int inputIndex = 0;
@ -186,11 +184,12 @@ public class CommandParser {
final Argument<?> argument = argumentResult.argument;
if (argumentResult.correct) {
// Fill context
context.setArg(argument.getId(), argumentResult.parsedValue);
context.setArg(argument.getId(), argumentResult.parsedValue, argumentResult.rawArg);
}
if (argument.hasSuggestion()) {
ArgumentQueryResult queryResult = new ArgumentQueryResult();
queryResult.syntax = syntax;
queryResult.argument = argument;
queryResult.context = context;
queryResult.input = argumentResult.rawArg;

View File

@ -33,6 +33,7 @@ public class TestCommand extends Command {
if (!input.isEmpty()) {
int num = Integer.valueOf(input) * 2;
suggestion.addEntry(new SuggestionEntry(String.valueOf(num), ColoredText.of(ChatColor.RED, "Hover")));
System.out.println("test: "+context.get("msg3"));
}
});