Merge pull request #2842 from Multiverse/file-locales

feat: Improve locale support
This commit is contained in:
Ben Woo 2023-02-28 23:13:23 +08:00 committed by GitHub
commit d819153690
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 147 additions and 22 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,15 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
this.commandManager.registerCommand(new UnloadCommand(this));
}
/**
* Resgister locales
*/
private void setUpLocales() {
this.commandManager.usePerIssuerLocale(true, true);
this.commandManager.getLocales().addFileResClassLoader(this);
this.commandManager.getLocales().addMessageBundles("multiverse-core");
}
/**
* Register all the destinations.
*/

View File

@ -22,7 +22,7 @@ import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlag;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagGroup;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandValueFlag;
import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags;
import com.onarandombox.MultiverseCore.locale.MVCorei18n;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldType;

View File

@ -9,7 +9,7 @@ import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.locale.MVCorei18n;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import org.jetbrains.annotations.NotNull;
@CommandAlias("mv")

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;
@ -20,6 +18,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 +26,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 +40,21 @@ 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

@ -0,0 +1,45 @@
package com.onarandombox.MultiverseCore.commandtools;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.BukkitLocales;
import com.onarandombox.MultiverseCore.utils.file.FileResClassLoader;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
/**
* Locale manager with additional methods for loading locales from plugin's locales folder.
*/
public class PluginLocales extends BukkitLocales {
private static final String DEFAULT_LOCALE_FOLDER_PATH = "locales";
/**
* Creates a new instance of {@link PluginLocales}.
*
* @param manager The command manager.
*/
public PluginLocales(BukkitCommandManager manager) {
super(manager);
}
/**
* Adds a {@link FileResClassLoader} to the list of class loaders to load locales data from.
*
* @param plugin The plugin.
* @return True if the class loader was added successfully.
*/
public boolean addFileResClassLoader(@NotNull Plugin plugin) {
return this.addBundleClassLoader(new FileResClassLoader(plugin, DEFAULT_LOCALE_FOLDER_PATH));
}
/**
* Adds a {@link FileResClassLoader} to the list of class loaders to load locales data from.
*
* @param plugin The plugin.
* @param localesFolderPath The path to the folder containing the locales.
* @return True if the class loader was added successfully.
*/
public boolean addFileResClassLoader(@NotNull Plugin plugin, @NotNull String localesFolderPath) {
return this.addBundleClassLoader(new FileResClassLoader(plugin, localesFolderPath));
}
}

View File

@ -1,4 +0,0 @@
/**
* This package contains all the locale reference for Multiverse-Core.
*/
package com.onarandombox.MultiverseCore.locale;

View File

@ -1,4 +1,4 @@
package com.onarandombox.MultiverseCore.locale;
package com.onarandombox.MultiverseCore.utils;
import co.aikar.locales.MessageKey;
import co.aikar.locales.MessageKeyProvider;

View File

@ -0,0 +1,59 @@
package com.onarandombox.MultiverseCore.utils.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.bukkit.plugin.Plugin;
/**
* A class loader that loads resources from the plugin's locales folder.
*/
public class FileResClassLoader extends ClassLoader {
private final transient File targetFolder;
/**
* Creates a new FileResClassLoader.
*
* @param plugin The plugin to load resources from.
* @param subFolder The subfolder to load resources from.
*/
public FileResClassLoader(final Plugin plugin, final String subFolder) {
super(plugin.getClass().getClassLoader());
this.targetFolder = new File(plugin.getDataFolder(), subFolder);
}
/**
* {@inheritDoc}
*/
@Override
public URL getResource(final String string) {
final File file = new File(targetFolder, string);
if (file.exists()) {
try {
return file.toURI().toURL();
} catch (final MalformedURLException ignored) {
}
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public InputStream getResourceAsStream(final String string) {
final File file = new File(targetFolder, string);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (final FileNotFoundException ignored) {
}
}
return null;
}
}

View File

@ -5,7 +5,7 @@
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.utils;
package com.onarandombox.MultiverseCore.utils.file;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;

View File

@ -0,0 +1,4 @@
/**
* This package contains all the classes that deal with files on disk.
*/
package com.onarandombox.MultiverseCore.utils.file;

View File

@ -32,7 +32,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.api.WorldPurger;
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
import com.onarandombox.MultiverseCore.utils.FileUtils;
import com.onarandombox.MultiverseCore.utils.file.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.Location;

View File

@ -1,9 +1,9 @@
mv-core.config.save.failed=§cUnable to save Multiverse-Core config.yml. Your changes will be temporary!
mv-core.config.save.failed=&cUnable to save Multiverse-Core config.yml. Your changes will be temporary!
mv-core.create.description=Creates a new world and loads it.
mv-core.create.name.description=New world name.
mv-core.create.environment.description=The world's environment. See: /mv environments
mv-core.create.flags.description=Additional world settings. See http://gg.gg/nn8bl for all possible flags.
mv-core.create.flags.description=Additional world settings. See https://gg.gg/nn8bl for all possible flags.
mv-core.create.properties=Creating world {worldName} with the following properties:
mv-core.create.properties.environment=- Environment: {environment}
mv-core.create.properties.seed=- Seed: {seed}
@ -12,12 +12,12 @@ mv-core.create.properties.adjustspawn=- Adjust Spawn: {adjustSpawn}
mv-core.create.properties.generator=- Generator: {generator}
mv-core.create.properties.structures=- Structures: {structures}
mv-core.create.loading=Creating world...
mv-core.create.failed=§cFailed to create world '{worldName}'! See console for details.
mv-core.create.success=§aWorld '{worldName}' created successfully!
mv-core.create.failed=&cFailed to create world '{worldName}'! See console for details.
mv-core.create.success=&aWorld '{worldName}' created successfully!
mv-core.debug.info.description=Show the current debug level.
mv-core.debug.info.off=§fMultiverse Debug mode is §cOFF§f.
mv-core.debug.info.on=§fMultiverse Debug mode is at §alevel {level}§f.
mv-core.debug.info.off=&fMultiverse Debug mode is &cOFF&f.
mv-core.debug.info.on=&fMultiverse Debug mode is at &alevel {level}&f.
mv-core.debug.change.description=Change debug level.
mv-core.debug.change.syntax=level
mv-core.debug.change.level.description=Debug level to set to.

View File

@ -1,5 +1,6 @@
package com.onarandombox.MultiverseCore.utils;
import com.onarandombox.MultiverseCore.utils.file.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -20,6 +20,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.listeners.MVEntityListener;
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener;
import com.onarandombox.MultiverseCore.listeners.MVWeatherListener;
import com.onarandombox.MultiverseCore.utils.file.FileUtils;
import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager;
import junit.framework.Assert;
import org.bukkit.Bukkit;