EntityDamageByEntityEvent

Discussion in 'Plugin Development' started by bjsnow, Mar 27, 2013.

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

    bjsnow

    ZeusAllMighty11
    Code:
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            Entity e = event.getEntity();
          Arrow arrow = (Arrow)event.getDamager();
          Player damager = (Player) event.getDamager();
            if (e instanceof Player && damager instanceof Player) {
                Player player = (Player) e;
           
        if (blueTeam.containsKey(player.getName()) && blueTeam.containsKey(damager.getName())) {
            event.setCancelled(true);
        }
        else if(redTeam.containsKey(player.getName()) && redTeam.containsKey(damager.getName())) {
            event.setCancelled(true);
        }
        else {
            event.setCancelled(false);
         
        }
                  if(e instanceof Player && damager instanceof Arrow)
    {
    if(((Arrow)damager).getShooter() instanceof Player)
    {
        Player shooter = (Player)arrow.getShooter();
        if(redTeam.containsKey(shooter.getName()) && redTeam.containsKey(player.getName())) {
       
             
                event.setCancelled(true);
            }
            else if(blueTeam.containsKey(shooter.getName()) && blueTeam.containsKey(player.getName())) {
             
                event.setCancelled(true);
            }
        }
     
     
              }
            }
             
            }
    
    When a player hits a player on the same team it cancels any damage but when a player shoots a player on the same team it damages them. But I want it to cancel the damage when a player shoots another play on the same team.
     
  2. Offline

    Qwahchees

    It's not shooting properly because you have the statement of checking the damager is an Arrow and the entity is player within " if (e instanceof Player && damager instanceof Player) {".

    This means you're saying, if the entity is being hurt is a player, and is being hurt by a player, and is being hurt by an arrow THEN cancel arrow damage. It doesn't make sense to check if the player is hurt by a player and an arrow arrow, am I wrong?
     
  3. Offline

    ZeusAllMighty11 Retired Staff

    Qwahchees

    No, it means that he checks:

    - If the victim is a player
    - If the damager is a player

    Perfectly fine to check, because that'd be like regular PVP.

    Then he checks

    - If victim is a player
    - if damager is an arrow, player is shooter

    Which is also fine.

    You can also remove the 'else event.setCancelled(false);' because it will do that by default
     
  4. Offline

    bjsnow

    Any ideas?
     
  5. Offline

    RingOfStorms

    At this point the damager is the arrow entity.

    What you'll have to do is on the player shooting the bow give all their arrows entity metadata, something like "team1" or whatever.

    Then do event.getEntity() is arrow, then check event.getEntity().hasMetadata("team1") and cancel that way.


    Edit:

    An easier way, I think arrow has a getShooter method, just use that to get the player damager.
     
  6. Offline

    agodwin987

    This is a complete side note, but if you are using a team system you could try to use player meta data instead of a hash map... it would go something like this:
    Code:
    e.getPlayer().setMetadata("team", new FixedMetadataValue(Main_Class.getPlugin(), "team_name"));
    and to check for meta:
    Code:
    e.getPlayer().getMetadata("team").get(0).asString().equals("team_name"))
     
  7. Offline

    BajanAmerican

    I did it in a boolean and was fine with it.
    Code:
    private boolean sameTeam(Player victim, Player attacker) {
            if(Game.red.contains(victim.getName()) && Game.red.contains(attacker.getName())) {
                // Both players are on red team
                attacker.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] " + ChatColor.DARK_AQUA + victim.getName() + ChatColor.RED + " Is On Your Team!");
                return true;
            } else if(Game.blue.contains(victim.getName()) && Game.blue.contains(attacker.getName())) {
                // Both players are on blue team
                attacker.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] " + ChatColor.DARK_AQUA + victim.getName() + ChatColor.BLUE + " Is On Your Team!");
                return true;
            }else if(Game.blue.contains(victim.getName()) || Game.red.contains(victim.getName()) && Game.spectors.contains(attacker.getName())){
                return true;
            }else if(Game.spectors.contains(victim.getName()) && Game.blue.contains(attacker.getName()) || Game.red.contains(attacker.getName())){
                return true;
            }
            return false;
        }
     
  8. Offline

    agodwin987

    I'm not saying don't use a boolean, im saying replace hashmap with metadata, it makes life a lot easier
     
  9. Offline

    RingOfStorms

    Would be good if he is just checking it for that reason, but if he has other classes needing a list of the players then it might be better to use a list/map.
     
  10. Offline

    agodwin987

    Yeah, I guess, but for the teams purpose it is quite good.
     
    BajanAmerican likes this.
  11. Offline

    Qwahchees

    Strange. I'm pretty new to coding as well, but I usually have a separate check for arrows.
    I usually check if the entity hurt is a player, and if the damager is an arrow, then continue my if statement.
     
Thread Status:
Not open for further replies.

Share This Page