Commands - JustKato/BukkitWiki GitHub Wiki
Commands are the fundamental basis for which plugins typically work. They are a slash followed by a segment of text that upon entering into the chat execute some code.
Ex: /murder Illuminatiiiiii
There are two methods to creating commands, distinguished upon the location of the command's code. The first is the command "executor" being within the main plugin class, and the second being within a separate class. Let's explore creating commands below.
We will start by creating the executor for the command which is in other words the block of code that will run when the command is executed.
The command can be represented with the onCommand method. This will go after the onEnable and onDisable methods.
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
//code in here
return true;
}There are only three important arguments for this method. Sender, command, and args. Sender represents the person who ran the command and args will contain and words that were added after the command. With command we can get the actual command that was run.
Now that we have the method for commands, we need to check to see what command was run before we can do anything further. If we don't check, then this code will be run if any code is executed. Do this:
if(command.getName().equalsIgnoreCase("die") && sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.BLACK + "DIE");
player.setHealth(0);
}else if(command.getName().equalsIgnoreCase("die") && !(sender instanceof Player)){
System.out.println("You need to be a player to execute this command, dummy.");
}All of this code may be overwhelming if it's your first time working with the API, but I'll break it down for you. Always read code line-by-line.
- Check to see if the command they executed was /die and if a player executed the command
- If line 1 is true, we create an object of Player from the sender since we know sender is now a player, not the console.
- We send the player a message in black telling them to die.
- We set the player's health to 0
OR
- Check to see if the command they executed was /die and if a player did not execute the command
- Output to the console letting the server know only a player can run this command
That's it! The reason we check to see if a player executed a command and not the console is because it does not make sense to kill a console. It's common practice in most plugins.
//Second command
//gives player some health
if(command.getName().equalsIgnoreCase("healthplease") && sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.DARK_PURPLE + "There u go");
player.setHealth(player.getHealth() + 1);
}else if(command.getName().equalsIgnoreCase("die") && !(sender instanceof Player)){
System.out.println("You need to be a player to execute this command, dummy.");
}Very similar to the last command, except we are giving health.
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
//First command
if(command.getName().equalsIgnoreCase("die") && sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.BLACK + "DIE");
player.setHealth(0);
}else if(command.getName().equalsIgnoreCase("die") && !(sender instanceof Player)){
System.out.println("You need to be a player to execute this command, dummy.");
}
//Second command
if(command.getName().equalsIgnoreCase("healthplease") && sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.DARK_PURPLE + "There u go");
player.setHealth(player.getHealth() + 1);
}else if(command.getName().equalsIgnoreCase("die") && !(sender instanceof Player)){
System.out.println("You need to be a player to execute this command, dummy.");
}
return true;
}Now that the code is created for the commands, they need to be registered so the plugin knows they exist and need to listen for them.
You don't need to for commands that are inside the main plugin class! <3
This file will be auto generated assuming you use the Minecraft plugin for IntelliJ. It uses YAML format so spacing and syntax is important. You will get the hang of it, just be careful.
name: SpigotSandboxProject
version: ${project.version}
main: me.illuminatiproductions.spigotsandboxproject.SpigotSandboxProject
api-version: 1.13
description: Plugin for testing and such
commands:
die:
description: Die fool.
usage: /<command>
healthplease:
description: Get some health
usage: /<command>Ignore the red if you see it. Anything indented is one tab length forward. That's important to maintain. Anyhow, this is my example plugin.yml, but the commands section is important. Each command is added with a description and usage. For this to work, you can usually have at least the command name and description. I don't usually add the usage. After you add this, you are done! The command will be usable in game.
Create a new class for your command. It's recommended to be located in a commands package for structure.
Insure that the class implements CommandExecutor so that it can get the onCommand method. Add that method. You should end up with this:
public class God implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
//code here
return true;
}
}Return must be true or the command will be repeated back to you in the game upon execution.
This time, you don't need to check and see which command was executed because there is a whole class that represents this whole command. We do still follow the practice of checking to see if a Player ran the command.
public class God implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player){
Player player = (Player) sender;
player.sendMessage("WOOSH");
player.setInvulnerable(true);
}
return true;
}
}A little different for this method.
We need to tell the plugin where to look for the command executor when a command is run!
@Override
public void onEnable() {
// Plugin startup logic
System.out.println("The plugin has started UP.");
//Register command
getCommand("god").setExecutor(new God());
}With the getCommand method, we set the command to be "listened" for and then with setExecutor, where to find the code for that command.
Same exact thing but with a different command
name: SpigotSandboxProject
version: ${project.version}
main: me.illuminatiproductions.spigotsandboxproject.SpigotSandboxProject
api-version: 1.13
description: Plugin for testing and such
commands:
god:
description: Become invincibleThat's it! Commands are really simply once you master these two methods of implementing them. The more you practice, the easier it will become. I suggest using a separate class for commands, but I showed both ways in case you come across it in the wild.