net.minestom.server.command.builder.arguments.ArgumentCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.command.builder.arguments;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.CommandDispatcher;
import net.minestom.server.command.builder.CommandResult;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
public class ArgumentCommand extends Argument {
public static final int INVALID_COMMAND_ERROR = 1;
private boolean onlyCorrect;
private String shortcut = "";
public ArgumentCommand(@NotNull String id) {
super(id, true, true);
}
@NotNull
@Override
public CommandResult parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
final String commandString = !shortcut.isEmpty() ?
shortcut + StringUtils.SPACE + input
: input;
CommandDispatcher dispatcher = MinecraftServer.getCommandManager().getDispatcher();
CommandResult result = dispatcher.parse(sender, commandString);
if (onlyCorrect && result.getType() != CommandResult.Type.SUCCESS)
throw new ArgumentSyntaxException("Invalid command", input, INVALID_COMMAND_ERROR);
return result;
}
@Override
public String parser() {
return null;
}
public boolean isOnlyCorrect() {
return onlyCorrect;
}
public ArgumentCommand setOnlyCorrect(boolean onlyCorrect) {
this.onlyCorrect = onlyCorrect;
return this;
}
@NotNull
public String getShortcut() {
return shortcut;
}
@ApiStatus.Experimental
public ArgumentCommand setShortcut(@NotNull String shortcut) {
this.shortcut = shortcut;
return this;
}
@Override
public String toString() {
return String.format("Command<%s>", getId());
}
}