ToxicLiquidz101
ToxicLiquidz101

Reputation: 17

Minecraft bukkit plugin right click item

I am making a plugin for a server I am developer on and I was developing a plugin! I wanted to do commands to spawn a boss egg in by doing /boss give lvl <lvl> slime after you did the command it would give you an item that you can right click to spawn the boss in! Well like all new developers stuff doesn't always go the way you think it does. Here's my code I put in for checking if a player right click air or a block with the item SLIME_BALL in the players hand.

@EventHandler
public void onPlayerClicks(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Action action = event.getAction();

     if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
         if (player.getItemInHand().getType() == Material.SLIME_BALL) {
             player.sendMessage("You have right click a slime ball!");
         } 
     }

}

Upvotes: 0

Views: 12269

Answers (2)

DontAskJosh
DontAskJosh

Reputation: 11

Maybe instead of action.equals(), you can use action ==, as in:

if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
     if (player.getItemInHand().getType() == Material.SLIME_BALL) {
         player.sendMessage("You have right click a slime ball!");
     } 
 }

Upvotes: 1

Frelling
Frelling

Reputation: 3507

Given that you are not seeing any stack traces in your logs, I would concur that your event listener is not registered. Let's say your listener class is called MyEventHandler it would be registered in onEnable() method, something similar to this

class MyPlugin extends JavaPlugin {
    ...
    public void onEnable() {
        Listener myEvtHnd = new MyEventHandler();
        Bukkit.getPluginManager().registerEvents( myEvtHnd, this );
        ...
    }
}

In general, your handler looks appropriate. PlayerInteractEvent provides a convenience method getItem() that returns the player's current item-in-hand. However, regardless of which method is used, you must check that the ItemStack returned is not null, which will happen if the player has no item in-hand.

@EventHandler
public void onPlayerClicks(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Action action = event.getAction();
    ItemStack item = event.getItem();

     if ( action.equals( Action.RIGHT_CLICK_AIR ) || action.equals( Action.RIGHT_CLICK_BLOCK ) ) {
         if ( item != null && item.getType() == Material.SLIME_BALL ) {
             player.sendMessage( "You have right click a slime ball!" );
         } 
     }

}

Upvotes: 2

Related Questions