Why does it not work? (Arrow hit damage)

Discussion in 'Plugin Development' started by HungerCraftNL, Dec 4, 2013.

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

    HungerCraftNL

    Hello,
    does anybody see the fould I made in my void?

    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent e){
    3. Player d = (Player) e.getDamager(); //Damager
    4. Player p = (Player) e.getEntity(); //Damaged
    5. if(e.getCause() == DamageCause.PROJECTILE){
    6. if (((String)teams.get(d.getName())).equalsIgnoreCase("red")) {
    7. if((((String)teams.get(p.getName())).equalsIgnoreCase("red"))){
    8. e.setCancelled(true);
    9. d.sendMessage(prefix + ChatColor.RED + "It's not allowed to hit your own team!");
    10. }
    11. }
    12. else if (((String)teams.get(d.getName())).equalsIgnoreCase("blue")) {
    13. if((((String)teams.get(p.getName())).equalsIgnoreCase("blue"))){
    14. e.setCancelled(true);
    15. d.sendMessage(prefix + ChatColor.RED + "It's not allowed to hit your own team!");
    16. }
    17. }
    18. }
    19. }
     
  2. Offline

    Pizza371

    HungerCraftNL I believe (I could be wrong) that the e.getDamager() returns any entity that damaged the player, e.g.
    player, arrow, etc
     
  3. Offline

    HungerCraftNL

    Pizza371

    But I checked of it was an arrow by doing
    Code:java
    1. if(e.getCause() == DamageCause.PROJECTILE){

    right?
     
  4. Offline

    Pizza371

    HungerCraftNL but you're casting it to a player
    if(e.getDamager() instanceof Arrow) {
    Arrow a = (Arrow) e.getDamager();
    Player damager = a.getShooter();
    //stuff
    }
     
  5. You have to check that if the entity is a Player.

    Example:

    Object o="test example";
    Integer i=(Integer)o; //this will throw a ClassCastException because String is not an subclass of Integer

    Similarly,
    e.getDamager(); may not be an instance of a Player.

    You need to check it by using
    if(e.getDamager() instanceof Player)

    Check e.getEntity() with this too.
     
  6. Offline

    HungerCraftNL

    Thank you
     
  7. No worries :)
     
  8. Offline

    HungerCraftNL

  9. Do you mean you recieve the message "dubble"?
     
Thread Status:
Not open for further replies.

Share This Page