#923 Add commands to run on unregister

This commit is contained in:
ljacqu 2017-04-29 19:08:10 +02:00
parent 578f63b944
commit d4c1370da6
8 changed files with 49 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.TeleportationService; 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.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -52,7 +53,11 @@ public class AsynchronousUnregister implements AsynchronousProcess {
@Inject @Inject
private AuthGroupHandler authGroupHandler; private AuthGroupHandler authGroupHandler;
AsynchronousUnregister() { } @Inject
private CommandManager commandManager;
AsynchronousUnregister() {
}
/** /**
* Processes a player's request to unregister himself. Unregisters the player after * 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()) { if (player == null || !player.isOnline()) {
return; return;
} }
commandManager.runCommandsOnUnregister(player);
if (service.getProperty(RegistrationSettings.FORCE)) { if (service.getProperty(RegistrationSettings.FORCE)) {
teleportationService.teleportOnJoin(player); teleportationService.teleportOnJoin(player);

View File

@ -13,6 +13,7 @@ public class CommandConfig {
private Map<String, Command> onJoin = new LinkedHashMap<>(); private Map<String, Command> onJoin = new LinkedHashMap<>();
private Map<String, Command> onLogin = new LinkedHashMap<>(); private Map<String, Command> onLogin = new LinkedHashMap<>();
private Map<String, Command> onRegister = new LinkedHashMap<>(); private Map<String, Command> onRegister = new LinkedHashMap<>();
private Map<String, Command> onUnregister = new LinkedHashMap<>();
public Map<String, Command> getOnJoin() { public Map<String, Command> getOnJoin() {
return onJoin; return onJoin;
@ -37,4 +38,12 @@ public class CommandConfig {
public void setOnRegister(Map<String, Command> onRegister) { public void setOnRegister(Map<String, Command> onRegister) {
this.onRegister = onRegister; this.onRegister = onRegister;
} }
public Map<String, Command> getOnUnregister() {
return onUnregister;
}
public void setOnUnregister(Map<String, Command> onUnregister) {
this.onUnregister = onUnregister;
}
} }

View File

@ -34,6 +34,7 @@ public class CommandManager implements Reloadable {
private WrappedTagReplacer<Command, Player> onJoinCommands; private WrappedTagReplacer<Command, Player> onJoinCommands;
private WrappedTagReplacer<Command, Player> onLoginCommands; private WrappedTagReplacer<Command, Player> onLoginCommands;
private WrappedTagReplacer<Command, Player> onRegisterCommands; private WrappedTagReplacer<Command, Player> onRegisterCommands;
private WrappedTagReplacer<Command, Player> onUnregisterCommands;
@Inject @Inject
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, GeoIpService geoIpService, CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, GeoIpService geoIpService,
@ -72,6 +73,15 @@ public class CommandManager implements Reloadable {
executeCommands(player, onLoginCommands.getAdaptedItems(player)); 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<Command> commands) { private void executeCommands(Player player, List<Command> commands) {
for (Command command : commands) { for (Command command : commands) {
final String execution = command.getCommand(); final String execution = command.getCommand();
@ -94,6 +104,7 @@ public class CommandManager implements Reloadable {
onJoinCommands = newReplacer(commandConfig.getOnJoin()); onJoinCommands = newReplacer(commandConfig.getOnJoin());
onLoginCommands = newReplacer(commandConfig.getOnLogin()); onLoginCommands = newReplacer(commandConfig.getOnLogin());
onRegisterCommands = newReplacer(commandConfig.getOnRegister()); onRegisterCommands = newReplacer(commandConfig.getOnRegister());
onUnregisterCommands = newReplacer(commandConfig.getOnUnregister());
} }
private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) { private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) {

View File

@ -30,13 +30,18 @@ class CommandMigrationService implements MigrationService {
final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource); final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource);
final boolean didMoveCommands = transformOldCommands(commandConfig); 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); resource.setValue("", commandConfig);
return true; return true;
} }
return false; 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. * Adds command settings from their old location (in config.yml) to the given command configuration object.
* *

View File

@ -48,10 +48,13 @@ public final class CommandSettingsHolder implements SettingsHolder {
" command: 'broadcast %p has joined, welcome back!'", " command: 'broadcast %p has joined, welcome back!'",
" executor: CONSOLE", " executor: CONSOLE",
"", "",
"Supported command events: onLogin, onJoin, onRegister" "Supported command events: onLogin, onJoin, onRegister, onUnregister"
}; };
Map<String, String[]> commentMap = new HashMap<>(); Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("", comments); commentMap.put("", comments);
commentMap.put("onUnregister", new String[]{
"Commands to run whenever a player is unregistered (by himself, or by an admin)"
});
return commentMap; return commentMap;
} }

View File

@ -11,7 +11,7 @@
# welcome: # welcome:
# command: 'msg %p Welcome to the server!' # command: 'msg %p Welcome to the server!'
# executor: CONSOLE # executor: CONSOLE
# #
# This will make the console execute the msg command to the player. # 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), # 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, # after which a mandatory 'command' field defines the command to run,
@ -23,8 +23,10 @@
# broadcast: # broadcast:
# command: 'broadcast %p has joined, welcome back!' # command: 'broadcast %p has joined, welcome back!'
# executor: CONSOLE # executor: CONSOLE
# #
# Supported command events: onLogin, onJoin, onRegister # Supported command events: onLogin, onJoin, onRegister, onUnregister
onJoin: {} onJoin: {}
onLogin: {} onLogin: {}
onRegister: {} onRegister: {}
# Commands to run whenever a player is unregistered (by himself, or by an admin)
onUnregister: {}

View File

@ -14,6 +14,7 @@ import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService; import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.TeleportationService; 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.RegistrationSettings;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -62,6 +63,8 @@ public class AsynchronousUnregisterTest {
private TeleportationService teleportationService; private TeleportationService teleportationService;
@Mock @Mock
private AuthGroupHandler authGroupHandler; private AuthGroupHandler authGroupHandler;
@Mock
private CommandManager commandManager;
@BeforeClass @BeforeClass
public static void initLogger() { public static void initLogger() {
@ -119,6 +122,7 @@ public class AsynchronousUnregisterTest {
verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED);
verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
verifyCalledUnregisterEventFor(player); verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
} }
@Test @Test
@ -149,6 +153,7 @@ public class AsynchronousUnregisterTest {
verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED);
verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
verifyCalledUnregisterEventFor(player); verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
} }
@Test @Test
@ -178,6 +183,7 @@ public class AsynchronousUnregisterTest {
verifyZeroInteractions(teleportationService, limboService); verifyZeroInteractions(teleportationService, limboService);
verify(bukkitService, never()).runTask(any(Runnable.class)); verify(bukkitService, never()).runTask(any(Runnable.class));
verifyCalledUnregisterEventFor(player); verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
} }
@Test @Test
@ -253,6 +259,7 @@ public class AsynchronousUnregisterTest {
verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED); verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED);
verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class)); verify(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
verifyCalledUnregisterEventFor(player); verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
} }
@Test @Test

View File

@ -6,7 +6,6 @@ import ch.jalu.configme.resource.YamlFileResource;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.SettingsMigrationService; import fr.xephi.authme.settings.SettingsMigrationService;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -115,7 +114,6 @@ public class CommandMigrationServiceTest {
} }
@Test @Test
@Ignore // TODO ConfigMe/#29: Create PropertyResource#getKeys
public void shouldRewriteForEmptyFile() { public void shouldRewriteForEmptyFile() {
// given // given
File commandFile = TestHelper.getJarFile("/fr/xephi/authme/settings/commandconfig/commands.empty.yml"); File commandFile = TestHelper.getJarFile("/fr/xephi/authme/settings/commandconfig/commands.empty.yml");