Skip to content

Commands

vansencool edited this page Oct 25, 2024 · 5 revisions

CommandBuilder

Here's a basic example of how you can make a command using CommandBuilder:

package com.example.myplugin;

import com.maximde.pluginsimplifier.PluginHolder;
import com.maximde.pluginsimplifier.command.CommandBuilder;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;

public final class MyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        PluginHolder.set(this);

        CommandBuilder.create("cool")
                .executor(context -> {
                    context.checkConsole("Only players can use this command!");
                    if (context.args().length == 0) {
                        context.sender().sendMessage("Usage: /cool <arg>");
                    }
                    switch (context.arg(0)) {
                        case "limit" -> context.player().sendMessage("The limit of coolness is 10!");
                        case "current" ->
                                context.player().sendMessage("Your coolness is " + new Coolness(context.player()).get());
                        case "add" -> new Coolness(context.player()).add(1);
                        case "remove" -> new Coolness(context.player()).remove(1);
                        default -> context.player().sendMessage("Usage: /cool <arg>");
                    }
                })
                .complete(context -> {
                    if (context.args().length == 1) {
                        return Arrays.asList("limit", "current", "add", "remove");
                    }
                    return null;
                })
                .register();
    }

    public record Coolness(Player player) {
        private static int coolness = 0;

        public void add(int amount) {
            coolness += amount;
            player.sendMessage("Coolness increased by " + amount + "!");
        }

        public void remove(int amount) {
            coolness -= amount;
            player.sendMessage("Coolness decreased by " + amount + "!");
        }
        
        public int get() {
            return coolness;
        }
    }
}

And here's a example with rest of the features:

package com.example.myplugin;

import com.maximde.pluginsimplifier.PluginHolder;
import com.maximde.pluginsimplifier.command.CommandBuilder;
import com.maximde.pluginsimplifier.command.CommandExecutionException;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public final class MyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        PluginHolder.set(this);

        CommandBuilder.create("flags")
                .executor(context -> {
                    context.checkPlayer("Only console can use this command!");
                    if (context.args().length < 1) {
                        throw new CommandExecutionException("Usage: /flags <flag>");
                    }
                    if (context.hasFlag("-cool")) { // if the command is "/flags -cool", the flag can be anywhere in the args
                        context.sender().sendMessage("Cool!");
                    } else {
                        context.sender().sendMessage("Not cool!");
                    }
                    if (context.hasFlag("-bad")) {
                        throw new CommandExecutionException("Bad!"); // sends that message to the sender
                    }
                })
                .aliases("f")
                .register();

        CommandBuilder.create("asynccompleter")
                // .executor(context -> {}), no executor
                .asyncComplete(context -> CompletableFuture.supplyAsync(() -> List.of("test1", "test2"))) // if done without completable future, it will not work
                .register();

        CommandBuilder.create("test")
                .executor(context -> context.sender().sendMessage("test"))
                .description("test") // if done /help test, it will show description as "test"
                .usage("/test") // if done "/help test", it will show usage "/test"
                .namespace("test") // registers the command to the "test" namespace, so "/test:test" will work, defaults to the plugin's name if not set
                .register();
    }
}

SimplifierCommand, SimplifierComplete

CommandBuilder seems hard? No worries!

SimplifierCommand and SimplifierComplete is a interface you can implement in your class, Which provides easy-to-use methods!

package com.example.myplugin.commands;

import com.maximde.pluginsimplifier.command.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.List;

public class TeleportCommand implements SimplifierCommand, SimplifierComplete {

    @Override
    @SuppressWarnings("all")
    public void execute(CommandContext context) {
        context.checkConsole("This command can only be run by players.");
        Player player = Bukkit.getPlayer(context.arg(0));
        if (player == null) throw new CommandExecutionException("Player " + context.arg(0) + " not found!");
        player.teleport(context.player());
        context.sender().sendMessage("Teleported " + context.arg(0) + " to you!");
    }

    @Override
    public List<String> complete(CommandContext context) {
        if (context.args().length == 1) {
            return Bukkit.getOnlinePlayers().parallelStream()
                    .map(Player::getName)
                    .filter(name -> !name.equals(context.player().getName()))
                    .toList();
        }
        return null;
    }

    public static void register() {
        CommandBuilder.create("teleport")
                .aliases("tp")
                .description("Teleport Command")
                .permission("teleport.use")
                .executor(new TeleportCommand())
                .complete(new TeleportCommand())
                .namespace("teleporter")
                .register();
    }
}

Now, To register the command itself, go to your onEnable, and type "TeleportCommand.register();', and that's it!

Note:

  • The "register" void is optimal to have, you can do the same in any method/class.

Clone this wiki locally