|
| 1 | +package org.mvplugins.multiverse.portals; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.mvplugins.multiverse.core.inject.PluginServiceLocator; |
| 5 | +import org.mvplugins.multiverse.portals.utils.PortalFiller; |
| 6 | +import org.mvplugins.multiverse.portals.utils.PortalManager; |
| 7 | + |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +public class MultiversePortalsApi { |
| 11 | + |
| 12 | + private static MultiversePortalsApi instance; |
| 13 | + |
| 14 | + static void init(@NotNull PluginServiceLocator serviceLocator) { |
| 15 | + if (instance != null) { |
| 16 | + throw new IllegalStateException("MultiversePortalsApi has already been initialized!"); |
| 17 | + } |
| 18 | + instance = new MultiversePortalsApi(serviceLocator); |
| 19 | + } |
| 20 | + |
| 21 | + static void shutdown() { |
| 22 | + instance = null; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets the MultiversePortalsApi. This will throw an exception if the Multiverse-Portals has not been initialized. |
| 27 | + * |
| 28 | + * @return The MultiversePortalsApi |
| 29 | + */ |
| 30 | + public static @NotNull MultiversePortalsApi get() { |
| 31 | + if (instance == null) { |
| 32 | + throw new IllegalStateException("MultiversePortalsApi has not been initialized!"); |
| 33 | + } |
| 34 | + return instance; |
| 35 | + } |
| 36 | + |
| 37 | + private final PluginServiceLocator serviceLocator; |
| 38 | + |
| 39 | + private MultiversePortalsApi(@NotNull PluginServiceLocator serviceProvider) { |
| 40 | + this.serviceLocator = serviceProvider; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets the instance of the PortalFiller. |
| 45 | + * |
| 46 | + * @return The PortalFiller instance |
| 47 | + */ |
| 48 | + public @NotNull PortalFiller getPortalFiller() { |
| 49 | + return Objects.requireNonNull(serviceLocator.getService(PortalFiller.class)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets the instance of the PortalManager. |
| 54 | + * |
| 55 | + * @return The PortalManager instance |
| 56 | + */ |
| 57 | + public @NotNull PortalManager getPortalManager() { |
| 58 | + return Objects.requireNonNull(serviceLocator.getService(PortalManager.class)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the instance of Multiverse-Portals's PluginServiceLocator. |
| 63 | + * <br/> |
| 64 | + * You can use this to hook into the hk2 dependency injection system used by Multiverse-Portals. |
| 65 | + * |
| 66 | + * @return The Multiverse-Portals's PluginServiceLocator |
| 67 | + */ |
| 68 | + public @NotNull PluginServiceLocator getServiceLocator() { |
| 69 | + return serviceLocator; |
| 70 | + } |
| 71 | +} |
0 commit comments