Add default arguments
This commit is contained in:
parent
11d40bdd87
commit
e623d72be7
42 changed files with 205 additions and 35 deletions
|
|
@ -1,4 +1,30 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~
|
||||||
|
~ MIT License
|
||||||
|
~
|
||||||
|
~ Copyright (c) 2020 IntellectualSites
|
||||||
|
~
|
||||||
|
~ Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
~ of this software and associated documentation files (the "Software"), to deal
|
||||||
|
~ in the Software without restriction, including without limitation the rights
|
||||||
|
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
~ copies of the Software, and to permit persons to whom the Software is
|
||||||
|
~ furnished to do so, subject to the following conditions:
|
||||||
|
~
|
||||||
|
~ The above copyright notice and this permission notice shall be included in all
|
||||||
|
~ copies or substantial portions of the Software.
|
||||||
|
~
|
||||||
|
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
~ SOFTWARE.
|
||||||
|
~
|
||||||
|
-->
|
||||||
|
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
@ -9,7 +35,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>commands-bukkit</artifactId>
|
<artifactId>cloud-bukkit</artifactId>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
|
|
@ -27,7 +53,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.intellectualsites</groupId>
|
<groupId>com.intellectualsites</groupId>
|
||||||
<artifactId>commands-core</artifactId>
|
<artifactId>cloud-core</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
@ -29,13 +29,13 @@
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>Commands</artifactId>
|
<artifactId>cloud</artifactId>
|
||||||
<groupId>com.intellectualsites</groupId>
|
<groupId>com.intellectualsites</groupId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>commands-core</artifactId>
|
<artifactId>cloud-core</artifactId>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -25,7 +25,8 @@ package com.intellectualsites.commands;
|
||||||
|
|
||||||
import com.intellectualsites.commands.components.CommandSyntaxFormatter;
|
import com.intellectualsites.commands.components.CommandSyntaxFormatter;
|
||||||
import com.intellectualsites.commands.components.StandardCommandSyntaxFormatter;
|
import com.intellectualsites.commands.components.StandardCommandSyntaxFormatter;
|
||||||
import com.intellectualsites.commands.context.CommandContext;
|
import com.intellectualsites.commands.context.CommandContextFactory;
|
||||||
|
import com.intellectualsites.commands.context.StandardCommandContextFactory;
|
||||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||||
import com.intellectualsites.commands.execution.CommandResult;
|
import com.intellectualsites.commands.execution.CommandResult;
|
||||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||||
|
|
@ -46,13 +47,15 @@ import java.util.function.Function;
|
||||||
*/
|
*/
|
||||||
public abstract class CommandManager<C extends CommandSender> {
|
public abstract class CommandManager<C extends CommandSender> {
|
||||||
|
|
||||||
|
private final CommandContextFactory<C> commandContextFactory = new StandardCommandContextFactory<>();
|
||||||
|
|
||||||
private final CommandExecutionCoordinator<C> commandExecutionCoordinator;
|
private final CommandExecutionCoordinator<C> commandExecutionCoordinator;
|
||||||
private final CommandRegistrationHandler commandRegistrationHandler;
|
private final CommandRegistrationHandler commandRegistrationHandler;
|
||||||
private final CommandTree<C> commandTree;
|
private final CommandTree<C> commandTree;
|
||||||
|
|
||||||
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
|
private CommandSyntaxFormatter<C> commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>();
|
||||||
|
|
||||||
protected CommandManager(@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
public CommandManager(@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||||
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
|
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
|
||||||
this.commandTree = CommandTree.newTree(this, commandRegistrationHandler);
|
this.commandTree = CommandTree.newTree(this, commandRegistrationHandler);
|
||||||
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
|
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
|
||||||
|
|
@ -68,8 +71,7 @@ public abstract class CommandManager<C extends CommandSender> {
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
|
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||||
final CommandContext<C> context = new CommandContext<>(commandSender);
|
return this.commandExecutionCoordinator.coordinateExecution(this.commandContextFactory.create(commandSender), tokenize(input));
|
||||||
return this.commandExecutionCoordinator.coordinateExecution(context, tokenize(input));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -82,8 +84,7 @@ public abstract class CommandManager<C extends CommandSender> {
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
|
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||||
final CommandContext<C> context = new CommandContext<>(commandSender);
|
return this.commandTree.getSuggestions(this.commandContextFactory.create(commandSender), tokenize(input));
|
||||||
return this.commandTree.getSuggestions(context, tokenize(input));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
|
@ -107,7 +107,11 @@ public class CommandTree<C extends CommandSender> {
|
||||||
}
|
}
|
||||||
if (child.getValue() != null) {
|
if (child.getValue() != null) {
|
||||||
if (commandQueue.isEmpty()) {
|
if (commandQueue.isEmpty()) {
|
||||||
if (child.isLeaf()) {
|
if (child.getValue().hasDefaultValue()) {
|
||||||
|
commandQueue.add(child.getValue().getDefaultValue());
|
||||||
|
} else if (!child.getValue().isRequired()) {
|
||||||
|
return Optional.ofNullable(child.getValue().getOwningCommand());
|
||||||
|
} else if (child.isLeaf()) {
|
||||||
/* Not enough arguments */
|
/* Not enough arguments */
|
||||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
|
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
|
||||||
.apply(Objects.requireNonNull(
|
.apply(Objects.requireNonNull(
|
||||||
|
|
@ -45,7 +45,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether or not the component is required
|
* Indicates whether or not the component is required
|
||||||
* or not. All components prior to any other required
|
* or not. All c$ git ls-files | xargs wc -lomponents prior to any other required
|
||||||
* component must also be required, such that the predicate
|
* component must also be required, such that the predicate
|
||||||
* (∀ c_i ∈ required)({c_0, ..., c_i-1} ⊂ required) holds true,
|
* (∀ c_i ∈ required)({c_0, ..., c_i-1} ⊂ required) holds true,
|
||||||
* where {c_0, ..., c_n-1} is the set of command components.
|
* where {c_0, ..., c_n-1} is the set of command components.
|
||||||
|
|
@ -61,17 +61,27 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
* into the corresponding command type
|
* into the corresponding command type
|
||||||
*/
|
*/
|
||||||
private final ComponentParser<C, T> parser;
|
private final ComponentParser<C, T> parser;
|
||||||
|
/**
|
||||||
|
* Default value, will be empty if none was supplied
|
||||||
|
*/
|
||||||
|
private final String defaultValue;
|
||||||
|
|
||||||
private Command<C> owningCommand;
|
private Command<C> owningCommand;
|
||||||
|
|
||||||
public CommandComponent(final boolean required, @Nonnull final String name,
|
public CommandComponent(final boolean required, @Nonnull final String name,
|
||||||
@Nonnull final ComponentParser<C, T> parser) {
|
@Nonnull final ComponentParser<C, T> parser, @Nonnull final String defaultValue) {
|
||||||
this.required = required;
|
this.required = required;
|
||||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||||
if (!NAME_PATTERN.asPredicate().test(name)) {
|
if (!NAME_PATTERN.asPredicate().test(name)) {
|
||||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||||
}
|
}
|
||||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandComponent(final boolean required, @Nonnull final String name,
|
||||||
|
@Nonnull final ComponentParser<C, T> parser) {
|
||||||
|
this(required, name, parser, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -179,6 +189,25 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the default value
|
||||||
|
*
|
||||||
|
* @return Default value
|
||||||
|
*/
|
||||||
|
@Nonnull public String getDefaultValue() {
|
||||||
|
return this.defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the component has a default value
|
||||||
|
*
|
||||||
|
* @return {@code true} if the component has a default value, {@code false} if not
|
||||||
|
*/
|
||||||
|
public boolean hasDefaultValue() {
|
||||||
|
return !this.isRequired() &&
|
||||||
|
!this.getDefaultValue().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutable builder for {@link CommandComponent} instances
|
* Mutable builder for {@link CommandComponent} instances
|
||||||
|
|
@ -191,6 +220,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
protected String name;
|
protected String name;
|
||||||
protected boolean required = true;
|
protected boolean required = true;
|
||||||
protected ComponentParser<C, T> parser;
|
protected ComponentParser<C, T> parser;
|
||||||
|
protected String defaultValue = "";
|
||||||
|
|
||||||
protected Builder() {
|
protected Builder() {
|
||||||
}
|
}
|
||||||
|
|
@ -237,6 +267,23 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that the component is optional.
|
||||||
|
* All components prior to any other required
|
||||||
|
* component must also be required, such that the predicate
|
||||||
|
* (∀ c_i ∈ required)({c_0, ..., c_i-1} ⊂ required) holds true,
|
||||||
|
* where {c_0, ..., c_n-1} is the set of command components.
|
||||||
|
*
|
||||||
|
* @param defaultValue Default value that will be used if none was supplied
|
||||||
|
* @return Builder instance
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public Builder<C, T> asOptionalWithDefault(@Nonnull final String defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
this.required = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the component parser
|
* Set the component parser
|
||||||
*
|
*
|
||||||
|
|
@ -256,7 +303,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public CommandComponent<C, T> build() {
|
public CommandComponent<C, T> build() {
|
||||||
return new CommandComponent<>(this.required, this.name, this.parser);
|
return new CommandComponent<>(this.required, this.name, this.parser, this.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -39,8 +39,8 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
|
||||||
private final byte min;
|
private final byte min;
|
||||||
private final byte max;
|
private final byte max;
|
||||||
|
|
||||||
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, final byte max) {
|
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, final byte max, final String defaultValue) {
|
||||||
super(required, name, new ByteParser<>(min, max));
|
super(required, name, new ByteParser<>(min, max), defaultValue);
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +81,7 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public ByteComponent<C> build() {
|
public ByteComponent<C> build() {
|
||||||
return new ByteComponent<>(this.required, this.name, this.min, this.max);
|
return new ByteComponent<>(this.required, this.name, this.min, this.max, this.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -153,12 +153,12 @@ public class ByteComponent<C extends CommandSender> extends CommandComponent<C,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().byteValue() == Byte.MIN_VALUE;
|
return this.getMin().byteValue() != Byte.MIN_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().byteValue() == Byte.MAX_VALUE;
|
return this.getMax().byteValue() != Byte.MAX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -39,8 +39,8 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
|
||||||
private final int min;
|
private final int min;
|
||||||
private final int max;
|
private final int max;
|
||||||
|
|
||||||
private IntegerComponent(final boolean required, @Nonnull final String name, final int min, final int max) {
|
private IntegerComponent(final boolean required, @Nonnull final String name, final int min, final int max, final String defaultValue) {
|
||||||
super(required, name, new IntegerParser<>(min, max));
|
super(required, name, new IntegerParser<>(min, max), defaultValue);
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
@ -57,6 +57,10 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
|
||||||
return IntegerComponent.<C>newBuilder().named(name).asOptional().build();
|
return IntegerComponent.<C>newBuilder().named(name).asOptional().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull public static <C extends CommandSender> CommandComponent<C, Integer> optional(@Nonnull final String name, final int defaultNum) {
|
||||||
|
return IntegerComponent.<C>newBuilder().named(name).asOptionalWithDefault(Integer.toString(defaultNum)).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Integer> {
|
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Integer> {
|
||||||
|
|
||||||
|
|
@ -76,7 +80,7 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public IntegerComponent<C> build() {
|
public IntegerComponent<C> build() {
|
||||||
return new IntegerComponent<>(this.required, this.name, this.min, this.max);
|
return new IntegerComponent<>(this.required, this.name, this.min, this.max, this.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -143,12 +147,12 @@ public class IntegerComponent<C extends CommandSender> extends CommandComponent<
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMin() {
|
public boolean hasMin() {
|
||||||
return this.getMin().intValue() == Integer.MIN_VALUE;
|
return this.getMin().intValue() != Integer.MIN_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMax() {
|
public boolean hasMax() {
|
||||||
return this.getMax().intValue() == Integer.MAX_VALUE;
|
return this.getMax().intValue() != Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -35,7 +35,7 @@ import java.util.Optional;
|
||||||
*
|
*
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
*/
|
*/
|
||||||
public class CommandContext<C extends CommandSender> {
|
public final class CommandContext<C extends CommandSender> {
|
||||||
|
|
||||||
private final Map<String, Object> internalStorage = new HashMap<>();
|
private final Map<String, Object> internalStorage = new HashMap<>();
|
||||||
private final C commandSender;
|
private final C commandSender;
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands.context;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for {@link CommandContext} instances
|
||||||
|
*/
|
||||||
|
public interface CommandContextFactory<C extends CommandSender> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command context
|
||||||
|
*
|
||||||
|
* @param sender Command sender
|
||||||
|
* @return Command context
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
CommandContext<C> create(@Nonnull C sender);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands.context;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
public final class StandardCommandContextFactory<C extends CommandSender> implements CommandContextFactory<C> {
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public CommandContext<C> create(@Nonnull final C sender) {
|
||||||
|
return new CommandContext<>(sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
package com.intellectualsites.commands;
|
package com.intellectualsites.commands;
|
||||||
|
|
||||||
import com.intellectualsites.commands.components.StaticComponent;
|
import com.intellectualsites.commands.components.StaticComponent;
|
||||||
|
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||||
import com.intellectualsites.commands.context.CommandContext;
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
import com.intellectualsites.commands.exceptions.NoPermissionException;
|
import com.intellectualsites.commands.exceptions.NoPermissionException;
|
||||||
import com.intellectualsites.commands.sender.CommandSender;
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
@ -36,8 +37,6 @@ import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class CommandTreeTest {
|
class CommandTreeTest {
|
||||||
|
|
||||||
private static CommandManager<CommandSender> commandManager;
|
private static CommandManager<CommandSender> commandManager;
|
||||||
|
|
@ -48,7 +47,10 @@ class CommandTreeTest {
|
||||||
commandManager.registerCommand(commandManager.commandBuilder("test")
|
commandManager.registerCommand(commandManager.commandBuilder("test")
|
||||||
.withComponent(StaticComponent.required("one")).build())
|
.withComponent(StaticComponent.required("one")).build())
|
||||||
.registerCommand(commandManager.commandBuilder("test")
|
.registerCommand(commandManager.commandBuilder("test")
|
||||||
.withComponent(StaticComponent.required("two")).withPermission("no").build());
|
.withComponent(StaticComponent.required("two")).withPermission("no").build())
|
||||||
|
.registerCommand(commandManager.commandBuilder("test")
|
||||||
|
.withComponent(StaticComponent.required("opt"))
|
||||||
|
.withComponent(IntegerComponent.optional("num", 15)).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -58,6 +60,10 @@ class CommandTreeTest {
|
||||||
Assertions.assertTrue(command.isPresent());
|
Assertions.assertTrue(command.isPresent());
|
||||||
Assertions.assertThrows(NoPermissionException.class, () -> commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
|
Assertions.assertThrows(NoPermissionException.class, () -> commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(
|
||||||
Arrays.asList("test", "two"))));
|
Arrays.asList("test", "two"))));
|
||||||
|
commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt")))
|
||||||
|
.ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender())));
|
||||||
|
commandManager.getCommandTree().parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt", "12")))
|
||||||
|
.ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>commands-jline</artifactId>
|
<artifactId>cloud-jline</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jline</groupId>
|
<groupId>org.jline</groupId>
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.intellectualsites</groupId>
|
<groupId>com.intellectualsites</groupId>
|
||||||
<artifactId>Commands</artifactId>
|
<artifactId>cloud</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
10
pom.xml
10
pom.xml
|
|
@ -5,22 +5,22 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.intellectualsites</groupId>
|
<groupId>com.intellectualsites</groupId>
|
||||||
<artifactId>Commands</artifactId>
|
<artifactId>cloud</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<modules>
|
<modules>
|
||||||
<!--<module>commands-jline</module>-->
|
<!--<module>commands-jline</module>-->
|
||||||
<module>commands-bukkit</module>
|
<module>cloud-bukkit</module>
|
||||||
<module>commands-core</module>
|
<module>cloud-core</module>
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<inceptionYear>2020</inceptionYear>
|
<inceptionYear>2020</inceptionYear>
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>MIT License</name>
|
<name>MIT License</name>
|
||||||
<url>https://raw.githubusercontent.com/Sauilitired/Commands/master/LICENSE</url>
|
<url>https://raw.githubusercontent.com/Sauilitired/cloud/master/LICENSE</url>
|
||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
<name>Commands</name>
|
<name>cloud</name>
|
||||||
<description>Commands for Java</description>
|
<description>Commands for Java</description>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue