#923 Add commands to run on unregister
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user