<commit_before>using System;
using System.Threading.Tasks;
using Alex.API.Services;
using Alex.Gamestates.Login;
using Alex.Gui;
using Alex.Net;
using Alex.Networking.Java;
using Alex.Services;
using Alex.Utils;
using Alex.Worlds.Abstraction;
using Microsoft.Extensions.DependencyInjection;
using MojangSharp.Api;
using MojangSharp.Endpoints;

namespace Alex.Worlds.Multiplayer.Java
{
	public class JavaServerType : ServerTypeImplementation
	{
		private const string ProfileType = "java";
		
		private       Alex   Alex { get; }
		/// <inheritdoc />
		public JavaServerType(Alex alex) : base(new JavaServerQueryProvider(alex), "Java", "java")
		{
			Alex = alex;
			ProtocolVersion = JavaProtocol.ProtocolVersion;
		}

		/// <inheritdoc />
		public override bool TryGetWorldProvider(ServerConnectionDetails connectionDetails,
			PlayerProfile playerProfile,
			out WorldProvider worldProvider,
			out NetworkProvider networkProvider)
		{
			worldProvider = new JavaWorldProvider(Alex, connectionDetails.EndPoint, playerProfile, out networkProvider)
			{
				Hostname = connectionDetails.Hostname
			};

			return true;
		}

		/// <inheritdoc />
		public override Task Authenticate(GuiPanoramaSkyBox skyBox, PlayerProfile activeProfile, Action<bool> callBack)
		{
			JavaLoginState loginState = new JavaLoginState(
				skyBox,
				() =>
				{
					callBack(true);
				}, activeProfile);


			Alex.GameStateManager.SetActiveState(loginState, true);
			
			return Task.CompletedTask;
		}

		/// <inheritdoc />
		public override async Task<bool> VerifyAuthentication(PlayerProfile currentProfile)
		{
			if (currentProfile == null  || !currentProfile.Authenticated)
			{
				var authenticationService = Alex.Services.GetService<IPlayerProfileService>();
				foreach (var profile in authenticationService.GetProfiles(ProfileType))
				{
					//profile.Type = "java";

					Requester.ClientToken = profile.ClientToken;

					if (await Validate(profile.AccessToken))
					{
						profile.Authenticated = true;
						authenticationService.Force(profile);

						//CurrentProfile = profile;
						return true;
					}
				}
			}
		
			return false;
		}
		
		private async Task<bool> Validate(string accessToken)
		{
			return await new Validate(accessToken)
			   .PerformRequestAsync()
			   .ContinueWith(task =>
				{
					if (task.IsFaulted)
					{
						//Authenticate?.Invoke(this, new PlayerProfileAuthenticateEventArgs("Validation faulted!"));
						return false;<commit_msg>Fix authentication issue<commit_after>using System;
using System.Threading.Tasks;
using Alex.API.Services;
using Alex.Gamestates.Login;
using Alex.Gui;
using Alex.Net;
using Alex.Networking.Java;
using Alex.Services;
using Alex.Utils;
using Alex.Worlds.Abstraction;
using Microsoft.Extensions.DependencyInjection;
using MojangSharp.Api;
using MojangSharp.Endpoints;

namespace Alex.Worlds.Multiplayer.Java
{
	public class JavaServerType : ServerTypeImplementation
	{
		private const string ProfileType = "java";
		
		private       Alex   Alex { get; }
		/// <inheritdoc />
		public JavaServerType(Alex alex) : base(new JavaServerQueryProvider(alex), "Java", "java")
		{
			Alex = alex;
			ProtocolVersion = JavaProtocol.ProtocolVersion;
		}

		/// <inheritdoc />
		public override bool TryGetWorldProvider(ServerConnectionDetails connectionDetails,
			PlayerProfile playerProfile,
			out WorldProvider worldProvider,
			out NetworkProvider networkProvider)
		{
			worldProvider = new JavaWorldProvider(Alex, connectionDetails.EndPoint, playerProfile, out networkProvider)
			{
				Hostname = connectionDetails.Hostname
			};

			return true;
		}

		/// <inheritdoc />
		public override Task Authenticate(GuiPanoramaSkyBox skyBox, PlayerProfile activeProfile, Action<bool> callBack)
		{
			JavaLoginState loginState = new JavaLoginState(
				skyBox,
				() =>
				{
					callBack(true);
				}, activeProfile);


			Alex.GameStateManager.SetActiveState(loginState, true);
			
			return Task.CompletedTask;
		}

		/// <inheritdoc />
		public override async Task<bool> VerifyAuthentication(PlayerProfile currentProfile)
		{
			var authenticationService = Alex.Services.GetService<IPlayerProfileService>();
			
			if (currentProfile != null)
			{
				if (await TryAuthenticate(authenticationService, currentProfile))
				{
					currentProfile.Authenticated= true;

					return true;
				}
			}
			else
			{
				foreach (var profile in authenticationService.GetProfiles(ProfileType))
				{
					if (await TryAuthenticate(authenticationService, profile))
					{
						return true;
					}
				}
			}

			return false;
		}

		private async Task<bool> TryAuthenticate(IPlayerProfileService authenticationService, PlayerProfile profile)
		{
			Requester.ClientToken = profile.ClientToken;

			if (await Validate(profile.AccessToken))
			{
				profile.Authenticated = true;
				authenticationService.Force(profile);

				//CurrentProfile = profile;
				return true;
			}

			return false;
		}
		
		private async Task<bool> Validate(string accessToken)
		{
			return await new Validate(accessToken)
			   .PerformRequestAsync()
			   .ContinueWith(task =>
				{
					if (task.IsFaulted)
					{
						//Authenticate?.Invoke(this, new PlayerProfileAuthenticateEventArgs("Validation faulted!"));
						return false;
{'fraction_non_alphanumeric': 0.09332321699544764, 'fraction_numerical': 0.0, 'mean_word_length': 3.763324299909666, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3428782', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1491, 'n_words': 328}