feat: Add support for loading locales from plugin dir

This commit is contained in:
Ben Woo 2023-02-17 00:12:28 +08:00
parent d90210d70f
commit b4549c6791
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
4 changed files with 82 additions and 7 deletions

View File

@ -164,6 +164,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
this.anchorManager.loadAnchors();
this.registerEvents();
this.registerCommands();
this.setUpLocales();
this.registerDestinations();
this.setupMetrics();
this.saveMVConfig();
@ -215,6 +216,14 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
this.commandManager.registerCommand(new UnloadCommand(this));
}
/**
* Resgister locales
*/
private void setUpLocales() {
this.commandManager.getLocales().addFileResClassLoader(this);
this.commandManager.getLocales().addMessageBundles("multiverse-core");
}
/**
* Register all the destinations.
*/

View File

@ -1,7 +1,5 @@
package com.onarandombox.MultiverseCore.commandtools;
import java.util.Locale;
import co.aikar.commands.BukkitCommandCompletionContext;
import co.aikar.commands.BukkitCommandExecutionContext;
import co.aikar.commands.CommandCompletions;
@ -10,6 +8,7 @@ import co.aikar.commands.PaperCommandManager;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager;
import com.onarandombox.MultiverseCore.commandtools.queue.CommandQueueManager;
import com.onarandombox.MultiverseCore.locale.PluginLocales;
import org.jetbrains.annotations.NotNull;
/**
@ -20,6 +19,7 @@ public class MVCommandManager extends PaperCommandManager {
private final MultiverseCore plugin;
private CommandFlagsManager flagsManager;
private CommandQueueManager commandQueueManager;
private PluginLocales pluginLocales;
public MVCommandManager(@NotNull MultiverseCore plugin) {
super(plugin);
@ -27,11 +27,6 @@ public class MVCommandManager extends PaperCommandManager {
// Setup conditions
MVCommandConditions.load(this, plugin);
// Setup locale
this.addSupportedLanguage(Locale.ENGLISH);
this.locales.addMessageBundles("multiverse-core");
this.locales.loadLanguages();
}
/**
@ -46,6 +41,16 @@ public class MVCommandManager extends PaperCommandManager {
return flagsManager;
}
@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

@ -0,0 +1,46 @@
package com.onarandombox.MultiverseCore.locale;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
* A class loader that loads resources from the plugin's locales folder.
*/
public class FileResClassLoader extends ClassLoader {
private final transient File localesFolder;
public FileResClassLoader(final ClassLoader classLoader, final Plugin plugin) {
super(classLoader);
this.localesFolder = new File(plugin.getDataFolder(), "locales");
}
@Override
public URL getResource(final String string) {
final File file = new File(localesFolder, string);
if (file.exists()) {
try {
return file.toURI().toURL();
} catch (final MalformedURLException ignored) {
}
}
return null;
}
@Override
public InputStream getResourceAsStream(final String string) {
final File file = new File(localesFolder, string);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (final FileNotFoundException ignored) {
}
}
return null;
}
}

View File

@ -0,0 +1,15 @@
package com.onarandombox.MultiverseCore.locale;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.BukkitLocales;
import org.bukkit.plugin.Plugin;
public class PluginLocales extends BukkitLocales {
public PluginLocales(BukkitCommandManager manager) {
super(manager);
}
public boolean addFileResClassLoader(Plugin plugin) {
return this.addBundleClassLoader(new FileResClassLoader(plugin.getClass().getClassLoader(), plugin));
}
}