package com.github.games647.fastlogin.bukkit.hook;

import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
import com.github.games647.fastlogin.core.hooks.AuthPlugin;
import com.lenis0012.bukkit.loginsecurity.LoginSecurity;
import com.lenis0012.bukkit.loginsecurity.session.AuthService;
import com.lenis0012.bukkit.loginsecurity.session.PlayerSession;
import com.lenis0012.bukkit.loginsecurity.session.action.LoginAction;
import com.lenis0012.bukkit.loginsecurity.session.action.RegisterAction;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

/**
 * GitHub: https://github.com/lenis0012/LoginSecurity-2 
 * <p>
 * Project page:
 * <p>
 * Bukkit: https://dev.bukkit.org/bukkit-plugins/loginsecurity/
 * <p>
 * Spigot: https://www.spigotmc.org/resources/loginsecurity.19362/
 */
public class LoginSecurityHook implements AuthPlugin<Player> {

    private final FastLoginBukkit plugin;

    public LoginSecurityHook(FastLoginBukkit plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean forceLogin(Player player) {
        Future<Boolean> future = Bukkit.getScheduler().callSyncMethod(plugin, () -> {
            PlayerSession session = LoginSecurity.getSessionManager().getPlayerSession(player);
            return session.isAuthorized()
                    || session.performAction(new LoginAction(AuthService.PLUGIN, plugin)).isSuccess();
        });

        try {
            return future.get();
        } catch (InterruptedException | ExecutionException ex) {
            plugin.getLog().error("Failed to forceLogin player: {}", player, ex);
            return false;
        }
    }

    @Override
    public boolean isRegistered(String playerName) {
        PlayerSession session = LoginSecurity.getSessionManager().getOfflineSession(playerName);
        return session.isRegistered();
    }

    @Override
    public boolean forceRegister(Player player, String password) {
        Future<Boolean> future = Bukkit.getScheduler().callSyncMethod(plugin, () -> {
            PlayerSession session = LoginSecurity.getSessionManager().getPlayerSession(player);
            return session.performAction(new RegisterAction(AuthService.PLUGIN, plugin, password)).isSuccess();
        });

        try {
            return future.get();
        } catch (InterruptedException | ExecutionException ex) {
            plugin.getLog().error("Failed to forceLogin player: {}", player, ex);
            return false;
        }
    }
}

@@ -499,119 +499,8 @@
 n;%0A%0A
-import java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.Future;%0A%0Aimport org.bukkit.Bukkit;%0A
 impo
@@ -588,17 +588,16 @@
 curity-2
- 
 %0A * %3Cp%3E%0A
@@ -1026,98 +1026,8 @@
 ) %7B%0A
-        Future%3CBoolean%3E future = Bukkit.getScheduler().callSyncMethod(plugin, () -%3E %7B%0A    
     
@@ -1114,36 +1114,32 @@
 layer);%0A        
-    
 return session.i
@@ -1168,20 +1168,16 @@
         
-  
-  
 %7C%7C sessi
@@ -1255,251 +1255,8 @@
 ();%0A
-        %7D);%0A%0A        try %7B%0A            return future.get();%0A        %7D catch (InterruptedException %7C ExecutionException ex) %7B%0A            plugin.getLog().error(%22Failed to forceLogin player: %7B%7D%22, player, ex);%0A            return false;%0A        %7D%0A
     
@@ -1553,98 +1553,8 @@
 ) %7B%0A
-        Future%3CBoolean%3E future = Bukkit.getScheduler().callSyncMethod(plugin, () -%3E %7B%0A    
     
@@ -1637,28 +1637,24 @@
 on(player);%0A
-    
         retu
@@ -1753,251 +1753,8 @@
 ();%0A
-        %7D);%0A%0A        try %7B%0A            return future.get();%0A        %7D catch (InterruptedException %7C ExecutionException ex) %7B%0A            plugin.getLog().error(%22Failed to forceLogin player: %7B%7D%22, player, ex);%0A            return false;%0A        %7D%0A
     

Migrate to asynchronous calls to LoginSecurity