Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dependencies {

// Tests
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21'
testImplementation 'org.mockbukkit.mockbukkit:mockbukkit-v1.21:4.21.0'
testImplementation 'org.mockbukkit.mockbukkit:mockbukkit-v1.21:4.22.2'
testImplementation('com.googlecode.json-simple:json-simple:1.1.1') {
exclude group: 'junit', module: 'junit'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.mvplugins.multiverse.core.api.MVCore;
import org.mvplugins.multiverse.core.commands.CoreCommand;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.PluginLocales;
import org.mvplugins.multiverse.core.config.MVCoreConfig;
import org.mvplugins.multiverse.core.destination.DestinationsProvider;
import org.mvplugins.multiverse.core.economy.MVEconomist;
Expand Down Expand Up @@ -65,8 +64,6 @@ 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;
Expand Down Expand Up @@ -219,8 +216,7 @@ private void registerCommands() {
*/
private void setUpLocales() {
Try.of(() -> commandManagerProvider.get())
.andThen(commandManager -> commandManager.usePerIssuerLocale(true, true))
.mapTry(commandManager -> pluginLocalesProvider.get())
.mapTry(MVCommandManager::getLocales)
.andThen(pluginLocales -> {
pluginLocales.addFileResClassLoader(this);
pluginLocales.addMessageBundles("multiverse-core");
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/mvplugins/multiverse/core/api/MVConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.placeholders.MultiverseCorePlaceholders;

import java.util.Locale;

@Contract
public interface MVConfig {

Expand Down Expand Up @@ -195,6 +197,30 @@ public interface MVConfig {
*/
boolean isRegisterPapiHook();

/**
* Sets default locale used for messages
* @param defaultLocale The new value
*/
void setDefaultLocale(Locale defaultLocale);

/**
* Gets default locale used for messages
* @return default locale
*/
Locale getDefaultLocale();

/**
* Sets whether to use each player's client locale.
* @param perPlayerLocale the new value
*/
void setPerPlayerLocale(boolean perPlayerLocale);

/**
* Gets whether to use each player's client locale.
* @return True if per player locale should be used.
*/
boolean getPerPlayerLocale();

/**
* Sets globalDebug.
* @param globalDebug The new value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MVCommandManager extends PaperCommandManager {
private final Provider<MVCommandContexts> commandContextsProvider;
private final Provider<MVCommandCompletions> commandCompletionsProvider;
private final MVCommandPermissions commandPermissions;
private final PluginLocales pluginLocales;

@Inject
MVCommandManager(
Expand All @@ -49,24 +50,20 @@ public class MVCommandManager extends PaperCommandManager {
this.commandContextsProvider = commandContextsProvider;
this.commandCompletionsProvider = commandCompletionsProvider;
this.commandPermissions = commandPermissions;
this.pluginLocales = new PluginLocales(this);
this.locales = this.pluginLocales;
this.pluginLocales.loadLanguages();

MVCommandConditions.load(this, worldManager, worldNameChecker);
this.enableUnstableAPI("help");
}

void loadLanguages(PluginLocales locales) {
if (this.locales == null) {
this.locales = locales;
this.locales.loadLanguages();
}
}

/**
* {@inheritDoc}
*/
@Override
public PluginLocales getLocales() {
return (PluginLocales) this.locales;
return this.pluginLocales;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/**
* 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";
Expand All @@ -21,10 +20,8 @@ public class PluginLocales extends BukkitLocales {
*
* @param manager The command manager.
*/
@Inject
public PluginLocales(MVCommandManager manager) {
super(manager);
manager.loadLanguages(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Objects;

import com.dumptruckman.minecraft.util.Logging;
Expand All @@ -15,6 +16,7 @@

import org.mvplugins.multiverse.core.MultiverseCore;
import org.mvplugins.multiverse.core.api.MVConfig;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.configuration.handle.CommentedYamlConfigHandle;
import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.configuration.migration.BooleanMigratorAction;
Expand All @@ -35,9 +37,9 @@
private final StringPropertyHandle stringPropertyHandle;

@Inject
MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager) {
MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager, @NotNull MVCommandManager commandManager) {

Check warning on line 40 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 128). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java:40:0: warning: Line is longer than 120 characters (found 128). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME);
this.configNodes = new MVCoreConfigNodes(pluginManager);
this.configNodes = new MVCoreConfigNodes(pluginManager, commandManager);
this.configHandle = CommentedYamlConfigHandle.builder(configPath, configNodes.getNodes())
.logger(Logging.getLogger())
.migrator(ConfigMigrator.builder(configNodes.VERSION)
Expand Down Expand Up @@ -246,6 +248,26 @@
return configHandle.get(configNodes.REGISTER_PAPI_HOOK);
}

@Override

Check warning on line 251 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'setDefaultLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'setDefaultLocale' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java:251:5: info: Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'setDefaultLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'setDefaultLocale' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
public void setDefaultLocale(Locale defaultLocale) {
configHandle.set(configNodes.DEFAULT_LOCALE, defaultLocale);
}

@Override

Check warning on line 256 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'getDefaultLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'getDefaultLocale' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java:256:5: info: Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'getDefaultLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'getDefaultLocale' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
public Locale getDefaultLocale() {
return configHandle.get(configNodes.DEFAULT_LOCALE);
}

@Override

Check warning on line 261 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'setPerPlayerLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'setPerPlayerLocale' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java:261:5: info: Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'setPerPlayerLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'setPerPlayerLocale' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
public void setPerPlayerLocale(boolean perPlayerLocale) {
configHandle.set(configNodes.PER_PLAYER_LOCALE, perPlayerLocale);
}

@Override

Check warning on line 266 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'getPerPlayerLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'getPerPlayerLocale' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfig.java:266:5: info: Class 'MVCoreConfig' looks like designed for extension (can be subclassed), but the method 'getPerPlayerLocale' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'MVCoreConfig' final or making the method 'getPerPlayerLocale' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
public boolean getPerPlayerLocale() {
return configHandle.get(configNodes.PER_PLAYER_LOCALE);
}

@Override
public void setGlobalDebug(int globalDebug) {
configHandle.set(configNodes.GLOBAL_DEBUG, globalDebug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
import io.vavr.control.Try;
import org.bukkit.plugin.PluginManager;

import org.jetbrains.annotations.NotNull;

Check warning on line 7 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Extra separation in import group before 'org.jetbrains.annotations.NotNull' Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java:7:1: warning: Extra separation in import group before 'org.jetbrains.annotations.NotNull' (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;

Check warning on line 8 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.commandtools.MVCommandManager' should be separated from previous imports. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java:8:1: warning: 'org.mvplugins.multiverse.core.commandtools.MVCommandManager' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import org.mvplugins.multiverse.core.configuration.node.ConfigHeaderNode;
import org.mvplugins.multiverse.core.configuration.node.ConfigNode;
import org.mvplugins.multiverse.core.configuration.node.Node;
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
import org.mvplugins.multiverse.core.event.MVDebugModeEvent;
import org.mvplugins.multiverse.core.exceptions.MultiverseException;

import java.util.Locale;

Check warning on line 16 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Wrong order for 'java.util.Locale' import. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java:16:1: warning: Wrong order for 'java.util.Locale' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

class MVCoreConfigNodes {

private final NodeGroup nodes = new NodeGroup();
private PluginManager pluginManager;
private MVCommandManager commandManager;

MVCoreConfigNodes(PluginManager pluginManager) {
MVCoreConfigNodes(@NotNull PluginManager pluginManager, @NotNull MVCommandManager commandManager) {
this.pluginManager = pluginManager;
this.commandManager = commandManager;
}

public NodeGroup getNodes() {
Expand Down Expand Up @@ -187,6 +193,28 @@
.name("register-papi-hook")
.build());

final ConfigNode<Locale> DEFAULT_LOCALE = node(ConfigNode.builder("messaging.default-locale", Locale.class)
.comment("")
.comment("This config option defines the default language Multiverse should use.")
.defaultValue(Locale.ENGLISH)
.name("default-locale")
.onSetValue((oldValue, newValue) -> {
commandManager.getLocales().setDefaultLocale(newValue);
})
.build());

final ConfigNode<Boolean> PER_PLAYER_LOCALE = node(ConfigNode.builder("messaging.per-player-locale", Boolean.class)
.comment("")
.comment("This config option defines if Multiverse should use the player's language based on their client's language.")

Check warning on line 208 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 131). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java:208:0: warning: Line is longer than 120 characters (found 131). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.comment("If the player's language does not have a translation, it will use the default language set above instead.")

Check warning on line 209 in src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 129). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/MVCoreConfigNodes.java:209:0: warning: Line is longer than 120 characters (found 129). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.defaultValue(true)
.name("per-player-locale")
.onSetValue((oldValue, newValue) -> {
// autoDetectFromClient will be done by MVLocalesListener instead
commandManager.usePerIssuerLocale(newValue, false);
})
.build());

private final ConfigHeaderNode MISC_HEADER = node(ConfigHeaderNode.builder("misc")
.comment("")
.comment("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mvplugins.multiverse.core.configuration.functions;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import co.aikar.commands.ACFUtil;
Expand Down Expand Up @@ -70,8 +71,25 @@ public Object serialize(Boolean object, Class<Boolean> type) {
}
};

private static final NodeSerializer<Locale> LOCALE_SERIALIZER = new NodeSerializer<>() {
@Override
public Locale deserialize(Object object, Class<Locale> type) {
if (object instanceof Locale) {
return (Locale) object;
}
String[] split = String.valueOf(object).split("_", 2);
return split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
}

@Override
public Object serialize(Locale object, Class<Locale> type) {
return object.toLanguageTag();
}
};

static {
addDefaultSerializer(Boolean.class, BOOLEAN_SERIALIZER);
addDefaultSerializer(Locale.class, LOCALE_SERIALIZER);
}

private DefaultSerializerProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.mvplugins.multiverse.core.listeners;

import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLocaleChangeEvent;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;

import java.util.Locale;

@Service
public class MVLocalesListener implements CoreListener {

private final MVCommandManager commandManager;

@Inject
MVLocalesListener(@NotNull MVCommandManager commandManager) {
this.commandManager = commandManager;
}

@EventHandler
void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Logging.finer(player.getName() + " joined with locale " + player.getLocale());
commandManager.setPlayerLocale(player, serializeLocale(player.getLocale()));
}

@EventHandler
void onLocaleChange(PlayerLocaleChangeEvent event) {
Player player = event.getPlayer();
Logging.finer(player.getName() + " changed locale from " + player.getLocale() + " to " + event.getLocale());
commandManager.setPlayerLocale(player, serializeLocale(event.getLocale()));
}

private Locale serializeLocale(String locale) {
String[] split = locale.split("_");
return split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.bukkit.command.ConsoleCommandSender
import org.bukkit.permissions.PermissionAttachment
import org.mockbukkit.mockbukkit.entity.PlayerMock
import org.mvplugins.multiverse.core.TestWithMockBukkit
import org.mvplugins.multiverse.core.commandtools.MVCommandManager
import org.mvplugins.multiverse.core.commandtools.PluginLocales
import org.mvplugins.multiverse.core.utils.message.Message
import kotlin.test.BeforeTest
Expand All @@ -21,8 +22,9 @@ abstract class BaseCommandTest : TestWithMockBukkit() {

@BeforeTest
fun setUpCommand() {
locales = serviceLocator.getActiveService(PluginLocales::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("PluginLocales is not available as a service") }
val commandManager = serviceLocator.getActiveService(MVCommandManager::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("MVCommandManager is not available as a service") }
locales = commandManager.locales

console = server.consoleSender
player = server.addPlayer("benwoo1110");
Expand Down
Loading
Loading