Darrionat Plugins
  • Welcome
  • Plugins
    • Bans+
      • Commands & Permissions
      • Configuration Files
      • GUIs
      • MySQL
      • Trivia
    • Command Cooldown
      • Commands & Permissions
      • Configuration Files
        • cooldowns.yml
        • config.yml
        • messages.yml
      • MySQL
      • Trivia
    • Custom Enchants+
      • API
      • Commands & Permissions
      • Configuration Files
        • config.yml
        • enchants.yml
        • lores.yml
        • messages.yml
      • Custom Blocks
      • Dependencies
      • Enchantments
      • GUIs
      • Scrolls
      • Wands
      • Trivia
    • PrisonPick
      • Autosell
      • Commands & Permissions
      • Configuration Files
        • autosell.yml
        • config.yml
        • enchants.yml
        • messages.yml
      • Dependencies
      • Enchantments
      • GUIs
      • Inventory Management
      • Placeholders
      • Saving Data
  • Libraries
    • PluginLib
      • Creating Your Plugin
      • Commands
      • Configs
      • ErrorHandler
      • Gui
  • Discord Bot
    • SpigotMC Bot
      • Commands
  • Links
    • Discord
    • GitHub
    • SpigotMC
  • Support Me
    • Patreon
Powered by GitBook
On this page
  • Creating a Command
  • Create a BaseCommand
  • SubCommands

Was this helpful?

  1. Libraries
  2. PluginLib

Commands

PluginLib offers a simple way to create your commands and handles a few errors in the process

Creating a Command

To create a command within your plugin, you can do the following

Add the command to your plugin.yml

name: FakePlugin
author: Darrionat
main: me.darrionat.FakePlugin
description: A fake plugin with commands
version: 1.0.0
api-version: 1.13

commands:
  ban:
    usage: /<command>

Create a BaseCommand

package me.darrionat.fakeplugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import me.darrionat.pluginlib.Plugin;
import me.darrionat.pluginlib.commands.BaseCommand;

public class FakeCommand extends BaseCommand {

	public FakeCommand(Plugin plugin) {
		super(plugin);
	}

	@Override
	public String getCommandLabel() {
		return "ban";
	}

	@Override
	protected void runNoArgs(CommandSender sender, Command command, String label, String[] args) {
		/*
		 * Do stuff
		 */
	}
}

Since the class above has no subcommands defined, it will always simply just run the runNoArgsmethod.

SubCommands

A SubCommand is defined as a command that uses the same label as its parent BaseCommand but it has a different first argument.

For example, your plugin has the base command /ban. You can add subcommands such as /ban list or /ban player.

Adding a SubCommand to your BaseCommand

To add a SubCommand to a BaseCommand, you can use BaseCommand#addSubCommand(SubCommand).

public FakeCommand(Plugin plugin) {
		super(plugin);
		addSubCommand(new FakeSubCommand(this, plugin));
}

Example of a SubCommand Class

package me.darrionat.fakeplugin.commands;

import org.bukkit.command.CommandSender;

import me.darrionat.pluginlib.Plugin;
import me.darrionat.pluginlib.commands.BaseCommand;
import me.darrionat.pluginlib.commands.SubCommand;

public class FakeSubCommand extends SubCommand {

	public FakeSubCommand(BaseCommand parentCommand, Plugin plugin) {
		super(parentCommand, plugin);
	}
	
	@Override
	public String getSubCommand() {
		// /ban list
		return "list";
	}

	@Override
	public int getRequiredArgs() {
		/*
		 * /ban list <- one argument
		 * This should always be greater than or equal to 1
		 */
		return 1;
	}

	@Override
	public boolean onlyPlayers() {
		return false;
	}

	@Override
	protected void runCommand(CommandSender sender, String[] args) {
		/*
		 * Do stuff
		 */
	}
}
PreviousCreating Your PluginNextConfigs

Last updated 8 months ago

Was this helpful?