-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInvSwitcher.java
More file actions
263 lines (231 loc) · 9.84 KB
/
Copy pathInvSwitcher.java
File metadata and controls
263 lines (231 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.wasteofplastic.invswitcher;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import com.wasteofplastic.invswitcher.commands.admin.AdminMoneyCommand;
import com.wasteofplastic.invswitcher.commands.user.BalanceCommand;
import com.wasteofplastic.invswitcher.commands.user.PayCommand;
import com.wasteofplastic.invswitcher.economy.InvEconomy;
import com.wasteofplastic.invswitcher.listeners.PlayerListener;
import net.milkbowl.vault.economy.Economy;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
import world.bentobox.bentobox.hooks.VaultHook;
/**
* Inventory switcher for worlds. Switches advancements too.
*
* @author tastybento
*
*/
public class InvSwitcher extends Addon {
private Store store;
private Settings settings;
private final Config<Settings> config = new Config<>(this, Settings.class);
private Set<World> worlds = new HashSet<>();
private InvEconomy economy;
@Override
public void onLoad() {
// Save default config.yml
this.saveDefaultConfig();
// Load the plugin's config
this.loadSettings();
}
@Override
public void allLoaded() {
// Load worlds
worlds = getSettings().getWorlds()
.stream()
.map(String::toLowerCase)
.map(Bukkit::getWorld)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (worlds.isEmpty()) {
logWarning("Did not hook into any worlds - disabling addon!");
this.setState(State.DISABLED);
return;
}
// Add nethers and ends
Set<World> netherEnds = new HashSet<>();
log("Hooking into the following worlds:");
worlds.forEach(w -> {
log(w.getName());
World nether = Bukkit.getWorld(w.getName() + "_nether");
if (nether != null) {
netherEnds.add(nether);
log(nether.getName());
}
World end = Bukkit.getWorld(w.getName() + "_the_end");
if (end != null) {
netherEnds.add(end);
log(end.getName());
}
});
worlds.addAll(netherEnds);
// Create the store
store = new Store(this);
// Register the listeners
registerListener(new PlayerListener(this));
// Now that worlds are known, register the economy commands and placeholders. The economy
// provider itself was registered earlier, in onEnable, so it beats shop plugins that cache
// their Vault provider during their own startup.
if (economy != null) {
registerEconomyCommands();
}
}
@Override
public void onEnable() {
// Verify that we're not running on a YAML database
if (this.getPlugin().getSettings().getDatabaseType().equals(DatabaseType.YAML)) {
this.setState(State.DISABLED);
this.logError("This addon is incompatible with YAML database. Please use another type, like JSON.");
return;
}
// Register the Vault economy provider as early as possible (here in onEnable, not in
// allLoaded) so it is in place before economy-consuming plugins (e.g. QuickShop) resolve
// and cache their provider. The store, worlds and delegate are resolved lazily by
// InvEconomy, so they do not need to exist yet.
if (getSettings() != null && getSettings().isMoney()) {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
logError("options.money is enabled but the Vault plugin is not installed - per-world money disabled.");
} else {
registerEconomyProvider();
}
}
}
/**
* Creates and registers InvSwitcher's per-world economy at the highest Vault priority so it
* intercepts every economy call. Runs once. The provider is lazy - it resolves the store and
* the delegate economy on first use - so this can run before those are ready.
*/
private void registerEconomyProvider() {
if (economy != null) {
return;
}
economy = new InvEconomy(this);
Bukkit.getServicesManager().register(Economy.class, economy, getPlugin(), ServicePriority.Highest);
// BentoBox captured its VaultHook during early hook registration, before us, so it (and
// addons that use it, e.g. Bank) still points at the previous economy. Re-run the hook so
// it re-reads the now-highest provider (us). The VaultHook is a single shared instance held
// by those addons, so refreshing it updates them too.
refreshBentoBoxVaultHook();
// Dump the current economy provider chain (debug only) so it is clear we win the registration.
if (getSettings().isEconomyDebug()) {
logEconomyRegistrations();
}
}
/**
* Registers the economy commands and placeholders against the game modes whose worlds
* InvSwitcher manages. Called from allLoaded, once worlds are known.
*/
private void registerEconomyCommands() {
PhManager phManager = new PhManager(this);
getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> worlds.contains(gm.getOverWorld()))
.forEach(gm -> {
gm.getPlayerCommand().ifPresent(pc -> {
new BalanceCommand(this, pc);
new PayCommand(this, pc);
});
gm.getAdminCommand().ifPresent(ac -> new AdminMoneyCommand(this, ac));
if (!phManager.registerPlaceholders(gm)) {
logWarning("Could not register economy placeholders - no PlaceholderManager available.");
}
log("Per-world economy hooking into " + gm.getDescription().getName());
});
}
@Override
public void onDisable() {
// Unregister our economy so a reload does not stack providers
if (economy != null) {
Bukkit.getServicesManager().unregister(Economy.class, economy);
economy = null;
// Re-point BentoBox's VaultHook at whatever economy remains (e.g. EssentialsC)
refreshBentoBoxVaultHook();
}
// save cache
if (store != null) {
getStore().saveOnShutdown();
}
}
/**
* Re-runs BentoBox's VaultHook so it re-reads the highest-priority economy currently
* registered with the services manager. BentoBox addons such as Bank hold this same hook
* instance, so they pick up the change without needing to re-hook themselves.
* <p>
* If BentoBox has no stored VaultHook, its early Vault hook failed because no economy was
* registered when BentoBox's early hooks ran (the standalone case: we are the only economy).
* A failed hook is discarded by {@code HooksManager}, so {@link world.bentobox.bentobox.BentoBox#getVault()}
* stays empty and there is nothing to refresh. Now that our provider is live, register a fresh
* VaultHook so {@code getVault()} is populated for Bank and the rest of BentoBox.
*/
private void refreshBentoBoxVaultHook() {
Optional<VaultHook> vault = getPlugin().getVault();
if (vault.isPresent()) {
vault.get().hook();
} else {
getPlugin().getHooks().registerHook(new VaultHook());
}
}
/**
* Logs all registered Vault economy providers (highest priority first) and which one Vault
* will hand out. Useful for confirming InvSwitcher won the registration and for spotting
* consumers that cached a different provider before we registered.
*/
private void logEconomyRegistrations() {
log("Vault economy providers now registered (used by new lookups):");
for (RegisteredServiceProvider<Economy> r : Bukkit.getServicesManager().getRegistrations(Economy.class)) {
log(" - " + r.getProvider().getName() + " (" + r.getProvider().getClass().getName()
+ ") priority=" + r.getPriority() + " registeredBy=" + r.getPlugin().getName());
}
RegisteredServiceProvider<Economy> top = Bukkit.getServicesManager().getRegistration(Economy.class);
if (top != null) {
log("Vault.getRegistration(Economy) returns: " + top.getProvider().getName() + " ("
+ top.getProvider().getClass().getName() + ")");
}
log("If a shop/economy plugin still uses the old balance, it cached its provider before now "
+ "and must be loaded after BentoBox (or re-resolve on ServiceRegisterEvent).");
}
/**
* @return the per-world economy, or null if money is disabled or Vault is absent
*/
public InvEconomy getEconomy() {
return economy;
}
/**
* @return the store
*/
public Store getStore() {
return store;
}
/**
* This method loads addon configuration settings in memory.
*/
private void loadSettings() {
this.settings = config.loadConfigObject();
if (this.settings == null) {
// Disable
this.logError("InvSwitcher settings could not load! Addon disabled.");
this.setState(State.DISABLED);
return;
}
// Save new version
this.config.saveConfigObject(settings);
}
public Settings getSettings() {
return this.settings;
}
/**
* Get the hooked worlds
* @return the worlds
*/
public Set<World> getWorlds() {
return worlds;
}
}