Exemplo n.º 1
0
  @Override
  public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

    // Define necessary fields
    Script script = scriptEntry.getScript();
    Duration duration = null;
    OfflinePlayer player = null;
    Type type = Type.PLAYER;

    // Set player in scriptEntry to the target
    if (scriptEntry.getPlayer() != null) player = scriptEntry.getPlayer();
    else if (scriptEntry.getOfflinePlayer() != null) player = scriptEntry.getOfflinePlayer();

    // Parse Arguments
    for (String arg : scriptEntry.getArguments()) {

      if (aH.matchesDuration(arg) || aH.matchesInteger(arg)) duration = aH.getDurationFrom(arg);

      // Default is PLAYER, but having both to choose from seems to be most straightforward
      else if (aH.matchesArg("GLOBAL, PLAYER", arg)) type = Type.valueOf(arg.toUpperCase());

      // Must be an actual script! If the script doesn't exist, matchesScript(...)
      // will echo an error.
      else if (aH.matchesScript(arg)) script = aH.getScriptFrom(arg);
      else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
    }

    // Check to make sure required arguments have been filled
    if (type == Type.PLAYER && player == null)
      throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER);
    if (script == null) throw new InvalidArgumentsException(Messages.ERROR_NO_SCRIPT);

    // Store necessary items in the scriptEntry
    scriptEntry.addObject("script", script);
    scriptEntry.addObject("duration", duration);
    scriptEntry.addObject("type", type);
    // Store the player only when necessary
    if (type == Type.PLAYER) scriptEntry.addObject("player", player);
  }