Register your own command

The API provides a way to register your own commands. This can be done by using the SuperiorSkyblockAPI.registerCommand(<SuperiorCommand>) method. In this tutorial, I will add a new command: /is near. It will return which islands are nearby, and in which direction. First of all, I will create my custom SuperiorCommand class:

public final class NearCommand implements SuperiorCommand {

    @Override
    public List<String> getAliases() {
        // A list of aliases. The first argument will be the label of the subcommand.
        return Arrays.asList("near", "nearby");
    }

    @Override
    public String getPermission() {
        // The required permission for the command. If you don't want a specific permission, use "".
        return "";
    }

    @Override
    public String getUsage(Locale locale) {
        // The usage of the command. Should only include the label & arguments of the command.
        return "near";
    }

    @Override
    public String getDescription(Locale locale) {
        // The description of the command, which will be shown in /is help.
        return "Locate nearby islands.";
    }

    @Override
    public int getMinArgs() {
        // Minimum arguments for the command, including the label.
        return 1;
    }

    @Override
    public int getMaxArgs() {
        // Maximum arguments for the command, including the label.
        return 1;
    }

    @Override
    public boolean canBeExecutedByConsole() {
        // Whether or not the command can be executed from Console.
        return false;
    }

    @Override
    public boolean displayCommand() {
        // Whether or not the command would be displayed in the /is help list.
        return true;
    }

    @Override
    public void execute(SuperiorSkyblock plugin, CommandSender sender, String[] args) {
        // TODO
    }

    @Override
    public List<String> tabComplete(SuperiorSkyblock plugin, CommandSender sender, String[] args) {
        // TODO
        return new ArrayList<>();
    }

}

That's it. The only thing that is left is to execute your code under the execute code, and implement the tab-complete. The execute() and tabComplete() methods contain 3 parameters:

  • plugin: The instance of the plugin.

  • sender: The command sender.

  • args: The arguments from the sender.

After filling my code, the final version of the command is the following:

public final class NearCommand implements SuperiorCommand {
    
    <all the other methods>

    private static final int MAX_ISLAND_SIZE = 200;
    
    @Override
    public void execute(SuperiorSkyblock plugin, CommandSender sender, String[] args) {
        // I know that the sender is a player, as I disabled the console from executing.
        SuperiorPlayer superiorPlayer = SuperiorSkyblockAPI.getPlayer((Player) sender);
        
        Island targetIsland = plugin.getGrid().getIslandAt(superiorPlayer.getLocation());
        
        if(targetIsland == null){
            sender.sendMessage("" + ChatColor.RED + ChatColor.BOLD + "Error | " + ChatColor.GRAY + "You must stand inside an island.");
            return;
        }
        
        StringBuilder message = new StringBuilder();
        int islandsAmount = 0;
        
        for(BlockFace blockFace : BlockFace.values()){
            // I want only to get north, west, east and south - I will just check if their modX is -+1 or their modZ is -+1, but not both.
            int modXAbs = Math.abs(blockFace.getModX()), modZAbs = Math.abs(blockFace.getModZ());
            if((modXAbs == 1 || modZAbs == 1) && modXAbs + modZAbs != 2){
                Location targetIslandLocation = targetIsland.getCenter(World.Environment.NORMAL)
                        .add(MAX_ISLAND_SIZE * 3 * blockFace.getModX(), 0, MAX_ISLAND_SIZE * 3 * blockFace.getModZ());
                if(plugin.getGrid().getIslandAt(targetIslandLocation) != null) {
                    islandsAmount++;
                    message.append(", ").append(blockFace.name());
                }
            }
        }
        
        String directionsMessage = message.length() == 0 ? "" : message.substring(2);
        
        switch (islandsAmount){
            case 0:
                sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "No islands were found.");
                break;
            case 1:
                sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "There is one island at " + directionsMessage + ".");
                break;
            default:
                sender.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "Island | " + ChatColor.GRAY + "There are islands at " + directionsMessage + ".");
                break;
        }
    }

    @Override
    public List<String> tabComplete(SuperiorSkyblock plugin, CommandSender sender, String[] args) {
        // I don't want any tab completes for this command.
        return new ArrayList<>();
    }

}

The last thing to do is to call SuperiorSkyblockAPI.registerCommand(<SuperiorCommand>), and that's it! You registered your own command! This command will be supported in all tab completes, /is help and argument restrictions! You can also register the commands without calling the method above. You can extract the class into an external jar, and put it in the commands folder of SuperiorSkyblock2.

Last updated