Implement onSessionLogin trigger for commands.yml (#1198)

* Implement onSessionLogin trigger for commands.yml

Needs test methods, @ljacqu any advice?

* Add simple onSessionLogin test
This commit is contained in:
Gabriele C 2017-05-19 19:21:05 +02:00 committed by GitHub
parent 4366e33508
commit cff8ccd76a
7 changed files with 46 additions and 3 deletions

View File

@ -123,6 +123,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
// Session logic
if (canResumeSession(player)) {
service.send(player, MessageKey.SESSION_RECONNECTION);
// Run commands
bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(() -> commandManager.runCommandsOnSessionLogin(player));
bukkitService.runTaskOptionallyAsync(() -> asynchronousLogin.forceLogin(player));
return;
}

View File

@ -12,6 +12,7 @@ public class CommandConfig {
private Map<String, Command> onJoin = new LinkedHashMap<>();
private Map<String, Command> onLogin = new LinkedHashMap<>();
private Map<String, Command> onSessionLogin = new LinkedHashMap<>();
private Map<String, Command> onRegister = new LinkedHashMap<>();
private Map<String, Command> onUnregister = new LinkedHashMap<>();
@ -31,6 +32,14 @@ public class CommandConfig {
this.onLogin = onLogin;
}
public Map<String, Command> getOnSessionLogin() {
return onSessionLogin;
}
public void setOnSessionLogin(Map<String, Command> onSessionLogin) {
this.onSessionLogin = onSessionLogin;
}
public Map<String, Command> getOnRegister() {
return onRegister;
}

View File

@ -33,6 +33,7 @@ public class CommandManager implements Reloadable {
private WrappedTagReplacer<Command, Player> onJoinCommands;
private WrappedTagReplacer<Command, Player> onLoginCommands;
private WrappedTagReplacer<Command, Player> onSessionLoginCommands;
private WrappedTagReplacer<Command, Player> onRegisterCommands;
private WrappedTagReplacer<Command, Player> onUnregisterCommands;
@ -73,6 +74,16 @@ public class CommandManager implements Reloadable {
executeCommands(player, onLoginCommands.getAdaptedItems(player));
}
/**
* Runs the configured commands for when a player has logged in successfully due to session.
*
* @param player the player that logged in
*/
public void runCommandsOnSessionLogin(Player player) {
executeCommands(player, onSessionLoginCommands.getAdaptedItems(player));
}
/**
* Runs the configured commands for when a player has been unregistered.
*
@ -103,6 +114,7 @@ public class CommandManager implements Reloadable {
CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
onJoinCommands = newReplacer(commandConfig.getOnJoin());
onLoginCommands = newReplacer(commandConfig.getOnLogin());
onSessionLoginCommands = newReplacer(commandConfig.getOnSessionLogin());
onRegisterCommands = newReplacer(commandConfig.getOnRegister());
onUnregisterCommands = newReplacer(commandConfig.getOnUnregister());
}

View File

@ -48,7 +48,7 @@ public final class CommandSettingsHolder implements SettingsHolder {
" command: 'broadcast %p has joined, welcome back!'",
" executor: CONSOLE",
"",
"Supported command events: onLogin, onJoin, onRegister, onUnregister"
"Supported command events: onLogin, onSessionLogin, onJoin, onRegister, onUnregister"
};
Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("", comments);

View File

@ -24,9 +24,10 @@
# command: 'broadcast %p has joined, welcome back!'
# executor: CONSOLE
#
# Supported command events: onLogin, onJoin, onRegister, onUnregister
# Supported command events: onLogin, onSessionLogin, onJoin, onRegister, onUnregister
onJoin: {}
onLogin: {}
onSessionLogin: {}
onRegister: {}
# Commands to run whenever a player is unregistered (by himself, or by an admin)
onUnregister: {}

View File

@ -92,6 +92,21 @@ public class CommandManagerTest {
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnSessionLogin() {
// given
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
initManager();
// when
manager.runCommandsOnSessionLogin(player);
// then
verify(bukkitService).dispatchConsoleCommand("msg Bobby Session login!");
verifyNoMoreInteractions(bukkitService);
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnJoin() {
// given

View File

@ -21,3 +21,7 @@ onLogin:
display_list:
command: 'list'
executor: PLAYER
onSessionLogin:
welcome:
command: 'msg %p Session login!'
executor: CONSOLE