Add QuickCommandsProtectionManager#processJoin(player)

This commit is contained in:
HexelDev 2018-03-19 22:33:53 +01:00
parent 7790fa5796
commit 84f97ea1c2
3 changed files with 40 additions and 17 deletions

View File

@ -16,13 +16,13 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle
private final PermissionsManager permissionsManager; private final PermissionsManager permissionsManager;
private final ExpiringSet<String> latestLogin; private final ExpiringSet<String> latestJoin;
@Inject @Inject
public QuickCommandsProtectionManager(Settings settings, PermissionsManager permissionsManager) { public QuickCommandsProtectionManager(Settings settings, PermissionsManager permissionsManager) {
this.permissionsManager = permissionsManager; this.permissionsManager = permissionsManager;
long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS); long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS);
latestLogin = new ExpiringSet<>(countTimeout, TimeUnit.MILLISECONDS); latestJoin = new ExpiringSet<>(countTimeout, TimeUnit.MILLISECONDS);
reload(settings); reload(settings);
} }
@ -30,8 +30,8 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle
* Save the player in the set * Save the player in the set
* @param name the player's name * @param name the player's name
*/ */
public void setLogin(String name) { private void setJoin(String name) {
latestLogin.add(name); latestJoin.add(name);
} }
/** /**
@ -39,27 +39,37 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle
* @param player the player to check * @param player the player to check
* @return true if the player has the permission, false otherwise * @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); 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 * Returns whether the given player is able to perform the command
* @param name the name of the player to check * @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 * @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) { public boolean isAllowed(String name) {
return !latestLogin.contains(name); return !latestJoin.contains(name);
} }
@Override @Override
public void reload(Settings settings) { public void reload(Settings settings) {
long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS); long countTimeout = settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS);
latestLogin.setExpiration(countTimeout, TimeUnit.MILLISECONDS); latestJoin.setExpiration(countTimeout, TimeUnit.MILLISECONDS);
} }
@Override @Override
public void performCleanup() { public void performCleanup() {
latestLogin.removeExpiredEntries(); latestJoin.removeExpiredEntries();
} }
} }

View File

@ -216,9 +216,7 @@ public class PlayerListener implements Listener {
teleportationService.teleportOnJoin(player); teleportationService.teleportOnJoin(player);
} }
if (quickCommandsProtectionManager.shouldSaveLogin(player)) { quickCommandsProtectionManager.processJoin(player);
quickCommandsProtectionManager.setLogin(player.getName());
}
management.performJoin(player); management.performJoin(player);

View File

@ -1,8 +1,10 @@
package fr.xephi.authme.data; package fr.xephi.authme.data;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings; import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.bukkit.entity.Player;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -11,6 +13,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/** /**
* Test for {@link QuickCommandsProtectionManager}. * Test for {@link QuickCommandsProtectionManager}.
@ -27,16 +30,19 @@ public class QuickCommandsProtectionManagerTest {
@Test @Test
public void shouldAllowCommand() { public void shouldAllowCommand() {
// given // given
String name1 = "TestName1"; String playername = "PlayerName";
String name2 = "TestName2"; Player player = mockPlayerWithName(playername);
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(0); 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(); QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
qcpm.setLogin(name2); qcpm.processJoin(player);
// when // when
boolean test1 = qcpm.isAllowed(name1); boolean test1 = qcpm.isAllowed(name);
boolean test2 = qcpm.isAllowed(name2); boolean test2 = qcpm.isAllowed(playername);
// then // then
assertThat(test1, equalTo(true)); assertThat(test1, equalTo(true));
@ -47,10 +53,12 @@ public class QuickCommandsProtectionManagerTest {
public void shouldDenyCommand() { public void shouldDenyCommand() {
// given // given
String name = "TestName1"; String name = "TestName1";
Player player = mockPlayerWithName(name);
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(5000); given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(5000);
QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager(); QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
qcpm.setLogin(name); given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true);
qcpm.processJoin(player);
// when // when
boolean test = qcpm.isAllowed(name); boolean test = qcpm.isAllowed(name);
@ -62,4 +70,11 @@ public class QuickCommandsProtectionManagerTest {
private QuickCommandsProtectionManager createQuickCommandsProtectioneManager() { private QuickCommandsProtectionManager createQuickCommandsProtectioneManager() {
return new QuickCommandsProtectionManager(settings, permissionsManager); return new QuickCommandsProtectionManager(settings, permissionsManager);
} }
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
} }