Minecraft Blogs / Tutorial

Creating Bukkit plugins part 3 - Multiple commands + Events

  • 2,589 views, 1 today
  • 3
  • 1
  • 3
gabe4356's Avatar gabe4356
Level 52 : Grandmaster Blob
205
Ok, so in part 3, I will show you how to add more than one command, and add Events. I will show you how I did my thor plugin basicly, I used the event to call down lightning when the player right clicks with the thor hammer (a diamond axe) and they receive a thor hammer when they type /thor. Shall we get started?

So lets continue from where we left off in the last tutorial, where we added the command. Lets add another command
DO NOT EVER EVER add (and I mean ever) another onCommand meathod, just do this:
Code - New Command
package com.gabe.tutorial;

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.AQUA + "Hello Cheese! I mean...World!");     
        
        }
        
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
                player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
            
            } else if (foo()) {
            player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
            }
        
    
        return true;
    }
    
    private boolean foo() {
// TODO Auto-generated method stub
return false;
    }
    
    public void onDisable() {
        //On Disable method stub
    }

    @SuppressWarnings("deprecation")
    public void onEnable() {
       //on Enable Method stub
        {
            
            for (Player player: Bukkit.getServer().getOnlinePlayers()) {
                if (player.hasPermission("hello.world")) {
                    player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
                }
            }

}
    }
}


That is what the code should look like now. All you have to do to add another command is just add this below the previous command and the previous }
Adding another command
        }
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
            player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");

That tests if the player typed the command "Alist" as well as if they typed the command above, if you add another on Comand method, it won't work. And you have to have the '{' bracket below the first command, or else it won't work. it closes the previous '{'

Now that you know how to add more than one command ,lets move on to adding and registering an event.

To add the event, paste this above you onCommand method

Code - Event
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
            player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
            player.damage(5);
            player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
        }
    }

That calls an event of lightning striking down when the player right clicks with a diamond axe, it also plays the sound of an enderdragon getting hit when he uses it. It also damages the player (player.damage(5);) when they use the Diamond axe. the Player player = event.getPlayer(); gets the player who used the diamond axe, then executes the red of the event. Now right now, that will not do anything, we still need to register it in the onCommand method, so now you paste this under the onCommand meathod

Code - Register event
                getServer().getPluginManager().registerEvents(this, this);
Now, there will be an error, to fix the error, he have to implent listener, so at the top, do this: If we do not implent the listener, then the plugin will not know when to register the event, or how to call the event, so now we implent the listener:
Code - implenting listener
 implements Listener
You have to put that right after "public class YourClassNameHere extends java plugin"
So now it should look like this:
Code: Implenting Listener
public class MainTutorial extends JavaPlugin implements Listener {

Final Code:

Final Code
package com.gabe.tutorial;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class MainTutorial extends JavaPlugin implements Listener {
    
    
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
            player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
            player.damage(5);
            player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
        }
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("Hello")){
        player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");     
        
        }
        
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
                player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
        }
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
            player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
            
            } else if (foo()) {
            player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
            }
            
        
    
        return true;
    }
    
    private boolean foo() {
// TODO Auto-generated method stub
return false;
    }
    
    public void onDisable() {
        //On Disable method stub
    }

    @SuppressWarnings("deprecation")
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
       //on Enable Method stub
        {
            
            for (Player player: Bukkit.getServer().getOnlinePlayers()) {
                if (player.hasPermission("hello.world")) {
                    player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
                }
            }

}
    }
}



Now you can have fun changing the event to what-ever, and whatnot. Please do not copy my Thor Plugin and re-post it.

If you have any questions, or suggestions, either PM me, or comment them.
 Click me to go to part 4!
Tags

1 Update Logs

Update #1 : by gabe4356 09/02/2014 3:56:45 pmSep 2nd, 2014

.Made everything a bit more clear

Create an account or sign in to comment.

Polski8350
09/02/2014 3:55 pm
Level 27 : Expert Mountaineer
Polski8350's Avatar
You have to be very broad when talking about plugin creation because as to newcomers most teachers fail to explain why for most things.  Assume you were a young chap and wanted to learn with little to no experience in plugin creation (the majority of minecrafters).  You wouldn't know what an event is and why would you use one and what "implementing listener" is and why we need it.  Good luck though and sorry but how do you reply lol < nvm on my phone I see now
1
Polski8350
09/02/2014 3:45 pm
Level 27 : Expert Mountaineer
Polski8350's Avatar
This could be quite useful, though you should explain each set of codes more clearly so I know everything we're doing and why.  Otherwise all I'm learning is that I can remember how to copy.
1
gabe4356
09/02/2014 3:51 pm
Level 52 : Grandmaster Blob
gabe4356's Avatar
Ok, I tried to make it clear, but I will edit it and try to make it more clear I suppose, what exactly do you think is not clear?
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome