mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-18 14:47:47 +01:00
#1449 Fix copying of Commands, add tests with command delay
This commit is contained in:
parent
035c2f352a
commit
22c08c9fc3
@ -19,26 +19,21 @@ public class Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Creates a copy of this Command object, setting the given command text on the copy.
|
||||||
*
|
*
|
||||||
* @param command the command
|
* @param command the command text to use in the copy
|
||||||
* @param executor the executor of the command
|
* @return copy of the source with the new command
|
||||||
*/
|
*/
|
||||||
public Command(String command, Executor executor) {
|
public Command copyWithCommand(String command) {
|
||||||
this(command, executor, 0);
|
Command copy = new Command();
|
||||||
|
setValuesToCopyWithNewCommand(copy, command);
|
||||||
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected void setValuesToCopyWithNewCommand(Command copy, String newCommand) {
|
||||||
* Constructor.
|
copy.command = newCommand;
|
||||||
*
|
copy.executor = this.executor;
|
||||||
* @param command the command
|
copy.delay = this.delay;
|
||||||
* @param executor the executor of the command
|
|
||||||
* @param delay the delay (in ticks) before executing command
|
|
||||||
*/
|
|
||||||
public Command(String command, Executor executor, long delay) {
|
|
||||||
this.command = command;
|
|
||||||
this.executor = executor;
|
|
||||||
this.delay = delay;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCommand() {
|
public String getCommand() {
|
||||||
|
@ -174,15 +174,12 @@ public class CommandManager implements Reloadable {
|
|||||||
|
|
||||||
private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) {
|
private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) {
|
||||||
return new WrappedTagReplacer<>(availableTags, commands.values(), Command::getCommand,
|
return new WrappedTagReplacer<>(availableTags, commands.values(), Command::getCommand,
|
||||||
(cmd, text) -> new Command(text, cmd.getExecutor()));
|
Command::copyWithCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
private WrappedTagReplacer<OnLoginCommand, Player> newOnLoginCmdReplacer(
|
private WrappedTagReplacer<OnLoginCommand, Player> newOnLoginCmdReplacer(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.getIfNumberOfAccountsAtLeast(),
|
OnLoginCommand::copyWithCommand);
|
||||||
cmd.getIfNumberOfAccountsLessThan()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Tag<Player>> buildAvailableTags() {
|
private List<Tag<Player>> buildAvailableTags() {
|
||||||
|
@ -41,8 +41,9 @@ class CommandMigrationService implements MigrationService {
|
|||||||
|
|
||||||
private boolean moveOtherAccountsConfig(CommandConfig commandConfig) {
|
private boolean moveOtherAccountsConfig(CommandConfig commandConfig) {
|
||||||
if (settingsMigrationService.hasOldOtherAccountsCommand()) {
|
if (settingsMigrationService.hasOldOtherAccountsCommand()) {
|
||||||
OnLoginCommand command = new OnLoginCommand(
|
OnLoginCommand command = new OnLoginCommand();
|
||||||
replaceOldPlaceholdersWithNew(settingsMigrationService.getOldOtherAccountsCommand()), Executor.CONSOLE);
|
command.setCommand(replaceOldPlaceholdersWithNew(settingsMigrationService.getOldOtherAccountsCommand()));
|
||||||
|
command.setExecutor(Executor.CONSOLE);
|
||||||
command.setIfNumberOfAccountsAtLeast(
|
command.setIfNumberOfAccountsAtLeast(
|
||||||
Optional.of(settingsMigrationService.getOldOtherAccountsCommandThreshold()));
|
Optional.of(settingsMigrationService.getOldOtherAccountsCommandThreshold()));
|
||||||
|
|
||||||
|
@ -17,28 +17,18 @@ public class OnLoginCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Creates a copy of this object, using the given command as new {@link Command#command command}.
|
||||||
*
|
*
|
||||||
* @param command the command to execute
|
* @param command the command text to use in the copy
|
||||||
* @param executor the executor of the command
|
* @return copy of the source with the new command
|
||||||
*/
|
*/
|
||||||
public OnLoginCommand(String command, Executor executor) {
|
@Override
|
||||||
super(command, executor);
|
public OnLoginCommand copyWithCommand(String command) {
|
||||||
}
|
OnLoginCommand copy = new OnLoginCommand();
|
||||||
|
setValuesToCopyWithNewCommand(copy, command);
|
||||||
/**
|
copy.ifNumberOfAccountsAtLeast = this.ifNumberOfAccountsAtLeast;
|
||||||
* Constructor.
|
copy.ifNumberOfAccountsLessThan = this.ifNumberOfAccountsLessThan;
|
||||||
*
|
return copy;
|
||||||
* @param command the command to execute
|
|
||||||
* @param executor the executor of the command
|
|
||||||
* @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> ifNumberOfAccountsAtLeast,
|
|
||||||
Optional<Integer> ifNumberOfAccountsLessThan) {
|
|
||||||
super(command, executor);
|
|
||||||
this.ifNumberOfAccountsAtLeast = ifNumberOfAccountsAtLeast;
|
|
||||||
this.ifNumberOfAccountsLessThan = ifNumberOfAccountsLessThan;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Integer> getIfNumberOfAccountsAtLeast() {
|
public Optional<Integer> getIfNumberOfAccountsAtLeast() {
|
||||||
|
@ -3,6 +3,7 @@ package fr.xephi.authme.settings.commandconfig;
|
|||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
import fr.xephi.authme.TestHelper;
|
import fr.xephi.authme.TestHelper;
|
||||||
import fr.xephi.authme.service.BukkitService;
|
import fr.xephi.authme.service.BukkitService;
|
||||||
|
import fr.xephi.authme.service.BukkitServiceTestHelper;
|
||||||
import fr.xephi.authme.service.GeoIpService;
|
import fr.xephi.authme.service.GeoIpService;
|
||||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -25,6 +26,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
|||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.only;
|
import static org.mockito.Mockito.only;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
@ -59,6 +61,7 @@ public class CommandManagerTest {
|
|||||||
public void setup() throws IOException {
|
public void setup() throws IOException {
|
||||||
testFolder = temporaryFolder.newFolder();
|
testFolder = temporaryFolder.newFolder();
|
||||||
player = mockPlayer();
|
player = mockPlayer();
|
||||||
|
BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -74,6 +77,8 @@ public class CommandManagerTest {
|
|||||||
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
|
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
|
||||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("motd"));
|
verify(bukkitService).dispatchCommand(any(Player.class), eq("motd"));
|
||||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(60L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(120L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
verifyZeroInteractions(geoIpService);
|
verifyZeroInteractions(geoIpService);
|
||||||
}
|
}
|
||||||
@ -92,6 +97,9 @@ public class CommandManagerTest {
|
|||||||
verify(bukkitService).dispatchCommand(player, "motd");
|
verify(bukkitService).dispatchCommand(player, "motd");
|
||||||
verify(bukkitService).dispatchCommand(player, "list");
|
verify(bukkitService).dispatchCommand(player, "list");
|
||||||
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(60L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(120L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(180L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
verifyZeroInteractions(geoIpService);
|
verifyZeroInteractions(geoIpService);
|
||||||
}
|
}
|
||||||
@ -111,6 +119,10 @@ public class CommandManagerTest {
|
|||||||
verify(bukkitService).dispatchCommand(player, "list");
|
verify(bukkitService).dispatchCommand(player, "list");
|
||||||
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
||||||
verify(bukkitService).dispatchConsoleCommand("log Bobby 127.0.0.3 many accounts");
|
verify(bukkitService).dispatchConsoleCommand("log Bobby 127.0.0.3 many accounts");
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(60L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(120L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(180L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(240L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
verifyZeroInteractions(geoIpService);
|
verifyZeroInteractions(geoIpService);
|
||||||
}
|
}
|
||||||
@ -129,6 +141,9 @@ public class CommandManagerTest {
|
|||||||
verify(bukkitService).dispatchCommand(player, "motd");
|
verify(bukkitService).dispatchCommand(player, "motd");
|
||||||
verify(bukkitService).dispatchCommand(player, "list");
|
verify(bukkitService).dispatchCommand(player, "list");
|
||||||
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
verify(bukkitService).dispatchConsoleCommand("helpop Player Bobby has more than 1 account");
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(60L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(120L));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(180L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
verifyZeroInteractions(geoIpService);
|
verifyZeroInteractions(geoIpService);
|
||||||
}
|
}
|
||||||
@ -138,6 +153,7 @@ public class CommandManagerTest {
|
|||||||
// given
|
// given
|
||||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||||
initManager();
|
initManager();
|
||||||
|
BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
manager.runCommandsOnLogin(player, Collections.emptyList());
|
manager.runCommandsOnLogin(player, Collections.emptyList());
|
||||||
@ -145,6 +161,7 @@ public class CommandManagerTest {
|
|||||||
// then
|
// then
|
||||||
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back, bob");
|
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back, bob");
|
||||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
||||||
|
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(100L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
verifyZeroInteractions(geoIpService);
|
verifyZeroInteractions(geoIpService);
|
||||||
}
|
}
|
||||||
@ -232,6 +249,7 @@ public class CommandManagerTest {
|
|||||||
// then
|
// then
|
||||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("me I just registered"));
|
verify(bukkitService).dispatchCommand(any(Player.class), eq("me I just registered"));
|
||||||
verify(bukkitService).dispatchConsoleCommand("log Bobby (127.0.0.3, Syldavia) registered");
|
verify(bukkitService).dispatchConsoleCommand("log Bobby (127.0.0.3, Syldavia) registered");
|
||||||
|
verify(bukkitService, times(2)).scheduleSyncDelayedTask(any(Runnable.class), eq(100L));
|
||||||
verifyNoMoreInteractions(bukkitService);
|
verifyNoMoreInteractions(bukkitService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,9 +8,11 @@ onRegister:
|
|||||||
announce:
|
announce:
|
||||||
command: 'me I just registered'
|
command: 'me I just registered'
|
||||||
executor: PLAYER
|
executor: PLAYER
|
||||||
|
delay: 100
|
||||||
notify:
|
notify:
|
||||||
command: 'log %p (%ip, %country) registered'
|
command: 'log %p (%ip, %country) registered'
|
||||||
executor: CONSOLE
|
executor: CONSOLE
|
||||||
|
delay: 100
|
||||||
onLogin:
|
onLogin:
|
||||||
welcome:
|
welcome:
|
||||||
command: 'msg %p Welcome back'
|
command: 'msg %p Welcome back'
|
||||||
@ -18,18 +20,22 @@ onLogin:
|
|||||||
show_motd:
|
show_motd:
|
||||||
command: 'motd'
|
command: 'motd'
|
||||||
executor: PLAYER
|
executor: PLAYER
|
||||||
|
delay: 60
|
||||||
display_list:
|
display_list:
|
||||||
command: 'list'
|
command: 'list'
|
||||||
executor: PLAYER
|
executor: PLAYER
|
||||||
|
delay: 120
|
||||||
warn_for_alts:
|
warn_for_alts:
|
||||||
command: 'helpop Player %p has more than 1 account'
|
command: 'helpop Player %p has more than 1 account'
|
||||||
executor: CONSOLE
|
executor: CONSOLE
|
||||||
ifNumberOfAccountsAtLeast: 2
|
ifNumberOfAccountsAtLeast: 2
|
||||||
|
delay: 180
|
||||||
log_suspicious_user:
|
log_suspicious_user:
|
||||||
command: 'log %p %ip many accounts'
|
command: 'log %p %ip many accounts'
|
||||||
executor: CONSOLE
|
executor: CONSOLE
|
||||||
ifNumberOfAccountsAtLeast: 5
|
ifNumberOfAccountsAtLeast: 5
|
||||||
ifNumberOfAccountsLessThan: 20
|
ifNumberOfAccountsLessThan: 20
|
||||||
|
delay: 240
|
||||||
onSessionLogin:
|
onSessionLogin:
|
||||||
welcome:
|
welcome:
|
||||||
command: 'msg %p Session login!'
|
command: 'msg %p Session login!'
|
||||||
|
@ -8,12 +8,14 @@ onLogin:
|
|||||||
welcome:
|
welcome:
|
||||||
command: 'msg %p Welcome back, %nick'
|
command: 'msg %p Welcome back, %nick'
|
||||||
executor: CONSOLE
|
executor: CONSOLE
|
||||||
|
delay: 0
|
||||||
show_motd:
|
show_motd:
|
||||||
# command: 'motd' <-- mandatory property, so entry should be ignored
|
# command: 'motd' <-- mandatory property, so entry should be ignored
|
||||||
executor: PLAYER
|
executor: PLAYER
|
||||||
display_list:
|
display_list:
|
||||||
command: 'list'
|
command: 'list'
|
||||||
executor: WRONG_EXECUTOR
|
executor: WRONG_EXECUTOR
|
||||||
|
delay: 100
|
||||||
doesNotExist:
|
doesNotExist:
|
||||||
wrongEntry:
|
wrongEntry:
|
||||||
command: 'should be ignored'
|
command: 'should be ignored'
|
||||||
|
Loading…
Reference in New Issue
Block a user