Solved Setting the damage of an arrow

Discussion in 'Plugin Development' started by AGuyWhoSkis, Sep 10, 2014.

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

    AGuyWhoSkis

    This might sound like a really basic question, but I'm having a lot of trouble with it. I need to make all arrows which hit a player instantly kill them. I have this right now:
    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onArrowHit(EntityDamageByEntityEvent e) {
            if (!(e.getEntity() instanceof Player)) {
                return;
            }
            if (!(e.getDamager() instanceof Arrow)) {
                return;
            }
           
            e.setDamage(50.0D);
        }
    You would expect this to work. It kills the player, but it does not handle the killer of the player properly, which is critical here. It needs to go through the normal event process of damage/death. Does anyone have any alternatives/ideas? Any help would be appreciated.
     
  2. Offline

    valon750

    AGuyWhoSkis

    Only if shot by a player? If so:

    Code:
    @EventHandler
    public void onArrowHit (EntityDamageByEntityEvent event){
        if (event.getEntity() instanceof Player){
            Player player = (Player) event.getEntity();
            if (event.getDamager() instanceof Arrow){
                Arrow arrow = (Arrow) event.getDamager();
                if (arrow.getShooter() instanceof Player){
                    player.damage(9001);
                }
            }
        }
    }
    Most likely broken since it's both 4am, and I typed this up within the code tags themselves.
     
  3. Offline

    AGuyWhoSkis

    Thanks for the reply, valon. This would work, but it needs to keep whoever shot the arrow as the killer.
     
  4. Offline

    valon750

    AGuyWhoSkis

    You can easily change the player.damage method to event.setDamage :)
     
  5. Offline

    AGuyWhoSkis

    I have also tried this:
    Code:
    ((Player) e.getEntity()).damage(50.0D, ((Arrow) e.getDamager()).getShooter());
    But this makes things weird. The first time you shoot someone, the killer is null. But any time after that the killer is set properly.
     
  6. Offline

    valon750

    AGuyWhoSkis

    Goooootcha, like I said it's 4am :3

    damage allows for a double argument for damage, followed by an Entity argument for the damager :)
     
  7. Offline

    fireblast709

    AGuyWhoSkis It seems you miss a very important part of the API structure :p. When you set the damage, the player will be killed by the Arrow. In turn,
    • You can listen to the PlayerDeathEvent
    • Get the last damage cause (documentation galore)
    • Check if it is an EntityDamageByEntityEvent
    • Cast it and get the damager
    • Check if the damage is an Arrow
    • Cast it and get the shooter
    • Check if the damager is a Player
    • Cast... and you have your Player.
     
    AGuyWhoSkis likes this.
  8. Offline

    AGuyWhoSkis

    fireblast709 Wow. This is exactly what I needed! Thank you so much.
     
Thread Status:
Not open for further replies.

Share This Page