Blogs Tutorial

Plugin Making Tutorial [Part 2]

  • 297 views 0 today
  • 2
  • 0
  • 2
Ghost001
Lvl 10Journeyman Explorer
4
Hey guys, part 2 of my plugin tutorial! If you haven't already, check out part 1:http://www.planetminecraft.com/blog/plugin-making-tutorial-basics-part-1/

Today, we should make a command! Let's get started! First, add this to your code:

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

There should be a red line under it, this is because it needs a return value. You see the word "boolean" right? That means true or false, so we need a return value of either true or false. We'll get into that later.

Next, we need to add a command. We need to do it inside those brackets, anything inside those brackets is read with that, is the best way I can explain it, I guess. So, inside those brackets add this:


if(cmd.getName().equalsIgnoreCase("tutorial")) {

}


Your entire class should look like this:


package me.btw001.Tutorial;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

public class Tutorial extends JavaPlugin {

public void onEnable() {

}

public void onDisable() {

}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {

}
}

}


Now, lets get into what this means, alright? "equalsIgnoreCase()" means the user can do the command in caps or no caps. So I can type the command like "THIS" or "ThiS" or "this". "("tutorial")" means anything in those parentheses and quotation marks are a command. So, "if(cmd.getName().equalsIgnoreCase("tutorial"))" means that if a user types "/tutorial" in caps lock or not it will do what is in those brackets.

Let's add a permission, shall we? Inside those brackets for the command, you wanna put

"if(sender.hasPermission("tutorial.tutorial")) {
}"

Your entire class should look like this:


package me.btw001.Tutorial;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

public class Tutorial extends JavaPlugin {

public void onEnable() {

}

public void onDisable() {

}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {
if(sender.hasPermission("tutorial.tutorial")) {

}

}
}

}


Now, I'm about to explain how the command works. If a user types in "/tutorial" in caps lock or not to the chat it will run anything within the brackets after the command. So it sees "If(sender.hasPermission("tutorial.tutorial")) {
}". That means if the command sender has the permission node "tutorial.tutorial" and it will to anything in those brackets. So, lets add a message! Add this inside the brackets for your permission:

sender.sendMessage("Banana's are sexy!");

The whole command should look like this:


public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {
if(sender.hasPermission("tutorial.tutorial")) {
sender.sendMessage("Banana's are sexy!");

}

}
}


And if your wondering, you can replace "Banana's are sexy!" with anything you wish. White is a dull color though, I don't like it, so let's change it! Do something like this:

sender.sendMessage(ChatColor.YELLOW + "Banana's are sexy!");

The whole command should look like this:


public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {
if(sender.hasPermission("tutorial.tutorial")) {
sender.sendMessage(ChatColor.YELLOW + "Banana's are sexy!");

}

}
}


Though, what if we want users that don't have permission to see another message to know they don't have permission? Add "else sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to see this other sexy message!")". You want the entire command to look like this:


public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {
if(sender.hasPermission("tutorial.tutorial")) {
sender.sendMessage(ChatColor.YELLOW + "Banana's are sexy!");

} else sender.sendMessage(ChatColor.DARK_RED + "You don't get to see this other message!");

}
}


What other message though? Let's add a command that people with and without the permission node can see. Add this under the brackets for what happens if the user has the permission "tutorial.tutorial"
sender.

sender.sendMessage(ChatColor.GREEN + "This is a command for Ghost's plugin tutorial!");

The plugin would send the messages out of order though. If you have permission for the command this would pop up in your chat box:

Banana's are sexy!
This is a command for Ghost's plugin tutorial!

Let's see what it would look like if you don't have permission:

You don't get to see this other message!
This is a command for Ghost's plugin tutorial!

Seems out of order right? So lets move the "This is a command for Ghost's plugin tutorial!" message to ABOVE the permission code. The command should look like this:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("tutorial")) {
sender.sendMessage(ChatColor.GREEN + "This is a command for Ghost's plugin tutorial!");
if(sender.hasPermission("tutorial.tutorial")) {
sender.sendMessage(ChatColor.YELLOW + "Banana's are sexy!");

} else sender.sendMessage(ChatColor.DARK_RED + "You don't get to see this other message!");
}

}

This is how it will look when a user has the permission:

This is a command for Ghost's plugin tutorial!
Banana's are sexy!

For users without permission:

This is a command for Ghost's plugin tutorial!
You don't get to see this other message!

Well, that's all for today. Next tutorial I'll teach you how to install this plugin onto a server!

More like this

  Have something to say?

evilguy4000
10/21/2013 1:35 am
Level 33 : Artisan Modder
I really need to say that your tutorials are kinda hard to follow and for beginners this code still looks like chinese.
1
Ghost001
10/21/2013 7:43 pm
Level 10 : Journeyman Explorer
:/
1

Welcome