Ejemplo n.º 1
0
 private String getWeapon(Player player) {
   ItemStack inHand = player.getItemInHand();
   if (inHand == null || inHand.getType() == Material.AIR) {
     return "their fists";
   } else {
     String name = FormatUtil.getFriendlyName(inHand.getType());
     String article = FormatUtil.getArticle(name);
     return "&3" + article + " &e" + name;
   }
 }
Ejemplo n.º 2
0
  @EventHandler(priority = EventPriority.HIGHEST)
  public void onEntityDeath(EntityDeathEvent event) {
    // Handles deaths inside arenas
    Entity died = event.getEntity();
    if (died == null) return;

    // Clear the drops if in an arena
    if (plugin.isInArena(died.getLocation())) {
      event.getDrops().clear();
      event.setDroppedExp(0);
    }

    if (died instanceof Player) {
      Player pdied = (Player) died;
      ArenaPlayer dp = plugin.getArenaPlayer(pdied);
      if (dp != null) {
        // Prevent duplicate deaths
        if (dp.isDead()) return;
        dp.onDeath();

        Arena ar = dp.getArena();

        if (pdied.getKiller() instanceof Player) {
          Player killer = pdied.getKiller();
          if (killer.getName().equals(pdied.getName())) // Suicide
          {
            ar.tellPlayers("&e{0} &3commited &esuicide&3!", pdied.getName());
            dp.displayStats();
          } else {
            // PvP
            ar.tellPlayers(
                "&e{0} &3killed &e{1} &3with {2}",
                killer.getName(), pdied.getName(), getWeapon(killer));
            dp.displayStats();

            // Handle killer
            ArenaPlayer kp = plugin.getArenaPlayer(killer);
            if (kp != null) {
              kp.setKills(kp.getKills() + 1);
              kp.setKillStreak(kp.getKillStreak() + 1);
              kp.getArena().handleKillStreak(kp);
              kp.addXP(100);

              kp.displayStats();
            }
          }
        } else {
          // From this point on, we will return when there is a valid match
          if (pdied.getKiller() instanceof LivingEntity) {
            LivingEntity lentity = pdied.getKiller();
            String name = FormatUtil.getFriendlyName(lentity.getType());

            ar.tellPlayers(
                "&e{0} &3was killed by &3{1} &e{2}",
                pdied.getName(), FormatUtil.getArticle(name), name);
            dp.displayStats();
            return;
          } else if (pdied.getKiller() instanceof Projectile) {
            Projectile proj = (Projectile) pdied.getKiller();
            if (proj.getShooter() instanceof Player) {
              Player killer = (Player) proj.getShooter();
              ar.tellPlayers(
                  "&e{0} &3killed &e{1} &3with &e{2}",
                  killer.getName(), pdied.getName(), getWeapon(killer));
              dp.displayStats();

              // Handle killer
              ArenaPlayer kp = plugin.getArenaPlayer(killer);
              kp.setKills(kp.getKills() + 1);
              kp.setKillStreak(kp.getKillStreak() + 1);
              kp.getArena().handleKillStreak(kp);
              kp.addXP(100);

              kp.displayStats();
              return;
            } else if (proj.getShooter() instanceof LivingEntity) {
              LivingEntity lentity = pdied.getKiller();
              String name = FormatUtil.getFriendlyName(lentity.getType());

              ar.tellPlayers(
                  "&e{0} &3was killed by {1} &e{2}",
                  pdied.getName(), FormatUtil.getArticle(name), name);
              dp.displayStats();
              return;
            }
          }

          // Attempt to grab from their last damage cause
          EntityDamageEvent damageEvent = pdied.getLastDamageCause();
          DamageCause cause = damageEvent != null ? damageEvent.getCause() : null;

          if (cause == DamageCause.ENTITY_ATTACK) {
            if (damageEvent instanceof EntityDamageByEntityEvent) {
              EntityDamageByEntityEvent damageByEntity = (EntityDamageByEntityEvent) damageEvent;
              Entity damager = damageByEntity.getDamager();
              if (damager != null) {
                if (damager instanceof Player) {
                  Player killer = (Player) damager;
                  ar.tellPlayers(
                      "&e{0} &3killed &e{1} &3with {2}",
                      killer.getName(), pdied.getName(), getWeapon(killer));
                  dp.displayStats();

                  // Handle killer
                  ArenaPlayer kp = plugin.getArenaPlayer(killer);
                  if (kp != null) {
                    kp.setKills(kp.getKills() + 1);
                    kp.setKillStreak(kp.getKillStreak() + 1);
                    kp.getArena().handleKillStreak(kp);
                    kp.addXP(100);

                    kp.displayStats();
                  }
                } else {
                  String name = FormatUtil.getFriendlyName(damager.getType());
                  ar.tellPlayers(
                      "&e{0} &3was killed by &3{1} &e{2}",
                      pdied.getName(), FormatUtil.getArticle(name), name);
                  dp.displayStats();
                }

                return;
              }
            }
          } else if (cause == DamageCause.PROJECTILE) {
            if (damageEvent instanceof EntityDamageByEntityEvent) {
              EntityDamageByEntityEvent damageByEntity = (EntityDamageByEntityEvent) damageEvent;
              Entity damager = damageByEntity.getDamager();
              if (damager != null) {
                if (damager instanceof Projectile) {
                  Projectile proj = (Projectile) damager;
                  if (proj.getShooter() != null) {
                    if (proj.getShooter() instanceof Player) {
                      Player killer = (Player) proj.getShooter();

                      ar.tellPlayers(
                          "&e{0} &3killed &e{1} &3with {2}",
                          killer.getName(), pdied.getName(), getWeapon(killer));

                      dp.displayStats();

                      // Handle killer
                      ArenaPlayer kp = plugin.getArenaPlayer(killer);
                      if (kp != null) {
                        kp.setKills(kp.getKills() + 1);
                        kp.setKillStreak(kp.getKillStreak() + 1);
                        kp.getArena().handleKillStreak(kp);
                        kp.addXP(100);

                        kp.displayStats();
                      }
                    } else {
                      String name = "";
                      if (proj.getShooter() instanceof LivingEntity)
                        name =
                            FormatUtil.getFriendlyName(
                                ((LivingEntity) proj.getShooter()).getType());

                      ar.tellPlayers(
                          "&e{0} &3was shot by &3{1} &e{2}",
                          pdied.getName(), FormatUtil.getArticle(name), name);
                      dp.displayStats();
                    }

                    return;
                  }
                }
              }
            }
          } else if (cause != null) {
            // There's probably nothing else we can do here, so just turn it into a string
            ar.tellPlayers(
                "&e{0} &3was killed by &e{1}", pdied.getName(), FormatUtil.getFriendlyName(cause));
            dp.displayStats();
          } else if (ar instanceof SpleefArena) {
            // If they were in spleef, they probably fell through the floor
            ar.tellPlayers("&e{0} &3fell through the floor!", pdied.getName());
            dp.displayStats();
          } else {
            // No idea
            ar.tellPlayers("&e{0} &3died of unknown causes", pdied.getName());
            dp.displayStats();
          }
        }
      }
    } else {
      if (died instanceof LivingEntity) {
        LivingEntity lentity = (LivingEntity) died;
        if (lentity.getKiller() instanceof Player) {
          Player killer = lentity.getKiller();
          if (plugin.isInArena(killer)) {
            ArenaPlayer ak = plugin.getArenaPlayer(killer);

            // Selectively count mob kills
            if (ak.getArena().isCountMobKills()) {
              ak.addXP(25);
              ak.setKills(ak.getKills() + 1);
              ak.setKillStreak(ak.getKillStreak() + 1);
              ak.getArena().handleKillStreak(ak);

              String name = FormatUtil.getFriendlyName(lentity.getType());
              ak.sendMessage(
                  "&e{0} &3killed {1} &e{2}", killer.getName(), FormatUtil.getArticle(name), name);
              ak.displayStats();
            }
          }
        }
      }
    }
  }