Bukkit Coding – (4) Commands

Commands are a very important feature in Bukkit. They allow the user of the plugin to communicate with the server. In this section, I’d like to expand off of the idea that we used in the last section. When the user types /hello into the chat, the server will output world into the chat. Let’s start out with the following code.

This is almost the same code from the last section. The only difference is what you’ll be outputting to the console. Now, I’m going to add a method to this code.

You should only really use this method for simple commands. As your plugins get more complex, it’s a better idea to put your commands in separate class files. We’ll learn how to do that in a later tutorial. For now, we’ll just put all of our commands in our main class.

This method is a little bit complicated, but just stick with me. Bukkit will ask you to import CommandSender, and Command. Be sure to import org.bukkit.command rather than org.bukkit.material. Now, I’ll try to explain this method line-by-line.

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ – This is just the start of our method. You notice the method has four parameters. CommandSender sender, Command cmd, String label and String[] args. The first part of each parameter is the type of variable to parameter is. Those are CommandSender, Command, String, and String[]. The second part of each parameter is the name of the variable. Those are sender, cmd, label, and args. Also note that our method starts with public boolean. “Public”, means that other classes can use this method, and “boolean” means that the method is expecting a boolean (true or false) to be returned (We’ll get to this later).

if(cmd.getName().equalsIgnoreCase(“hello”)){ – This is a simple if statement. We’ll get more into these later, but pretty much if the if statement is true, then the code in the body of it will be run. So in this case, the if statement is cmd.getName().equalsIgnoreCase(“hello”). In English, this is pretty much saying: if the command that the player typed is equal to “hello”. Note: You do not need to include the / when typing the command. That’s the basics of this code, but let’s dive in a bit deeper. First, we have cmd. If you look above, cmd is mentioned in the method above as a variable. In fact, cmd is actually the command itself. It is not however, the string of the command. That’s what getName() is for. getName() returns the string of the command. Next, we need to see if that’s equal to something. That’s where equalsIgnoreCase(“hello”) comes in. It’s pretty much returning true, or false depending on whether or not the string inside of equalsIgnoreCase is the same as the command the player typed, without paying attention to the case of the command. That means the player could type “Hello”, “hELLO”, “hello”, etc. and it’d all be treated the same.

sender.sendMessage(“world”); – This is the meat of the code. This is what outputs text to the player. Like in the command above, “sender” is a variable that we defined in the method above. The sender is who typed the command. It could be either the player, or the console. We’ll learn how to determine which of these it is in the future, but for now, we’ll just assume it’s a player. Now, we have “sendMessage(“world”)”. This is what sends the message to the sender. What ever string is put as a parameter will be sent to the player. In our case, the string we’re putting as the parameter, is “world”.

return false; – Remember earlier how I said that our constructor needed to be returned a boolean? This is where it comes into play.

Now that you’ve written that method, you should have THIS code (I’ll be posting longer code on pastebin).

You’re almost done with the new plugin, although you still have to update your plugin.yml by adding the command to it. Adding a simple command to your plugin.yml would look something like this.

IMPORTANT NOTE! Use two or four spaces rather than tabs. Using tabs in yml files will cause errors!

Here’s what my plugin.yml looks like.

That’s pretty much it for creating a simple command! Just export your plugin like before, move it to your server, and start up your server. Then, just login to your server, and type “/hello”. The text “world” should then appear in the chat! Congrats! You’ve made your first command!

Be the first to comment

Leave a Reply

Your email address will not be published.