AntiBot - make public field private

This commit is contained in:
ljacqu 2016-06-26 09:25:52 +02:00
parent df060ff29c
commit d72d6ddf5a
4 changed files with 71 additions and 4 deletions

View File

@ -24,7 +24,7 @@ public class AntiBot {
private final Messages messages;
private final PermissionsManager permissionsManager;
private final BukkitService bukkitService;
public final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<String>();
private final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<String>();
private final CopyOnWriteArrayList<String> antibotPlayers = new CopyOnWriteArrayList<String>();
private AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED;
@ -112,6 +112,27 @@ public class AntiBot {
}, 15 * TICKS_PER_SECOND);
}
/**
* Returns whether the player was kicked because of activated antibot. The list is reset
* when antibot is deactivated.
*
* @param name the name to check
* @return true if the given name has been kicked because of Antibot
*/
public boolean wasPlayerKicked(String name) {
return antibotKicked.contains(name.toLowerCase());
}
/**
* Adds a name to the list of players kicked by antibot. Should only be used when a player
* is determined to be kicked because of failed antibot verification.
*
* @param name the name to add
*/
public void addPlayerKick(String name) {
antibotKicked.addIfAbsent(name.toLowerCase());
}
public enum AntiBotStatus {
LISTENING,
DISABLED,

View File

@ -239,7 +239,7 @@ public class AuthMePlayerListener implements Listener {
event.setQuitMessage(null);
}
if (antiBot.antibotKicked.contains(player.getName())) {
if (antiBot.wasPlayerKicked(player.getName())) {
return;
}
@ -250,7 +250,7 @@ public class AuthMePlayerListener implements Listener {
public void onPlayerKick(PlayerKickEvent event) {
Player player = event.getPlayer();
if (!antiBot.antibotKicked.contains(player.getName())) {
if (!antiBot.wasPlayerKicked(player.getName())) {
management.performQuit(player, true);
}
}

View File

@ -68,7 +68,7 @@ class OnJoinVerifier implements Reloadable {
*/
public void checkAntibot(String playerName, boolean isAuthAvailable) throws FailedVerificationException {
if (antiBot.getAntiBotStatus() == AntiBot.AntiBotStatus.ACTIVE && !isAuthAvailable) {
antiBot.antibotKicked.addIfAbsent(playerName);
antiBot.addPlayerKick(playerName);
throw new FailedVerificationException(MessageKey.KICK_ANTIBOT);
}
}

View File

@ -36,6 +36,7 @@ import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -377,6 +378,51 @@ public class OnJoinVerifierTest {
verifyZeroInteractions(bukkitService);
}
@Test
public void shouldCheckAntiBot() throws FailedVerificationException {
// given
String name = "user123";
boolean hasAuth = false;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.LISTENING);
// when
onJoinVerifier.checkAntibot(name, hasAuth);
// then
verify(antiBot).getAntiBotStatus();
}
@Test
public void shouldAllowUserWithAuth() throws FailedVerificationException {
// given
String name = "Bobby";
boolean hasAuth = true;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
// when
onJoinVerifier.checkAntibot(name, hasAuth);
// then
verify(antiBot).getAntiBotStatus();
}
@Test
public void shouldThrowForActiveAntiBot() {
// given
String name = "Bobby";
boolean hasAuth = false;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
// when / then
try {
onJoinVerifier.checkAntibot(name, hasAuth);
fail("Expected exception to be thrown");
} catch (FailedVerificationException e) {
assertThat(e, exceptionWithData(MessageKey.KICK_ANTIBOT));
verify(antiBot).addPlayerKick(name);
}
}
private static Player newPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);