#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.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);

View File

@ -13,6 +13,7 @@ public class CommandConfig {
private Map<String, Command> onJoin = new LinkedHashMap<>();
private Map<String, Command> onLogin = new LinkedHashMap<>();
private Map<String, Command> onRegister = new LinkedHashMap<>();
private Map<String, Command> onUnregister = new LinkedHashMap<>();
public Map<String, Command> getOnJoin() {
return onJoin;
@ -37,4 +38,12 @@ public class CommandConfig {
public void setOnRegister(Map<String, Command> 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> onLoginCommands;
private WrappedTagReplacer<Command, Player> onRegisterCommands;
private WrappedTagReplacer<Command, Player> 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<Command> 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<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 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.
*

View File

@ -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<String, String[]> 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;
}

View File

@ -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: {}

View File

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

View File

@ -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");