A better way to handle commands.
Explore the docs »
SpigotMC Page
·
Report Bug
·
Request Feature
Are you tired of endless onCommand methods, with several if, else statements to check all the arguments a player entered?
CommandFactory is a simple library which abstracts onCommand methods and argument parsing.
TODO
- Static Tokens
- Required Tokens
- Optional Tokens
- Permission Support
- Open Source (GPL-3.0, see License info for further details)
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.EnderSuite:CommandFactory:1.1'
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependency>
<groupId>com.github.EnderSuite</groupId>
<artifactId>CommandFactory</artifactId>
<version>1.1</version>
</dependency>
A token is a single string inside a full command. Multiple tokens are separated by a whitespace.
Static tokens are used to build unique commands.
Example: /myplugin reload
When you want to force the user to input some values, you can use a required token. The command will error if no value is specified.
Example: /myplugin echo <message>
When you want to give the user the option to input additional values, you can use an optional token. The command won't error if no values is specified.
Example: /myplugin echo [optionalMessage]
- Create a Class for your own command that extends the
Command
class.
public class SetMoneyCommand extends Command {
...
}
- Configure your command.
...
public SetMoneyCommand(String name) {
super(name);
// Setup tokens.
this.addToken("myplugin", "mp")
.addToken("set")
.addToken("money")
.addToken("<player>")
.addToken("<amount>");
// Setup permissions.
this.addRequiredPermission("myplugin.permission");
}
...
What does this code do?
super(name)
needs to be called to ensure internal logic. ThenaddToken()
is used, to add a token to the CommandPattern. As one can see, it is possible to use static tokens like myplugin, set, money, as well as required (<player>
) and optional tokens ([player]
) depending on your needs. Lastly, there also is a required permission added. This will cause theonNoPermission
method to be called, if a player wants to use this command without the right permission(s).
- Implement your command logic.
Now, that you have a basic framework, you can implement your custom command logic. To do this, there are 3 methods you can override:
onPlayerCall(Player sender, Map<String, CommandArg> args)
onConsoleCall(CommandSender sender, Map<String, CommandArg> args)
onNoPermission(Player sender, String permission)
onPlayerCall()
Gets called when a player enters the command (args are the provided arguments).
onConsoleCall()
Gets called when the console enters the command (args are the provided arguments).
onNoPermission()
Gets called when a player enters the command but does not have all the required permissions (permission is the first one that's missing if multiple are configured).
...
@Override
public void onPlayerCall(Player sender, Map<String, CommandArg> args) {
// Get the data from the args.
String playerName = args.get("<player>").getValue();
double amount = Double.parse(args.get("<amount>").getValue());
// Update the players money.
someAPI.updatePlayerMoeny(playerName, amount);
// Example for an optional argument: /myplugin money get [name]
// Check whether to get own money or other players.
if (args.get("[name]").isGiven()) {
// Get other players money.
}
else {
// Get own money.
}
}
@Override
public void onConsoleCall(CommandSender sender, Map<String, CommandArg> args) {
// Put some logic here...
}
@Override
public void onNoPermission(Player sender, String permission) {
sender.sendMessage("§cYou do not have the permission: §e"+permission);
}
...
- Register your command.
Finally, you just need to register your command inside your main plugin class (the class that extends JavaPlugin).
! It is assumed, that you already registered your command inside plugin.yml
CommandFactory cmdFac = new CommandFactory();
cmdFac.addCommand(new SetMoneyCommand("setMoney"));
this.getCommand("myplugin").setExecutor(cmdFac);
See the open issues for a list of proposed features (and known issues).
If you want to improve the project, feel free to follow the following steps:
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Maximilian Heidenreich - [email protected]
Project Link: https://github.com/EnderSuite/CommandFactory