在 bukkit 1.8 上取消饮用和发射药水

Cancelling drinking and launching potions on bukkit 1.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.
}


要检查玩家投掷了哪种药水,您可以使用 ProjectileLaunchEvent

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

如果我想取消 PotionThrow:

@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;
           }
       }
    }
}