diff --git a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java index f03322385..e5a3a36ea 100644 --- a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java +++ b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java @@ -15,6 +15,7 @@ import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.command.CommandSender; @@ -52,7 +53,11 @@ public class AsynchronousUnregister implements AsynchronousProcess { @Inject private AuthGroupHandler authGroupHandler; - AsynchronousUnregister() { } + @Inject + private CommandManager commandManager; + + AsynchronousUnregister() { + } /** * Processes a player's request to unregister himself. Unregisters the player after @@ -107,6 +112,7 @@ public class AsynchronousUnregister implements AsynchronousProcess { if (player == null || !player.isOnline()) { return; } + commandManager.runCommandsOnUnregister(player); if (service.getProperty(RegistrationSettings.FORCE)) { teleportationService.teleportOnJoin(player); diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandConfig.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandConfig.java index 19c05505f..4bf836912 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandConfig.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandConfig.java @@ -13,6 +13,7 @@ public class CommandConfig { private Map onJoin = new LinkedHashMap<>(); private Map onLogin = new LinkedHashMap<>(); private Map onRegister = new LinkedHashMap<>(); + private Map onUnregister = new LinkedHashMap<>(); public Map getOnJoin() { return onJoin; @@ -37,4 +38,12 @@ public class CommandConfig { public void setOnRegister(Map onRegister) { this.onRegister = onRegister; } + + public Map getOnUnregister() { + return onUnregister; + } + + public void setOnUnregister(Map onUnregister) { + this.onUnregister = onUnregister; + } } diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java index f996640c9..abebd9b79 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java @@ -34,6 +34,7 @@ public class CommandManager implements Reloadable { private WrappedTagReplacer onJoinCommands; private WrappedTagReplacer onLoginCommands; private WrappedTagReplacer onRegisterCommands; + private WrappedTagReplacer onUnregisterCommands; @Inject CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, GeoIpService geoIpService, @@ -72,6 +73,15 @@ public class CommandManager implements Reloadable { executeCommands(player, onLoginCommands.getAdaptedItems(player)); } + /** + * Runs the configured commands for when a player has been unregistered. + * + * @param player the player that has been unregistered + */ + public void runCommandsOnUnregister(Player player) { + executeCommands(player, onUnregisterCommands.getAdaptedItems(player)); + } + private void executeCommands(Player player, List commands) { for (Command command : commands) { final String execution = command.getCommand(); @@ -94,6 +104,7 @@ public class CommandManager implements Reloadable { onJoinCommands = newReplacer(commandConfig.getOnJoin()); onLoginCommands = newReplacer(commandConfig.getOnLogin()); onRegisterCommands = newReplacer(commandConfig.getOnRegister()); + onUnregisterCommands = newReplacer(commandConfig.getOnUnregister()); } private WrappedTagReplacer newReplacer(Map commands) { diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java index 092d66d2a..1a945692f 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java @@ -30,13 +30,18 @@ class CommandMigrationService implements MigrationService { final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource); final boolean didMoveCommands = transformOldCommands(commandConfig); - if (didMoveCommands) { // TODO ConfigMe/#29: Check that loaded file isn't completely empty + if (didMoveCommands || isFileEmpty(resource)) { resource.setValue("", commandConfig); return true; } return false; } + private boolean isFileEmpty(PropertyResource resource) { + Object root = resource.getObject(""); + return (root instanceof Map) && ((Map) root).isEmpty(); + } + /** * Adds command settings from their old location (in config.yml) to the given command configuration object. * diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandSettingsHolder.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandSettingsHolder.java index 7104cee2b..9024ca9a6 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandSettingsHolder.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandSettingsHolder.java @@ -48,10 +48,13 @@ public final class CommandSettingsHolder implements SettingsHolder { " command: 'broadcast %p has joined, welcome back!'", " executor: CONSOLE", "", - "Supported command events: onLogin, onJoin, onRegister" + "Supported command events: onLogin, onJoin, onRegister, onUnregister" }; Map commentMap = new HashMap<>(); commentMap.put("", comments); + commentMap.put("onUnregister", new String[]{ + "Commands to run whenever a player is unregistered (by himself, or by an admin)" + }); return commentMap; } diff --git a/src/main/resources/commands.yml b/src/main/resources/commands.yml index e0330bf9d..80fbd103c 100644 --- a/src/main/resources/commands.yml +++ b/src/main/resources/commands.yml @@ -11,7 +11,7 @@ # welcome: # command: 'msg %p Welcome to the server!' # executor: CONSOLE -# +# # This will make the console execute the msg command to the player. # Each command under an event has a name you can choose freely (e.g. 'welcome' as above), # after which a mandatory 'command' field defines the command to run, @@ -23,8 +23,10 @@ # broadcast: # command: 'broadcast %p has joined, welcome back!' # executor: CONSOLE -# -# Supported command events: onLogin, onJoin, onRegister +# +# Supported command events: onLogin, onJoin, onRegister, onUnregister onJoin: {} onLogin: {} onRegister: {} +# Commands to run whenever a player is unregistered (by himself, or by an admin) +onUnregister: {} \ No newline at end of file diff --git a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java index 9ff0faeed..25289adac 100644 --- a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java +++ b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java @@ -14,6 +14,7 @@ import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -62,6 +63,8 @@ public class AsynchronousUnregisterTest { private TeleportationService teleportationService; @Mock private AuthGroupHandler authGroupHandler; + @Mock + private CommandManager commandManager; @BeforeClass public static void initLogger() { @@ -119,6 +122,7 @@ public class AsynchronousUnregisterTest { verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verifyCalledUnregisterEventFor(player); + verify(commandManager).runCommandsOnUnregister(player); } @Test @@ -149,6 +153,7 @@ public class AsynchronousUnregisterTest { verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verifyCalledUnregisterEventFor(player); + verify(commandManager).runCommandsOnUnregister(player); } @Test @@ -178,6 +183,7 @@ public class AsynchronousUnregisterTest { verifyZeroInteractions(teleportationService, limboService); verify(bukkitService, never()).runTask(any(Runnable.class)); verifyCalledUnregisterEventFor(player); + verify(commandManager).runCommandsOnUnregister(player); } @Test @@ -253,6 +259,7 @@ public class AsynchronousUnregisterTest { verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verifyCalledUnregisterEventFor(player); + verify(commandManager).runCommandsOnUnregister(player); } @Test diff --git a/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java b/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java index b03a1c8e4..be7b55b6c 100644 --- a/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java +++ b/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java @@ -6,7 +6,6 @@ import ch.jalu.configme.resource.YamlFileResource; import fr.xephi.authme.TestHelper; import fr.xephi.authme.settings.SettingsMigrationService; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -115,7 +114,6 @@ public class CommandMigrationServiceTest { } @Test - @Ignore // TODO ConfigMe/#29: Create PropertyResource#getKeys public void shouldRewriteForEmptyFile() { // given File commandFile = TestHelper.getJarFile("/fr/xephi/authme/settings/commandconfig/commands.empty.yml");