mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-09-19 18:31:45 +02:00
Use timestamp to determine if antibot should be activated
- removed handleJoin method.
This commit is contained in:
parent
b55805ff87
commit
b3fd6170fe
@ -9,14 +9,14 @@ import fr.xephi.authme.message.Messages;
|
|||||||
import fr.xephi.authme.permission.PermissionsManager;
|
import fr.xephi.authme.permission.PermissionsManager;
|
||||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||||
import fr.xephi.authme.service.AntiBotService;
|
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.Settings;
|
||||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||||
import fr.xephi.authme.service.BukkitService;
|
|
||||||
import fr.xephi.authme.util.StringUtils;
|
import fr.xephi.authme.util.StringUtils;
|
||||||
import fr.xephi.authme.util.Utils;
|
import fr.xephi.authme.util.Utils;
|
||||||
import fr.xephi.authme.service.ValidationService;
|
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.player.PlayerLoginEvent;
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
|
@ -6,14 +6,14 @@ import fr.xephi.authme.message.MessageKey;
|
|||||||
import fr.xephi.authme.message.Messages;
|
import fr.xephi.authme.message.Messages;
|
||||||
import fr.xephi.authme.process.Management;
|
import fr.xephi.authme.process.Management;
|
||||||
import fr.xephi.authme.service.AntiBotService;
|
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.Settings;
|
||||||
import fr.xephi.authme.settings.SpawnLoader;
|
import fr.xephi.authme.settings.SpawnLoader;
|
||||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
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.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -233,7 +233,6 @@ public class PlayerListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
antiBotService.handlePlayerJoin();
|
|
||||||
teleportationService.teleportOnJoin(player);
|
teleportationService.teleportOnJoin(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,12 +305,7 @@ public class PlayerListener implements Listener {
|
|||||||
* @note little hack cause InventoryOpenEvent cannot be cancelled for
|
* @note little hack cause InventoryOpenEvent cannot be cancelled for
|
||||||
* real, cause no packet is send to server by client for the main inv
|
* real, cause no packet is send to server by client for the main inv
|
||||||
*/
|
*/
|
||||||
bukkitService.scheduleSyncDelayedTask(new Runnable() {
|
bukkitService.scheduleSyncDelayedTask(player::closeInventory, 1);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
player.closeInventory();
|
|
||||||
}
|
|
||||||
}, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
||||||
|
@ -7,10 +7,11 @@ import fr.xephi.authme.permission.AdminPermission;
|
|||||||
import fr.xephi.authme.permission.PermissionsManager;
|
import fr.xephi.authme.permission.PermissionsManager;
|
||||||
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.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE;
|
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 Messages messages;
|
||||||
private final PermissionsManager permissionsManager;
|
private final PermissionsManager permissionsManager;
|
||||||
private final BukkitService bukkitService;
|
private final BukkitService bukkitService;
|
||||||
|
private final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<>();
|
||||||
// Settings
|
// Settings
|
||||||
private int duration;
|
private int duration;
|
||||||
private int sensibility;
|
private int sensibility;
|
||||||
private int delay;
|
private int delay;
|
||||||
|
|
||||||
// Service status
|
// Service status
|
||||||
private AntiBotStatus antiBotStatus;
|
private AntiBotStatus antiBotStatus;
|
||||||
private boolean startup;
|
private boolean startup;
|
||||||
private BukkitTask disableTask;
|
private BukkitTask disableTask;
|
||||||
private int antibotPlayers;
|
private Instant lastFlaggedJoin;
|
||||||
private final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<>();
|
private int flagged = 0;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AntiBotService(Settings settings, Messages messages, PermissionsManager permissionsManager,
|
AntiBotService(Settings settings, Messages messages, PermissionsManager permissionsManager,
|
||||||
@ -47,7 +47,7 @@ public class AntiBotService implements SettingsDependent {
|
|||||||
this.bukkitService = bukkitService;
|
this.bukkitService = bukkitService;
|
||||||
// Initial status
|
// Initial status
|
||||||
disableTask = null;
|
disableTask = null;
|
||||||
antibotPlayers = 0;
|
flagged = 0;
|
||||||
antiBotStatus = AntiBotStatus.DISABLED;
|
antiBotStatus = AntiBotStatus.DISABLED;
|
||||||
startup = true;
|
startup = true;
|
||||||
// Load settings and start if required
|
// Load settings and start if required
|
||||||
@ -71,12 +71,7 @@ public class AntiBotService implements SettingsDependent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bot activation task
|
// Bot activation task
|
||||||
Runnable enableTask = new Runnable() {
|
Runnable enableTask = () -> antiBotStatus = AntiBotStatus.LISTENING;
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
antiBotStatus = AntiBotStatus.LISTENING;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Delay the schedule on first start
|
// Delay the schedule on first start
|
||||||
if (startup) {
|
if (startup) {
|
||||||
@ -94,19 +89,12 @@ public class AntiBotService implements SettingsDependent {
|
|||||||
antiBotStatus = AntiBotStatus.ACTIVE;
|
antiBotStatus = AntiBotStatus.ACTIVE;
|
||||||
|
|
||||||
// Inform admins
|
// Inform admins
|
||||||
for (Player player : bukkitService.getOnlinePlayers()) {
|
bukkitService.getOnlinePlayers().stream()
|
||||||
if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) {
|
.filter(player -> permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES))
|
||||||
messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE);
|
.forEach(player -> messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schedule auto-disable
|
// Schedule auto-disable
|
||||||
disableTask = bukkitService.runTaskLater(new Runnable() {
|
disableTask = bukkitService.runTaskLater(this::stopProtection, duration * TICKS_PER_MINUTE);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
stopProtection();
|
|
||||||
}
|
|
||||||
}, duration * TICKS_PER_MINUTE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopProtection() {
|
private void stopProtection() {
|
||||||
@ -116,7 +104,7 @@ public class AntiBotService implements SettingsDependent {
|
|||||||
|
|
||||||
// Change status
|
// Change status
|
||||||
antiBotStatus = AntiBotStatus.LISTENING;
|
antiBotStatus = AntiBotStatus.LISTENING;
|
||||||
antibotPlayers = 0;
|
flagged = 0;
|
||||||
antibotKicked.clear();
|
antibotKicked.clear();
|
||||||
|
|
||||||
// Cancel auto-disable task
|
// Cancel auto-disable task
|
||||||
@ -124,11 +112,10 @@ public class AntiBotService implements SettingsDependent {
|
|||||||
disableTask = null;
|
disableTask = null;
|
||||||
|
|
||||||
// Inform admins
|
// Inform admins
|
||||||
for (Player player : bukkitService.getOnlinePlayers()) {
|
String durationString = Integer.toString(duration);
|
||||||
if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) {
|
bukkitService.getOnlinePlayers().stream()
|
||||||
messages.send(player, MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, Integer.toString(duration));
|
.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.
|
* Returns if a player should be kicked due to antibot service.
|
||||||
*
|
*
|
||||||
* @param isAuthAvailable if the player is registered
|
* @param isAuthAvailable if the player is registered
|
||||||
|
*
|
||||||
* @return if the player should be kicked
|
* @return if the player should be kicked
|
||||||
*/
|
*/
|
||||||
public boolean shouldKick(boolean isAuthAvailable) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user