#411 Migrate settings to new structure (work in progress)
- Work in progress: config.yml cannot be loaded after migration
This commit is contained in:
@@ -10,6 +10,23 @@ public class Command {
|
||||
/** The executor of the command. */
|
||||
private Executor executor = Executor.PLAYER;
|
||||
|
||||
/**
|
||||
* Default constructor (for bean mapping).
|
||||
*/
|
||||
public Command() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param command the command
|
||||
* @param executor the executor of the command
|
||||
*/
|
||||
public Command(String command, Executor executor) {
|
||||
this.command = command;
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -10,9 +10,9 @@ import java.util.Map;
|
||||
*/
|
||||
public class CommandConfig {
|
||||
|
||||
private Map<String, Command> onJoin = Collections.emptyMap();
|
||||
private Map<String, Command> onLogin = Collections.emptyMap();
|
||||
private Map<String, Command> onRegister = Collections.emptyMap();
|
||||
private Map<String, Command> onJoin = new HashMap<>();
|
||||
private Map<String, Command> onLogin = new HashMap<>();
|
||||
private Map<String, Command> onRegister = new HashMap<>();
|
||||
|
||||
public Map<String, Command> getOnJoin() {
|
||||
return onJoin;
|
||||
|
||||
@@ -2,9 +2,11 @@ package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import com.github.authme.configme.SettingsManager;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -27,6 +29,12 @@ public class CommandManager implements Reloadable {
|
||||
@Inject
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Inject
|
||||
private CommandsMigrater commandsMigrater;
|
||||
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
|
||||
CommandManager() {
|
||||
}
|
||||
@@ -39,6 +47,10 @@ public class CommandManager implements Reloadable {
|
||||
executeCommands(player, commandConfig.getOnRegister());
|
||||
}
|
||||
|
||||
public void runCommandsOnLogin(Player player) {
|
||||
executeCommands(player, commandConfig.getOnLogin());
|
||||
}
|
||||
|
||||
private void executeCommands(Player player, Map<String, Command> commands) {
|
||||
for (Command command : commands.values()) {
|
||||
final String execution = command.getCommand().replace("%p", player.getName());
|
||||
@@ -58,7 +70,20 @@ public class CommandManager implements Reloadable {
|
||||
|
||||
SettingsManager settingsManager = new SettingsManager(
|
||||
new YamlFileResource(file), null, CommandSettingsHolder.class);
|
||||
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
|
||||
CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
|
||||
|
||||
if (commandsMigrater.transformOldCommands(commandConfig)) {
|
||||
ConsoleLogger.warning("Old setting properties (such as settings.forceCommands) were found. "
|
||||
+ "They have been moved to commands.yml");
|
||||
settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig);
|
||||
settingsManager.save();
|
||||
settingsManager.reload();
|
||||
settings.save();
|
||||
settings.reload();
|
||||
|
||||
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
|
||||
}
|
||||
this.commandConfig = commandConfig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
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.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Migrates the commands from their old location, in config.yml, to the dedicated commands configuration file.
|
||||
*/
|
||||
class CommandsMigrater {
|
||||
|
||||
@Inject
|
||||
private SettingsMigrationService settingsMigrationService;
|
||||
|
||||
CommandsMigrater() {
|
||||
}
|
||||
|
||||
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::getOnJoin),
|
||||
|
||||
ON_JOIN_CONSOLE(
|
||||
SettingsMigrationService::getOnLoginConsoleCommands,
|
||||
Executor.CONSOLE,
|
||||
CommandConfig::getOnJoin),
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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 = legacyCommandsGetter;
|
||||
this.executor = executor;
|
||||
this.commandMapGetter = commandMapGetter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 -> new Command(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("Migrated " + commands.size() + " of type " + this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user