diff --git a/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java b/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java index 9e3dd580d..dd1edd4dc 100644 --- a/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java +++ b/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java @@ -15,6 +15,7 @@ import jakarta.inject.Provider; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; @@ -238,7 +239,11 @@ private void logEnableMessage() { * Gets the MultiverseCoreApi * * @return The MultiverseCoreApi + * + * @deprecated Use {@link MultiverseCoreApi#get()} directly. */ + @Deprecated(since = "5.1", forRemoval = true) + @ApiStatus.ScheduledForRemoval(inVersion = "6.0") public MultiverseCoreApi getApi() { return MultiverseCoreApi.get(); } diff --git a/src/main/java/org/mvplugins/multiverse/core/MultiverseCoreApi.java b/src/main/java/org/mvplugins/multiverse/core/MultiverseCoreApi.java index 3e486914a..b185613ce 100644 --- a/src/main/java/org/mvplugins/multiverse/core/MultiverseCoreApi.java +++ b/src/main/java/org/mvplugins/multiverse/core/MultiverseCoreApi.java @@ -2,6 +2,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.ServicePriority; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.mvplugins.multiverse.core.anchor.AnchorManager; import org.mvplugins.multiverse.core.config.CoreConfig; @@ -15,7 +16,10 @@ import org.mvplugins.multiverse.core.world.biomeprovider.BiomeProviderFactory; import org.mvplugins.multiverse.core.world.generators.GeneratorProvider; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; +import java.util.function.Consumer; /** * Provides access to the MultiverseCore API. @@ -23,6 +27,7 @@ public class MultiverseCoreApi { private static MultiverseCoreApi instance; + private static final List> whenLoadedCallbacks = new ArrayList<>(); static void init(@NotNull MultiverseCore multiverseCore) { if (instance != null) { @@ -30,6 +35,9 @@ static void init(@NotNull MultiverseCore multiverseCore) { } instance = new MultiverseCoreApi(multiverseCore.getServiceLocator()); Bukkit.getServicesManager().register(MultiverseCoreApi.class, instance, multiverseCore, ServicePriority.Normal); + + whenLoadedCallbacks.forEach(c -> c.accept(instance)); + whenLoadedCallbacks.clear(); } static void shutdown() { @@ -37,10 +45,49 @@ static void shutdown() { instance = null; } + /** + * Hook your plugin into the MultiverseCoreApi here to ensure you only start using the API after it has been initialized. + * Use this if you know your plugin may load before Multiverse-Core is fully initialized. + *
+ * This handy method removes the need for you to check with plugin manager or listen to plugin enable event. + *
+ * Callback will be called immediately if the MultiverseCoreApi has already been initialized. + * + * @param consumer The callback to execute when the MultiverseCoreApi has been initialized. + * + * @since 5.1 + */ + @ApiStatus.AvailableSince("5.1") + public static void whenLoaded(@NotNull Consumer consumer) { + if (instance != null) { + consumer.accept(instance); + } else { + whenLoadedCallbacks.add(consumer); + } + } + + /** + * Checks if the MultiverseCoreApi has been initialized. + * + * @return True if the MultiverseCoreApi has been initialized, false otherwise + * + * @since 5.1 + */ + @ApiStatus.AvailableSince("5.1") + public static boolean isLoaded() { + return instance != null; + } + /** * Gets the MultiverseCoreApi. This will throw an exception if the Multiverse-Core has not been initialized. + *
+ * You can check if the MultiverseCoreApi has been initialized with {@link #isLoaded()} before using this method. + *
+ * Alternatively, you can use {@link #whenLoaded(Consumer)} to hook into the MultiverseCoreApi if your plugin may + * load before Multiverse-Core is fully initialized. * * @return The MultiverseCoreApi + * @throws IllegalStateException if the MultiverseCoreApi has not been initialized */ public static @NotNull MultiverseCoreApi get() { if (instance == null) { diff --git a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java index 57c597b9f..11d961afb 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java @@ -660,10 +660,11 @@ public boolean isShowingDonateMessage() { } /** - * Gets the underlying config file object + * Gets the underlying config file object. For internal use only. * * @return The config file */ + @ApiStatus.Internal public FileConfiguration getConfig() { return configHandle.getConfig(); }