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;
   }
 }
  @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
  public void onEntityDamageByEntityHighest(EntityDamageByEntityEvent event) {
    Player attacker = getPlayer(event.getDamager());
    if (attacker == null) return;

    Player defender = getPlayer(event.getEntity());
    if (defender == null) return;

    ArenaPlayer ap = plugin.getArenaPlayer(attacker);
    if (ap != null) {
      ArenaPlayer dp = plugin.getArenaPlayer(defender);
      if (dp != null) {
        Arena arena = ap.getArena();
        if (arena.isInLobby()) {
          // Prevent lobby PvP
          ap.sendMessage("&cYou cannot PvP in the lobby!");
          event.setCancelled(true);
          return;
        }

        // Prevent team killing
        if (!arena.isAllowTeamKilling()) {
          if (dp.getTeam() == ap.getTeam()) {
            ap.sendMessage("&cYou cannot hurt your team mate!");
            event.setCancelled(true);
            return;
          }
        }
      } else {
        ap.sendMessage("&cYou cannot hurt players not in the arena!");
        event.setCancelled(true);
        return;
      }
    } else {
      if (plugin.isInArena(defender)) {
        attacker.sendMessage(
            plugin.getPrefix()
                + FormatUtil.format("&cYou cannot hurt players while they are in an arena!"));
        event.setCancelled(true);
        return;
      }
    }
  }
  @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();
            }
          }
        }
      }
    }
  }
Esempio n. 4
0
  @SuppressWarnings("unchecked")
  private static void parse(SwornPlugin plugin, Class<?> clazz, Object object) {
    FileConfiguration config = plugin.getConfig();

    for (Field field : clazz.getDeclaredFields()) {
      if (!field.isAccessible()) field.setAccessible(true);

      Key key = field.getAnnotation(Key.class);
      if (key != null) {
        String path = key.value();

        try {
          Object value = config.get(path);
          if (value != null) {
            ValueOptions options = field.getAnnotation(ValueOptions.class);
            if (options != null) {
              for (ValueOption option : options.value()) {
                switch (option) {
                  case FORMAT:
                    value = FormatUtil.format(value.toString());
                    break;
                  case LIST_LOWER_CASE:
                    List<String> list = new ArrayList<>();
                    for (String line : (List<String>) value) list.add(line.toLowerCase());
                    value = list;
                    break;
                  case LOWER_CASE:
                    value = value.toString().toLowerCase();
                    break;
                  case MINUTE_TO_MILLIS:
                    value = TimeUnit.MINUTES.toMillis(NumberUtil.toLong(value));
                    break;
                  case PARSE_ITEM:
                    value = ItemUtil.readItem(value.toString(), plugin);
                    break;
                  case PARSE_ITEMS:
                    value = ItemUtil.readItems((List<String>) value, plugin);
                    break;
                  case SECOND_TO_MILLIS:
                    value = TimeUnit.SECONDS.toMillis(NumberUtil.toLong(value));
                    break;
                }
              }

              for (Class<?> custom : options.custom()) {
                Method convert = custom.getMethod("convert", Object.class);
                if (convert.isAccessible()) {
                  value = convert.invoke(null, value);
                }
              }
            }

            field.set(object, value);
          }
        } catch (Throwable ex) {
          plugin
              .getLogHandler()
              .log(Level.SEVERE, Util.getUsefulStack(ex, "loading value from {0}", path));
        }
      }
    }
  }