Skip to content

EnderSuite/CommandFactory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Forks Stargazers Issues GPLv3 License


Logo

CommandFactory

A better way to handle commands.
Explore the docs »

SpigotMC Page · Report Bug · Request Feature

Banner

Table of Contents



About The Project

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.

Features:

TODO

  • Static Tokens
  • Required Tokens
  • Optional Tokens
  • Permission Support
  • Open Source (GPL-3.0, see License info for further details)



Getting Started

Installation

gradle.build

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}
dependencies {
    implementation 'com.github.EnderSuite:CommandFactory:1.1'
}

pom.xml

<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>

Concepts

Token

A token is a single string inside a full command. Multiple tokens are separated by a whitespace.

Static Token

Static tokens are used to build unique commands.

Example: /myplugin reload

Required Tokens

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>

Optional Tokens

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]

Usage

  1. Create a Class for your own command that extends the Command class.
public class SetMoneyCommand extends Command {
    ...
}
  1. 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. Then addToken() 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 the onNoPermission method to be called, if a player wants to use this command without the right permission(s).

  1. 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);
    }
...
  1. 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);



Roadmap

See the open issues for a list of proposed features (and known issues).



Contributing

If you want to improve the project, feel free to follow the following steps:

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request



Contact

Maximilian Heidenreich - [email protected]

Project Link: https://github.com/EnderSuite/CommandFactory

About

A better way to handle commands.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages