#1035 Migrate other accounts config from config.yml to commands.yml
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user