Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Arguments reimplementation #26

Merged
merged 15 commits into from
Jul 25, 2024
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,27 @@ For example, you could make your command require the invoker to be a player and
builder.type(InvokerType.PLAYER).permission("test.permission");
```

#### Adding Arguments
In order to add arguments to your command you can use the `Arguments` class to generate the arguments.

For example lets make a string argument.
```java
builder.argument(Arguments.string());
```

To make the argument optional simply do that instead:
```java
builder.argument(Arguments.string().optional(true));
```

**INFO:** Arguments are required by default.

#### Make the command do something
In order to make the command do something, you can use the `execute` builder function.

In this example, we make the command send "hello" to the player who executed the command (for Spigot):
In this example, we make the command send "hello (argument 1)" to the player who executed the command (for Spigot):
```java
builder.execute((ctx) -> (((PlayerInvoker)ctx.getInvoker()).getPlayer()).sendMessage("Hello"))
builder.execute((ctx) -> (((PlayerInvoker)ctx.getInvoker()).getPlayer()).sendMessage("Hello " + ctx.get(0, String.class)))
```

#### Building the command
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.zffu.hardened.examples.api;

import net.zffu.hardened.api.args.Argument;
import net.zffu.hardened.api.args.ArgumentTypes;
import net.zffu.hardened.api.commands.Command;
import net.zffu.hardened.api.commands.builder.CommandBuilder;
import net.zffu.hardened.api.invoker.InvokerType;
Expand Down
29 changes: 0 additions & 29 deletions api/src/main/java/net/zffu/hardened/api/args/ArgumentTypes.java

This file was deleted.

28 changes: 28 additions & 0 deletions api/src/main/java/net/zffu/hardened/api/args/Arguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.zffu.hardened.api.args;

import net.zffu.hardened.api.args.types.IntegerArgument;
import net.zffu.hardened.api.args.types.StringArgument;

/**
* Useful class to generate {@link Argument} instances faster.
* @since 1.0.0
* @see {@link Argument}
*/
public class Arguments {

/**
* Gets a simple integer argument.
* @return
*/
public static IntegerArgument integer() {
return new IntegerArgument();
}

/**
* Gets a simple string argument.
* @return
*/
public static StringArgument string() {
return new StringArgument();
}
}
11 changes: 10 additions & 1 deletion api/src/main/java/net/zffu/hardened/api/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.zffu.hardened.api.commands;

import net.zffu.hardened.api.args.Argument;
import net.zffu.hardened.api.commands.validator.CommandValidator;
import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.CommandInvoker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* <p>The base of every command registered trough the Hardened API. Any command that uses this API should implement this.</p>
* <p>An interface that stores basic command metadata, the {@link CommandValidator} and the execution function.</p>
Expand All @@ -27,6 +29,13 @@ public interface Command<T extends CommandValidator> extends CommandExecution {
*/
@NotNull String[] getAliases();

/**
* <p>Gets the arguments that the command contains. Used in argument parsing</p>
* <p><b>Note:</b> No checks for required arguments are done by default, you should use a validator node for that.</p>
* @return the command arguments as an {@link Argument[]}
*/
@NotNull List<Argument> getArguments();

/**
* <p>Gets the command validator. The command validator is used to validate that a specific context can run the command.</p>
* @return the {@link CommandValidator}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zffu.hardened.api.commands.builder;

import net.zffu.hardened.api.args.Argument;
import net.zffu.hardened.api.commands.Command;
import net.zffu.hardened.api.commands.CommandExecution;
import net.zffu.hardened.api.commands.validator.CommandValidator;
Expand All @@ -8,12 +9,16 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class BuilderCommand implements Command {

protected String primaryName;
protected String[] aliases;
protected NodeCommandValidator validator = new NodeCommandValidator();
protected CommandExecution execution;
protected List<Argument> arguments = new ArrayList<>();

public BuilderCommand(String primaryName) {
this.primaryName = primaryName;
Expand All @@ -29,6 +34,11 @@ public BuilderCommand(String primaryName) {
return this.aliases;
}

@Override
public @NotNull List<Argument> getArguments() {
return this.arguments;
}

@Nullable
@Override
public CommandValidator getValidator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zffu.hardened.api.commands.builder;

import net.zffu.hardened.api.args.Argument;
import net.zffu.hardened.api.commands.CommandExecution;
import net.zffu.hardened.api.commands.validator.nodes.NodeCommandValidator;
import net.zffu.hardened.api.commands.validator.nodes.ValidatorNode;
Expand Down Expand Up @@ -68,6 +69,16 @@ public CommandBuilder disabled(boolean disabled) {
return this.node(new DisabledNode(disabled));
}

/**
* Adds an {@link Argument} inside the command.
* @param argument
* @return
*/
public CommandBuilder argument(Argument argument) {
this.command.arguments.add(argument);
return this;
}

/**
* Sets the command's execution logic.
* @param execution the logic as a {@link CommandExecution}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.zffu.hardened.api.context;

import com.sun.org.apache.xpath.internal.Arg;
import net.zffu.hardened.api.args.Argument;
import net.zffu.hardened.api.args.ArgumentType;
import net.zffu.hardened.api.commands.types.ArgCommand;
import net.zffu.hardened.api.commands.Command;
import net.zffu.hardened.api.invoker.CommandInvoker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -41,7 +41,7 @@ public CommandContext(@NotNull CommandInvoker invoker, @Nullable Object[] args,
/**
* <p>Locates the argument in the preformatted argument values pool and casts it to the according class.</p>
* <p><b>Note: </b>This function automatically checks for casting</p>
* @param index the argument value's index to find it (also is the argument's index in the {@link ArgCommand})
* @param index the argument value's index to find it (also is the argument's index in the {@link Command})
* @param clazz the {@link Class} of the output data type.
* @param <T> the output data type.
* @return the argument value casted to the provided type.
Expand All @@ -65,15 +65,16 @@ public CommandContext(@NotNull CommandInvoker invoker, @Nullable Object[] args,
}

/**
* <p>Preformats / parses the given raw {@link String} arguments and outputs the parsed arguments based on the registered arguments in the {@link ArgCommand}</p>
* @param command the {@link ArgCommand}. Used to get the argument types.
* <p>Preformats / parses the given raw {@link String} arguments and outputs the parsed arguments based on the registered arguments in the {@link Command}</p>
* @param command the {@link Command}. Used to get the argument types.
* @param args the raw string argument array.
* @return the parsed arguments as a {@link Object[]};
*/
public static Object[] preFormatArguments(@NotNull ArgCommand command, @NotNull String[] args) {
public static Object[] preFormatArguments(@NotNull Command<?> command, @NotNull String[] args) {
Object[] arguments = new Object[args.length];

int index = 0;

for(Argument argument : command.getArguments()) {
if(argument.isOptional()) continue; //todo: add optional argument handling
Object o = argument.getType().fromString(args[index]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.zffu.hardened.spigot.handler;

import net.zffu.hardened.api.commands.types.ArgCommand;
import net.zffu.hardened.api.context.CommandContext;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand All @@ -20,12 +19,12 @@ public SpigotCommand(net.zffu.hardened.api.commands.Command command) {
super(command.getPrimaryName());
this.command = command;
if(this.command.getAliases() != null && this.command.getAliases().length > 0) this.setAliases(Arrays.asList(this.command.getAliases()));
this.parseArgs = this.command instanceof ArgCommand;
this.parseArgs = !this.command.getArguments().isEmpty();
}

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
CommandContext context = new CommandContext(SpigotCommandHandler.fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments((ArgCommand) this.command, args) : null), commandLabel);
CommandContext context = new CommandContext(SpigotCommandHandler.fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments(this.command, args) : null), commandLabel);
this.command.run(context);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.zffu.hardened.spigot.handler;

import net.zffu.hardened.api.commands.types.ArgCommand;
import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.CommandInvoker;
import net.zffu.hardened.spigot.invokers.ConsoleInvoker;
Expand All @@ -23,11 +22,11 @@ public class SpigotCommandHandler implements CommandExecutor {

public SpigotCommandHandler(net.zffu.hardened.api.commands.Command<?> command) {
this.command = command;
this.parseArgs = this.command instanceof ArgCommand;
this.parseArgs = !this.command.getArguments().isEmpty();
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
CommandContext context = new CommandContext(fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments((ArgCommand) this.command, args) : null), label);
CommandContext context = new CommandContext(fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments(this.command, args) : null), label);
this.command.run(context);
return true;
}
Expand Down
Loading