EntityDamageByEntityEvent Help

Discussion in 'Plugin Development' started by VinexAx789, Jun 25, 2015.

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

    VinexAx789

    I want to make a this when a player gets hit they will get speed for 4 seconds per hit it will gain more time of speed it maxes out at 30 seconds here's my code:

    Code:
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player
                    && event.getEntity() instanceof Player) {
                Player attacker = (Player) event.getDamager();
                Player damaged = (Player) event.getEntity();
                if (kitselected.get(damaged).equals("edgy")) {
                    if (attacker.getItemInHand() != null) {
                        PotionEffect potionEffect = new PotionEffect(
                                PotionEffectType.SPEED, 80, 2);
                        potionEffect.apply(damaged);
                    }
                }
            }
        }
     
  2. Code:
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                Player attacker = (Player) event.getDamager();
                Player damaged = (Player) event.getEntity();
                if (kitselected.get(damaged).equals("edgy")) {
                    if (attacker.getItemInHand() != null) {
                        int duration = 80; // Set the initial duration of the potion effect.
                        if (damaged.hasPotionEffect(PotionEffectType.SPEED)) { // Check if they already have a speed potion effect.
                            for (PotionEffect potionEffect : damaged.getActivePotionEffects()) { // Loop through every potion effect to search for the speed potion effect.
                                if (potionEffect.getType() == PotionEffectType.SPEED) {
                                    duration = Math.min(potionEffect.getDuration() + 80, 600); // Set the duration to the current speed duration + 4 seconds (4 * 20 = 80). Cap it at 30 seconds (30 * 20 = 600) using Math.min, as it chooses the smallest number and if the current duration + 4 is greater than 30, 30 is obviously smaller thus it'd use 30.
                                    break; // Exit out of the loop to improve performance.
                                }
                            }
                        }
                        damaged.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, duration, 2), true); // Apply the effect.
                    }
                }
            }
        }
    
    Notes/Tips/Warnings:
    • Rename the variable "kitselected" to "kitSelected" for better looks (does not affect performance).
    • From what I see (Map#get(Player)), you're storing a Player in a Map. Do NOT do that. Store either their UUID or their username, preferably their UUID, regardless of whether information is stored or not.
     
  3. Offline

    Zettelkasten

    @KingFaris11 It is completely fine to store a Player reference, as long as you remove it when the player leaves the server.
     
  4. I only say this to people who seem new to Bukkit as they most likely won't handle it. Though I should have mentioned that point too.
     
    Last edited: Jun 25, 2015
  5. Offline

    VinexAx789

    @KingFaris11 Thanks thread solved

    @KingFaris11

    Code:
       
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                Player attacker = (Player) event.getDamager();
                Player damaged = (Player) event.getEntity();
                if (kitselected.get(damaged).equals("edgy")) {
                    if (attacker.getItemInHand() != null) {
                        int duration = 80; // Set the initial duration of the potion effect.
                        if (damaged.hasPotionEffect(PotionEffectType.SPEED)) { // Check if they already have a speed potion effect.
                            for (PotionEffect potionEffect : damaged.getActivePotionEffects()) { // Loop through every potion effect to search for the speed potion effect.
                                if (potionEffect.getType() == PotionEffectType.SPEED) {
                                    duration = Math.min(potionEffect.getDuration() + 80, 600); // Set the duration to the current speed duration + 4 seconds (4 * 20 = 80). Cap it at 30 seconds (30 * 20 = 600) using Math.min, as it chooses the smallest number and if the current duration + 4 is greater than 30, 30 is obviously smaller thus it'd use 30.
                                    break; // Exit out of the loop to improve performance.
                                }
                            }
                        }
                        damaged.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, duration, 2), true); // Apply the effect.
                        damaged.sendMessage("§aYou were hit by §f" + attacker.getName() + "§9+ " + duration);
                    }
                }
            }
        }
    Giving the speed works but it's not stacking every time it gets hit it's supposed to add on from 4 and then you can't go over 30 which is not working.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Weird, before the break statement, add player.removePotionEffect(potionEffect.getType()); and also, what does it say in the duration message for the duration? Does it always say 80 or does it increase?
     
    Last edited: Jun 26, 2015
  7. Offline

    VinexAx789

    Says +80
     
  8. It's always +80? Did you add what I said before (removePotionEffect)? Try removing that if damaged.hasPotionEffect line and try again. Also print a message inside the for loop (System.out.println(potionEffect.getType().getName() + ":" + potionEffect.getDuration()))

    If it prints nothing when a player already has the speed effect, something is definitely wrong...
    If it prints "SPEED:80" then it should be working...
     
  9. Offline

    VinexAx789

    It always said +80 It should say like +4 seconds etc.
    @KingFaris11
     
  10. No it shouldn't, it should say +80 because "duration" is in ticks. You must divide it by 20 to get it into seconds. But yeah, it should say +80 then +160, etc. Can you send your latest code and I'll test it myself.
     
  11. Offline

    VinexAx789

    @KingFaris11
    Code:
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                Player attacker = (Player) event.getDamager();
                Player damaged = (Player) event.getEntity();
                if (kitselected.get(damaged).equals("edgy")) {
                    if (attacker.getItemInHand() != null) {
                        int duration = 80; // Set the initial duration of the potion effect.
                        if (damaged.hasPotionEffect(PotionEffectType.SPEED)) { // Check if they already have a speed potion effect.
                            for (PotionEffect potionEffect : damaged.getActivePotionEffects()) { // Loop through every potion effect to search for the speed potion effect.
                                if (potionEffect.getType() == PotionEffectType.SPEED) {
                                    duration = Math.min(potionEffect.getDuration() + 80, 600); // Set the duration to the current speed duration + 4 seconds (4 * 20 = 80). Cap it at 30 seconds (30 * 20 = 600) using Math.min, as it chooses the smallest number and if the current duration + 4 is greater than 30, 30 is obviously smaller thus it'd use 30.
                                    System.out.println(potionEffect.getType().getName() + ":" + potionEffect.getDuration());
                                    damaged.removePotionEffect((PotionEffectType.SPEED));
                                    break; // Exit out of the loop to improve performance.
                                }
                            }
                        }
                        damaged.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, duration, 2), true); // Apply the effect.
                        damaged.sendMessage("§aYou were hit by §f" + attacker.getName() + "§9+ " + duration);
                    }
                }
            }
        }
     
  12. 'aight, I'll be back.
     
    VinexAx789 likes this.
  13. Offline

    VinexAx789

  14. I found out where it stopped working, it's this:
    Code:
    if (potionEffect.getType()== PotionEffectType.SPEED) {
    For some reason, even though when I print out the potionEffect's name, it is SPEED, this statement always returns false. My only solution to this (other than reporting this bug) is:
    Code:
    if (potionEffect.getType().getName().equals(PotionEffectType.SPEED.getName()) {
    This works (tested), and you can remove your System.out.println debugging now.
     
  15. Offline

    VinexAx789

    @KingFaris11 put it back where the System.out.println was?
     
  16. No, remove all these System.out.println()'s. And I meant, replace the old code I quoted (first piece of code) with my solution code.
     
  17. Offline

    VinexAx789

    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page