Minecraft Blogs / Tutorial

Creating Bukkit Plugins part 2 - Commands + Permissions

  • 674 views, 3 today
  • 2
  • 0
gabe4356's Avatar gabe4356
Level 52 : Grandmaster Blob
205
Hey guys, in the last tutorial, I showed you how to setup the basic plugin layout. In this one, I will show you how to actually make it do something. We will be adding commands to your plugin, and the permission the player needs to run the command successfully.

So, now those imports will be usefull. Just Paste this:

Code - Command Sender
package com.gabe;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class MainTutorial extends JavaPlugin {
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("Hello")){
        player.sendMessage(ChatColor.LIGHT_PURPLE+ "Hello Cheese...I mean World!");
        
        }
        return true;
    }

public void onDisable() {
    //On Disable method stub
}

public void onEnable() {
   //on Enable Method stub
    {
    
}
}
}

So, the public boolean declares that this will be true or false (boolean) and then we will create onCommand(Command sender, Command So, when the player types the command, it will say something to that player like this:
if(cmd.getname().equalsIgnoreCase("Hello)){
player.sendMessage(ChatColor.LightPurple+ "Hello Cheese...I mean World!");
So, the ChatColor.LightPurlle Decalres that the message will said in purple, then +The message that will be sent to the player.
Then we return true, what this does, is makes it so that when you type the command in minecraft, it doesn't say "/Hello" after the command is typed.

Now we need to add this to the plugins.yml file. Open the plugin.yml file.and add this:
Code - plugin.yml
commands:
  Hello:
    description: Hello World!
    usage: /Hello
that makes it so that when you type /help <name of your plugin> It wil say "Hello - Hello World!" So that the player kwows what the command does and it also registers the command.
 Now, you might want to add a custom permission node to the plugin, so that only players with that permission can run the command.  So add this underneath the "usage: /Hello"
Code - plugin.yml
    permission: hello.hello
    permission-message: You can not say hello!
So not the plugin.yml should  look something like this:
plugin.yml
name: Tutorial
main: com.gabe.MainTutorial
version: 1.0
commands:
  Hello:
    description: says Hello World!
    usage: /Hello
    permission: hello.hello
    permission-message: You can not say hello!
the "permission" decalres what permission the player will need in-order to run the command.
the "permission-message" is what the plugin should say if the player does not have permission to run the command.

And the MainTutorial Class should look like this:

Code - Tutorial
package com.gabe;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class MainTutorial extends JavaPlugin {
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("Hello")){
        player.sendMessage(ChatColor.LIGHT_PURPLE+ "Hello Cheese...I mean World!");
        
        }
        return true;
    }

public void onDisable() {
    //On Disable method stub
}

public void onEnable() {
   //on Enable Method stub
    {
    
}
}
}


Now, if you want, you can make it let the player know if they have the certain hello.hello pernission. By adding this to the code:
Code - Main TUtoiral
        for (Player player: Bukkit.getServer().getOnlinePlayers()) {
            if (player.hasPermission("hello.hello")) {
                player.sendMessage(ChatColor.AQUA + "You can say hello! /hello");
            }
        }


Put it underneath the onEnable method.

Now, you can export your plugin to test it by doing this:
Right click on the Java Project "Tutorial"
Export
Click "Jar File"
De-select .classpath and .project on the right.
Click the "Browse" button, and save it where ever you want, then name it something (anything) Then click "Finish"
Now to Test it, you just create a local-host server (Doesn't require good internet) Just copy the "Craft Bukkit" file you downloaded earlier and paste it in a new folder, run it, and eula isn't neccisary for a local-host server, as no one can join. So just put that to true in eula.txt, then in server.properties, just change the server-ip: to localhost and then join "localhost" in minecraft, after dragging the plugin into the plugins folder, and it should work!



Well, I hope this was helpful!

Part 3: Click me
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome