Refactor 2fa and add more messages when events are cancelled
This commit is contained in:
parent
8af04af8fd
commit
f3c3ee806d
@ -9,13 +9,16 @@ import java.util.UUID;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryEvent;
|
||||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||||
|
import org.bukkit.event.player.PlayerEvent;
|
||||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||||
@ -83,6 +86,11 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
PermissionGroup.ADMIN.setPermission(Perm.RESET_2FA, true, true);
|
PermissionGroup.ADMIN.setPermission(Perm.RESET_2FA, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendTokenRequest(Player player)
|
||||||
|
{
|
||||||
|
player.sendMessage(F.main("2FA", "Please enter your two-factor auth code"));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCommands()
|
public void addCommands()
|
||||||
{
|
{
|
||||||
@ -110,7 +118,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
{
|
{
|
||||||
new TwoFactorResetCommand(caller.getName(), caller.getUniqueId().toString(), caller.getName(), caller.getUniqueId().toString()).publish();
|
new TwoFactorResetCommand(caller.getName(), caller.getUniqueId().toString(), caller.getName(), caller.getUniqueId().toString()).publish();
|
||||||
});
|
});
|
||||||
_repository.deletePlayerData(_clientManager.getAccountId(caller)).whenComplete(BukkitFuture.complete((__, err) ->
|
_repository.deletePlayerData(_clientManager.getAccountId(caller)).whenComplete(BukkitFuture.<Void>complete((__, err) ->
|
||||||
{
|
{
|
||||||
if (err != null)
|
if (err != null)
|
||||||
{
|
{
|
||||||
@ -145,7 +153,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
{
|
{
|
||||||
new TwoFactorResetCommand(caller.getName(), caller.getUniqueId().toString(), client.getName(), client.getUniqueId() == null ? "null" : client.getUniqueId().toString()).publish();
|
new TwoFactorResetCommand(caller.getName(), caller.getUniqueId().toString(), client.getName(), client.getUniqueId() == null ? "null" : client.getUniqueId().toString()).publish();
|
||||||
});
|
});
|
||||||
_repository.deletePlayerData(client.getAccountId()).whenComplete(BukkitFuture.complete((__, err) ->
|
_repository.deletePlayerData(client.getAccountId()).whenComplete(BukkitFuture.<Void>complete((__, err) ->
|
||||||
{
|
{
|
||||||
if (err != null)
|
if (err != null)
|
||||||
{
|
{
|
||||||
@ -218,7 +226,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
if (data.getSecretKey().isPresent())
|
if (data.getSecretKey().isPresent())
|
||||||
{
|
{
|
||||||
// Hooray 2FA
|
// Hooray 2FA
|
||||||
player.sendMessage(F.main("2FA", "Please enter your two-factor auth code"));
|
sendTokenRequest(player);
|
||||||
authenticating.add(player.getUniqueId());
|
authenticating.add(player.getUniqueId());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -295,7 +303,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
Get(player).setSecretKey(secret);
|
Get(player).setSecretKey(secret);
|
||||||
|
|
||||||
player.sendMessage(F.main("2FA", "Saving secret.."));
|
player.sendMessage(F.main("2FA", "Saving secret.."));
|
||||||
_repository.saveSecret(player, secret).whenComplete(BukkitFuture.complete((v, throwable) ->
|
_repository.saveSecret(player, secret).whenComplete(BukkitFuture.<Void>complete((v, throwable) ->
|
||||||
{
|
{
|
||||||
if (!player.isOnline())
|
if (!player.isOnline())
|
||||||
{
|
{
|
||||||
@ -320,6 +328,47 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
authenticating.remove(player.getUniqueId());
|
authenticating.remove(player.getUniqueId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param event - The event being called
|
||||||
|
* @param player - The player in question for this event
|
||||||
|
* @return Whether the player is currently authenticating and action had to be taken
|
||||||
|
*/
|
||||||
|
private <T extends Cancellable> boolean handleCancelAuth(T event, Player player)
|
||||||
|
{
|
||||||
|
if (isAuthenticating(player))
|
||||||
|
{
|
||||||
|
sendTokenRequestIfReady(player);
|
||||||
|
event.setCancelled(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends PlayerEvent & Cancellable> boolean handleCancelAuth(T event)
|
||||||
|
{
|
||||||
|
return handleCancelAuth(event, event.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends InventoryEvent & Cancellable> boolean handleCancelAuth(T event)
|
||||||
|
{
|
||||||
|
return handleCancelAuth(event, (Player)event.getView().getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean canSendTokenRequest(Player player)
|
||||||
|
{
|
||||||
|
return Recharge.Instance.use(player, "two-factor message cooldown", 3000L, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendTokenRequestIfReady(Player player)
|
||||||
|
{
|
||||||
|
if (canSendTokenRequest(player))
|
||||||
|
{
|
||||||
|
sendTokenRequest(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cancel relevant events
|
// Cancel relevant events
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
@ -335,61 +384,39 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onClick(InventoryClickEvent event)
|
public void onClick(InventoryClickEvent event)
|
||||||
{
|
{
|
||||||
Player player = (Player) event.getWhoClicked();
|
handleCancelAuth(event);
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onDrag(InventoryDragEvent event)
|
public void onDrag(InventoryDragEvent event)
|
||||||
{
|
{
|
||||||
Player player = (Player) event.getWhoClicked();
|
handleCancelAuth(event);
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onDrop(PlayerDropItemEvent event)
|
public void onDrop(PlayerDropItemEvent event)
|
||||||
{
|
{
|
||||||
Player player = event.getPlayer();
|
handleCancelAuth(event);
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onInteract(PlayerInteractEvent event)
|
public void onInteract(PlayerInteractEvent event)
|
||||||
{
|
{
|
||||||
Player player = event.getPlayer();
|
handleCancelAuth(event);
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||||
public void onInteractWithEntity(PlayerInteractEntityEvent event)
|
public void onInteractWithEntity(PlayerInteractEntityEvent event)
|
||||||
{
|
{
|
||||||
Player player = event.getPlayer();
|
handleCancelAuth(event);
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onCommand(PlayerCommandPreprocessEvent event)
|
public void onCommand(PlayerCommandPreprocessEvent event)
|
||||||
{
|
{
|
||||||
Player player = event.getPlayer();
|
if (handleCancelAuth(event))
|
||||||
if (isAuthenticating(player))
|
|
||||||
{
|
{
|
||||||
event.setMessage("/");
|
event.setMessage("/");
|
||||||
event.setCancelled(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -399,10 +426,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
|
|||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
if (isAuthenticating(player))
|
if (isAuthenticating(player))
|
||||||
{
|
{
|
||||||
if (Recharge.Instance.use(player, "two-factor message cooldown", 3000L, false, false))
|
sendTokenRequestIfReady(player);
|
||||||
{
|
|
||||||
player.sendMessage(F.main("2FA", "Please enter your two-factor auth code"));
|
|
||||||
}
|
|
||||||
event.getTo().setX(event.getFrom().getX());
|
event.getTo().setX(event.getFrom().getX());
|
||||||
event.getTo().setZ(event.getFrom().getZ());
|
event.getTo().setZ(event.getFrom().getZ());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user