#1035 Forced commands: add more tests, rename account constraints, update commands.yml comments

This commit is contained in:
ljacqu 2018-01-16 20:32:17 +01:00
parent 3c0236e15e
commit f19f8502d8
7 changed files with 118 additions and 26 deletions

View File

@ -135,10 +135,10 @@ public class CommandManager implements Reloadable {
}
private static boolean shouldCommandBeRun(OnLoginCommand command, int numberOfOtherAccounts) {
return (!command.getNumberOfOtherAccountsAtLeast().isPresent()
|| command.getNumberOfOtherAccountsAtLeast().get() <= numberOfOtherAccounts)
&& (!command.getNumberOfOtherAccountsLessThan().isPresent()
|| command.getNumberOfOtherAccountsLessThan().get() >= numberOfOtherAccounts);
return (!command.getIfNumberOfAccountsAtLeast().isPresent()
|| command.getIfNumberOfAccountsAtLeast().get() <= numberOfOtherAccounts)
&& (!command.getIfNumberOfAccountsLessThan().isPresent()
|| command.getIfNumberOfAccountsLessThan().get() > numberOfOtherAccounts);
}
@Override
@ -167,8 +167,8 @@ public class CommandManager implements Reloadable {
Map<String, OnLoginCommand> commands) {
return new WrappedTagReplacer<>(availableTags, commands.values(), Command::getCommand,
(cmd, text) -> new OnLoginCommand(text, cmd.getExecutor(), cmd.getNumberOfOtherAccountsAtLeast(),
cmd.getNumberOfOtherAccountsLessThan()));
(cmd, text) -> new OnLoginCommand(text, cmd.getExecutor(), cmd.getIfNumberOfAccountsAtLeast(),
cmd.getIfNumberOfAccountsLessThan()));
}
private List<Tag<Player>> buildAvailableTags() {

View File

@ -49,8 +49,17 @@ public final class CommandSettingsHolder implements SettingsHolder {
" executor: CONSOLE",
"",
"Supported command events: onLogin, onSessionLogin, onFirstLogin, onJoin, onLogout, onRegister, "
+ "onUnregister"
+ "onUnregister",
"",
"For onLogin and onFirstLogin, you can use 'ifNumberOfAccountsLessThan' and 'ifNumberOfAccountsAtLeast'",
"to specify limits to how many accounts a player can have (matched by IP) for a command to be run:",
"onLogin:",
" warnOnManyAccounts:",
" command: 'say Uh oh! %p has many alt accounts!'",
" executor: CONSOLE",
" ifNumberOfAccountsAtLeast: 5"
};
Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("", rootComments);
commentMap.put("onFirstLogin", new String[]{

View File

@ -7,8 +7,8 @@ import java.util.Optional;
*/
public class OnLoginCommand extends Command {
private Optional<Integer> numberOfOtherAccountsAtLeast;
private Optional<Integer> numberOfOtherAccountsLessThan;
private Optional<Integer> ifNumberOfAccountsAtLeast;
private Optional<Integer> ifNumberOfAccountsLessThan;
/**
* Default constructor (for bean mapping).
@ -31,29 +31,29 @@ public class OnLoginCommand extends Command {
*
* @param command the command to execute
* @param executor the executor of the command
* @param numberOfOtherAccountsAtLeast required number of accounts for the command to run
* @param numberOfOtherAccountsLessThan max threshold of accounts, from which the command will not be run
* @param ifNumberOfAccountsAtLeast required number of accounts for the command to run
* @param ifNumberOfAccountsLessThan max threshold of accounts, from which the command will not be run
*/
public OnLoginCommand(String command, Executor executor, Optional<Integer> numberOfOtherAccountsAtLeast,
Optional<Integer> numberOfOtherAccountsLessThan) {
public OnLoginCommand(String command, Executor executor, Optional<Integer> ifNumberOfAccountsAtLeast,
Optional<Integer> ifNumberOfAccountsLessThan) {
super(command, executor);
this.numberOfOtherAccountsAtLeast = numberOfOtherAccountsAtLeast;
this.numberOfOtherAccountsLessThan = numberOfOtherAccountsLessThan;
this.ifNumberOfAccountsAtLeast = ifNumberOfAccountsAtLeast;
this.ifNumberOfAccountsLessThan = ifNumberOfAccountsLessThan;
}
public Optional<Integer> getNumberOfOtherAccountsAtLeast() {
return numberOfOtherAccountsAtLeast;
public Optional<Integer> getIfNumberOfAccountsAtLeast() {
return ifNumberOfAccountsAtLeast;
}
public void setNumberOfOtherAccountsAtLeast(Optional<Integer> numberOfOtherAccountsAtLeast) {
this.numberOfOtherAccountsAtLeast = numberOfOtherAccountsAtLeast;
public void setIfNumberOfAccountsAtLeast(Optional<Integer> ifNumberOfAccountsAtLeast) {
this.ifNumberOfAccountsAtLeast = ifNumberOfAccountsAtLeast;
}
public Optional<Integer> getNumberOfOtherAccountsLessThan() {
return numberOfOtherAccountsLessThan;
public Optional<Integer> getIfNumberOfAccountsLessThan() {
return ifNumberOfAccountsLessThan;
}
public void setNumberOfOtherAccountsLessThan(Optional<Integer> numberOfOtherAccountsLessThan) {
this.numberOfOtherAccountsLessThan = numberOfOtherAccountsLessThan;
public void setIfNumberOfAccountsLessThan(Optional<Integer> ifNumberOfAccountsLessThan) {
this.ifNumberOfAccountsLessThan = ifNumberOfAccountsLessThan;
}
}

View File

@ -25,6 +25,14 @@
# executor: CONSOLE
#
# Supported command events: onLogin, onSessionLogin, onFirstLogin, onJoin, onLogout, onRegister, onUnregister
#
# For onLogin and onFirstLogin, you can use 'ifNumberOfAccountsLessThan' and 'ifNumberOfAccountsAtLeast'
# to specify limits to how many accounts a player can have (matched by IP) for a command to be run:
# onLogin:
# warnOnManyAccounts:
# command: 'say Uh oh! %p has many alt accounts!'
# executor: CONSOLE
# ifNumberOfAccountsAtLeast: 5
# Commands to run for players logging in whose 'last login date' was empty
onFirstLogin: {}
onJoin: {}

View File

@ -17,6 +17,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import static org.mockito.ArgumentMatchers.any;
@ -77,6 +78,61 @@ public class CommandManagerTest {
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnLoginWithTwoAlts() {
// given
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
initManager();
// when
manager.runCommandsOnLogin(player, Arrays.asList("willy", "nilly", "billy", "silly"));
// then
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
verify(bukkitService).dispatchCommand(player, "motd");
verify(bukkitService).dispatchCommand(player, "list");
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
verifyNoMoreInteractions(bukkitService);
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnLoginWithFifteenAlts() {
// given
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
initManager();
// when
manager.runCommandsOnLogin(player, Collections.nCopies(15, "swag"));
// then
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
verify(bukkitService).dispatchCommand(player, "motd");
verify(bukkitService).dispatchCommand(player, "list");
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
verify(bukkitService).dispatchConsoleCommand("log Bobby 127.0.0.3 many accounts");
verifyNoMoreInteractions(bukkitService);
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnLoginWithTwentyFiveAlts() {
// given
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
initManager();
// when
manager.runCommandsOnLogin(player, Collections.nCopies(25, "yolo"));
// then
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
verify(bukkitService).dispatchCommand(player, "motd");
verify(bukkitService).dispatchCommand(player, "list");
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
verifyNoMoreInteractions(bukkitService);
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldExecuteCommandsOnLoginWithIncompleteConfig() {
// given
@ -123,6 +179,19 @@ public class CommandManagerTest {
verifyZeroInteractions(geoIpService);
}
@Test
public void shouldNotExecuteFirstLoginCommandWhoseThresholdIsNotMet() {
// given
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
initManager();
// when
manager.runCommandsOnFirstLogin(player, Arrays.asList("u", "wot", "m8"));
// then
verifyZeroInteractions(bukkitService, geoIpService);
}
@Test
public void shouldExecuteCommandsOnJoin() {
// given

View File

@ -88,7 +88,7 @@ public class CommandMigrationServiceTest {
Map<String, OnLoginCommand> onLoginCommands = new LinkedHashMap<>();
OnLoginCommand existingCommand = new OnLoginCommand("helpop %p has many alts", Executor.CONSOLE);
existingCommand.setNumberOfOtherAccountsAtLeast(Optional.of(2));
existingCommand.setIfNumberOfAccountsAtLeast(Optional.of(2));
onLoginCommands.put("alert_on_alts", existingCommand);
commandConfig.setOnLogin(onLoginCommands);
Map<String, Command> onRegisterCommands = new LinkedHashMap<>();

View File

@ -23,8 +23,13 @@ onLogin:
executor: PLAYER
warn_for_alts:
command: 'helpop Player %p has more than 1 account'
exeuctor: CONSOLE
numberOfOtherAccountsAtLeast: 2
executor: CONSOLE
ifNumberOfAccountsAtLeast: 2
log_suspicious_user:
command: 'log %p %ip many accounts'
executor: CONSOLE
ifNumberOfAccountsAtLeast: 5
ifNumberOfAccountsLessThan: 20
onSessionLogin:
welcome:
command: 'msg %p Session login!'
@ -33,6 +38,7 @@ onFirstLogin:
give_money:
command: 'pay %p 30'
executor: CONSOLE
ifNumberOfAccountsLessThan: 3
onUnregister: {}
onLogout:
announce: