use of github.scarsz.configuralize.Provider 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 + ".");
}
}
use of github.scarsz.configuralize.Provider in project DiscordSRV by Scarsz.
the class ConfigUtil method migrate.
public static void migrate() {
String configVersionRaw = DiscordSRV.config().getString("ConfigVersion");
if (configVersionRaw.contains("/"))
configVersionRaw = configVersionRaw.substring(0, configVersionRaw.indexOf("/"));
String pluginVersionRaw = DiscordSRV.getPlugin().getDescription().getVersion();
if (configVersionRaw.equals(pluginVersionRaw))
return;
Version configVersion = configVersionRaw.split("\\.").length == 3 ? Version.valueOf(configVersionRaw.replace("-SNAPSHOT", "")) : Version.valueOf("1." + configVersionRaw.replace("-SNAPSHOT", ""));
Version pluginVersion = Version.valueOf(pluginVersionRaw.replace("-SNAPSHOT", ""));
// no migration necessary
if (configVersion.equals(pluginVersion))
return;
if (configVersion.greaterThan(pluginVersion)) {
DiscordSRV.warning("You're attempting to use a higher config version than the plugin. Things probably won't work correctly.");
return;
}
String oldVersionName = configVersion.toString();
DiscordSRV.info("Your DiscordSRV config file was outdated; attempting migration...");
try {
Provider configProvider = DiscordSRV.config().getProvider("config");
Provider messageProvider = DiscordSRV.config().getProvider("messages");
Provider voiceProvider = DiscordSRV.config().getProvider("voice");
Provider linkingProvider = DiscordSRV.config().getProvider("linking");
Provider synchronizationProvider = DiscordSRV.config().getProvider("synchronization");
Provider alertsProvider = DiscordSRV.config().getProvider("alerts");
migrate("config.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getConfigFile(), configProvider);
migrate("messages.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getMessagesFile(), messageProvider);
migrate("voice.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getVoiceFile(), voiceProvider);
migrate("linking.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getLinkingFile(), linkingProvider);
migrate("synchronization.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getSynchronizationFile(), synchronizationProvider);
migrate("alerts.yml-build." + oldVersionName + ".old", DiscordSRV.getPlugin().getAlertsFile(), alertsProvider);
DiscordSRV.info("Successfully migrated configuration files to version " + pluginVersionRaw);
} catch (Exception e) {
DiscordSRV.error("Failed migrating configs: " + e.getMessage());
DiscordSRV.debug(ExceptionUtils.getStackTrace(e));
}
}
Aggregations