Solved Stop friendly fire. Swords/Bows/Potions

Discussion in 'Plugin Development' started by MineCrypt, Jan 31, 2013.

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

    MineCrypt

    Alright I need to be able to make friendly fire on my 2 teams. I have code that works for swords but not for bows and potions, how can I achieve both?

    Swords:
    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerDamage(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player damaged = (Player) event.getEntity();
                if (event.getDamager() instanceof Player) {
                    Player damager = (Player) event.getDamager();
     
                    if (CTFCommandExecutor.BlueTeam.contains(damager.getName())
                            && CTFCommandExecutor.BlueTeam.contains(damaged
                                    .getName())) {
                        event.setCancelled(true);
                    }
     
                    if (CTFCommandExecutor.RedTeam.contains(damager.getName())
                            && CTFCommandExecutor.RedTeam.contains(damaged
                                    .getName())) {
                        event.setCancelled(true);
                    }
     
                }
            }
        }
     
  2. Offline

    ZeusAllMighty11

    Bows + Potions = ProjectileHitEvent
     
  3. Check if it's an instanceof Projectile, instead of a Player.
     
  4. Offline

    MineCrypt

    If I do that can I keep the rest code of the same?
     
  5. Not exactly
    Code:
    Player damager = (Player) event.getDamager();
    Should change into something like
    Code:
    event.getShooter()
    Try fiddling around with it. ;)
    Also check the javadocs for more information.
     
  6. Offline

    McCastleWars

    My code
    Code:
          if(event.getEntity() instanceof Projectile){
              Projectile proj = (Projectile) event.getEntity();
              if(proj instanceof Arrow){
                Arrow arrow = (Arrow) proj;
                if(arrow.getShooter() instanceof Player){ //Could be shot from dispenser or skeleton
                  Player shooter = (Player) arrow.getShooter();
                    if ((CastleWars.redTeam.contains(shooter)) && (CastleWars.redTeam.contains(e)))
                        event.setCancelled(true);
                      else if ((CastleWars.blueTeam.contains(shooter)) && (CastleWars.blueTeam.contains(e))) {
                        event.setCancelled(true);
                      }
                }
              }
            }
    This is for arrows. idk potions yet. Since I dont think potions will be used in my plugins
     
  7. Offline

    kiwhen

    I recently posted a class for this sort of thing here.

    In its current configuration, it can handle no more than two teams, but it does block pretty much any kind of weapons damage, including the splash potion of posion - which is not registered as a projectile hit event.
     
  8. Offline

    MineCrypt

    I like that very much and I will use that. That addresses everything so thank you, and thank you everyone for the relpies.
     
  9. Offline

    BajanAmerican

    What did you put as a constructor for "e"? (CastleWars.redTeam.contains(e)), what is the e?
     
    CookCreeperz likes this.
Thread Status:
Not open for further replies.

Share This Page