Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.hugog.minecraft</groupId>
<artifactId>dev-command</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package dev.hugog.minecraft.dev_command.commands;

import dev.hugog.minecraft.dev_command.DevCommand;
import dev.hugog.minecraft.dev_command.arguments.CommandArgument;
import dev.hugog.minecraft.dev_command.arguments.parsers.ICommandArgumentParser;
import dev.hugog.minecraft.dev_command.commands.data.BukkitCommandData;
import dev.hugog.minecraft.dev_command.dependencies.DependencyHandler;
import dev.hugog.minecraft.dev_command.exceptions.ArgumentsConfigException;
import dev.hugog.minecraft.dev_command.exceptions.InvalidArgumentsException;
import dev.hugog.minecraft.dev_command.exceptions.InvalidDependencyException;
import dev.hugog.minecraft.dev_command.exceptions.PermissionConfigException;
import dev.hugog.minecraft.dev_command.factories.ArgumentParserFactory;
import dev.hugog.minecraft.dev_command.validation.IAutoValidationConfiguration;
import lombok.Generated;
import lombok.Getter;
import dev.hugog.minecraft.dev_command.DevCommand;
import dev.hugog.minecraft.dev_command.dependencies.DependencyHandler;
import dev.hugog.minecraft.dev_command.exceptions.ArgumentsConfigException;
import dev.hugog.minecraft.dev_command.exceptions.PermissionConfigException;
import dev.hugog.minecraft.dev_command.factories.ArgumentParserFactory;
import dev.hugog.minecraft.dev_command.arguments.parsers.ICommandArgumentParser;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Optional;

Expand Down Expand Up @@ -135,7 +136,10 @@ public boolean performAutoValidation(IAutoValidationConfiguration configuration)
}
if (commandData.getAutoValidationData().validateArguments() && commandData.getArguments() != null) {
if (!hasValidArgs()) {
getCommandSender().sendMessage(configuration.getInvalidArgumentsMessage(this));
CommandArgument firstInvalidArgument = Arrays.stream(getInvalidArguments()).findFirst()
.orElseThrow(() -> new InvalidArgumentsException("Unable to find the first invalid argument. This should never happen."));
getCommandSender().sendMessage(MessageFormat.format(configuration.getInvalidArgumentsMessage(this),
firstInvalidArgument.name(), String.valueOf(firstInvalidArgument.position() + 1)));
return false;
}
}
Expand All @@ -153,4 +157,11 @@ public <T> T getDependency(Class<T> dependencyClass) throws InvalidDependencyExc
return (T) dependencyHandler.getDependencyInstance(commandData.getIntegration(), dependencyClass);
}

@Override
public CommandArgument[] getInvalidArguments() {
return Arrays.stream(commandData.getArguments())
.filter(commandArgument -> !getArgumentParser(commandArgument.position()).isValid())
.toArray(CommandArgument[]::new);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hugog.minecraft.dev_command.commands;

import dev.hugog.minecraft.dev_command.arguments.CommandArgument;
import dev.hugog.minecraft.dev_command.arguments.parsers.ICommandArgumentParser;
import dev.hugog.minecraft.dev_command.validation.IAutoValidationConfiguration;

Expand Down Expand Up @@ -29,14 +30,49 @@ public interface IDevCommand {
*/
Optional<ICommandArgumentParser<?>> getOptionalArgumentParser(int argumentPosition);

/**
* Check if the player has permission to execute the command.
*
* @return true if the player has permission, false otherwise
*/
boolean hasPermissionToExecuteCommand();

/**
* Check if all arguments provided are valid.
*
* @return true if all arguments are valid, false otherwise
*/
boolean hasValidArgs();

/**
* Check if the command sender can execute the command.
*
* @return true if the command sender can execute the command, false otherwise
*/
boolean canSenderExecuteCommand();

/**
* Perform auto-validation of the command.
*
* @param configuration the configuration for auto-validation
* @return true if the command is valid, false otherwise
*/
boolean performAutoValidation(IAutoValidationConfiguration configuration);

/**
* Get a dependency injected into the command.
*
* @param dependencyClass the class of the dependency to get
* @param <T> the type of the dependency
* @return the dependency instance
*/
<T> T getDependency(Class<T> dependencyClass);

/**
* Get all invalid arguments for the command.
*
* @return an array of invalid arguments, or an empty array if all arguments are valid
*/
CommandArgument[] getInvalidArguments();

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public String getNoPermissionMessage(BukkitDevCommand command) {

@Override
public String getInvalidArgumentsMessage(BukkitDevCommand command) {
return "You have provided invalid arguments. Please try again.";
return "You have provided invalid arguments. The argument {0} on position {1} is invalid.";
}

@Override
Expand Down