Skip to content

Implement MultiversePortalsApi #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ public void onEnable() {

// Register our events AFTER the config.
this.registerEvents();

getServer().getPluginManager().registerEvents(new WorldEditPluginListener(), this);

MultiversePortalsApi.init(this.serviceLocator);

Logging.log(true, Level.INFO, " Enabled - By %s", StringFormatter.joinAnd(getDescription().getAuthors()));
}

Expand Down Expand Up @@ -321,6 +322,7 @@ public boolean savePortalsConfig() {
}

public void onDisable() {
MultiversePortalsApi.shutdown();
shutdownDependencyInjection();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.mvplugins.multiverse.portals;

import org.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.core.inject.PluginServiceLocator;
import org.mvplugins.multiverse.portals.utils.PortalFiller;
import org.mvplugins.multiverse.portals.utils.PortalManager;

import java.util.Objects;

public class MultiversePortalsApi {

private static MultiversePortalsApi instance;

static void init(@NotNull PluginServiceLocator serviceLocator) {
if (instance != null) {
throw new IllegalStateException("MultiversePortalsApi has already been initialized!");
}
instance = new MultiversePortalsApi(serviceLocator);
}

static void shutdown() {
instance = null;
}

/**
* Gets the MultiversePortalsApi. This will throw an exception if the Multiverse-Portals has not been initialized.
*
* @return The MultiversePortalsApi
*/
public static @NotNull MultiversePortalsApi get() {
if (instance == null) {
throw new IllegalStateException("MultiversePortalsApi has not been initialized!");
}
return instance;
}

private final PluginServiceLocator serviceLocator;

private MultiversePortalsApi(@NotNull PluginServiceLocator serviceProvider) {
this.serviceLocator = serviceProvider;
}

/**
* Gets the instance of the PortalFiller.
*
* @return The PortalFiller instance
*/
public @NotNull PortalFiller getPortalFiller() {
return Objects.requireNonNull(serviceLocator.getService(PortalFiller.class));
}

/**
* Gets the instance of the PortalManager.
*
* @return The PortalManager instance
*/
public @NotNull PortalManager getPortalManager() {
return Objects.requireNonNull(serviceLocator.getService(PortalManager.class));
}

/**
* Gets the instance of Multiverse-Portals's PluginServiceLocator.
* <br/>
* You can use this to hook into the hk2 dependency injection system used by Multiverse-Portals.
*
* @return The Multiverse-Portals's PluginServiceLocator
*/
public @NotNull PluginServiceLocator getServiceLocator() {
return serviceLocator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
@Service
public class DisplayUtils {

private final PortalManager portalManager;
private final WorldManager worldManager;
private final MVEconomist economist;

@Inject
DisplayUtils(
@NotNull PortalManager portalManager,
@NotNull WorldManager worldManager,
@NotNull MVEconomist economist) {
this.portalManager = portalManager;
this.worldManager = worldManager;
this.economist = economist;
}
Expand Down
Loading