addPotionEffect(); doesn't work

Discussion in 'Plugin Development' started by Higgsboson728, Nov 2, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Higgsboson728

    Title says it all. My code:

    Code:
    @EventHandler   
    public void onPlayerUse(PlayerInteractEvent event) {
        Player p = event.getPlayer();
       
        if (p.getItemInHand().getType() == Material.SUGAR) {
            if (p.getItemInHand().getAmount() > 1) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 1));
                p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
            } else {
                p.getItemInHand().setAmount(0);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
            }
        } else if (p.getItemInHand().getType() == Material.BLAZE_POWDER) {
            if (p.getItemInHand().getAmount() > 1) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                     p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 5, 1));
                     p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 18000, 3));
            } else {
                p.getItemInHand().setAmount(0);
                     p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 5, 1));
                     p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 18000, 3));
            }
        }
       
    }

    I right click sugar, and it disappears but I don't get the potion effects. Can anyone tell me what the problem is? :D
     
    DoggyCode™ likes this.
  2. Offline

    Scimiguy

    If it disappears, that means you're only holding one sugar.
    According to your code, you want to be holding more than one for the effect to work
     
  3. Offline

    DoggyCode™

    Lol. Face palm for OP
     
  4. Offline

    Higgsboson728

    @Scimiguy

    I tested it with a stack of sugar, and it the potion effect didn't take place.

    New code:

    Code:
    @EventHandler  
    public void onPlayerUse(PlayerInteractEvent event) {
        Player p = event.getPlayer();
      
        if (p.getItemInHand().getType() == Material.SUGAR) {
            if (p.getItemInHand().getAmount() > 2) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 1));
                p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
            } else {
                p.getItemInHand().setAmount(0);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
            }
        } else if (p.getItemInHand().getType() == Material.BLAZE_POWDER) {
            if (p.getItemInHand().getAmount() > 2) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                     p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 5, 1));
                     p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 18000, 3));
            } else {
                p.getItemInHand().setAmount(0);
                     p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 5, 1));
                     p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 18000, 3));
            }
        }
      
    }
    EDIT: Ok, I made a mistake. Potion effects still don't work, though

    @DoggyCode™

    That is not a very nice thing to say. I just started coding bukkit plugins.
     
    Last edited: Nov 3, 2015
  5. Offline

    Scimiguy

    Well first of all, 5 ticks is not going to be noticeable at all.
    You're essentially giving confusion for 1/4 of a second.

    Second of all, you're not checking if they even have an item, you're gonna get NullPointerExceptions everywhere

    Thirdly, Have you even registered your listener?
     
    benzimmer123 and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page