Use timestamp to determine if antibot should be activated

- removed handleJoin method.
This commit is contained in:
DNx5 2016-10-18 02:53:20 +07:00
parent b55805ff87
commit b3fd6170fe
3 changed files with 47 additions and 65 deletions

View File

@ -9,14 +9,14 @@ import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerLoginEvent;

View File

@ -6,14 +6,14 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -233,7 +233,6 @@ public class PlayerListener implements Listener {
return;
}
antiBotService.handlePlayerJoin();
teleportationService.teleportOnJoin(player);
}
@ -306,12 +305,7 @@ public class PlayerListener implements Listener {
* @note little hack cause InventoryOpenEvent cannot be cancelled for
* real, cause no packet is send to server by client for the main inv
*/
bukkitService.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
player.closeInventory();
}
}, 1);
bukkitService.scheduleSyncDelayedTask(player::closeInventory, 1);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)

View File

@ -7,10 +7,11 @@ import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.CopyOnWriteArrayList;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE;
@ -25,18 +26,17 @@ public class AntiBotService implements SettingsDependent {
private final Messages messages;
private final PermissionsManager permissionsManager;
private final BukkitService bukkitService;
private final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<>();
// Settings
private int duration;
private int sensibility;
private int delay;
// Service status
private AntiBotStatus antiBotStatus;
private boolean startup;
private BukkitTask disableTask;
private int antibotPlayers;
private final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<>();
private Instant lastFlaggedJoin;
private int flagged = 0;
@Inject
AntiBotService(Settings settings, Messages messages, PermissionsManager permissionsManager,
@ -47,7 +47,7 @@ public class AntiBotService implements SettingsDependent {
this.bukkitService = bukkitService;
// Initial status
disableTask = null;
antibotPlayers = 0;
flagged = 0;
antiBotStatus = AntiBotStatus.DISABLED;
startup = true;
// Load settings and start if required
@ -71,15 +71,10 @@ public class AntiBotService implements SettingsDependent {
}
// Bot activation task
Runnable enableTask = new Runnable() {
@Override
public void run() {
antiBotStatus = AntiBotStatus.LISTENING;
}
};
Runnable enableTask = () -> antiBotStatus = AntiBotStatus.LISTENING;
// Delay the schedule on first start
if(startup) {
if (startup) {
bukkitService.scheduleSyncDelayedTask(enableTask, delay * TICKS_PER_SECOND);
startup = false;
} else {
@ -94,19 +89,12 @@ public class AntiBotService implements SettingsDependent {
antiBotStatus = AntiBotStatus.ACTIVE;
// Inform admins
for (Player player : bukkitService.getOnlinePlayers()) {
if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) {
messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE);
}
}
bukkitService.getOnlinePlayers().stream()
.filter(player -> permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES))
.forEach(player -> messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE));
// Schedule auto-disable
disableTask = bukkitService.runTaskLater(new Runnable() {
@Override
public void run() {
stopProtection();
}
}, duration * TICKS_PER_MINUTE);
disableTask = bukkitService.runTaskLater(this::stopProtection, duration * TICKS_PER_MINUTE);
}
private void stopProtection() {
@ -116,7 +104,7 @@ public class AntiBotService implements SettingsDependent {
// Change status
antiBotStatus = AntiBotStatus.LISTENING;
antibotPlayers = 0;
flagged = 0;
antibotKicked.clear();
// Cancel auto-disable task
@ -124,11 +112,10 @@ public class AntiBotService implements SettingsDependent {
disableTask = null;
// Inform admins
for (Player player : bukkitService.getOnlinePlayers()) {
if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) {
messages.send(player, MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, Integer.toString(duration));
}
}
String durationString = Integer.toString(duration);
bukkitService.getOnlinePlayers().stream()
.filter(player -> permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES))
.forEach(player -> messages.send(player, MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, durationString));
}
/**
@ -155,36 +142,37 @@ public class AntiBotService implements SettingsDependent {
}
}
/**
* Handles a player joining the server and checks if AntiBot needs to be activated.
*/
public void handlePlayerJoin() {
if (antiBotStatus != AntiBotStatus.LISTENING) {
return;
}
antibotPlayers++;
if (antibotPlayers > sensibility) {
startProtection();
return;
}
bukkitService.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
antibotPlayers--;
}
}, 5 * TICKS_PER_SECOND);
}
/**
* Returns if a player should be kicked due to antibot service.
*
* @param isAuthAvailable if the player is registered
*
* @return if the player should be kicked
*/
public boolean shouldKick(boolean isAuthAvailable) {
return !isAuthAvailable && (antiBotStatus == AntiBotStatus.ACTIVE);
if (antiBotStatus == AntiBotStatus.DISABLED || isAuthAvailable) {
return false;
}
if (antiBotStatus == AntiBotStatus.ACTIVE) {
return true;
}
if (lastFlaggedJoin == null) {
lastFlaggedJoin = Instant.now();
}
if (ChronoUnit.SECONDS.between(Instant.now(), lastFlaggedJoin) <= 5) {
flagged++;
} else {
// reset to 1 because this player is also count as not registered
flagged = 1;
lastFlaggedJoin = null;
}
if (flagged > sensibility) {
startProtection();
return true;
}
return false;
}
/**