use of github.scarsz.configuralize.Source in project DiscordSRV by Scarsz.
the class CommandLanguage method execute.
@Command(commandNames = { "language", "setlanguage", "lang", "setlang" }, helpMessage = "Changes the language of DiscordSRV to whatever is specified.", permission = "discordsrv.language", usageExample = "language japanese")
public static void execute(CommandSender sender, String[] args) throws IOException {
Language currentLanguage = DiscordSRV.config().getLanguage();
String currentLanguageName = StringUtils.capitalize(currentLanguage.getName().toLowerCase());
Language targetLanguage = null;
outer: for (String arg : args) {
for (Language language : Language.values()) {
if (language.getCode().equalsIgnoreCase(arg) || language.getName().equalsIgnoreCase(arg)) {
targetLanguage = language;
break outer;
}
}
}
if (targetLanguage == null) {
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV is currently in " + currentLanguageName + ". " + "Change it by giving a language as an argument.");
return;
}
String targetLanguageName = StringUtils.capitalize(targetLanguage.getName().toLowerCase());
if (!DiscordSRV.config().isLanguageAvailable(targetLanguage)) {
String available = Arrays.stream(Language.values()).filter(DiscordSRV.config()::isLanguageAvailable).map(language -> StringUtils.capitalize(language.getName().toLowerCase())).collect(Collectors.joining(", "));
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV does not have a translation for " + targetLanguageName + ". " + "Supported languages are as follows: " + available + ".");
return;
}
if (Arrays.stream(args).noneMatch(s -> s.equalsIgnoreCase("-confirm"))) {
TextComponent message = Component.text("This will reset your DiscordSRV configuration files to be in ", NamedTextColor.DARK_AQUA).append(Component.text(targetLanguageName, NamedTextColor.WHITE)).append(Component.text(". Your old config files will be renamed to have ", NamedTextColor.DARK_AQUA)).append(Component.text(currentLanguageName + ".", NamedTextColor.WHITE)).append(Component.text(" on the beginning of the file name. ")).append(Component.text("[Confirm" + (sender instanceof Player ? "?" : " by running the command again, adding \" -confirm\" to the end") + "]").color(NamedTextColor.GREEN).clickEvent(ClickEvent.runCommand("/discord language " + targetLanguage.getCode() + " -confirm")).hoverEvent(HoverEvent.showText(Component.text("Click to confirm the config change.", NamedTextColor.GREEN))));
MessageUtil.sendMessage(sender, message);
} else {
DiscordSRV.config().setLanguage(targetLanguage);
for (Map.Entry<Source, Provider> entry : DiscordSRV.config().getSources().entrySet()) {
File source = entry.getKey().getFile();
File target = new File(source.getParentFile(), currentLanguageName + "." + source.getName());
FileUtils.moveFile(source, target);
entry.getValue().saveDefaults();
// set the ForcedLanguage value to the new language so language change will be persistent
if (entry.getKey().getResourceName().equals("config")) {
String file = FileUtils.readFileToString(source, "UTF-8");
file = file.replace("\nForcedLanguage: none", "\nForcedLanguage: " + targetLanguageName);
FileUtils.writeStringToFile(source, file, "UTF-8");
}
}
MessageUtil.sendMessage(sender, ChatColor.DARK_AQUA + "DiscordSRV language successfully changed to " + targetLanguageName + ".");
}
}
Aggregations