1.8.8 -

Discussion in 'Spigot Plugin Development' started by ZBLL, Jun 12, 2021.

Thread Status:
Not open for further replies.
  1. I've got an enchantment that does something with a 50% chance (bow ench). However, in 10 shots, the enchantment did not happen.
    Code (Java):

    public PoisonEnchantment(int id) {
        super(id);
    }

    @EventHandler
    public void onHit(EntityDamageByEntityEvent e) {
        if (e.getDamager() instanceof Arrow && ((Arrow) e.getDamager()).getShooter() instanceof Player && e.getEntity() instanceof Player && ((Player) e.getEntity()).getItemInHand().getEnchantments().containsKey(Enchantment.getById(UnderscoreEnchants.poisonEnchantment.getId())))
            if (Math.random() < 0.55) ((Player) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 40, 4));
    }

    @Override
    public String getName() {
        return "Poison";
    }

    @Override
    public int getMaxLevel() {
        return 1;
    }

    @Override
    public int getStartLevel() {
        return 1;
    }

    @Override
    public EnchantmentTarget getItemTarget() {
        return EnchantmentTarget.BOW;
    }

    @Override
    public boolean conflictsWith(Enchantment other) {
        return false;
    }

    @Override
    public boolean canEnchantItem(ItemStack item) {
        return true;
    }
     
    The only thing that matters here is the event in eventHandler. Btw, the chance is even 55%. Am I doing something wrong?..
     
  2. Make sure the if statement is being evaluated as true. If it is then the code is fine and you just got unlucky.
     
Thread Status:
Not open for further replies.