代码之家  ›  专栏  ›  技术社区  ›  user7353558

取消饮酒并在bukkit 1.8上推出药水

  •  2
  • user7353558  · 技术社区  · 8 年前

    我如何检查玩家是否投掷或饮用特定药剂?我想取消一些在我的项目中使用的特定药剂。

    据我所知,当玩家尝试投掷药剂时,或当他喝完药剂时,都没有方法调用。

    我已经找到了一个方法,当玩家右键单击某个项目时调用该方法,但我只想检测它是被喝了还是被扔了。

    如何取消我想要的活动?

    1 回复  |  直到 8 年前
        1
  •  2
  •   LeoColman    8 年前

    要验证玩家是否使用了药剂,您可以使用 PlayerItemConsume event :

    @EventHandler
    public void onItemConsume (PlayerItemConsumeEvent e) {
        ItemStack consumed = e.getItem();
        //Make your checks if this is the Potion you want to cancel
    
        if (/*conditions*/) e.setCancelled(true);    //Will cancel the potion drink, not applying the effects nor using the item.
    }
    


    要检查玩家投掷了哪种药剂,可以使用ProjectleLaunchEvent

    @EventHandler
    public void onProjectileLaunch(ProjectileLaunchEvent e) {
        Projectile projectile = e.getEntity();
        //Make the checks to know if this is the potion you want to cancel
        if (/*conditions*/) e.setCancelled(true);   //Cancels the potion launching
    }
    

    ----------

    例如,如果我想取消健康药水的饮用操作:

    @EventHandler
    public void onItemConsume (PlayerItemConsumeEvent e) {
        ItemStack consumed = e.getItem();
        if (consumed.getType.equals(Material.POTION) {
            //It's a potion
            Potion potion = Potion.fromItemStack(consumed);
            PotionType type = potion.getType();
            if (type.equals(PotionType.INSTANT_HEAL) e.setCancelled(true);
        }
    }
    

    如果我想取消魔药投掷:

    @EventHandler
    public void onProjectileLaunch(ProjectileLaunchEvent e) {
        Projectile projectile = e.getEntity();
    
        if (projectile instanceof ThrownPotion) {
           //It's a Potion
           ThrownPotion pot = (ThrownPotion) projectile;
           Collection<PotionEffect> effects = pot.getEffects();
           for (PotionEffect p : effects) {
               if (p.getType().equals(PotionEffectType.INSTANT_HEAL)){
                   e.setCancelled(true);
                   break;
               }
           }
        }
    }