Added language selection using command (as alternative to GUI)

Implements https://github.com/BentoBoxWorld/BentoBox/issues/1411
The IslandLanguageCommand now have tab-completion.
Added LocalesManager#isLocaleAvailable(Locale).
This commit is contained in:
Florian CUNY 2020-06-22 18:52:16 +02:00
parent 360a417a4a
commit 0e3535d0cc
3 changed files with 46 additions and 2 deletions

View File

@ -1,10 +1,12 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.*;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.panels.LanguagePanel;
import world.bentobox.bentobox.util.Util;
/**
* @author Poslovitch
@ -20,11 +22,40 @@ public class IslandLanguageCommand extends CompositeCommand {
setPermission("island.language");
setOnlyPlayer(true);
setDescription("commands.island.language.description");
setParametersHelp("commands.island.language.parameters");
}
@Override
public boolean execute(User user, String label, List<String> args) {
LanguagePanel.openPanel(user);
if (args.size() == 1) {
// The user provided a language code
Locale locale = Locale.forLanguageTag(args.get(0));
if (getPlugin().getLocalesManager().isLocaleAvailable(locale)) {
// Check if that locale is not already selected
if (!user.getLocale().equals(locale)) {
getPlugin().getPlayers().setLocale(user.getUniqueId(), locale.toLanguageTag());
user.sendMessage("language.edited", "[lang]", locale.toLanguageTag());
} else {
user.sendMessage("commands.island.language.already-selected");
return false;
}
} else {
user.sendMessage("commands.island.language.not-available");
return false;
}
} else {
LanguagePanel.openPanel(user);
}
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
List<String> options = new ArrayList<>();
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
for (Locale locale : getPlugin().getLocalesManager().getAvailableLocales(true)) {
options.add(locale.toLanguageTag());
}
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -296,6 +296,16 @@ public class LocalesManager {
}
}
/**
* Returns {@code true} if this locale is available, {@code false} otherwise.
* @param locale the locale, not null. Consider using {@link Locale#forLanguageTag(String)} if needed.
* @return {@code true} if this locale is available, {@code false} otherwise.
* @since 1.14.0
*/
public boolean isLocaleAvailable(@NonNull Locale locale) {
return languages.containsKey(locale);
}
/**
* @return raw map of system locales to BentoBox locales
*/

View File

@ -657,6 +657,9 @@ commands:
description: "display island settings"
language:
description: "select language"
parameters: "[language]"
not-available: "&c This language is not available."
already-selected: "&c You are already using this language."
expel:
description: "expel a player from your island"
parameters: "<player>"