private final void chooseInfected(int tries) {
   if (tries < 16) {
     ArenaPlayer ap = active.get(Util.random(active.size()));
     if (ap != null && ap.isOnline()) {
       ap.setTeam(Team.BLUE);
       ap.sendMessage(getMessage("patientZero"));
       onSpawn(ap);
       tellPlayers(getMessage("zombie"), ap.getName());
     } else {
       chooseInfected(tries++);
     }
   } else {
     // Shouldn't happen...
     tellPlayers(getMessage("couldntFindZombie"));
     stop();
   }
 }
  @Override
  public NPermissionManager deserialize(
      JsonElement json, Type t, JsonDeserializationContext context) {
    try {
      JsonObject obj = json.getAsJsonObject();

      JsonArray roles = obj.getAsJsonArray(ROLES);
      JsonArray players = obj.getAsJsonArray(PLAYERS);

      Map<String, HashMap<NPermission, Boolean>> playerPerms =
          new HashMap<String, HashMap<NPermission, Boolean>>();
      Map<Role, HashSet<NPermission>> rankPerms = new HashMap<Role, HashSet<NPermission>>();

      for (JsonElement e : roles) {
        String[] s = e.getAsString().split(",");
        Role r = Role.match(s[0]);
        HashSet<NPermission> perms = new HashSet<NPermission>();
        for (int i = 1; i < s.length; i++) {
          perms.add(NPermission.match(s[i]));
        }
        rankPerms.put(r, perms);
      }

      for (JsonElement e : players) {
        String[] s = e.getAsString().split(",");
        String id = s[0];
        HashMap<NPermission, Boolean> perms = new HashMap<NPermission, Boolean>();
        for (int i = 1; i < s.length; i++) {
          String[] ss = s[i].split(":");

          perms.put(NPermission.match(ss[0]), (ss[1].equalsIgnoreCase("t")) ? true : false);
        }

        // Make sure id is a UUID
        if (id.length() != 36) {
          UUID uniqueId = UUIDFetcher.getUUID(id);
          if (uniqueId == null) {
            OfflinePlayer player = Util.matchOfflinePlayer(id);
            if (player != null) uniqueId = player.getUniqueId();
          }

          if (uniqueId != null) {
            id = uniqueId.toString();
          } else {
            SwornNations.get().log(Level.WARNING, "Failed to resolve UUID for %s", id);
            continue;
          }
        }

        playerPerms.put(id, perms);
      }

      return new NPermissionManager(playerPerms, rankPerms);

    } catch (Exception ex) {
      ex.printStackTrace();
      SwornNations.get()
          .log(Level.WARNING, "Error encountered while deserializing a NPermissionManager.");
      return null;
    }
  }
Example #3
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));
        }
      }
    }
  }