Exemplo n.º 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));
  }
Exemplo n.º 2
0
  @Override
  public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
      if (!scriptEntry.hasObject("type")
          && arg.matchesPrefix("type")
          && arg.matchesEnum(Type.values())) scriptEntry.addObject("type", arg.asElement());
      else if (!scriptEntry.hasObject("file") && arg.matchesPrefix("file"))
        scriptEntry.addObject("file", arg.asElement());
      else if (!scriptEntry.hasObject("message")) scriptEntry.addObject("message", arg.asElement());
      else arg.reportUnhandled();
    }

    if (!scriptEntry.hasObject("message"))
      throw new InvalidArgumentsException("Must specify a message.");

    if (!scriptEntry.hasObject("file")) throw new InvalidArgumentsException("Must specify a file.");

    if (!scriptEntry.hasObject("type")) scriptEntry.addObject("type", new Element("INFO"));
  }
Exemplo n.º 3
0
  public boolean execute(ScriptEntry scriptEntry) {
    Matcher m;
    StringBuffer sb;
    if (scriptEntry.getCommandName().indexOf('%') != -1) {
      m = definition_pattern.matcher(scriptEntry.getCommandName());
      sb = new StringBuffer();
      while (m.find()) {
        String definition = scriptEntry.getResidingQueue().getDefinition(m.group(1));
        if (definition == null) definition = "null";
        m.appendReplacement(sb, definition);
      }
      m.appendTail(sb);
      scriptEntry.setCommandName(sb.toString());
    }

    // Get the command instance ready for the execution of the scriptEntry
    AbstractCommand command = plugin.getCommandRegistry().get(scriptEntry.getCommandName());

    if (command == null) {
      dB.echoDebug(
          scriptEntry, DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
      dB.echoError(
          scriptEntry.getCommandName() + " is an invalid dCommand! Are you sure it loaded?");
      dB.echoDebug(scriptEntry, DebugElement.Footer);
      return false;
    }

    // Debugger information
    if (scriptEntry.getPlayer() != null)
      dB.echoDebug(
          scriptEntry,
          DebugElement.Header,
          "Executing dCommand: "
              + scriptEntry.getCommandName()
              + "/"
              + scriptEntry.getPlayer().getName());
    else
      dB.echoDebug(
          scriptEntry,
          DebugElement.Header,
          "Executing dCommand: "
              + scriptEntry.getCommandName()
              + (scriptEntry.getNPC() != null ? "/" + scriptEntry.getNPC().getName() : ""));

    // Don't execute() if problems arise in parseArgs()
    boolean keepGoing = true;

    try {

      // Throw exception if arguments are required for this command, but not supplied.
      if (command.getOptions().REQUIRED_ARGS > scriptEntry.getArguments().size())
        throw new InvalidArgumentsException("");

      if (scriptEntry.has_tags)
        scriptEntry.setArguments(
            TagManager.fillArguments(
                scriptEntry.getArguments(), scriptEntry, true)); // Replace tags

      /*  If using NPC:# or PLAYER:Name arguments, these need to be changed out immediately because...
       *  1) Denizen/Player flags need the desired NPC/PLAYER before parseArgs's getFilledArguments() so that
       *     the Player/Denizen flags will read from the correct Object. If using PLAYER or NPCID arguments,
       *     the desired Objects are obviously not the same objects that were sent with the ScriptEntry.
       *  2) These arguments should be valid for EVERY ScriptCommand, so why not just take care of it
       *     here, instead of requiring each command to take care of the argument.
       */

      List<String> newArgs = new ArrayList<String>();

      // Don't fill in tags if there were brackets detected..
      // This means we're probably in a nested if.
      int nested_depth = 0;
      // Watch for IF command to avoid filling player and npc arguments
      // prematurely
      boolean if_ignore = false;

      for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
        if (arg.getValue().equals("{")) nested_depth++;
        if (arg.getValue().equals("}")) nested_depth--;

        // If nested, continue.
        if (nested_depth > 0) {
          newArgs.add(arg.raw_value);
          continue;
        }

        if (arg.raw_value.indexOf('%') != -1) {
          m = definition_pattern.matcher(arg.raw_value);
          sb = new StringBuffer();
          while (m.find()) {
            String definition = scriptEntry.getResidingQueue().getDefinition(m.group(1));
            if (definition == null) definition = "null";
            m.appendReplacement(sb, definition);
          }
          m.appendTail(sb);
          arg = aH.Argument.valueOf(sb.toString());
        }

        // If using IF, check if we've reached the command + args
        // so that we don't fill player: or npc: prematurely
        if (command.getName().equalsIgnoreCase("if")
            && DenizenAPI.getCurrentInstance().getCommandRegistry().get(arg.getValue()) != null)
          if_ignore = true;

        // Fill player/off-line player
        if (arg.matchesPrefix("player") && !if_ignore) {
          dB.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
          String value =
              TagManager.tag(
                  scriptEntry.getPlayer(),
                  scriptEntry.getNPC(),
                  arg.getValue(),
                  false,
                  scriptEntry);
          dPlayer player = dPlayer.valueOf(value);
          if (player == null || !player.isValid()) {
            dB.echoError(value + " is an invalid player!");
            return false;
          }
          scriptEntry.setPlayer(player);
        }

        // Fill NPCID/NPC argument
        else if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
          dB.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
          String value =
              TagManager.tag(
                  scriptEntry.getPlayer(),
                  scriptEntry.getNPC(),
                  arg.getValue(),
                  false,
                  scriptEntry);
          dNPC npc = dNPC.valueOf(value);
          if (npc == null || !npc.isValid()) {
            dB.echoError(value + " is an invalid NPC!");
            return false;
          }
          scriptEntry.setNPC(npc);
        }

        // Save the scriptentry if needed later for fetching scriptentry context
        else if (arg.matchesPrefix("save") && !if_ignore) {
          dB.echoDebug(scriptEntry, "...remembering this script entry!");
          scriptEntry.getResidingQueue().holdScriptEntry(arg.getValue(), scriptEntry);
        } else newArgs.add(arg.raw_value);
      }

      // Add the arguments back to the scriptEntry.
      scriptEntry.setArguments(newArgs);

      // Now process non-instant tags.
      if (scriptEntry.has_tags)
        scriptEntry.setArguments(
            TagManager.fillArguments(scriptEntry.getArguments(), scriptEntry, false));

      // Parse the rest of the arguments for execution.
      command.parseArgs(scriptEntry);

    } catch (InvalidArgumentsException e) {

      keepGoing = false;
      // Give usage hint if InvalidArgumentsException was called.
      dB.echoError("Woah! Invalid arguments were specified!");
      if (e.getMessage() != null && e.getMessage().length() > 0)
        dB.log(
            ChatColor.YELLOW
                + "+> MESSAGE follows: "
                + ChatColor.WHITE
                + "'"
                + e.getMessage()
                + "'");
      dB.log("Usage: " + command.getUsageHint());
      dB.echoDebug(scriptEntry, DebugElement.Footer);
      scriptEntry.setFinished(true);

    } catch (Exception e) {

      keepGoing = false;
      dB.echoError("Woah! An exception has been called with this command!");
      dB.echoError(e);
      dB.echoDebug(scriptEntry, DebugElement.Footer);
      scriptEntry.setFinished(true);

    } finally {

      if (keepGoing)
        try {
          // Run the execute method in the command
          command.execute(scriptEntry);
        } catch (Exception e) {
          dB.echoError("Woah!! An exception has been called with this command!");
          dB.echoError(e);
          scriptEntry.setFinished(true);
        }
    }

    return true;
  }
Exemplo n.º 4
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);
  }