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

View File

@ -49,8 +49,17 @@ public final class CommandSettingsHolder implements SettingsHolder {
" executor: CONSOLE", " executor: CONSOLE",
"", "",
"Supported command events: onLogin, onSessionLogin, onFirstLogin, onJoin, onLogout, onRegister, " "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<>(); Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("", rootComments); commentMap.put("", rootComments);
commentMap.put("onFirstLogin", new String[]{ commentMap.put("onFirstLogin", new String[]{

View File

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

View File

@ -25,6 +25,14 @@
# executor: CONSOLE # executor: CONSOLE
# #
# Supported command events: onLogin, onSessionLogin, onFirstLogin, onJoin, onLogout, onRegister, onUnregister # 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 # Commands to run for players logging in whose 'last login date' was empty
onFirstLogin: {} onFirstLogin: {}
onJoin: {} onJoin: {}

View File

@ -17,6 +17,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -77,6 +78,61 @@ public class CommandManagerTest {
verifyZeroInteractions(geoIpService); 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 @Test
public void shouldExecuteCommandsOnLoginWithIncompleteConfig() { public void shouldExecuteCommandsOnLoginWithIncompleteConfig() {
// given // given
@ -123,6 +179,19 @@ public class CommandManagerTest {
verifyZeroInteractions(geoIpService); 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 @Test
public void shouldExecuteCommandsOnJoin() { public void shouldExecuteCommandsOnJoin() {
// given // given

View File

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

View File

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