예제 #1
0
  @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;
      }
    }
  }
예제 #2
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));
        }
      }
    }
  }