예제 #1
0
  @Override
  public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

      if (arg.matches("passive, passively")) scriptEntry.addObject("passively", new Element(true));
      else if (!scriptEntry.hasObject("outcome"))
        scriptEntry.addObject("outcome", new Element(arg.raw_value));
      else arg.reportUnhandled();
    }

    // Set defaults
    scriptEntry.defaultObject("passively", new Element(false));
    scriptEntry.defaultObject("outcome", new Element(false));
  }
예제 #2
0
  @Override
  public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

    // Comparables check the logic
    List<Comparable> comparables = new ArrayList<Comparable>();
    // Insert new comparable into the list
    comparables.add(new Comparable());

    // Indicate that comparables are building
    boolean buildingComparables = true;

    // What to do depending on the logic of the comparables
    // is stored in two tree maps
    TreeMap<Integer, ArrayList<String>> thenOutcome = new TreeMap<Integer, ArrayList<String>>();
    TreeMap<Integer, ArrayList<String>> elseOutcome = new TreeMap<Integer, ArrayList<String>>();

    // Keep tracking of whether we're inside the Else part of the statement or not
    boolean insideElse = false;

    // Keep track of this to avoid Denizen overlooking comparedTo when an operator is used
    // with a value that matches the name of a command. (Good find dimensionZ!)
    boolean usedOperator = false;

    // Track whether we are adding a new command or not
    boolean newCommand = false;

    // Track whether we are inside nested brackets whose contents
    // should be added as arguments to our current If, not as commands
    int bracketsEntered = 0;

    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
      if (buildingComparables) {

        // Set logic to NEGATIVE
        if (arg.startsWith("!")) {
          comparables.get(comparables.size() - 1).setNegativeLogic();
          if (arg.getValue().length() == 1) continue;
          if (arg.startsWith("!=")) arg.replaceValue("==");
          else arg.replaceValue(arg.getValue().substring(1));
        }

        // Replace symbol-operators/bridges with ENUM value for matching
        if (arg.getValue().equals("==")) arg.replaceValue("EQUALS");
        else if (arg.getValue().equals(">=")) arg.replaceValue("OR_MORE");
        else if (arg.getValue().equals("<=")) arg.replaceValue("OR_LESS");
        else if (arg.getValue().equals("<")) arg.replaceValue("LESS");
        else if (arg.getValue().equals(">")) arg.replaceValue("MORE");
        else if (arg.getValue().equals("||")) arg.replaceValue("OR");
        else if (arg.getValue().equals("&&")) arg.replaceValue("AND");

        // Set bridge
        if (arg.matchesEnum(Comparable.Bridge.values())) {
          // new Comparable to add to the list
          comparables.add(new Comparable());
          comparables.get(comparables.size() - 1).bridge =
              Comparable.Bridge.valueOf(arg.getValue().toUpperCase());
        }

        // Set operator (Optional, default is EQUALS)
        else if (arg.matchesEnum(Comparable.Operator.values())) {
          comparables.get(comparables.size() - 1).operator =
              Comparable.Operator.valueOf(arg.getValue().toUpperCase());
          usedOperator = true;
        }

        // Set comparable
        else if (comparables.get(comparables.size() - 1).comparable == null) {
          // If using MATCHES operator, keep as string.
          comparables.get(comparables.size() - 1).setComparable(arg.getValue());
        } else if (!usedOperator && arg.matches("{")) {
          buildingComparables = false;
        }

        // Check if filling comparables are done by checking the command registry for valid
        // commands.
        // If using an operator though, skip on to compared-to!
        else if (!usedOperator
            && denizen.getCommandRegistry().get(arg.getValue().replace("^", "")) != null) {
          buildingComparables = false;
        }

        // Set compared-to
        else {
          comparables.get(comparables.size() - 1).setComparedto(arg.getValue());
          usedOperator = false;
        }
      }

      if (!buildingComparables) {

        // Read "-" as meaning we are moving to a new command, unless we
        // are inside nested brackets (i.e. bracketsEntered of at least 2)
        // in which case we just treat what follows as arguments
        if (arg.matches("-") && bracketsEntered < 2) {
          newCommand = true;
        } else if (!insideElse) {

          // Move to else commands if we read an "else" and we're not
          // currently going through nested arguments
          if (arg.matches("else") && bracketsEntered == 0) {
            insideElse = true;
          }

          // If we find a bracket, and we're already inside
          // nested brackets, add the bracket to the current
          // command's arguments
          else if (arg.matches("{")) {
            bracketsEntered++;

            if (bracketsEntered > 1) {
              thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
            }
          } else if (arg.matches("}")) {
            bracketsEntered--;

            if (bracketsEntered > 0) {
              thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
            }
          }

          // Add new outcome command if the last argument was a non-nested "-"
          // or if there are no outcome commands yet
          else if (newCommand || thenOutcome.size() == 0) {
            thenOutcome.put(thenOutcome.size(), new ArrayList<String>());
            thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
            newCommand = false;
          }

          // Add new outcome argument
          else {
            thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
          }
        } else if (insideElse) {

          // If we find a bracket, and we're already inside
          // nested brackets, add the bracket to the current
          // command's arguments
          if (arg.matches("{")) {
            bracketsEntered++;

            if (bracketsEntered > 1) {
              elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
            }
          } else if (arg.matches("}")) {
            bracketsEntered--;

            if (bracketsEntered > 0) {
              elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
            }
          }

          // Add new else command if the last argument was a non-nested "-"
          // or if it was "else" and we have no else commands yet
          else if (newCommand || elseOutcome.size() == 0) {
            newCommand = false;

            // Important!
            //
            // If we find an "else if", act like we entered a set of
            // brackets, so we treat the if's commands as arguments
            // and don't add them to our current else commands
            if (arg.matches("if") && elseOutcome.size() == 0) {
              bracketsEntered++;
            }

            // Add the new else command
            elseOutcome.put(elseOutcome.size(), new ArrayList<String>());
            elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
          }

          // Add new else argument
          else {
            elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
          }
        }
      }
    }

    // Stash objects required to execute() into the ScriptEntry
    scriptEntry
        .addObject("comparables", comparables)
        .addObject("then-outcome", thenOutcome)
        .addObject("else-outcome", elseOutcome);
  }