From 84f97ea1c2211d6025019b964409885f3e1c0d92 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Mon, 19 Mar 2018 22:33:53 +0100 Subject: [PATCH] Add QuickCommandsProtectionManager#processJoin(player) --- .../data/QuickCommandsProtectionManager.java | 26 ++++++++++++------ .../xephi/authme/listener/PlayerListener.java | 4 +-- .../QuickCommandsProtectionManagerTest.java | 27 ++++++++++++++----- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/main/java/fr/xephi/authme/data/QuickCommandsProtectionManager.java b/src/main/java/fr/xephi/authme/data/QuickCommandsProtectionManager.java index 6e697db16..335944c82 100644 --- a/src/main/java/fr/xephi/authme/data/QuickCommandsProtectionManager.java +++ b/src/main/java/fr/xephi/authme/data/QuickCommandsProtectionManager.java @@ -16,13 +16,13 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle private final PermissionsManager permissionsManager; - private final ExpiringSet latestLogin; + private final ExpiringSet latestJoin; @Inject public QuickCommandsProtectionManager(Settings settings, PermissionsManager permissionsManager) { this.permissionsManager = permissionsManager; long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS); - latestLogin = new ExpiringSet<>(countTimeout, TimeUnit.MILLISECONDS); + latestJoin = new ExpiringSet<>(countTimeout, TimeUnit.MILLISECONDS); reload(settings); } @@ -30,8 +30,8 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle * Save the player in the set * @param name the player's name */ - public void setLogin(String name) { - latestLogin.add(name); + private void setJoin(String name) { + latestJoin.add(name); } /** @@ -39,27 +39,37 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle * @param player the player to check * @return true if the player has the permission, false otherwise */ - public boolean shouldSaveLogin(Player player) { + private boolean shouldSavePlayer(Player player) { return permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION); } + /** + * Process the player join + * @param player the player to process + */ + public void processJoin(Player player) { + if(shouldSavePlayer(player)) { + setJoin(player.getName()); + } + } + /** * Returns whether the given player is able to perform the command * @param name the name of the player to check * @return true if the player is not in the set (so it's allowed to perform the command), false otherwise */ public boolean isAllowed(String name) { - return !latestLogin.contains(name); + return !latestJoin.contains(name); } @Override public void reload(Settings settings) { long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS); - latestLogin.setExpiration(countTimeout, TimeUnit.MILLISECONDS); + latestJoin.setExpiration(countTimeout, TimeUnit.MILLISECONDS); } @Override public void performCleanup() { - latestLogin.removeExpiredEntries(); + latestJoin.removeExpiredEntries(); } } diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index 6ceeb835c..7857114e3 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -216,9 +216,7 @@ public class PlayerListener implements Listener { teleportationService.teleportOnJoin(player); } - if (quickCommandsProtectionManager.shouldSaveLogin(player)) { - quickCommandsProtectionManager.setLogin(player.getName()); - } + quickCommandsProtectionManager.processJoin(player); management.performJoin(player); diff --git a/src/test/java/fr/xephi/authme/data/QuickCommandsProtectionManagerTest.java b/src/test/java/fr/xephi/authme/data/QuickCommandsProtectionManagerTest.java index 10ef88eb8..704075674 100644 --- a/src/test/java/fr/xephi/authme/data/QuickCommandsProtectionManagerTest.java +++ b/src/test/java/fr/xephi/authme/data/QuickCommandsProtectionManagerTest.java @@ -1,8 +1,10 @@ package fr.xephi.authme.data; import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.ProtectionSettings; +import org.bukkit.entity.Player; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -11,6 +13,7 @@ import org.mockito.junit.MockitoJUnitRunner; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * Test for {@link QuickCommandsProtectionManager}. @@ -27,16 +30,19 @@ public class QuickCommandsProtectionManagerTest { @Test public void shouldAllowCommand() { // given - String name1 = "TestName1"; - String name2 = "TestName2"; + String playername = "PlayerName"; + Player player = mockPlayerWithName(playername); given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(0); + given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true); + + String name = "TestName"; QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager(); - qcpm.setLogin(name2); + qcpm.processJoin(player); // when - boolean test1 = qcpm.isAllowed(name1); - boolean test2 = qcpm.isAllowed(name2); + boolean test1 = qcpm.isAllowed(name); + boolean test2 = qcpm.isAllowed(playername); // then assertThat(test1, equalTo(true)); @@ -47,10 +53,12 @@ public class QuickCommandsProtectionManagerTest { public void shouldDenyCommand() { // given String name = "TestName1"; + Player player = mockPlayerWithName(name); given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(5000); QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager(); - qcpm.setLogin(name); + given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true); + qcpm.processJoin(player); // when boolean test = qcpm.isAllowed(name); @@ -62,4 +70,11 @@ public class QuickCommandsProtectionManagerTest { private QuickCommandsProtectionManager createQuickCommandsProtectioneManager() { return new QuickCommandsProtectionManager(settings, permissionsManager); } + + private static Player mockPlayerWithName(String name) { + Player player = mock(Player.class); + given(player.getName()).willReturn(name); + return player; + } + }