Search in sources :

Example 1 with Source

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 + ".");
    }
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) CommandSender(org.bukkit.command.CommandSender) Arrays(java.util.Arrays) Provider(github.scarsz.configuralize.Provider) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ClickEvent(net.kyori.adventure.text.event.ClickEvent) Player(org.bukkit.entity.Player) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) File(java.io.File) MessageUtil(github.scarsz.discordsrv.util.MessageUtil) Language(github.scarsz.configuralize.Language) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) Source(github.scarsz.configuralize.Source) Component(net.kyori.adventure.text.Component) Map(java.util.Map) ChatColor(org.bukkit.ChatColor) HoverEvent(net.kyori.adventure.text.event.HoverEvent) TextComponent(net.kyori.adventure.text.TextComponent) Player(org.bukkit.entity.Player) Language(github.scarsz.configuralize.Language) Map(java.util.Map) File(java.io.File) Source(github.scarsz.configuralize.Source) Provider(github.scarsz.configuralize.Provider)

Aggregations

Language (github.scarsz.configuralize.Language)1 Provider (github.scarsz.configuralize.Provider)1 Source (github.scarsz.configuralize.Source)1 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 MessageUtil (github.scarsz.discordsrv.util.MessageUtil)1 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Component (net.kyori.adventure.text.Component)1 TextComponent (net.kyori.adventure.text.TextComponent)1 ClickEvent (net.kyori.adventure.text.event.ClickEvent)1 HoverEvent (net.kyori.adventure.text.event.HoverEvent)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 FileUtils (org.apache.commons.io.FileUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ChatColor (org.bukkit.ChatColor)1 CommandSender (org.bukkit.command.CommandSender)1 Player (org.bukkit.entity.Player)1