#1218 Add onLogout to commands.yml

This commit is contained in:
ljacqu
2017-05-21 13:42:49 +02:00
parent 1c46c92b4e
commit 5c6af0330e
11 changed files with 121 additions and 39 deletions
@@ -15,6 +15,7 @@ public class CommandConfig {
private Map<String, Command> onSessionLogin = new LinkedHashMap<>();
private Map<String, Command> onRegister = new LinkedHashMap<>();
private Map<String, Command> onUnregister = new LinkedHashMap<>();
private Map<String, Command> onLogout = new LinkedHashMap<>();
public Map<String, Command> getOnJoin() {
return onJoin;
@@ -55,4 +56,12 @@ public class CommandConfig {
public void setOnUnregister(Map<String, Command> onUnregister) {
this.onUnregister = onUnregister;
}
public Map<String, Command> getOnLogout() {
return onLogout;
}
public void setOnLogout(Map<String, Command> onLogout) {
this.onLogout = onLogout;
}
}
@@ -36,6 +36,7 @@ public class CommandManager implements Reloadable {
private WrappedTagReplacer<Command, Player> onSessionLoginCommands;
private WrappedTagReplacer<Command, Player> onRegisterCommands;
private WrappedTagReplacer<Command, Player> onUnregisterCommands;
private WrappedTagReplacer<Command, Player> onLogoutCommands;
@Inject
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, GeoIpService geoIpService,
@@ -93,6 +94,15 @@ public class CommandManager implements Reloadable {
executeCommands(player, onUnregisterCommands.getAdaptedItems(player));
}
/**
* Runs the configured commands for when a player logs out (by command or by quitting the server).
*
* @param player the player that is no longer logged in
*/
public void runCommandsOnLogout(Player player) {
executeCommands(player, onLogoutCommands.getAdaptedItems(player));
}
private void executeCommands(Player player, List<Command> commands) {
for (Command command : commands) {
final String execution = command.getCommand();
@@ -117,6 +127,7 @@ public class CommandManager implements Reloadable {
onSessionLoginCommands = newReplacer(commandConfig.getOnSessionLogin());
onRegisterCommands = newReplacer(commandConfig.getOnRegister());
onUnregisterCommands = newReplacer(commandConfig.getOnUnregister());
onLogoutCommands = newReplacer(commandConfig.getOnLogout());
}
private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) {
@@ -48,13 +48,19 @@ public final class CommandSettingsHolder implements SettingsHolder {
" command: 'broadcast %p has joined, welcome back!'",
" executor: CONSOLE",
"",
"Supported command events: onLogin, onSessionLogin, onJoin, onRegister, onUnregister"
"Supported command events: onLogin, onSessionLogin, onJoin, onLogout, 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)"
});
commentMap.put("onLogout", new String[]{
"These commands are called whenever a logged in player uses /logout or quits.",
"The commands are not run if a player that was not logged in quits the server.",
"Note: if your server crashes, these commands won't be run, so don't rely on them to undo",
"'onLogin' commands that would be dangerous for non-logged in players to have!"
});
return commentMap;
}