#1035 Migrate other accounts config from config.yml to commands.yml

This commit is contained in:
ljacqu
2018-01-21 18:58:20 +01:00
parent 456693ea59
commit 761ee2f05b
10 changed files with 158 additions and 40 deletions
@@ -5,8 +5,13 @@ import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyResource;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import fr.xephi.authme.settings.SettingsMigrationService;
import fr.xephi.authme.util.RandomStringUtils;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Migrates the commands from their old location, in config.yml, to the dedicated commands configuration file.
@@ -18,19 +23,42 @@ class CommandMigrationService implements MigrationService {
static final List<String> COMMAND_CONFIG_PROPERTIES = ImmutableList.of(
"onJoin", "onLogin", "onSessionLogin", "onFirstLogin", "onRegister", "onUnregister", "onLogout");
@Inject
private SettingsMigrationService settingsMigrationService;
CommandMigrationService() {
}
@Override
public boolean checkAndMigrate(PropertyResource resource, List<Property<?>> properties) {
final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource);
if (isFileEmpty(resource)) {
if (moveOtherAccountsConfig(commandConfig) || isFileEmpty(resource)) {
resource.setValue("", commandConfig);
return true;
}
return false;
}
private boolean moveOtherAccountsConfig(CommandConfig commandConfig) {
if (settingsMigrationService.hasOldOtherAccountsCommand()) {
OnLoginCommand command = new OnLoginCommand(
replaceOldPlaceholdersWithNew(settingsMigrationService.getOldOtherAccountsCommand()), Executor.CONSOLE);
command.setIfNumberOfAccountsAtLeast(
Optional.of(settingsMigrationService.getOldOtherAccountsCommandThreshold()));
Map<String, OnLoginCommand> onLoginCommands = commandConfig.getOnLogin();
onLoginCommands.put(RandomStringUtils.generate(10), command);
return true;
}
return false;
}
private static String replaceOldPlaceholdersWithNew(String oldOtherAccountsCommand) {
return oldOtherAccountsCommand
.replace("%playername%", "%p")
.replace("%playerip%", "%ip");
}
private static boolean isFileEmpty(PropertyResource resource) {
return COMMAND_CONFIG_PROPERTIES.stream().anyMatch(property -> resource.getObject(property) == null);
}
@@ -7,8 +7,8 @@ import java.util.Optional;
*/
public class OnLoginCommand extends Command {
private Optional<Integer> ifNumberOfAccountsAtLeast;
private Optional<Integer> ifNumberOfAccountsLessThan;
private Optional<Integer> ifNumberOfAccountsAtLeast = Optional.empty();
private Optional<Integer> ifNumberOfAccountsLessThan = Optional.empty();
/**
* Default constructor (for bean mapping).