Solved How to check the length of a potion effect

Discussion in 'Plugin Development' started by KarimAKL, Mar 15, 2018.

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

    KarimAKL

    As the title says: How do i check how long a potion effect lasts?
    EDIT: Yes, i have looking for this info and found nothing.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    KarimAKL

    I tried the 'getDuration' like this:
    Code:
    if (p.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE).getDuration) {
    
    But i stopped trying to do it like that because that's not how i'm supposed to do it, i think.
    Then how would i do it?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL It is a function, you need ( and ) after it.
    And you use it on an PotionEffect, you can't check the time this way.
     
  5. Offline

    KarimAKL

    I'm sorry but could you give me an example? I don't get it.
     
  6. All methods are written ending with parentheses. If a method accepts no parameters, it just has the parentheses with nothing inside them. I know it may be a pain to take an entire introductory class to Java just to do Bukkit coding, but I'd recommend reading or watching something about how to write and call methods, so that you're able to better know how to write exactly what you want to occur (and then the process of coding speeds up). I found a simple tutorial you might like here.

    As for that example, just write getDuration() with () at the end. If it's a field (example: "duration"), it will have no parentheses. If it's a method it will have parentheses.
     
  7. Offline

    KarimAKL

    I'm sorry, but i still don't get it.. :/
     
  8. Offline

    KarimAKL

  9. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL Start by getting the active potioneffects and looping through them.
    In your case you should also check the type.
     
  10. Offline

    KarimAKL

    Would this work for the loop bit?
    Code:
    for (PotionEffect effect : player.getActivePotionEffects())
    
     
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    KarimAKL

    Not yet because i don't know how/where i should check the duration. :/
     
  13. @KarimAKL
    With that loop check if the potion effect.getType is equal to the one you're looking for, if so get the duration
    int dur = effect.getDuration();
    and then do what you want with the duration
     
  14. Offline

    nullYoung

    Code:
    for (PotionEffect potionEffect : player.getActivePotionEffects()) {
      int duration = potionEffect.getDuration();
    }
     
  15. Offline

    KarimAKL

    Okay, thanks. :) Now do i do it like this?
    Code:
    for (PotionEffect potionEffect : p.getActivePotionEffects()) {
      int duration = potionEffect.getDuration();
      p.addPotionEffect(new PotionEffect(PotionEffectType.Whatever_Effect, Time, Amplifier).duration);
    }
    
    ?
    EDIT: I know, i'm hopeless, sorry.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL Why do you add something?
    Just check the duration, don't know what you want to do with it.
     
  17. Offline

    KarimAKL

    I added something because i wanna remove/add potion effects after getting the duration of the potion effect.
    What i would like to do is something like this:
    Check if the player has a certain potion effect, then if the player does have that i want to check how long it lasts for and if it's something like higher than 1 minute (> 1200) i want to remove/add potion effects
     
  18. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL You have the duration, divide it by 20, then you have the time left in seconds.
     
  19. Offline

    KarimAKL

    I know about ticks and seconds but i still don't understand what i should do about the duration.
     
  20. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL You compare it to check if it is over 1 minute, as you want.
     
  21. Offline

    KarimAKL

    Compare, how? Sorry. :/
     
  22. Offline

    timtower Administrator Administrator Moderator

  23. Offline

    KarimAKL

    Again, i'm sorry but i don't get it. :(
     
  24. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL if(duration/20 > secondsyouwant)
     
  25. Offline

    KarimAKL

    I just tried this and it seemed to work, thank you so much. :D
    Code:
    for (PotionEffect potioneffect : p.getActivePotionEffects()) {
                    int duration = potioneffect.getDuration();
                    if (p.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE)) {
                        if (duration/20 > 60) {
                            p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 600, 3));
                        }
                    }
                }
    
    And i guess it would be the same for amplifier, right? Just change .getDuration() to .getAmplifier() ? :)
    Anyway, i'll change this thread to solved now.
     
  26. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL It should, starts counting at 0.
     
  27. Offline

    KarimAKL

    Yeah, thanks. :)
     
Thread Status:
Not open for further replies.

Share This Page