#1035 Migrate other accounts config from config.yml to commands.yml
This commit is contained in:
@@ -13,6 +13,7 @@ import fr.xephi.authme.security.HashAlgorithm;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
@@ -37,6 +38,13 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
|
||||
private final File pluginFolder;
|
||||
|
||||
// Stores old "other accounts command" config if present.
|
||||
// We need to store it in here for retrieval when we build the CommandConfig. Retrieving it from the config.yml is
|
||||
// not possible since this migration service may trigger the config.yml to be resaved. As the old command settings
|
||||
// don't exist in the code anymore, as soon as config.yml is resaved we lose this information.
|
||||
private String oldOtherAccountsCommand;
|
||||
private int oldOtherAccountsCommandThreshold;
|
||||
|
||||
@Inject
|
||||
SettingsMigrationService(@DataFolder File pluginFolder) {
|
||||
this.pluginFolder = pluginFolder;
|
||||
@@ -51,6 +59,8 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
changes = true;
|
||||
}
|
||||
|
||||
setOldOtherAccountsCommandFieldsIfSet(resource);
|
||||
|
||||
// Note ljacqu 20160211: Concatenating migration methods with | instead of the usual ||
|
||||
// ensures that all migrations will be performed
|
||||
return changes
|
||||
@@ -75,7 +85,8 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
"Hooks.customAttributes", "Security.stop.kickPlayersBeforeStopping",
|
||||
"settings.restrictions.keepCollisionsDisabled", "settings.forceCommands", "settings.forceCommandsAsConsole",
|
||||
"settings.forceRegisterCommands", "settings.forceRegisterCommandsAsConsole",
|
||||
"settings.sessions.sessionExpireOnIpChange"};
|
||||
"settings.sessions.sessionExpireOnIpChange", "settings.restrictions.otherAccountsCmd",
|
||||
"settings.restrictions.otherAccountsCmdThreshold"};
|
||||
for (String deprecatedPath : deprecatedProperties) {
|
||||
if (resource.contains(deprecatedPath)) {
|
||||
return true;
|
||||
@@ -84,6 +95,20 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// --------
|
||||
// Old other accounts
|
||||
// --------
|
||||
public boolean hasOldOtherAccountsCommand() {
|
||||
return !StringUtils.isEmpty(oldOtherAccountsCommand);
|
||||
}
|
||||
|
||||
public String getOldOtherAccountsCommand() {
|
||||
return oldOtherAccountsCommand;
|
||||
}
|
||||
|
||||
public int getOldOtherAccountsCommandThreshold() {
|
||||
return oldOtherAccountsCommandThreshold;
|
||||
}
|
||||
|
||||
// --------
|
||||
// Specific migrations
|
||||
@@ -288,6 +313,22 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the old config to run a command when alt accounts are detected and sets them to this instance
|
||||
* for further processing.
|
||||
*
|
||||
* @param resource The property resource
|
||||
*/
|
||||
private void setOldOtherAccountsCommandFieldsIfSet(PropertyResource resource) {
|
||||
Property<String> commandProperty = newProperty("settings.restrictions.otherAccountsCmd", "");
|
||||
Property<Integer> commandThresholdProperty = newProperty("settings.restrictions.otherAccountsCmdThreshold", 0);
|
||||
|
||||
if (commandProperty.isPresent(resource) && commandThresholdProperty.getValue(resource) >= 2) {
|
||||
oldOtherAccountsCommand = commandProperty.getValue(resource);
|
||||
oldOtherAccountsCommandThreshold = commandThresholdProperty.getValue(resource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an old property path and moves it to a new path if it is present and the new path is not yet set.
|
||||
*
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -180,18 +180,6 @@ public final class RestrictionSettings implements SettingsHolder {
|
||||
public static final Property<List<String>> UNRESTRICTED_NAMES =
|
||||
newLowercaseListProperty("settings.unrestrictions.UnrestrictedName");
|
||||
|
||||
@Comment("Threshold of the other accounts command, a value less than 2 means disabled.")
|
||||
public static final Property<Integer> OTHER_ACCOUNTS_CMD_THRESHOLD =
|
||||
newProperty("settings.restrictions.otherAccountsCmdThreshold", 0);
|
||||
|
||||
@Comment({
|
||||
"Command to run when a user has more accounts than the configured threshold.",
|
||||
"Available variables: %playername%, %playerip%"
|
||||
})
|
||||
public static final Property<String> OTHER_ACCOUNTS_CMD =
|
||||
newProperty("settings.restrictions.otherAccountsCmd",
|
||||
"say The player %playername% with ip %playerip% has multiple accounts!");
|
||||
|
||||
private RestrictionSettings() {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user