Class Compiler

java.lang.Object
valorless.rarespawns.compilers.Compiler
Direct Known Subclasses:
AbilityCompiler, SoulPowerCompiler

public class Compiler extends Object
Runtime Java source compiler and loader for RareSpawns scripts.

Compiles .java files located under the plugin data folder for different script types (e.g., abilities and soul powers), then loads the resulting class into memory using a dedicated class loader. The compiler configures a classpath that includes the plugin, server, Bukkit API, ValorlessUtils, EntityData, optional NMS (if present), and optionally additional entries from advanced.yml when the "i-understand" flag is true.

  • Field Details

    • compiler

      protected final JavaCompiler compiler
      Java compiler instance for dynamic compilation.
    • fileManager

      protected final StandardJavaFileManager fileManager
      File manager for handling Java source files.
    • seperator

      protected static final String seperator
      Path separator for the current operating system.
    • rarespawns

      protected static final String rarespawns
      RareSpawns classpath.
    • server

      protected static final String server
      Server classpath (java.class.path).
    • bukkit

      protected static final String bukkit
      Bukkit classpath.
    • entityData

      protected static final String entityData
      EntityData classpath.
    • nms

      protected static final String nms
      NMS (net.minecraft.server) classpath, if available.
    • valorlessutils

      protected static final String valorlessutils
      ValorlessUtils classpath.
    • classpath

      protected static final String classpath
      Combined classpath for compilation.
  • Constructor Details

    • Compiler

      public Compiler()
      Creates a compiler instance backed by the system JDK compiler and a standard file manager. Requires the server to run on a JDK (not a JRE).
  • Method Details

    • compileAndCacheCode

      public Class<?> compileAndCacheCode(String id, CompilerType type) throws Exception
      Compiles the source associated with the provided id in the folder designated by the given CompilerType, writes a temporary source file with additional imports, and loads the resulting class via a type-specific dynamic class loader.

      The compilation classpath is built from internal paths (plugin/server/Bukkit/etc.) and may be extended from advanced.yml if "i-understand" is true.

      Parameters:
      id - filename without extension under the type path (e.g. "Fireball")
      type - which script category to compile (determines subfolder and loader)
      Returns:
      the loaded Class if compilation and loading succeed; otherwise null
      Throws:
      Exception - if the Java compiler is unavailable or compilation fails
    • getPluginJar

      public static File getPluginJar()
      Gets the plugin's JAR file location.
      Returns:
      The File object representing the plugin JAR.
    • getBukkit

      public static File getBukkit()
      Gets the Bukkit JAR file location.
      Returns:
      The File object representing the Bukkit JAR.
    • getNMS

      public static String getNMS()
      Gets the NMS (net.minecraft.server) JAR file location if available.
      Returns:
      The absolute path to the NMS JAR, or empty string if not found.
    • getEntityData

      public static File getEntityData()
      Gets the EntityData JAR file location.
      Returns:
      The File object representing the EntityData JAR.
    • getPluginClasspath

      public static String getPluginClasspath(String plugin)
      Gets the classpath for a given plugin located in the plugins folder.
      Parameters:
      plugin - The name of the plugin (e.g., "SomePlugin.jar").
      Returns:
      The absolute path to the plugin JAR, or null if an error occurs.