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

    // Get objects
    Action action = (Action) scriptEntry.getObject("action");
    dLocation location = (dLocation) scriptEntry.getObject("location");
    Element range = (Element) scriptEntry.getObject("range");
    Element id = (Element) scriptEntry.getObject("id");

    // Report to dB
    dB.report(
        scriptEntry,
        getName(),
        aH.debugObj("NPC", scriptEntry.getNPC().toString())
            + action.name()
            + id.debug()
            + (location != null ? location.debug() : "")
            + (range != null ? range.debug() : ""));

    dNPC npc = scriptEntry.getNPC();

    switch (action) {
      case ADD:
        npc.getCitizen().getTrait(Anchors.class).addAnchor(id.asString(), location);
        return;

      case ASSUME:
        npc.getEntity()
            .teleport(
                npc.getCitizen().getTrait(Anchors.class).getAnchor(id.asString()).getLocation());
        return;

      case WALKNEAR:
        npc.getNavigator()
            .setTarget(
                Utilities.getWalkableLocationNear(
                    npc.getCitizen().getTrait(Anchors.class).getAnchor(id.asString()).getLocation(),
                    range.asInt()));
        return;

      case WALKTO:
        npc.getNavigator()
            .setTarget(
                npc.getCitizen().getTrait(Anchors.class).getAnchor(id.asString()).getLocation());
        return;

      case REMOVE:
        npc.getCitizen()
            .getTrait(Anchors.class)
            .removeAnchor(npc.getCitizen().getTrait(Anchors.class).getAnchor(id.asString()));
    }
  }
Exemplo n.º 2
0
  private void doCommand(ScriptEntry scriptEntry, String mapName) {
    TreeMap<Integer, ArrayList<String>> commandMap =
        (TreeMap<Integer, ArrayList<String>>) scriptEntry.getObject(mapName);

    if (commandMap == null || commandMap.size() == 0) return;

    List<ScriptEntry> entries = new ArrayList<ScriptEntry>();

    for (Map.Entry<Integer, ArrayList<String>> pairs : commandMap.entrySet()) {
      ArrayList<String> commandArray = pairs.getValue();
      String command = commandArray.get(0);
      commandArray.remove(0);
      String[] arguments = commandArray.toArray(new String[commandArray.size()]);

      try {
        ScriptEntry entry =
            new ScriptEntry(
                    command,
                    arguments,
                    (scriptEntry.getScript() == null
                        ? null
                        : scriptEntry.getScript().getContainer()))
                .setPlayer(scriptEntry.getPlayer())
                .setNPC(scriptEntry.getNPC())
                .setInstant(true)
                .addObject("reqId", scriptEntry.getObject("reqId"));

        entries.add(entry);

      } catch (ScriptEntryCreationException e) {
        dB.echoError("There has been a problem running the Command. Check syntax.");
        dB.echoError(e);
      }
    }

    // Put tracked objects into new script entries.
    for (String tracked_object : scriptEntry.tracked_objects) {
      ScriptBuilder.addObjectToEntries(
          entries, tracked_object, scriptEntry.getObject(tracked_object));
    }

    scriptEntry.getResidingQueue().injectEntries(entries, 0);
  }
Exemplo n.º 3
0
  @Override
  public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

    NPC npc = scriptEntry.getNPC().getCitizen();
    SittingTrait trait = npc.getTrait(SittingTrait.class);

    if (!npc.hasTrait(SittingTrait.class)) {
      npc.addTrait(SittingTrait.class);
      dB.echoDebug("...added sitting trait");
    }

    if (!trait.isSitting()) {
      dB.echoError("...NPC is already standing, removing trait");
      npc.removeTrait(SittingTrait.class);
      return;
    }

    trait.stand();
    npc.removeTrait(SittingTrait.class);
  }
Exemplo n.º 4
0
  @Override
  public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

    // Parse Arguments
    for (String arg : scriptEntry.getArguments()) {
      if (aH.matchesState(arg))
        scriptEntry.addObject("state", Action.valueOf(aH.getStringFrom(arg).toUpperCase()));
      else if (aH.matchesArg("NPC, PLAYER", arg))
        scriptEntry.addObject("target", Target.valueOf(aH.getStringFrom(arg).toUpperCase()));
      else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg);
    }

    if (scriptEntry.getObject("state") == null)
      throw new InvalidArgumentsException("Must specify a state action!");

    if (scriptEntry.getObject("target") == null)
      throw new InvalidArgumentsException("Must specify a target!");

    if ((scriptEntry.getObject("target") == Target.NPC && scriptEntry.getNPC() == null)
        || (scriptEntry.getObject("target") == Target.PLAYER && scriptEntry.getPlayer() == null))
      throw new InvalidArgumentsException("NPC not found!");
  }
Exemplo n.º 5
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.º 6
0
  @Override
  public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    Action action = (Action) scriptEntry.getObject("state");
    Target target = (Target) scriptEntry.getObject("target");

    // Report to dB
    dB.report(
        getName(),
        aH.debugObj("Toggle", action.name())
            + aH.debugObj(
                "Target",
                target == Target.NPC
                    ? scriptEntry.getNPC().toString()
                    : scriptEntry.getPlayer().getName()));

    switch (target) {
      case NPC:
        if (!scriptEntry.getNPC().getCitizen().hasTrait(InvisibleTrait.class))
          scriptEntry.getNPC().getCitizen().addTrait(InvisibleTrait.class);
        InvisibleTrait trait = scriptEntry.getNPC().getCitizen().getTrait(InvisibleTrait.class);

        switch (action) {
          case FALSE:
            trait.setInvisible(false);
            break;

          case TRUE:
            trait.setInvisible(true);
            break;

          case TOGGLE:
            trait.toggle();
            break;
        }

        break;

      case PLAYER:
        if (scriptEntry.getPlayer() != null) {

          Player player = scriptEntry.getPlayer().getPlayerEntity();
          PotionEffect invis =
              new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1);

          switch (action) {
            case FALSE:
              player.removePotionEffect(PotionEffectType.INVISIBILITY);
              break;

            case TRUE:
              invis.apply(player);
              break;

            case TOGGLE:
              if (player.hasPotionEffect(PotionEffectType.INVISIBILITY))
                player.removePotionEffect(PotionEffectType.INVISIBILITY);
              else invis.apply(player);

              break;
          }
        }
    }
  }