Class Exceptions

java.lang.Object
me.croabeast.common.util.Exceptions

public class Exceptions extends Object
A collection of utility methods for runtime checks and validations, such as verifying plugin availability and enforcing preconditions.

This class provides methods to:

  • Check if a plugin is installed or enabled on the server.
  • Require certain conditions on objects, throwing exceptions if violated.
  • Ensure that code executing on behalf of one plugin cannot be called by another.

See Also:
  • Constructor Details

    • Exceptions

      public Exceptions()
  • Method Details

    • isPluginEnabled

      public boolean isPluginEnabled(@NotNull @NotNull String name)
      Checks if the given plugin is enabled (loaded and running).
      Parameters:
      name - the name of the plugin (case-sensitive)
      Returns:
      true if the plugin is installed and enabled; false otherwise
    • arePluginsEnabled

      @Deprecated public boolean arePluginsEnabled(boolean inclusive, @NotNull @NotNull Collection<String> names)
      Deprecated.
      use anyPluginEnabled(Collection) instead for clarity
      Checks if at least one of the specified plugins is enabled.
      Parameters:
      names - the plugin names to check
      Returns:
      true if any plugin in names is enabled; false if none are enabled or if names is empty
    • anyPluginEnabled

      public boolean anyPluginEnabled(@NotNull @NotNull Collection<String> names)
      Returns true if any of the provided plugin names correspond to an enabled plugin.
      Parameters:
      names - the plugin names to check
      Returns:
      true if at least one plugin is enabled; false otherwise
    • allPluginsEnabled

      public boolean allPluginsEnabled(@NotNull @NotNull Collection<String> names)
      Returns true only if all of the provided plugin names correspond to enabled plugins.
      Parameters:
      names - the plugin names to check
      Returns:
      true if every plugin is enabled; false otherwise
    • validate

      public <T, X extends Throwable> T validate(T object, Predicate<T> predicate, Supplier<X> supplier) throws X
      Ensures that the given object satisfies the provided predicate.

      If the predicate test fails, the supplied exception is thrown.

      Type Parameters:
      T - the type of the input object
      Parameters:
      object - the object to validate (must not be null)
      predicate - the condition to test
      supplier - supplies the exception to throw if the test fails
      Returns:
      the validated object
      Throws:
      X - the exception returned by supplier
    • validate

      @NotNull public <T> T validate(T object, Predicate<T> predicate, String errorMessage) throws IllegalStateException
      Ensures that the given object satisfies the provided predicate.

      If the predicate test fails, an IllegalStateException is thrown.

      Type Parameters:
      T - the type of the input object
      Parameters:
      object - the object to validate (must not be null)
      predicate - the condition to test
      errorMessage - the error message to be shown
      Returns:
      the validated object
      Throws:
      IllegalStateException - if the predicate test fails
    • validate

      @NotNull public <T> T validate(T object, Predicate<T> predicate) throws IllegalStateException
      Ensures that the given object satisfies the provided predicate.

      If the predicate test fails, an IllegalStateException is thrown.

      Type Parameters:
      T - the type of the input object
      Parameters:
      object - the object to validate (must not be null)
      predicate - the condition to test
      Returns:
      the validated object
      Throws:
      IllegalStateException - if the predicate test fails
    • validate

      @NotNull public <T> T validate(T object, boolean b)
      Ensures that the given object satisfies the provided predicate.

      If the predicate test fails, an IllegalStateException is thrown.

      Type Parameters:
      T - the type of the input object
      Parameters:
      object - the object to validate (must not be null)
      b - the condition to test
      Returns:
      the validated object
      Throws:
      IllegalStateException - if the predicate test fails
    • requirePluginAccess

      public void requirePluginAccess(@Nullable @Nullable org.bukkit.plugin.Plugin plugin, @NotNull @NotNull Class<?> clazz)
      Ensures that the caller of the protected API belongs to the given plugin.

      If the calling class was not provided by plugin, throws the supplied exception.

      Parameters:
      plugin - the plugin that must own the caller class (might be null)
      clazz - the class invoking the protected API
      Throws:
      IllegalStateException - if clazz was not provided by plugin
    • getCallerClass

      @Deprecated public Class<?> getCallerClass(int depth) throws ClassNotFoundException
      Deprecated.
      this approach is fragile; consider passing explicit context instead
      Retrieves the class of a caller up the current thread's stack trace.

      Edge cases: May return unexpected results under aggressive JVM inlining.

      Parameters:
      depth - the zero-based index into the stack trace (0 is this method)
      Returns:
      the Class object at the requested stack frame
      Throws:
      ClassNotFoundException - if the class name cannot be resolved