Make PluginLocales injectable.

This commit is contained in:
Jeremy Wood 2023-03-29 00:51:54 -04:00
parent 2f834ebfd6
commit 40d5417629
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
4 changed files with 58 additions and 29 deletions

View File

@ -22,6 +22,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.commandtools.PluginLocales;
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.economy.MVEconomist;
@ -67,6 +68,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
private Provider<MetricsConfigurator> metricsConfiguratorProvider;
@Inject
private Provider<MVEconomist> economistProvider;
@Inject
private Provider<PluginLocales> pluginLocalesProvider;
// Counter for the number of plugins that have registered with us
private int pluginCount;
@ -173,13 +176,19 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
private void loadEconomist() {
Try.run(() -> economistProvider.get())
.onFailure(e -> Logging.severe("Failed to load economy integration", e));
.onFailure(e -> {
Logging.severe("Failed to load economy integration");
e.printStackTrace();
});
}
private void loadAnchors() {
Try.of(() -> anchorManagerProvider.get())
.onSuccess(AnchorManager::loadAnchors)
.onFailure(e -> Logging.severe("Failed to load anchors", e));
.onFailure(e -> {
Logging.severe("Failed to load anchors");
e.printStackTrace();
});
}
/**
@ -204,7 +213,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
serviceLocator.getAllServices(MultiverseCommand.class)
.forEach(commandManager::registerCommand);
})
.onFailure(e -> Logging.severe("Failed to register commands", e));
.onFailure(e -> {
Logging.severe("Failed to register commands");
e.printStackTrace();
});
}
/**
@ -212,12 +224,18 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
*/
private void setUpLocales() {
Try.of(() -> commandManagerProvider.get())
.andThenTry(commandManager -> {
.andThen(commandManager -> {
commandManager.usePerIssuerLocale(true, true);
commandManager.getLocales().addFileResClassLoader(this);
commandManager.getLocales().addMessageBundles("multiverse-core");
})
.onFailure(e -> Logging.severe("Failed to register locales", e));
.mapTry(commandManager -> pluginLocalesProvider.get())
.andThen(pluginLocales -> {
pluginLocales.addFileResClassLoader(this);
pluginLocales.addMessageBundles("multiverse-core");
})
.onFailure(e -> {
Logging.severe("Failed to register locales");
e.printStackTrace();
});
}
/**
@ -229,7 +247,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
serviceLocator.getAllServices(Destination.class)
.forEach(destinationsProvider::registerDestination);
})
.onFailure(e -> Logging.severe("Failed to register destinations", e));
.onFailure(e -> {
Logging.severe("Failed to register destinations");
e.printStackTrace();
});
}
/**
@ -239,7 +260,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
if (TestingMode.isDisabled()) {
// Load metrics
Try.of(() -> metricsConfiguratorProvider.get())
.onFailure(e -> Logging.severe("Failed to setup metrics", e));
.onFailure(e -> {
Logging.severe("Failed to setup metrics");
e.printStackTrace();
});
} else {
Logging.info("Metrics are disabled in testing mode.");
}
@ -260,7 +284,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
private void loadPlaceholderAPIIntegration() {
if(getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
Try.run(() -> serviceLocator.createAndInitialize(MultiverseCorePlaceholders.class))
.onFailure(e -> Logging.severe("Failed to load PlaceholderAPI integration.", e));
.onFailure(e -> {
Logging.severe("Failed to load PlaceholderAPI integration.");
e.printStackTrace();
});
}
}
@ -348,7 +375,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
return getConfigProvider().saveConfig()
.map(v -> true)
.recover(e -> {
Logging.severe(e.getMessage(), e);
Logging.severe(e.getMessage());
e.printStackTrace();
return false;
})
.get();

View File

@ -28,7 +28,6 @@ public class MVCommandManager extends PaperCommandManager {
private final CommandQueueManager commandQueueManager;
private final Provider<MVCommandContexts> commandContextsProvider;
private final Provider<MVCommandCompletions> commandCompletionsProvider;
private PluginLocales pluginLocales;
@Inject
public MVCommandManager(
@ -48,6 +47,13 @@ public class MVCommandManager extends PaperCommandManager {
MVCommandConditions.load(this, worldManager);
}
void loadLanguages(PluginLocales locales) {
if (this.locales == null) {
this.locales = locales;
this.locales.loadLanguages();
}
}
/**
* Gets class responsible for flag handling.
*
@ -57,21 +63,6 @@ public class MVCommandManager extends PaperCommandManager {
return flagsManager;
}
/**
* Gets class responsible for locale handling.
*
* @return A not-null {@link PluginLocales}.
*/
@Override
public PluginLocales getLocales() {
if (this.pluginLocales == null) {
this.pluginLocales = new PluginLocales(this);
this.locales = pluginLocales; // For parent class
this.pluginLocales.loadLanguages();
}
return this.pluginLocales;
}
/**
* Manager for command that requires /mv confirm before execution.
*

View File

@ -1,14 +1,16 @@
package com.onarandombox.MultiverseCore.commandtools;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.BukkitLocales;
import com.onarandombox.MultiverseCore.utils.file.FileResClassLoader;
import jakarta.inject.Inject;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
/**
* Locale manager with additional methods for loading locales from plugin's locales folder.
*/
@Service
public class PluginLocales extends BukkitLocales {
private static final String DEFAULT_LOCALE_FOLDER_PATH = "locales";
@ -18,8 +20,10 @@ public class PluginLocales extends BukkitLocales {
*
* @param manager The command manager.
*/
public PluginLocales(BukkitCommandManager manager) {
@Inject
public PluginLocales(MVCommandManager manager) {
super(manager);
manager.loadLanguages(this);
}
/**

View File

@ -9,6 +9,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager
import com.onarandombox.MultiverseCore.api.SafeTTeleporter
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand
import com.onarandombox.MultiverseCore.commandtools.PluginLocales
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider
import com.onarandombox.MultiverseCore.economy.MVEconomist
import com.onarandombox.MultiverseCore.listeners.MVChatListener
@ -142,4 +143,9 @@ class InjectionTest : TestWithMockBukkit() {
// Also making sure this is not loaded automatically since it's supposed to be disabled during tests
assertNull(multiverseCore.getService(MetricsConfigurator::class.java))
}
@Test
fun `PluginLocales is available as a service`() {
assertNotNull(multiverseCore.getService(PluginLocales::class.java))
}
}