Skip to content

Commit 4bcee54

Browse files
committed
Implement MultiverseInventoriesApi
1 parent 723a8fd commit 4bcee54

4 files changed

Lines changed: 132 additions & 2 deletions

File tree

src/main/java/org/mvplugins/multiverse/inventories/MultiverseInventories.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public final void onEnable() {
123123
this.dupingPatch = InventoriesDupingPatch.enableDupingPatch(this);
124124
this.playerNamesMapperProvider.get().loadMap();
125125

126+
// Init api
127+
MultiverseInventoriesApi.init(this.serviceLocator);
128+
126129
Logging.config("Version %s (API v%s) Enabled - By %s",
127130
this.getDescription().getVersion(), getVersionAsNumber(), StringFormatter.joinAnd(this.getDescription().getAuthors()));
128131
}
@@ -143,6 +146,7 @@ public void onDisable() {
143146
}
144147
}
145148

149+
MultiverseInventoriesApi.shutdown();
146150
this.dupingPatch.disable();
147151
this.shutdownDependencyInjection();
148152
Logging.shutdown();
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.mvplugins.multiverse.inventories;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.mvplugins.multiverse.core.inject.PluginServiceLocator;
5+
import org.mvplugins.multiverse.inventories.config.InventoriesConfig;
6+
import org.mvplugins.multiverse.inventories.dataimport.DataImportManager;
7+
import org.mvplugins.multiverse.inventories.profile.PlayerNamesMapper;
8+
import org.mvplugins.multiverse.inventories.profile.ProfileCacheManager;
9+
import org.mvplugins.multiverse.inventories.profile.ProfileDataSource;
10+
import org.mvplugins.multiverse.inventories.profile.container.ProfileContainerStoreProvider;
11+
import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager;
12+
13+
import java.util.Objects;
14+
15+
/**
16+
* Provides access to the Multiverse-Inventories API.
17+
*/
18+
public final class MultiverseInventoriesApi {
19+
20+
private static MultiverseInventoriesApi instance;
21+
22+
static void init(@NotNull PluginServiceLocator serviceLocator) {
23+
if (instance != null) {
24+
throw new IllegalStateException("MultiverseCoreApi has already been initialized!");
25+
}
26+
instance = new MultiverseInventoriesApi(serviceLocator);
27+
}
28+
29+
static void shutdown() {
30+
instance = null;
31+
}
32+
33+
/**
34+
* Gets the MultiverseInventoriesApi. This will throw an exception if the Multiverse-Inventories has not been initialized.
35+
*
36+
* @return The MultiverseInventoriesApi
37+
*/
38+
public static @NotNull MultiverseInventoriesApi get() {
39+
if (instance == null) {
40+
throw new IllegalStateException("MultiverseInventoriesApi has not been initialized!");
41+
}
42+
return instance;
43+
}
44+
45+
private final PluginServiceLocator serviceLocator;
46+
47+
private MultiverseInventoriesApi(@NotNull PluginServiceLocator serviceProvider) {
48+
this.serviceLocator = serviceProvider;
49+
}
50+
51+
/**
52+
* Gets instance of our DataImportManager api.
53+
*
54+
* @return The DataImportManager instance
55+
*/
56+
public @NotNull DataImportManager getDataImportManager() {
57+
return Objects.requireNonNull(serviceLocator.getService(DataImportManager.class));
58+
}
59+
60+
/**
61+
* Gets instance of our InventoriesConfig api.
62+
*
63+
* @return The InventoriesConfig instance
64+
*/
65+
public @NotNull InventoriesConfig getInventoriesConfig() {
66+
return Objects.requireNonNull(serviceLocator.getService(InventoriesConfig.class));
67+
}
68+
69+
/**
70+
* Gets instance of our PlayerNamesMapper api.
71+
*
72+
* @return The PlayerNamesMapper instance
73+
*/
74+
public @NotNull PlayerNamesMapper getPlayerNamesMapper() {
75+
return Objects.requireNonNull(serviceLocator.getService(PlayerNamesMapper.class));
76+
}
77+
78+
/**
79+
* Gets instance of our ProfileCacheManager api.
80+
*
81+
* @return The ProfileCacheManager instance
82+
*/
83+
public @NotNull ProfileCacheManager getProfileCacheManager() {
84+
return Objects.requireNonNull(serviceLocator.getService(ProfileCacheManager.class));
85+
}
86+
87+
/**
88+
* Gets instance of our ProfileContainerStoreProvider api.
89+
*
90+
* @return The ProfileContainerStoreProvider instance
91+
*/
92+
public @NotNull ProfileContainerStoreProvider getProfileContainerStoreProvider() {
93+
return Objects.requireNonNull(serviceLocator.getService(ProfileContainerStoreProvider.class));
94+
}
95+
96+
/**
97+
* Gets instance of our ProfileDataSource api.
98+
*
99+
* @return The ProfileDataSource instance
100+
*/
101+
public @NotNull ProfileDataSource getProfileDataSource() {
102+
return Objects.requireNonNull(serviceLocator.getService(ProfileDataSource.class));
103+
}
104+
105+
/**
106+
* Gets instance of our WorldGroupManager api.
107+
*
108+
* @return The WorldGroupManager instance
109+
*/
110+
public @NotNull WorldGroupManager getWorldGroupManager() {
111+
return Objects.requireNonNull(serviceLocator.getService(WorldGroupManager.class));
112+
}
113+
114+
/**
115+
* Gets the instance of Multiverse-Inventories's PluginServiceLocator.
116+
* <br/>
117+
* You can use this to hook into the hk2 dependency injection system used by Multiverse-Inventories.
118+
*
119+
* @return The Multiverse-Inventories's PluginServiceLocator
120+
*/
121+
public @NotNull PluginServiceLocator getServiceLocator() {
122+
return serviceLocator;
123+
}
124+
}

src/main/java/org/mvplugins/multiverse/inventories/profile/PlayerNamesMapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public final class PlayerNamesMapper {
2929
private static PlayerNamesMapper instance;
3030

3131
public static PlayerNamesMapper getInstance() {
32-
return Objects.requireNonNull(instance);
32+
if (instance == null) {
33+
throw new IllegalStateException("Player names mapper has not been initialized yet.");
34+
}
35+
return instance;
3336
}
3437

3538
private static final String FILENAME = "playernames.json";

src/main/java/org/mvplugins/multiverse/inventories/profile/group/WorldGroupManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,3 @@ public sealed interface WorldGroupManager permits AbstractWorldGroupManager {
115115
*/
116116
void recalculateApplicableShares();
117117
}
118-

0 commit comments

Comments
 (0)