Get EntityDamageEvent weapon

Discussion in 'Plugin Development' started by XxShadow_NinjaxxX, Jul 28, 2014.

Thread Status:
Not open for further replies.
  1. Is there any way I can get the weapon/cause of damage from EntityDamageEvent?
    Get item in hand doesn't work well with bows :/
     
  2. Offline

    GameplayJDK

    XxShadow_NinjaxxX Just use the getCause() method which returns a damagecause. To get the weapon, check if getDamager() is an instance of Player, cast the damager to a player and use getItemInHand().getType() to get the material which the damaging player is holding.
     
  3. GameplayJDK
    There's the problem I'm talking about.
    Ex: PlayerA shoots an arrow at PlayerB and switches to a sword, when PlayerB gets shot, I get the sword, not the bow
     
  4. Offline

    GameplayJDK

    XxShadow_NinjaxxX So you could assume, that if the damager is instance of an Arrow, and the projectile source (the shooter) of that arrow is instance of a player, he has launched the arrow using a bow.. Right? :p
     
  5. Is there a way to tell which bow it was shot from? Or do you have to loop through the players inventory?
    EDIT:
    Cause there are people who just like to screw with you, and try and break plugins :/
     
  6. Offline

    GameplayJDK

    XxShadow_NinjaxxX Another possibility would be to keep traxk of all projectiles that are shot, store data about the shooter, the item in his hand, the location and the projectile in a new object, and listen for projectile to compare the projectile with the ones, which are stored and get the shooter and the item that way.
    Or just add a custom metadata value to the projectile which identifies the player and bow it comes from.
     
  7. Offline

    Dragonphase

    XxShadow_NinjaxxX

    This is a really good question. You could use an EntityShootBowEvent, get the projectile and use setMetadata to set the projectile's source to the bow:

    Code:java
    1. @EventHandler
    2. public void onBowShoot(EntityShootBowEvent event){
    3. if (event.getEntity() instanceof Player){
    4. event.getProjectile().setMetadata("bow", new FixedMetadataValue(plugin, event.getBow()));
    5. }
    6. }


    Then, in your damage event, get the projectile, and retrieve the metadata from it:

    Code:java
    1. if (arrow.hasMetadata("bow")){
    2. ItemStack bow = (ItemStack)arrow.getMetadata("bow").get(0).value();
    3. }


    To avoid casting to ItemStack, you could set the value of the Metadata to an Integer, which stores the index of the bow in the player's inventory. Then, you could use:

    Code:java
    1. event.getShooter().getInventory().getItem(index);
     
    GameplayJDK likes this.
  8. Is it possible to store all the bow's info in the arrow's metadata? And would that affect server speed or anything?
     
  9. Offline

    Dragonphase

    XxShadow_NinjaxxX
    This method would store a reference to the bow (ItemStack). Since arrows don't last long, you don't need to worry that much. See my edit in my last post.
     
  10. Offline

    GameplayJDK

    XxShadow_NinjaxxX You should be able to store as many values in metadata as you want, without causing a significant change of ingame performance.
     
Thread Status:
Not open for further replies.

Share This Page