Search code examples
javaminecraftbukkit

Minecraft bukkit player shot another player with a bow


How can I detect if a player shot another player with a bow? I want to get the Names of the players. So how can I do that?


Solution

  • Use bukkit events, specifically EntityDamageByEntityEvent. Then simply check who the players are:

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    
       Entity damager = event.getDamager();
    
       if(damager instanceof Arrow) { // check if the damager is an arrow
    
           Arrow arrow = (Arrow) damager;
           if(arrow.getShooter() instanceof Player) {
                // the arrow.getShooter() here is the player who fired the arrow
           }
    
           Entity entityHit = event.getEntity();
           if(entityHit instanceof Player) {
               Player playerHit = (Player) entityHit;
               // playerHit here is the player who got hit
           }
    
    }
    

    That's basically how you get the players, now you only need to use that information to print it out in chat or whatever you want to do. Good luck!