Minecraft bukkit插件右键单击项目

时间:2017-02-10 02:25:22

标签: java plugins minecraft bukkit

我正在为我开发的服务器制作一个插件,我正在开发一个插件! 我想做命令,通过执行/boss give lvl <lvl> slime来产生一个老板蛋,在你执行命令后它会给你一个项目,你可以右击它来产生老板!就像所有新开发者一样,东西并不像你想象的那样。这是我输入的代码,用于检查玩家是否右击空气或玩家手中的物品SLIME_BALL。

@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!");
         } 
     }

}

2 个答案:

答案 0 :(得分:3)

鉴于您没有在日志中看到任何堆栈跟踪,我会同意您的事件监听器未注册。假设您的侦听器类名为MyEventHandler,它将在onEnable()方法中注册,类似于此

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

通常,您的处理程序看起来合适。 PlayerInteractEvent提供了一种便捷方法getItem(),可以返回玩家当前的项目。但是,无论使用哪种方法,必须检查返回的ItemStack是否为空,如果玩家手中没有项目,则会发生这种情况。

@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!" );
         } 
     }

}

答案 1 :(得分:1)

也许代替action.equals(),您可以使用action ==,如:

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!");
     } 
 }