Solved EntityDamageByEntity Event

Discussion in 'Spigot Plugin Development' started by MarkIsCool, Jun 22, 2018.

  1. Code (Text):

        @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {

            System.out.println(1);

            if (e.getDamager() instanceof Player && e.getEntity() instanceof Player) {

                System.out.println(2);
                Player damager = (Player) e.getDamager();
                Player damaged = (Player) e.getEntity();
               
                if (damager.hasPermission(plugin.guard)) {

                    System.out.println(3);

                    if (damaged.getHealth() < 1.5) {

                        System.out.println(4);

                        plugin.players.put(damaged, true);

                    }

                }

            }
        }
     
    I've added some print lines to the code. For now, just focus on the if statements.
    Right now, I'm not able to pass the first if statement; meaning my code is only printing "1" and not "2".

    (Just trying to add a player to a hashmap if the damager is of a certain player and if the player who took damage is lower than 1.5 hearts)..

    Any ideas?
     
  2. First, check if e.getDamager() != null AND make sure e.getEntity() != null
    ^ That might help.

    Also: Instead of System.out.println(2); why not use Bukkit.getLogger().info("text here..."); ?
     
  3. I'll check that out, thanks!

    I find System.out.println() so much more convenient.
    If you didn't know already, typing "Syso" and then pressing Ctrl + Space + Enter will instantly put it to "System.out.println()"
     
    • Useful Useful x 1
  4. Try this:
    Code (Java):
    if ((e.getDamager().getType() == EntityType.PLAYER) && (e.getEntity().getType() == EntityType.PLAYER)) {
     
  5. You could also use Bukkit.getConsoleSender().sendMessage("message");
     
  6. I'm still shooting myself with an arrow, and I'm still getting 1's.

    Here is the new code, I hope I put it the way you wanted me to xD
    Code (Text):

    @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
           
            if (e.getDamager() == null) {
                return;
            }
            if (e.getEntity() == null) {
                return;
            }
           
            System.out.println(1);

            if (e.getDamager() instanceof Player && e.getEntity() instanceof Player) {

                System.out.println(2);
                Player damager = (Player) e.getDamager();
                Player damaged = (Player) e.getEntity();
               
                if (damager.hasPermission(plugin.guard)) {

                    System.out.println(3);

                    if (damaged.getHealth() < 1.5) {

                        System.out.println(4);

                        plugin.players.put(damaged, true);

                    }

                }

            }
        }
     
     
  7. I forgot to mention I'm shooting myself with an arrow.
    But this isn't working.

    Code (Text):

    @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
           
            System.out.println(1);
           
            if ((e.getDamager().getType() == EntityType.PLAYER) && (e.getEntity().getType() == EntityType.PLAYER)) {
               
                System.out.println(2);
               
                Player damager = (Player) e.getDamager();
                Player damaged = (Player) e.getEntity();
               
                if (damager.hasPermission(plugin.guard)) {

                    System.out.println(3);

                    if (damaged.getHealth() < 1.5) {

                        System.out.println(4);

                        plugin.players.put(damaged, true);

                    }

                }
            }
     
     
  8. Instead of checking for e.getDamager() try checking for e.getCause().equals(DamageCause.PROJECTILE)
     
  9. Try editing the e.getEntity().getType() == EntityType.PLAYER to EntityType.ARROW
     
  10. I'm not trying to check if the player has been hit by an arrow.
    I'm trying to check if the PLAYER1 has been hit by PLAYER2
     
  11. e.getEntity().getType() would be the player who is getting damaged?
    Check for e.getDamager().getType()
     
  12. I might be stupid, but wouldn't it still trigger if I'm hitting myself with an arrow?
     
  13. Ahh so you're shooting yourself if an arrow. That would've been important to know lol.

    Since the entity that's damaging you is a Projectile, you'll have to cast the entity to a projectile and then get who shot it. From there you can test whether or not it was a player.
     
  14. I solved it :D
     
    • Like Like x 1
  15. If you are using arrows then the event is actually saying the damager is an arrow, not a player. you will have to get the source of the arrow and check if it was a player. ;)