@Override
 public AbstractRequirement as(String requirementName) {
   // Register command with Registry
   plugin.getRequirementRegistry().register(requirementName, this);
   onEnable();
   return this;
 }
Beispiel #2
0
 public dNPCRegistry(Denizen denizen) {
   plugin = denizen;
   if (Depends.citizens != null) {
     plugin.getServer().getPluginManager().registerEvents(this, plugin);
   }
   actionHandler = new ActionHandler(plugin);
 }
 @Override
 public AbstractRequirement activate() {
   plugin = (Denizen) Bukkit.getPluginManager().getPlugin("Denizen");
   // Reference Helper Classes
   sH = plugin.getScriptEngine().getScriptHelper();
   sB = plugin.getScriptEngine().getScriptBuilder();
   return this;
 }
Beispiel #4
0
  @SuppressWarnings("unchecked")
  public void loader() {

    dB.log("Loading external dependencies for run-time compiler.");
    dependencies = new ArrayList<String>();
    try {
      File file =
          new File(
              denizen.getDataFolder()
                  + File.separator
                  + "externals"
                  + File.separator
                  + "dependencies");
      for (File f : file.listFiles(jarFilter)) {
        dependencies.add(f.getPath());
        dB.log("Loaded  " + f.getName());
      }
    } catch (Exception error) {
      dB.log("No dependencies to load or error loading dependencies: " + error.getMessage());
    }
    dB.log("Loading plugins as dependencies for run-time compiler.");
    for (File f : pluginsFolder.listFiles(jarFilter)) {
      dependencies.add(f.getPath());
      dB.log("Loaded  " + f.getName());
    }
    dB.log("Attempting to add CraftBukkit as dependency for run-time compiler.");
    try {
      File file = new File(".");
      for (File f : file.listFiles(jarFilter)) {
        dependencies.add(f.getPath());
        dB.log("Loaded  " + f.getName());
      }
    } catch (Exception error) {
      dB.log("Could not find CraftBukkit jar or error reading it: " + error.getMessage());
    }

    if (!externalsFolder.exists() || externalsFolder.list().length == 0) {
      return;
    }
    try {
      File file = new File(denizen.getDataFolder() + File.separator + "externals");
      File[] files = file.listFiles(javaFilter);
      if (files != null && files.length > 0) {
        for (File f : files) {
          String fileName = f.getName();

          dB.log("Processing '" + fileName + "'... ");

          JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
          JavaSourceCompiler.CompilationUnit compilationUnit =
              javaSourceCompiler.createCompilationUnit();
          if (!dependencies.isEmpty()) {
            compilationUnit.addClassPathEntries(dependencies);
          }

          try {
            compilationUnit.addJavaSource(
                fileName.replace(".java", ""), readFile(f.getAbsolutePath()));
            ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
            Class<dExternal> load =
                (Class<dExternal>) classLoader.loadClass(fileName.replace(".java", ""));
            dExternal loadedClass = load.newInstance();
            loadedClass.load();
            loadedExternals.add(loadedClass);
          } catch (Exception e) {
            if (e instanceof IllegalStateException) {
              dB.echoError("No JDK found! External .java files will not be loaded.");
              dB.echoError(e);
            } else {
              dB.echoError(ChatColor.RED + "Error compiling " + fileName + "!");
              dB.echoError(e);
            }
          }
        }
        dB.echoApproval("All externals loaded!");
      }
    } catch (Exception error) {
      dB.echoError(error);
    }
  }
Beispiel #5
0
 public RuntimeCompiler(Denizen denizen) {
   this.denizen = denizen;
   this.externalsFolder = new File(denizen.getDataFolder() + File.separator + "externals");
 }
Beispiel #6
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;
  }