Class SoulPowerCompiler

java.lang.Object
valorless.rarespawns.compilers.Compiler
valorless.rarespawns.compilers.SoulPowerCompiler

public class SoulPowerCompiler extends Compiler
Compiles, caches, and invokes Soul Power scripts located under the plugin's data folder (soulpowers/).

Responsibilities:

  • Discover .java source files in soulpowers/
  • Compile them asynchronously and cache the resulting classes
  • Expose reflective invocation for executing a power
The compile step runs off the main thread; use isReady() to check when the registry is available.

  • Field Details

    • ready

      protected static Boolean ready
      Indicates whether the async compilation stage has finished and the registry is ready for lookups and execution.
  • Constructor Details

    • SoulPowerCompiler

      public SoulPowerCompiler()
  • Method Details

    • isReady

      public static Boolean isReady()
      Returns whether the async loading/compiling has completed.
      Returns:
      true if powers are compiled and cached; false otherwise
    • getPowers

      public static HashMap<String,Class<? extends SoulPower>> getPowers()
      Provides a shallow copy of the compiled powers registry.
      Returns:
      a copy of the ID -> Class mapping
    • getPower

      public static Class<? extends SoulPower> getPower(String id)
      Gets the compiled class for a Soul Power by ID.
      Parameters:
      id - the power ID (typically the filename without extension)
      Returns:
      the compiled class, or null if not present
    • addPower

      public static boolean addPower(String id, Class<? extends SoulPower> instance) throws Exception
      Adds a compiled Soul Power class to the registry.
      Parameters:
      id - the power ID (must be non-empty)
      instance - the compiled class implementing SoulPower
      Returns:
      true if added; false if duplicate or error
      Throws:
      Exception - if validation fails (id null/empty) — errors are caught internally and false is returned
    • reload

      public static void reload()
      Clears and asynchronously recompiles all Soul Power scripts from the soulpowers/ directory, then updates the in-memory registry. Logs load time and cleans up temp files afterward.
    • readFile

      public static String readFile(String filename)
      Reads a source file from the soulpowers/ folder into a single String.
      Parameters:
      filename - the base name without .java extension
      Returns:
      the file contents, or null if not found
    • listPowers

      public static List<String> listPowers()
      Retrieves the names (without extension) of all .java files in the soulpowers/ folder within the plugin's data directory.
      Returns:
      a lexicographically sorted list of power file names
    • usePower

      public static void usePower(String id, ItemData data, org.bukkit.entity.Player player) throws Exception
      Creates an instance of the compiled power by ID and invokes its execute method with the provided item data and player.
      Parameters:
      id - the power ID
      data - the item's source data to pass to the power
      player - the player context for execution
      Throws:
      Exception - if the class cannot be found/instantiated or reflection fails
    • cleanupTemporaryFiles

      public static void cleanupTemporaryFiles()
      Removes temporary .java files from the soulpowers/compiled directory.