在bukkit 1.8取消饮酒和药水

时间:2017-01-25 14:14:44

标签: java minecraft bukkit

如何检查玩家是否抛出特定药水?我想取消一些在我的项目中使用的特定药水。

据我所知,当玩家试图扔药水时,也没有一个方法调用,也没有当他喝完药水时。

我找到了一个方法,当玩家右键单击一个项目时调用该方法,但我只想在它被喝或被抛出时进行检测。

如何取消我想要的活动?

1 个答案:

答案 0 :(得分:1)

要验证玩家是否消耗了药水,您可以使用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;
           }
       }
    }
}