Class ExpansionManager

java.lang.Object
me.hsgamer.hscore.expansion.common.ExpansionManager

public class ExpansionManager extends Object
A class that manages all Expansions
  • Field Details

    • DEFAULT_EXPANSION_FACTORY

      public static final Function<ExpansionClassLoader,Expansion> DEFAULT_EXPANSION_FACTORY
      The default factory to create a new instance of the main class using the no-args constructor
    • classLoaders

      protected final Map<String,ExpansionClassLoader> classLoaders
      The class loader map keyed expansion's id, valued expansion's class loader
  • Constructor Details

    • ExpansionManager

      public ExpansionManager(@NotNull @NotNull File expansionsDir, @NotNull @NotNull Function<JarFile,ExpansionDescription> descriptionFactory, @NotNull @NotNull ClassLoader parentClassLoader)
      Create a new expansion manager
      Parameters:
      expansionsDir - the directory to store expansion files
      descriptionFactory - the factory to load description
      parentClassLoader - the parent class loader to load all expansions
    • ExpansionManager

      public ExpansionManager(@NotNull @NotNull File expansionsDir, @NotNull @NotNull Function<JarFile,ExpansionDescription> descriptionFactory)
      Create a new expansion manager
      Parameters:
      expansionsDir - the directory to store expansion files
      descriptionFactory - the factory to load description
  • Method Details

    • getExpansionsDir

      @NotNull public final @NotNull File getExpansionsDir()
      Get the expansion directory
      Returns:
      the directory
    • getParentClassLoader

      @NotNull public @NotNull ClassLoader getParentClassLoader()
      Get the parent class loader
      Returns:
      the parent class loader
    • addStateListener

      public void addStateListener(@NotNull @NotNull BiConsumer<ExpansionClassLoader,ExpansionState> listener)
      Add a new state listener
      Parameters:
      listener - the listener
    • removeStateListener

      public void removeStateListener(@NotNull @NotNull BiConsumer<ExpansionClassLoader,ExpansionState> listener)
      Remove a state listener
      Parameters:
      listener - the listener
    • getClassLoaders

      public Map<String,ExpansionClassLoader> getClassLoaders()
      Get all loaded expansion class loaders
      Returns:
      the map of expansion class loaders
    • setExceptionHandler

      public void setExceptionHandler(@NotNull @NotNull Consumer<Throwable> exceptionHandler)
      Set the exception handler
      Parameters:
      exceptionHandler - the exception handler
    • setSortAndFilterFunction

      public void setSortAndFilterFunction(@NotNull @NotNull UnaryOperator<Map<String,ExpansionClassLoader>> sortAndFilterFunction)
      Set the function to sort and filter the ExpansionClassLoaders
      Parameters:
      sortAndFilterFunction - the function
    • setExpansionFactory

      public void setExpansionFactory(@NotNull @NotNull Function<ExpansionClassLoader,Expansion> expansionFactory)
      Set the factory to create Expansion from ExpansionClassLoader
      Parameters:
      expansionFactory - the factory
    • loadExpansions

      public void loadExpansions()
      Load all expansions from the expansion directory. Also call Expansion.onLoad()
    • enableExpansions

      public void enableExpansions()
      Enable all loaded expansions
    • disableExpansions

      public void disableExpansions()
      Disable all enabled expansions
    • clearExpansions

      public void clearExpansions()
      Clear all expansions
    • call

      public void call(Consumer<Expansion> consumer)
      Call the consumer for all enabled expansions
      Parameters:
      consumer - the consumer
    • call

      public <T> void call(Class<T> clazz, Consumer<T> consumer)
      Call the consumer for all enabled expansions that match the specific class
      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the class to limit what expansion can be called
      consumer - the consumer to call the cast expansion
    • getExpansionClassLoader

      public Optional<ExpansionClassLoader> getExpansionClassLoader(@NotNull @NotNull String name)
      Get the loaded expansion class loader
      Parameters:
      name - the name of the expansion
      Returns:
      the expansion class loader
    • getExpansion

      public Optional<Expansion> getExpansion(@NotNull @NotNull String name)
      Get the loaded expansion
      Parameters:
      name - the name of the expansion
      Returns:
      the expansion
    • getEnabledExpansions

      @NotNull public @NotNull Map<String,Expansion> getEnabledExpansions()
      Get all enabled expansions
      Returns:
      the enabled expansions