Skip to content

Commit a789354

Browse files
authored
Merge pull request #672 from Multiverse/feat/api
Implement MultiversePortalsApi
2 parents 968a915 + e3e0880 commit a789354

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/main/java/org/mvplugins/multiverse/portals/MultiversePortals.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ public void onEnable() {
127127

128128
// Register our events AFTER the config.
129129
this.registerEvents();
130-
131130
getServer().getPluginManager().registerEvents(new WorldEditPluginListener(), this);
132131

132+
MultiversePortalsApi.init(this.serviceLocator);
133+
133134
Logging.log(true, Level.INFO, " Enabled - By %s", StringFormatter.joinAnd(getDescription().getAuthors()));
134135
}
135136

@@ -321,6 +322,7 @@ public boolean savePortalsConfig() {
321322
}
322323

323324
public void onDisable() {
325+
MultiversePortalsApi.shutdown();
324326
shutdownDependencyInjection();
325327
}
326328

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

src/main/java/org/mvplugins/multiverse/portals/utils/DisplayUtils.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
@Service
1414
public class DisplayUtils {
1515

16-
private final PortalManager portalManager;
1716
private final WorldManager worldManager;
1817
private final MVEconomist economist;
1918

2019
@Inject
2120
DisplayUtils(
22-
@NotNull PortalManager portalManager,
2321
@NotNull WorldManager worldManager,
2422
@NotNull MVEconomist economist) {
25-
this.portalManager = portalManager;
2623
this.worldManager = worldManager;
2724
this.economist = economist;
2825
}

0 commit comments

Comments
 (0)