PublicGameProfile class

Пакет: com.hypixel.hytale.server.core.auth

Файл: com/hypixel/hytale/server/core/auth/ProfileServiceClient.java

Поля (3)

МодификаторыТипИмя
public static final BuilderCodec<ProfileServiceClient.PublicGameProfile> CODEC
private String username
private UUID uuid

Методы (2)

МодификаторыВозвратСигнатура
public String getUsernamepublic String getUsername()
public UUID getUuidpublic UUID getUuid()

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.server.core.auth;

class="kw">import com.hypixel.hytale.codec.Codec;
class="kw">import com.hypixel.hytale.codec.EmptyExtraInfo;
class="kw">import com.hypixel.hytale.codec.KeyedCodec;
class="kw">import com.hypixel.hytale.codec.builder.BuilderCodec;
class="kw">import com.hypixel.hytale.codec.util.RawJsonReader;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.server.core.util.ServiceHttpClientFactory;
class="kw">import java.io.IOException;
class="kw">import java.net.URI;
class="kw">import java.net.URLEncoder;
class="kw">import java.net.http.HttpClient;
class="kw">import java.net.http.HttpRequest;
class="kw">import java.net.http.HttpResponse;
class="kw">import java.net.http.HttpResponse.BodyHandlers;
class="kw">import java.nio.charset.StandardCharsets;
class="kw">import java.util.UUID;
class="kw">import java.util.concurrent.CompletableFuture;
class="kw">import java.util.logging.Level;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class ProfileServiceClient {
   class="kw">private class="kw">static class="kw">final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
   class="kw">private class="kw">final HttpClient httpClient;
   class="kw">private class="kw">final String profileServiceUrl;

   class="kw">public ProfileServiceClient(@Nonnull String var1) {
      if (var1 != null && !var1.isEmpty()) {
         this.profileServiceUrl = var1.endsWith("/") ? var1.substring(0, var1.length() - 1) : var1;
         this.httpClient = ServiceHttpClientFactory.create(AuthConfig.HTTP_TIMEOUT);
         LOGGER.at(Level.INFO).log("Profile Service client initialized for: %s", this.profileServiceUrl);
      } else {
         throw new IllegalArgumentException("Profile Service URL cannot be null or empty");
      }
   }

   @Nullable
   class="kw">public ProfileServiceClient.PublicGameProfile getProfileByUuid(@Nonnull UUID var1, @Nonnull String var2) {
      try {
         HttpRequest var3 = HttpRequest.newBuilder()
            .uri(URI.create(this.profileServiceUrl + "/profile/uuid/" + var1.toString()))
            .header("Accept", "application/json")
            .header("Authorization", "Bearer " + var2)
            .header("User-Agent", AuthConfig.USER_AGENT)
            .timeout(AuthConfig.HTTP_TIMEOUT)
            .GET()
            .build();
         LOGGER.at(Level.FINE).log("Fetching profile by UUID: %s", var1);
         HttpResponse var4 = this.httpClient.send(var3, BodyHandlers.ofString());
         if (var4.statusCode() != 200) {
            LOGGER.at(Level.WARNING).log("Failed to fetch profile by UUID: HTTP %d - %s", var4.statusCode(), var4.body());
            class="kw">return null;
         } else {
            ProfileServiceClient.PublicGameProfile var5 = ProfileServiceClient.PublicGameProfile.CODEC
               .decodeJson(new RawJsonReader(var4.body().toCharArray()), EmptyExtraInfo.EMPTY);
            if (var5 == null) {
               LOGGER.at(Level.WARNING).log("Profile Service returned invalid response for UUID: %s", var1);
               class="kw">return null;
            } else {
               LOGGER.at(Level.FINE).log("Successfully fetched profile: %s (%s)", var5.getUsername(), var5.getUuid());
               class="kw">return var5;
            }
         }
      } catch (IOException var6) {
         LOGGER.at(Level.WARNING).log("IO error while fetching profile by UUID: %s", var6.getMessage());
         class="kw">return null;
      } catch (InterruptedException var7) {
         LOGGER.at(Level.WARNING).log("Request interrupted while fetching profile by UUID");
         Thread.currentThread().interrupt();
         class="kw">return null;
      } catch (Exception var8) {
         LOGGER.at(Level.WARNING).log("Unexpected error fetching profile by UUID: %s", var8.getMessage());
         class="kw">return null;
      }
   }

   class="kw">public CompletableFuture<ProfileServiceClient.PublicGameProfile> getProfileByUuidAsync(@Nonnull UUID var1, @Nonnull String var2) {
      class="kw">return CompletableFuture.supplyAsync(() -> this.getProfileByUuid(var1, var2));
   }

   @Nullable
   class="kw">public ProfileServiceClient.PublicGameProfile getProfileByUsername(@Nonnull String var1, @Nonnull String var2) {
      try {
         String var3 = URLEncoder.encode(var1, StandardCharsets.UTF_8);
         HttpRequest var4 = HttpRequest.newBuilder()
            .uri(URI.create(this.profileServiceUrl + "/profile/username/" + var3))
            .header("Accept", "application/json")
            .header("Authorization", "Bearer " + var2)
            .header("User-Agent", AuthConfig.USER_AGENT)
            .timeout(AuthConfig.HTTP_TIMEOUT)
            .GET()
            .build();
         LOGGER.at(Level.FINE).log("Fetching profile by username: %s", var1);
         HttpResponse var5 = this.httpClient.send(var4, BodyHandlers.ofString());
         if (var5.statusCode() != 200) {
            LOGGER.at(Level.WARNING).log("Failed to fetch profile by username: HTTP %d - %s", var5.statusCode(), var5.body());
            class="kw">return null;
         } else {
            ProfileServiceClient.PublicGameProfile var6 = ProfileServiceClient.PublicGameProfile.CODEC
               .decodeJson(new RawJsonReader(var5.body().toCharArray()), EmptyExtraInfo.EMPTY);
            if (var6 == null) {
               LOGGER.at(Level.WARNING).log("Profile Service returned invalid response for username: %s", var1);
               class="kw">return null;
            } else {
               LOGGER.at(Level.FINE).log("Successfully fetched profile: %s (%s)", var6.getUsername(), var6.getUuid());
               class="kw">return var6;
            }
         }
      } catch (IOException var7) {
         LOGGER.at(Level.WARNING).log("IO error while fetching profile by username: %s", var7.getMessage());
         class="kw">return null;
      } catch (InterruptedException var8) {
         LOGGER.at(Level.WARNING).log("Request interrupted while fetching profile by username");
         Thread.currentThread().interrupt();
         class="kw">return null;
      } catch (Exception var9) {
         LOGGER.at(Level.WARNING).log("Unexpected error fetching profile by username: %s", var9.getMessage());
         class="kw">return null;
      }
   }

   class="kw">public CompletableFuture<ProfileServiceClient.PublicGameProfile> getProfileByUsernameAsync(@Nonnull String var1, @Nonnull String var2) {
      class="kw">return CompletableFuture.supplyAsync(() -> this.getProfileByUsername(var1, var2));
   }

   class="kw">private class="kw">static <T> KeyedCodec<T> externalKey(String var0, Codec<T> var1) {
      class="kw">return new KeyedCodec<>(var0, var1, false, true);
   }

   class="kw">public class="kw">static class PublicGameProfile {
      class="kw">public class="kw">static class="kw">final BuilderCodec<ProfileServiceClient.PublicGameProfile> CODEC = BuilderCodec.builder(
            ProfileServiceClient.PublicGameProfile.class, ProfileServiceClient.PublicGameProfile::new
         )
         .append(ProfileServiceClient.externalKey("uuid", Codec.UUID_STRING), (var0, var1) -> var0.uuid = var1, ProfileServiceClient.PublicGameProfile::getUuid)
         .add()
         .append(
            ProfileServiceClient.externalKey("username", Codec.STRING),
            (var0, var1) -> var0.username = var1,
            ProfileServiceClient.PublicGameProfile::getUsername
         )
         .add()
         .build();
      class="kw">private UUID uuid;
      class="kw">private String username;

      class="kw">public PublicGameProfile() {
      }

      class="kw">public PublicGameProfile(UUID var1, String var2) {
         this.uuid = var1;
         this.username = var2;
      }

      class="kw">public UUID getUuid() {
         class="kw">return this.uuid;
      }

      class="kw">public String getUsername() {
         class="kw">return this.username;
      }
   }
}