#857 Create event for failed authentication

This commit is contained in:
ljacqu
2017-04-29 14:46:06 +02:00
parent dbb8ea7f38
commit d65556d893
5 changed files with 100 additions and 4 deletions
@@ -0,0 +1,45 @@
package fr.xephi.authme.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Event fired when a player enters a wrong password.
*/
public class FailedLoginEvent extends CustomEvent {
private static final HandlerList handlers = new HandlerList();
private final Player player;
/**
* Constructor.
*
* @param player The player
*/
public FailedLoginEvent(Player player, boolean isAsync) {
super(isAsync);
this.player = player;
}
/**
* @return The player entering a wrong password
*/
public Player getPlayer() {
return player;
}
/**
* Return the list of handlers, equivalent to {@link #getHandlers()} and required by {@link Event}.
*
* @return The list of handlers
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}
@@ -18,7 +18,6 @@ import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.PlayerUtils;
@@ -115,9 +114,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
// Protect inventory
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
final boolean isAsync = service.getProperty(PluginSettings.USE_ASYNC_TASKS);
ProtectInventoryEvent ev = new ProtectInventoryEvent(player, isAsync);
bukkitService.callEvent(ev);
ProtectInventoryEvent ev = bukkitService.createAndCallEvent(
isAsync -> new ProtectInventoryEvent(player, isAsync));
if (ev.isCancelled()) {
player.updateInventory();
ConsoleLogger.fine("ProtectInventoryEvent has been cancelled for " + player.getName() + "...");
@@ -9,6 +9,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.events.FailedLoginEvent;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AdminPermission;
@@ -180,6 +181,8 @@ public class AsynchronousLogin implements AsynchronousProcess {
*/
private void handleWrongPassword(Player player, String ip) {
ConsoleLogger.fine(player.getName() + " used the wrong password");
bukkitService.createAndCallEvent(isAsync -> new FailedLoginEvent(player, isAsync));
if (tempbanManager.shouldTempban(ip)) {
tempbanManager.tempbanPlayer(player);
} else if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) {
@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import java.util.function.Function;
/**
* Service for operations requiring the Bukkit API, such as for scheduling.
@@ -263,6 +264,20 @@ public class BukkitService implements SettingsDependent {
Bukkit.getPluginManager().callEvent(event);
}
/**
* Creates an event with the provided function and emits it.
*
* @param eventSupplier the event supplier: function taking a boolean specifying whether AuthMe is configured
* in async mode or not
* @param <E> the event type
* @return the event that was created and emitted
*/
public <E extends Event> E createAndCallEvent(Function<Boolean, E> eventSupplier) {
E event = eventSupplier.apply(useAsyncTasks);
callEvent(event);
return event;
}
/**
* Gets the world with the given name.
*