mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-27 12:45:57 +01:00
Add QuickCommandsProtectionManager#processJoin(player)
This commit is contained in:
parent
7790fa5796
commit
84f97ea1c2
@ -16,13 +16,13 @@ public class QuickCommandsProtectionManager implements SettingsDependent, HasCle
|
||||
|
||||
private final PermissionsManager permissionsManager;
|
||||
|
||||
private final ExpiringSet<String> latestLogin;
|
||||
private final ExpiringSet<String> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user