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.5</version>
<version>0.0.6</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.ToString;

import java.util.Arrays;
import java.util.Optional;

@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
Expand All @@ -20,15 +21,20 @@ public boolean isValid() {
}

@Override
public Boolean parse() {
public Optional<Boolean> parse() {
String[] trueTokens = new String[] {"true", "yes", "enabled"};
String[] falseTokens = new String[] {"false", "no", "disabled"};

if (!isValid()) {
return Optional.empty();
}

if (Arrays.stream(trueTokens).anyMatch(s -> s.equalsIgnoreCase(getArgument()))) {
return true;
return Optional.of(true);
} else if (Arrays.stream(falseTokens).anyMatch(s -> s.equalsIgnoreCase(getArgument()))) {
return false;
return Optional.of(false);
} else {
return null;
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Optional;

@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class DoubleArgumentParser extends CommandArgumentParser<Double> {
Expand All @@ -17,8 +19,11 @@ public boolean isValid() {
}

@Override
public Double parse() {
return Double.parseDouble(getArgument());
public Optional<Double> parse() {
if (!isValid()) {
return Optional.empty();
}
return Optional.of(Double.parseDouble(getArgument()));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package dev.hugog.minecraft.dev_command.arguments.parsers;

import java.util.Optional;

public interface ICommandArgumentParser<T> {

/**
* Checks if the argument is valid
*
* @return true if the argument is valid, false otherwise
*/
boolean isValid();
T parse();

/**
* Parses the argument into the desired type
*
* @return an {@link Optional} containing the parsed argument, or an empty {@link Optional} if the argument is invalid
*/
Optional<T> parse();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Optional;

@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class IntegerArgumentParser extends CommandArgumentParser<Integer> {
Expand All @@ -17,8 +19,11 @@ public boolean isValid() {
}

@Override
public Integer parse() {
return Integer.parseInt(getArgument());
public Optional<Integer> parse() {
if (!isValid()) {
return Optional.empty();
}
return Optional.of(Integer.parseInt(getArgument()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Optional;

@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class StringArgumentParser extends CommandArgumentParser<String> {
Expand All @@ -17,8 +19,11 @@ public boolean isValid() {
}

@Override
public String parse() {
return getArgument();
public Optional<String> parse() {
if (!isValid()) {
return Optional.empty();
}
return Optional.of(getArgument());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,15 @@ public boolean hasValidArgs() {

@Override
public ICommandArgumentParser<?> getArgumentParser(int argumentPosition) {
if (!hasValidArgs()) {
throw new InvalidArgumentsException(String.format("The arguments provided for the command %s are invalid.", commandData.getName()));
}

if (args.length <= argumentPosition) {
throw new InvalidArgumentsException(String.format("The argument position %d is out of bounds for the command %s.", argumentPosition, commandData.getName()));
}

ICommandArgumentParser<?> parser = Arrays.stream(commandData.getArguments())
return Arrays.stream(commandData.getArguments())
.filter(commandArgument -> commandArgument.position() == argumentPosition)
.findFirst()
.map(commandArgument -> new ArgumentParserFactory(args[argumentPosition]).generate(commandArgument.validator()))
.orElseThrow();

if (!parser.isValid()) {
throw new InvalidArgumentsException(String.format("The argument at position %d is invalid for the command %s.", argumentPosition, commandData.getName()));
}

return parser;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean executeCommand(Integration integration, CommandSender commandSend

// Take into account the case where the command is empty -> root command with no arguments
if (arguments.length == 0) {
Tree.Node<String> emptyNode = commandTree.findNode(commandTree.getRoot(), "");
Tree.Node<String> emptyNode = commandTree.findDirectChild(commandTree.getRoot(), "");
if (emptyNode != null) {
lastArgumentNode = emptyNode;
}
Expand All @@ -52,7 +52,7 @@ public boolean executeCommand(Integration integration, CommandSender commandSend
}

if (!lastArgumentNode.isLeaf()) {
log.warn("The command '{}' from '{}' was not found in the command tree.", String.join(" ", arguments), integration.getName());
log.info("The command '{}' from '{}' was not found in the command tree.", String.join(" ", arguments), integration.getName());
return false;
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/dev/hugog/minecraft/dev_command/utils/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Tree(T rootData) {
public Node<T> findPath(List<T> sequence) {
Node<T> lastNode = root;
for (T data : sequence) {
Node<T> node = findNode(lastNode, data);
Node<T> node = findDirectChild(lastNode, data);
if (node == null) {
return lastNode;
}
Expand All @@ -51,23 +51,20 @@ public Node<T> findPath(List<T> sequence) {
}

/**
* Finds a node in the tree that contains the given data.
*
* <p>
* It uses a depth-first search to find the node that contains the given data.
* Finds a direct child of the given node that contains the given data.
*
* @param node the parent node
* @param data the data to find
* @return the node that contains the data, or null if the data is not found
* @return the direct child node that contains the data, or null if not found
*/
public Node<T> findNode(Node<T> node, T data) {
public Node<T> findDirectChild(Node<T> node, T data) {
if (node.getData().equals(data)) {
return node;
}

for (Node<T> child : node.getChildren()) {
Node<T> found = findNode(child, data);
if (found != null) {
return found;
if (child.getData().equals(data)) {
return child;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package dev.hugog.minecraft.dev_command.commands;

import dev.hugog.minecraft.dev_command.arguments.CommandArgument;
import dev.hugog.minecraft.dev_command.commands.data.BukkitCommandData;
import dev.hugog.minecraft.dev_command.exceptions.ArgumentsConfigException;
import dev.hugog.minecraft.dev_command.exceptions.InvalidArgumentsException;
import dev.hugog.minecraft.dev_command.exceptions.PermissionConfigException;
import dev.hugog.minecraft.dev_command.arguments.parsers.BooleanArgumentParser;
import dev.hugog.minecraft.dev_command.arguments.parsers.DoubleArgumentParser;
import dev.hugog.minecraft.dev_command.arguments.parsers.IntegerArgumentParser;
import dev.hugog.minecraft.dev_command.arguments.parsers.StringArgumentParser;
import dev.hugog.minecraft.dev_command.commands.data.BukkitCommandData;
import dev.hugog.minecraft.dev_command.exceptions.ArgumentsConfigException;
import dev.hugog.minecraft.dev_command.exceptions.PermissionConfigException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -21,7 +20,8 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -390,33 +390,6 @@ public List<String> onTabComplete(String[] args) {

}

@Test
@DisplayName("Test getArgumentParser() method with invalid arguments.")
void parseArgument_Invalid() {

String[] arguments = new String[] {"-78","yes",".22", "-0.44"};
bukkitDevCommandStub = new BukkitDevCommand(bukkitCommandDataMock, commandSenderMock, arguments) {
@Override
public void execute() {
System.out.println("Command Executed");
}
@Override
public List<String> onTabComplete(String[] args) {
return List.of();
}
};

when(bukkitCommandDataMock.getArguments()).thenReturn(new CommandArgument[]{
new CommandArgument("integer", "Integer to test", 0, IntegerArgumentParser.class, false),
new CommandArgument("boolean", "Boolean to test", 1, BooleanArgumentParser.class, false),
new CommandArgument("integer", "Integer to test", 2, IntegerArgumentParser.class, false)}
);

assertFalse(bukkitDevCommandStub.hasValidArgs());
assertThrows(InvalidArgumentsException.class, () -> bukkitDevCommandStub.getArgumentParser(2));

}

@Test
@DisplayName("Test for call of argument validation without configuration.")
void argumentsConfigExceptionTest() {
Expand Down