#411 Migrate settings to new structure (work in progress)

- Work in progress: config.yml cannot be loaded after migration
This commit is contained in:
ljacqu 2016-11-22 21:16:56 +01:00
parent 4214c6dc80
commit 254655abdb
9 changed files with 206 additions and 45 deletions

View File

@ -20,6 +20,8 @@ public class SettingsProvider implements Provider<Settings> {
@Inject @Inject
@DataFolder @DataFolder
private File dataFolder; private File dataFolder;
@Inject
private SettingsMigrationService migrationService;
SettingsProvider() { SettingsProvider() {
} }
@ -36,7 +38,6 @@ public class SettingsProvider implements Provider<Settings> {
FileUtils.create(configFile); FileUtils.create(configFile);
} }
PropertyResource resource = new YamlFileResource(configFile); PropertyResource resource = new YamlFileResource(configFile);
SettingsMigrationService migrationService = new SettingsMigrationService(dataFolder);
ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData(); ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData();
return new Settings(dataFolder, resource, migrationService, configurationData); return new Settings(dataFolder, resource, migrationService, configurationData);
} }

View File

@ -11,6 +11,7 @@ import fr.xephi.authme.listener.PlayerListener;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BungeeService; import fr.xephi.authme.service.BungeeService;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService; import fr.xephi.authme.service.TeleportationService;
@ -49,6 +50,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject @Inject
private DataSource dataSource; private DataSource dataSource;
@Inject
private CommandManager commandManager;
ProcessSyncPlayerLogin() { ProcessSyncPlayerLogin() {
} }
@ -60,16 +64,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
} }
} }
private void forceCommands(Player player) {
for (String command : service.getProperty(RegistrationSettings.FORCE_COMMANDS)) {
player.performCommand(command.replace("%p", player.getName()));
}
for (String command : service.getProperty(RegistrationSettings.FORCE_COMMANDS_AS_CONSOLE)) {
Bukkit.getServer().dispatchCommand(
Bukkit.getServer().getConsoleSender(), command.replace("%p", player.getName()));
}
}
public void processPlayerLogin(Player player) { public void processPlayerLogin(Player player) {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
@ -124,7 +118,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
} }
// Login is now finished; we can force all commands // Login is now finished; we can force all commands
forceCommands(player); commandManager.runCommandsOnLogin(player);
// Send Bungee stuff. The service will check if it is enabled or not. // Send Bungee stuff. The service will check if it is enabled or not.
bungeeService.connectPlayer(player); bungeeService.connectPlayer(player);

View File

@ -68,7 +68,7 @@ public class Settings extends SettingsManager {
private void loadSettingsFromFiles() { private void loadSettingsFromFiles() {
passwordEmailMessage = readFile("email.html"); passwordEmailMessage = readFile("email.html");
recoveryCodeEmailMessage = readFile("recovery_code_email.html"); recoveryCodeEmailMessage = readFile("recovery_code_email.html");
welcomeMessage = readFile("welcome.txt").split("\n"); welcomeMessage = readFile("welcome.txt").split("\\n");
} }
@Override @Override

View File

@ -2,16 +2,20 @@ package fr.xephi.authme.settings;
import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.migration.PlainMigrationService;
import com.github.authme.configme.properties.Property; import com.github.authme.configme.properties.Property;
import com.github.authme.configme.properties.StringListProperty;
import com.github.authme.configme.resource.PropertyResource; import com.github.authme.configme.resource.PropertyResource;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.output.LogLevel; import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import javax.inject.Inject;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.List; import java.util.List;
import static com.github.authme.configme.properties.PropertyInitializer.newListProperty; import static com.github.authme.configme.properties.PropertyInitializer.newListProperty;
@ -28,10 +32,20 @@ import static fr.xephi.authme.settings.properties.RestrictionSettings.FORCE_SPAW
*/ */
public class SettingsMigrationService extends PlainMigrationService { public class SettingsMigrationService extends PlainMigrationService {
private final File pluginFolder; @Inject
@DataFolder
private File pluginFolder;
public SettingsMigrationService(File pluginFolder) { // Stores old commands that need to be migrated to the new commands configuration
this.pluginFolder = pluginFolder; // 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();
SettingsMigrationService() {
} }
@Override @Override
@ -42,6 +56,8 @@ public class SettingsMigrationService extends PlainMigrationService {
changes = true; changes = true;
} }
gatherOldCommandSettings(resource);
// Note ljacqu 20160211: Concatenating migration methods with | instead of the usual || // Note ljacqu 20160211: Concatenating migration methods with | instead of the usual ||
// ensures that all migrations will be performed // ensures that all migrations will be performed
return changes return changes
@ -59,7 +75,9 @@ public class SettingsMigrationService extends PlainMigrationService {
"Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications", "Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications",
"Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt", "Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt",
"VeryGames", "settings.restrictions.allowAllCommandsIfRegistrationIsOptional", "DataSource.mySQLWebsite", "VeryGames", "settings.restrictions.allowAllCommandsIfRegistrationIsOptional", "DataSource.mySQLWebsite",
"Hooks.customAttributes", "Security.stop.kickPlayersBeforeStopping"}; "Hooks.customAttributes", "Security.stop.kickPlayersBeforeStopping",
"settings.restrictions.keepCollisionsDisabled", "settings.forceCommands", "settings.forceCommandsAsConsole",
"settings.forceRegisterCommands", "settings.forceRegisterCommandsAsConsole"};
for (String deprecatedPath : deprecatedProperties) { for (String deprecatedPath : deprecatedProperties) {
if (resource.contains(deprecatedPath)) { if (resource.contains(deprecatedPath)) {
return true; return true;
@ -68,6 +86,37 @@ public class SettingsMigrationService extends PlainMigrationService {
return false; 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) {
List<String> entries = new StringListProperty(path).getFromResource(resource);
return entries == null ? Collections.emptyList() : entries;
}
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 // Specific migrations
// -------- // --------

View File

@ -10,6 +10,23 @@ public class Command {
/** The executor of the command. */ /** The executor of the command. */
private Executor executor = Executor.PLAYER; 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() { public String getCommand() {
return command; return command;
} }

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.settings.commandconfig; package fr.xephi.authme.settings.commandconfig;
import java.util.Collections; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -10,9 +10,9 @@ import java.util.Map;
*/ */
public class CommandConfig { public class CommandConfig {
private Map<String, Command> onJoin = Collections.emptyMap(); private Map<String, Command> onJoin = new HashMap<>();
private Map<String, Command> onLogin = Collections.emptyMap(); private Map<String, Command> onLogin = new HashMap<>();
private Map<String, Command> onRegister = Collections.emptyMap(); private Map<String, Command> onRegister = new HashMap<>();
public Map<String, Command> getOnJoin() { public Map<String, Command> getOnJoin() {
return onJoin; return onJoin;

View File

@ -2,9 +2,11 @@ package fr.xephi.authme.settings.commandconfig;
import com.github.authme.configme.SettingsManager; import com.github.authme.configme.SettingsManager;
import com.github.authme.configme.resource.YamlFileResource; import com.github.authme.configme.resource.YamlFileResource;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.FileUtils; import fr.xephi.authme.util.FileUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -27,6 +29,12 @@ public class CommandManager implements Reloadable {
@Inject @Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@Inject
private CommandsMigrater commandsMigrater;
@Inject
private Settings settings;
CommandManager() { CommandManager() {
} }
@ -39,6 +47,10 @@ public class CommandManager implements Reloadable {
executeCommands(player, commandConfig.getOnRegister()); executeCommands(player, commandConfig.getOnRegister());
} }
public void runCommandsOnLogin(Player player) {
executeCommands(player, commandConfig.getOnLogin());
}
private void executeCommands(Player player, Map<String, Command> commands) { private void executeCommands(Player player, Map<String, Command> commands) {
for (Command command : commands.values()) { for (Command command : commands.values()) {
final String execution = command.getCommand().replace("%p", player.getName()); final String execution = command.getCommand().replace("%p", player.getName());
@ -58,7 +70,20 @@ public class CommandManager implements Reloadable {
SettingsManager settingsManager = new SettingsManager( SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(file), null, CommandSettingsHolder.class); 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;
} }

View File

@ -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;
}
}
}

View File

@ -4,9 +4,6 @@ import com.github.authme.configme.Comment;
import com.github.authme.configme.SettingsHolder; import com.github.authme.configme.SettingsHolder;
import com.github.authme.configme.properties.Property; import com.github.authme.configme.properties.Property;
import java.util.List;
import static com.github.authme.configme.properties.PropertyInitializer.newListProperty;
import static com.github.authme.configme.properties.PropertyInitializer.newProperty; import static com.github.authme.configme.properties.PropertyInitializer.newProperty;
public class RegistrationSettings implements SettingsHolder { public class RegistrationSettings implements SettingsHolder {
@ -48,26 +45,6 @@ public class RegistrationSettings implements SettingsHolder {
public static final Property<Boolean> FORCE_LOGIN_AFTER_REGISTER = public static final Property<Boolean> FORCE_LOGIN_AFTER_REGISTER =
newProperty("settings.registration.forceLoginAfterRegister", false); newProperty("settings.registration.forceLoginAfterRegister", false);
@Comment("Force these commands after /login, without any '/', use %p to replace with player name")
public static final Property<List<String>> FORCE_COMMANDS =
newListProperty("settings.forceCommands");
@Comment({
"Force these commands after /login as service console, without any '/'.",
"Use %p to replace with player name"})
public static final Property<List<String>> FORCE_COMMANDS_AS_CONSOLE =
newListProperty("settings.forceCommandsAsConsole");
@Comment("Force these commands after /register, without any '/', use %p to replace with player name")
public static final Property<List<String>> FORCE_REGISTER_COMMANDS =
newListProperty("settings.forceRegisterCommands");
@Comment({
"Force these commands after /register as a server console, without any '/'.",
"Use %p to replace with player name"})
public static final Property<List<String>> FORCE_REGISTER_COMMANDS_AS_CONSOLE =
newListProperty("settings.forceRegisterCommandsAsConsole");
@Comment({ @Comment({
"Enable to display the welcome message (welcome.txt) after a login", "Enable to display the welcome message (welcome.txt) after a login",
"You can use colors in this welcome.txt + some replaced strings:", "You can use colors in this welcome.txt + some replaced strings:",