Minecraft Blogs / Tutorial

Bukkit tutorial #2 - Commands and Permissions!

  • 10,931 views, 0 today
  • 8
  • 5
  • 4
superpeanut911's Avatar superpeanut911
Level 54 : Grandmaster Network
Remember to diamond, favorite, and subscribe for more! <3

this is for 1.3.2, it should still work for 1.4 though, but there are no 1.4 bukkit builds out yet soz...

Welcome to Tutorial #2 of my Java/Bukkit tutorials!

If you want to learn how to start a plugin, as this tutorial will not show you how to begin a plugin and such, please visit tutorial #1 - http://www.planetminecraft.com/blog/tut-1---how-to-start-a-bukkit-plugin-handling-events/

As we learned in tutorial number one, how to handle events, we will now be doing commands and permission nodes! We will make a few commands in this tutorial, let's start off with a /lightning command, that will strike lightning on the block you are looking at, We will do a /pmc command, that will send a player a message, and we will be doing a plugin reloading command - Which we will do with multiple arguments! (2 part command: EX: /we reload)

Adding a command!

We will now start adding a command to the plugin we started in tutorial one! (http://www.planetminecraft.com/blog/tut-1---how-to-start-a-bukkit-plugin-handling-events/)

All of these tutorials will directly relate so remember to keep up! :)

As it is always a great idea to keep all your classes separate, such as another Command class and an Event class, we will keep them all in the main class to make things abit easier to follow!

A command is a boolean, a boolean is value in java that will always return True, or false.

When a command is executed, it returns true or false to the server. Before you start the actual command, you need to implement CommandExecutor to your main class! Because we already implemented Listener in our class last tutorial, we can just add a coma after listener, and write CommandExecutor after it! Yes, this is a messy coding habit, I will cover moving everything to different classes in later tutorials, it just removes some hastle of doing other stuff if it's all kept in the main class! So... This is how your top class defining line should look like! (READ TUT #1!! This needs the plugin we made there!!! : http://www.planetminecraft.com/blog/tut-1---how-to-start-a-bukkit-plugin-handling-events/)


Where it says "TutMain", that's where your class name goes, I named mine TutMain, I would suggest having named yours the same as that may come up abit in the code!

Now, to add the command! We will go to under the EventHandler, and we will define our command! We will do it with this snipit of code:


public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

This here serves no purpose to us really, it is needed to make a command though :P Do not delete any of it, even if Eclipse tells you to, if it tells you to, you went wrong somewhere!! This will be our lightning command! We will just use /l to not conflict with essentials or anything!
The command is added with an if statement! : if (cmd.getName().equalsIgnoreCase("l")) {

This will get the command name, which is "l" and uppercase or lowercase will not matter!

An variable is now needed, we will use "player" just like we did with the above EventHandler

Player player = (Player)sender;

That piece of code assigns the player entity, to "player" and it means the sender of the command, which is cast to a player! The Player cast avoids Null Pointer Exceptions! Now we can get to permission nodes for this command!

We will add this line right under the variable:


This simply asks, Does this player have the permission node pmc.lightning? If the player does not have it, the plugin will just stop the command execution right there and nothing will happen! This is so easy thanks to bukkits new permissions system! This way here will work with just about all permission handlers! (I recommend PEX)

A final thing needed to our command... It needs to do something! This is our lightning command, So we will get the target block the player is looking at, make sure its within 200 blocks of the player, and strike lightning on that position! Also send the player a message! To do this:

Click to reveal

player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation()); //Gets the block in sight, won't work if block is more than 200 blocks away!
player.sendMessage(ChatColor.YELLOW + "[PMCTutorial]" + ChatColor.GREEN + " You control the power of the lightning!!");

Gets the player, the world, strikes lightning on the target block! You see the //Gets the...

That is a comment! It does nothing to your actual code, you can just write little notes to yourself! At the end of this tutorial, you can see my code, which I filled with tons of comments for you guys to follow on what it says!

Okay, that is it for the lightning code! All we have to do to it to work is the define the permission node and the command in the plugin.yml, we will do this later!

Now to add a second command, this is very easy!

We will just simply use an else statement! We will now just do the easy /pmctut reload command which will reload the plugin! This also has 2 arguements so things may get alittle different here!

Click to reveal

} else {
if (cmd.getName().equalsIgnoreCase("pmctut")) {
if (args.length == 1) {
if (args[0].equalsIgnoreCase("reload")) {

This is the same way of adding commands, just has else at the top! So this also checks amount of arguments! There is one argument here, so we put 1 there! Also the second arg will be "reload"

Again, lets use the variable "player", the same as above, we will cast Player to sender!

Under the cast, the permission node line, same as above: if(player.hasPermission("pmc.reload"));

The permission node here is, pmc.reload

The reload will have a few variables, I have made lots of comments all over it so you can follow:
Click to reveal

PluginManager plg = Bukkit.getPluginManager(); //Assigns plg to Bukkit.getPluginManager()
Plugin plgname = plg.getPlugin("PMCTutorial"); //What you named the plugin in plugin.yml goes here! Assigns plgname to your plugin name!
plg.disablePlugin(plgname); //Disables your plugin!
plg.enablePlugin(plgname); //Enables your plugin!
player.sendMessage(ChatColor.YELLOW + "[PMCTutorial]" + ChatColor.GREEN + ChatColor.BOLD + " Plugin Reloaded!");

Variables, plg, plgname! We use the plugin manager, get the plugin name, disable the plugin, than enable the plugin again! After all of this is done, it shall send a message to the player saying Plugin Reloaded! Yay!

Now for the final, /pmc command, we will simply send a message to the player, just for extra practice. Lets see if you can do this without looking at the below full code picture or this spoiler with all the correct code: (Try adding bold chat too :))

Click to reveal

} else {
if (cmd.getName().equalsIgnoreCase("pmc")) {
Player player = (Player)sender;
player.sendMessage(ChatColor.YELLOW + "[PMCTutorial]" + ChatColor.GREEN + ChatColor.BOLD + " PlanetMinecraft! Do /pmctut reload to reload this plugin!");

Now to add the return statements!

This will have 2 return true statements, the top wottp://uld probably be underlined red, it will be a quick fix, just make sure you change the false to true!

Plugin.yml essentials!

Okay! Lets do the defining the commands and permissions in your plugin.yml file! This has alot of specific formating so I'm just going to give you a copy/paste link of mine, you can figure out what all that stuff there stands for :)


Create a new .txt document, rename it to a .yml, paste that in, save it, drag it to the plugins jar file OR compile it with the .yml included!

Now you can export your plugin and test it!

Here is the finished code: (Alot is cut off, most important info is here!)





JakoLanterenHead STEVE!

Plugin that does this: http://www.planetminecraft.com/mod/blockhats-bukkit/

1 Update Logs

Update #1 : by superpeanut911 10/25/2012 7:13:30 pmOct 25th, 2012

Should still work for 1.4... Waiting on an actual bukkit release to test...

Create an account or sign in to comment.

09/09/2014 1:47 pm
Level 1 : New Miner
wizardsambolton2003's Avatar
hey i am trying to make a plugin tht can clear peoples xp how would i do that?
04/09/2014 4:44 pm
Level 21 : Expert Geek
logical23's Avatar
More please! :D
09/08/2013 9:43 pm
Level 1 : New Crafter
1poseidon3's Avatar
We can haz more tutorial?
10/25/2012 3:57 am
Level 61 : High Grandmaster Programmer
ice374's Avatar
working on reading this right now, thanks for doing a 2nd tut
Planet Minecraft


© 2010 - 2024