'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!");
}
}
}
Solution 1:[1]
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!" );
}
}
}
Solution 2:[2]
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!");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frelling |
Solution 2 | Chava Geldzahler |