Skip to content

Hey there! If you like my work, you can support me by sponsoring me on GitHub. Thanks! ❤

Sponsor me
on GitHub

Custom Types

You can create custom context providers for objects like User, Account, Member, Guild, or Party, eliminating the need to first retrieve data from the Player object and then use a service or manager.

Boring theory

To do this, implement the ContextProvider<SENDER, T> interface and register it.

  • SENDER is a platform-specific sender type. (see What is SENDER?)
  • T is the context type you want to provide.

Practical example with Account

Imagine you have an Account class with player data like balance, level, and experience. With a context provider you can access the Account directly in command methods.

In this example:

SENDER is platform-specific sender type CommandSender
T is the context type Account

java
// AccountContextProvider.java
public class AccountContextProvider implements ContextProvider<CommandSender, Account> {

    private final AccountManager accountManager;

    public AccountContext(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    @Override
    public ContextResult<Account> provide(Invocation<CommandSender> invocation) {
        if (!(invocation.sender() instanceof Player player)) {
            return ContextResult.error("&cOnly players can use this command!");
        }
        
        return ContextResult.ok(() -> {
            Account account = this.accountManager.getAccount(player.getName());
            if (account == null) {
                throw new IllegalStateException("&cAccount " + player.getName() + " not found");
            }

            return account;
        });
    }
}

Now we can create a BalanceCommand that access the sender's Account:

java
// BalanceCommand.java
@Command(name = "balance")
public class BalanceCommand {
    @Execute
    void balance(
            @Context CommandSender sender,
            @Context Account account
    ) {
        double balance = account.getBalance();
        sender.sendMessage("Your current balance is: " + balance);
    }
}

And finally, we can register the custom context provider in the LiteCommands builder:

java
.commands(
    new AccountCommand()
)
.context(Account.class, new AccountContextProvider(accountManager))
.build();
Run the command /balance
Input
/balance
Output
Your current balance is: 100.0

This example shows how custom context providers in LiteCommands simplify command logic and improve code readability by providing direct access to relevant objects.

Check out real examples!

This code is a simplified version of the code from the EternalEconomy Plugin.
See the full code of the EternalEconomy Plugin on GitHub.

Made by LiteDevelopers and Contributors with ❤️

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.