#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
@@ -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);
@@ -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;
}
}
@@ -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) {
@@ -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.
*
@@ -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;
}
+5 -3
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: {}