Package me.croabeast.common.util
Class Exceptions
java.lang.Object
me.croabeast.common.util.Exceptions
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanallPluginsEnabled(@NotNull Collection<String> names) Returnstrueonly if all of the provided plugin names correspond to enabled plugins.booleananyPluginEnabled(@NotNull Collection<String> names) Returnstrueif any of the provided plugin names correspond to an enabled plugin.booleanarePluginsEnabled(boolean inclusive, @NotNull Collection<String> names) Deprecated.Class<?> getCallerClass(int depth) Deprecated.this approach is fragile; consider passing explicit context insteadbooleanisPluginEnabled(@NotNull String name) Checks if the given plugin is enabled (loaded and running).voidrequirePluginAccess(@Nullable org.bukkit.plugin.Plugin plugin, @NotNull Class<?> clazz) Ensures that the caller of the protected API belongs to the given plugin.<T> Tvalidate(T object, boolean b) Ensures that the given object satisfies the provided predicate.<T> TEnsures that the given object satisfies the provided predicate.<T> TEnsures that the given object satisfies the provided predicate.<T,X extends Throwable>
TEnsures that the given object satisfies the provided predicate.
-
Constructor Details
-
Exceptions
public Exceptions()
-
-
Method Details
-
isPluginEnabled
Checks if the given plugin is enabled (loaded and running).- Parameters:
name- the name of the plugin (case-sensitive)- Returns:
trueif the plugin is installed and enabled;falseotherwise
-
arePluginsEnabled
@Deprecated public boolean arePluginsEnabled(boolean inclusive, @NotNull @NotNull Collection<String> names) Deprecated.useanyPluginEnabled(Collection)instead for clarityChecks if at least one of the specified plugins is enabled.- Parameters:
names- the plugin names to check- Returns:
trueif any plugin innamesis enabled;falseif none are enabled or ifnamesis empty
-
anyPluginEnabled
Returnstrueif any of the provided plugin names correspond to an enabled plugin.- Parameters:
names- the plugin names to check- Returns:
trueif at least one plugin is enabled;falseotherwise
-
allPluginsEnabled
Returnstrueonly if all of the provided plugin names correspond to enabled plugins.- Parameters:
names- the plugin names to check- Returns:
trueif every plugin is enabled;falseotherwise
-
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 testsupplier- supplies the exception to throw if the test fails- Returns:
- the validated object
- Throws:
X- the exception returned bysupplier
-
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
IllegalStateExceptionis thrown.- Type Parameters:
T- the type of the input object- Parameters:
object- the object to validate (must not be null)predicate- the condition to testerrorMessage- the error message to be shown- Returns:
- the validated object
- Throws:
IllegalStateException- if thepredicatetest fails
-
validate
Ensures that the given object satisfies the provided predicate.If the predicate test fails, an
IllegalStateExceptionis 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 thepredicatetest 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
IllegalStateExceptionis 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 thepredicatetest 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- ifclazzwas not provided byplugin
-
getCallerClass
Deprecated.this approach is fragile; consider passing explicit context insteadRetrieves 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
Classobject at the requested stack frame - Throws:
ClassNotFoundException- if the class name cannot be resolved
-
anyPluginEnabled(Collection)instead for clarity