Skip to content

Commit 4720244

Browse files
committed
feat(core): update system
1 parent 6be4737 commit 4720244

File tree

13 files changed

+206
-302
lines changed

13 files changed

+206
-302
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Be sure to relocate commandsAPI in to prevent bugs with other plugins.
5757
To get started with CommandsAPI, create a new command by extending the `Command<T extends JavaPlugin>` class. Here’s a simple example:
5858

5959
```java
60-
public class HelloWorldCommand extends SimpleCommand {
60+
public class HelloWorldCommand extends Command<MyPlugin> {
6161

6262
public HelloWorldCommand(JavaPlugin plugin) {
6363
super(plugin, "helloworld");
@@ -79,7 +79,7 @@ public class MyPlugin extends JavaPlugin {
7979

8080
@Override
8181
public void onEnable() {
82-
CommandManager commandManager = new CommandManager(this);
82+
CommandManager<MyPlugin> commandManager = new CommandManager<>(this);
8383
commandManager.registerCommands(new HelloWorldCommand(this));
8484
}
8585
}

src/main/java/fr/traqueur/commands/api/Command.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
import fr.traqueur.commands.api.exceptions.ArgsWithInfiniteArgumentException;
66
import fr.traqueur.commands.api.requirements.Requirement;
77
import org.bukkit.command.CommandSender;
8-
import org.bukkit.plugin.java.JavaPlugin;
8+
import org.bukkit.plugin.Plugin;
99

1010
import java.util.ArrayList;
1111
import java.util.Arrays;
1212
import java.util.List;
13+
import java.util.stream.Collectors;
1314

1415
/**
1516
* This class is the base class for all commands.
1617
* It contains all the necessary methods to create a command.
1718
* It is abstract and must be inherited to be used.
1819
* @param <T> The plugin that owns the command.
1920
*/
20-
public abstract class Command<T extends JavaPlugin> {
21+
public abstract class Command<T extends Plugin> {
22+
23+
private CommandManager<T> manager;
2124

22-
private CommandManager manager;
23-
// Attributs de la classe
2425
/**
2526
* The plugin that owns the command.
2627
*/
@@ -39,7 +40,7 @@ public abstract class Command<T extends JavaPlugin> {
3940
/**
4041
* The subcommands of the command.
4142
*/
42-
private final List<Command<?>> subcommands;
43+
private final List<Command<T>> subcommands;
4344

4445
/**
4546
* The arguments of the command.
@@ -110,7 +111,7 @@ public Command(T plugin, String name) {
110111
* This method is called to set the manager of the command.
111112
* @param manager The manager of the command.
112113
*/
113-
protected void setManager(CommandManager manager) {
114+
protected void setManager(CommandManager<T> manager) {
114115
this.manager = manager;
115116
}
116117

@@ -184,7 +185,7 @@ public final List<String> getAliases() {
184185
* This method is called to get the subcommands of the command.
185186
* @return The subcommands of the command.
186187
*/
187-
public final List<Command<?>> getSubcommands() {
188+
public final List<Command<T>> getSubcommands() {
188189
return subcommands;
189190
}
190191

@@ -280,9 +281,10 @@ public final void addAlias(String alias) {
280281
* This method is called to add subcommands to the command.
281282
* @param commands The subcommands to add.
282283
*/
283-
public final void addSubCommand(Command<?>... commands) {
284-
List<Command<?>> commandsList = Arrays.asList(commands);
285-
commandsList.forEach(command -> command.setSubcommand(true));
284+
@SafeVarargs
285+
public final void addSubCommand(Command<T>... commands) {
286+
List<Command<T>> commandsList = Arrays.asList(commands);
287+
commandsList.forEach(Command::setSubcommand);
286288
this.subcommands.addAll(commandsList);
287289
}
288290

@@ -447,19 +449,45 @@ public final boolean isSubCommand() {
447449
return subcommand;
448450
}
449451

450-
/**
451-
* Set if the command is subcommand
452-
* @param subcommand the new value
453-
*/
454-
public final void setSubcommand(boolean subcommand) {
455-
this.subcommand = subcommand;
456-
}
457-
458452
/**
459453
* This method is called to get the plugin that owns the command.
460454
* @return The plugin that owns the command.
461455
*/
462456
public final T getPlugin() {
463457
return plugin;
464458
}
459+
460+
/**
461+
* This method is called to generate a default usage for the command.
462+
* @return The default usage of the command.
463+
*/
464+
protected String generateDefaultUsage(CommandSender sender, String label) {
465+
StringBuilder usage = new StringBuilder();
466+
usage.append("/");
467+
Arrays.stream(label.split("\\.")).forEach(s -> usage.append(s).append(" "));
468+
469+
StringBuilder firstArg = new StringBuilder();
470+
this.getSubcommands()
471+
.stream().filter(subCommand -> subCommand.getPermission().isEmpty() || sender.hasPermission(subCommand.getPermission()))
472+
.forEach(subCommand -> firstArg.append(subCommand.getName()).append("|"));
473+
if(firstArg.length() > 0) {
474+
firstArg.deleteCharAt(firstArg.length() - 1);
475+
usage.append("<").append(firstArg).append(">");
476+
}
477+
if((!this.getArgs().isEmpty() || !this.getOptinalArgs().isEmpty()) && firstArg.length() > 0) {
478+
usage.append("|");
479+
}
480+
481+
usage.append(this.getArgs().stream().map(argument -> "<" + argument.arg() + ">").collect(Collectors.joining(" ")));
482+
usage.append(" ");
483+
usage.append(this.getOptinalArgs().stream().map(argument -> "[" + argument.arg() + "]").collect(Collectors.joining(" ")));
484+
return usage.toString();
485+
}
486+
487+
/**
488+
* Set if the command is subcommand
489+
*/
490+
private void setSubcommand() {
491+
this.subcommand = true;
492+
}
465493
}

0 commit comments

Comments
 (0)