Remove migration of commands from config.yml to commands.yml
- Migration was shipped with the 5.2 release and is now becoming harder to maintain; since it's quite old we drop it
This commit is contained in:
@@ -2,7 +2,6 @@ package fr.xephi.authme.settings;
|
||||
|
||||
import ch.jalu.configme.migration.PlainMigrationService;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import ch.jalu.configme.properties.StringListProperty;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
@@ -19,7 +18,6 @@ import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -39,15 +37,6 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
|
||||
private final File pluginFolder;
|
||||
|
||||
// Stores old commands that need to be migrated to the new commands configuration
|
||||
// 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 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 List<String> onLoginCommands = Collections.emptyList();
|
||||
private List<String> onLoginConsoleCommands = Collections.emptyList();
|
||||
private List<String> onRegisterCommands = Collections.emptyList();
|
||||
private List<String> onRegisterConsoleCommands = Collections.emptyList();
|
||||
|
||||
@Inject
|
||||
SettingsMigrationService(@DataFolder File pluginFolder) {
|
||||
this.pluginFolder = pluginFolder;
|
||||
@@ -62,8 +51,6 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
changes = true;
|
||||
}
|
||||
|
||||
gatherOldCommandSettings(resource);
|
||||
|
||||
// Note ljacqu 20160211: Concatenating migration methods with | instead of the usual ||
|
||||
// ensures that all migrations will be performed
|
||||
return changes
|
||||
@@ -97,35 +84,6 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ----------------
|
||||
// Forced commands relocation (from config.yml to commands.yml)
|
||||
// ----------------
|
||||
private void gatherOldCommandSettings(PropertyResource resource) {
|
||||
onLoginCommands = getStringList(resource, "settings.forceCommands");
|
||||
onLoginConsoleCommands = getStringList(resource, "settings.forceCommandsAsConsole");
|
||||
onRegisterCommands = getStringList(resource, "settings.forceRegisterCommands");
|
||||
onRegisterConsoleCommands = getStringList(resource, "settings.forceRegisterCommandsAsConsole");
|
||||
}
|
||||
|
||||
private List<String> getStringList(PropertyResource resource, String path) {
|
||||
return new StringListProperty(path).getValue(resource);
|
||||
}
|
||||
|
||||
public List<String> getOnLoginCommands() {
|
||||
return onLoginCommands;
|
||||
}
|
||||
|
||||
public List<String> getOnLoginConsoleCommands() {
|
||||
return onLoginConsoleCommands;
|
||||
}
|
||||
|
||||
public List<String> getOnRegisterCommands() {
|
||||
return onRegisterCommands;
|
||||
}
|
||||
|
||||
public List<String> getOnRegisterConsoleCommands() {
|
||||
return onRegisterConsoleCommands;
|
||||
}
|
||||
|
||||
// --------
|
||||
// Specific migrations
|
||||
|
||||
@@ -5,16 +5,8 @@ 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.ConsoleLogger;
|
||||
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.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Migrates the commands from their old location, in config.yml, to the dedicated commands configuration file.
|
||||
@@ -26,131 +18,20 @@ 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);
|
||||
final boolean didMoveCommands = transformOldCommands(commandConfig);
|
||||
|
||||
if (didMoveCommands || isFileEmpty(resource)) {
|
||||
if (isFileEmpty(resource)) {
|
||||
resource.setValue("", commandConfig);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isFileEmpty(PropertyResource resource) {
|
||||
private static boolean isFileEmpty(PropertyResource resource) {
|
||||
return COMMAND_CONFIG_PROPERTIES.stream().anyMatch(property -> resource.getObject(property) == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds command settings from their old location (in config.yml) to the given command configuration object.
|
||||
*
|
||||
* @param commandConfig the command config object to move old commands to
|
||||
* @return true if commands have been moved, false if no migration was necessary
|
||||
*/
|
||||
@VisibleForTesting
|
||||
boolean transformOldCommands(CommandConfig commandConfig) {
|
||||
boolean didMoveCommands = false;
|
||||
for (MigratableCommandSection section : MigratableCommandSection.values()) {
|
||||
didMoveCommands |= section.convertCommands(settingsMigrationService, commandConfig);
|
||||
}
|
||||
return didMoveCommands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum defining the forced command settings that should be moved from config.yml to the new commands.yml file.
|
||||
*/
|
||||
private enum MigratableCommandSection {
|
||||
|
||||
ON_JOIN(
|
||||
SettingsMigrationService::getOnLoginCommands,
|
||||
Executor.PLAYER,
|
||||
CommandConfig::getOnLogin,
|
||||
OnLoginCommand::new),
|
||||
|
||||
ON_JOIN_CONSOLE(
|
||||
SettingsMigrationService::getOnLoginConsoleCommands,
|
||||
Executor.CONSOLE,
|
||||
CommandConfig::getOnLogin,
|
||||
OnLoginCommand::new),
|
||||
|
||||
ON_REGISTER(
|
||||
SettingsMigrationService::getOnRegisterCommands,
|
||||
Executor.PLAYER,
|
||||
CommandConfig::getOnRegister),
|
||||
|
||||
ON_REGISTER_CONSOLE(
|
||||
SettingsMigrationService::getOnRegisterConsoleCommands,
|
||||
Executor.CONSOLE,
|
||||
CommandConfig::getOnRegister);
|
||||
|
||||
private final Function<SettingsMigrationService, List<String>> legacyCommandsGetter;
|
||||
private final Executor executor;
|
||||
private final Function<CommandConfig, Map<String, Command>> commandMapGetter;
|
||||
private final BiFunction<String, Executor, Command> commandConstructor;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param legacyCommandsGetter getter on MigrationService to get the deprecated command entries
|
||||
* @param executor the executor of the commands
|
||||
* @param commandMapGetter the getter for the commands map in the new settings structure to add the old
|
||||
* settings to after conversion
|
||||
*/
|
||||
MigratableCommandSection(Function<SettingsMigrationService, List<String>> legacyCommandsGetter,
|
||||
Executor executor,
|
||||
Function<CommandConfig, Map<String, Command>> commandMapGetter) {
|
||||
this(legacyCommandsGetter, executor, commandMapGetter, Command::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param legacyCommandsGetter getter on MigrationService to get the deprecated command entries
|
||||
* @param executor the executor of the commands
|
||||
* @param commandMapGetter the getter for the commands map in the new settings structure to add the old
|
||||
* settings to after conversion
|
||||
* @param commandConstructor constructor for creating a command object
|
||||
*/
|
||||
<T extends Command> MigratableCommandSection(
|
||||
Function<SettingsMigrationService, List<String>> legacyCommandsGetter,
|
||||
Executor executor,
|
||||
Function<CommandConfig, Map<String, T>> commandMapGetter,
|
||||
BiFunction<String, Executor, T> commandConstructor) {
|
||||
|
||||
this.legacyCommandsGetter = legacyCommandsGetter;
|
||||
this.executor = executor;
|
||||
// This is horrible to be doing but this way we don't need to cast in convertCommands()
|
||||
this.commandMapGetter = (Function) commandMapGetter;
|
||||
this.commandConstructor = (BiFunction) commandConstructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the commands from the sections' settings migration service to the appropriate place in the new
|
||||
* command config object.
|
||||
*
|
||||
* @param settingsMigrationService settings migration service to read old commands from
|
||||
* @param commandConfig command config object to add converted commands to
|
||||
* @return true if there were commands to migrate, false otherwise
|
||||
*/
|
||||
boolean convertCommands(SettingsMigrationService settingsMigrationService, CommandConfig commandConfig) {
|
||||
List<Command> commands = legacyCommandsGetter.apply(settingsMigrationService).stream()
|
||||
.map(cmd -> commandConstructor.apply(cmd, executor)).collect(Collectors.toList());
|
||||
|
||||
if (commands.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Map<String, Command> commandMap = commandMapGetter.apply(commandConfig);
|
||||
commands.forEach(cmd -> commandMap.put(RandomStringUtils.generate(10), cmd));
|
||||
ConsoleLogger.info("Moving " + commands.size() + " commands of type " + this
|
||||
+ " from config.yml to commands.yml");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user