diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/user/UserSupportTextures.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/user/UserSupportTextures.java index cfe47016..07b08148 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/user/UserSupportTextures.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/user/UserSupportTextures.java @@ -3,6 +3,9 @@ import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.Texture; +import java.util.HashMap; +import java.util.Map; + public interface UserSupportTextures { Texture getSkinTexture(); @@ -15,4 +18,17 @@ default Texture getSkinTexture(ClientProfile profile) { default Texture getCloakTexture(ClientProfile profile) { return getCloakTexture(); } + + default Map getUserAssets() { + var skin = getSkinTexture(); + var cape = getCloakTexture(); + Map map = new HashMap<>(); + if(skin != null) { + map.put("SKIN", skin); + } + if(cape != null) { + map.put("CAPE", cape); + } + return map; + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/JsonTextureProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/JsonTextureProvider.java index be19ca1e..ff9dba4e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/JsonTextureProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/JsonTextureProvider.java @@ -1,5 +1,6 @@ package pro.gravit.launchserver.auth.texture; +import com.google.gson.reflect.TypeToken; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import pro.gravit.launcher.HTTPRequest; @@ -7,12 +8,16 @@ import pro.gravit.launcher.profiles.Texture; import java.io.IOException; +import java.lang.reflect.Type; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; public class JsonTextureProvider extends TextureProvider { public String url; private transient final Logger logger = LogManager.getLogger(); + private transient static final Type MAP_TYPE = new TypeToken>() {}.getType(); @Override public void close() throws IOException { @@ -22,23 +27,36 @@ public void close() throws IOException { @Override public Texture getCloakTexture(UUID uuid, String username, String client) throws IOException { logger.warn("Ineffective get cloak texture for {}", username); - return getTextures(uuid, username, client).cloak; + return getAssets(uuid, username, client).get("CAPE"); } @Override public Texture getSkinTexture(UUID uuid, String username, String client) throws IOException { logger.warn("Ineffective get skin texture for {}", username); - return getTextures(uuid, username, client).skin; + return getAssets(uuid, username, client).get("SKIN"); } @Override - public SkinAndCloakTextures getTextures(UUID uuid, String username, String client) { + public Map getAssets(UUID uuid, String username, String client) { try { var result = HTTPRequest.jsonRequest(null, "GET", new URL(RequestTextureProvider.getTextureURL(url, uuid, username, client))); - return Launcher.gsonManager.gson.fromJson(result, SkinAndCloakTextures.class); + + Map map = Launcher.gsonManager.gson.fromJson(result, MAP_TYPE); + if(map == null) { + return new HashMap<>(); + } + if(map.get("skin") != null) { // Legacy script + map.put("SKIN", map.get("skin")); + map.remove("skin"); + } + if(map.get("cloak") != null) { + map.put("CAPE", map.get("cloak")); + map.remove("cloak"); + } + return map; } catch (IOException e) { logger.error("JsonTextureProvider", e); - return new SkinAndCloakTextures(null, null); + return new HashMap<>(); } } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/TextureProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/TextureProvider.java index c8487a48..f0530b3d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/TextureProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/TextureProvider.java @@ -4,6 +4,8 @@ import pro.gravit.utils.ProviderMap; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; public abstract class TextureProvider implements AutoCloseable { @@ -31,6 +33,7 @@ public static void registerProviders() { public abstract Texture getSkinTexture(UUID uuid, String username, String client) throws IOException; + @Deprecated public static class SkinAndCloakTextures { public final Texture skin; public final Texture cloak; @@ -41,6 +44,7 @@ public SkinAndCloakTextures(Texture skin, Texture cloak) { } } + @Deprecated public SkinAndCloakTextures getTextures(UUID uuid, String username, String client) { Texture skin; @@ -60,4 +64,32 @@ public SkinAndCloakTextures getTextures(UUID uuid, String username, String clien return new SkinAndCloakTextures(skin, cloak); } + + public Map getAssets(UUID uuid, String username, String client) { + + Texture skin; + try { + skin = getSkinTexture(uuid, username, client); + } catch (IOException e) { + skin = null; + } + + // Get cloak texture + Texture cloak; + try { + cloak = getCloakTexture(uuid, username, client); + } catch (IOException e) { + cloak = null; + } + + Map map = new HashMap<>(); + if(skin != null) { + map.put("SKIN", skin); + } + if(cloak != null) { + map.put("CAPE", cloak); + } + + return map; + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java index 5fafa6bc..eaa75f7a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/AuthManager.java @@ -8,6 +8,7 @@ import pro.gravit.launcher.events.request.AuthRequestEvent; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.PlayerProfile; +import pro.gravit.launcher.profiles.Texture; import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.password.*; import pro.gravit.launchserver.LaunchServer; @@ -207,7 +208,7 @@ public PlayerProfile getPlayerProfile(Client client) { return getPlayerProfile(client.uuid, client.username, client.profile == null ? null : client.profile.getTitle(), client.auth.textureProvider, new HashMap<>()); } // Return combined profile - return new PlayerProfile(client.uuid, client.username, null, null); + return new PlayerProfile(client.uuid, client.username, null, null, new HashMap<>()); } public PlayerProfile getPlayerProfile(AuthProviderPair pair, String username) { @@ -229,7 +230,7 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, String username, Cl if (pair.textureProvider != null) { return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider, new HashMap<>()); } - return new PlayerProfile(uuid, username, null, null); + return new PlayerProfile(uuid, username, null, null, new HashMap<>()); } public PlayerProfile getPlayerProfile(AuthProviderPair pair, UUID uuid) { @@ -261,8 +262,8 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, User user) { } else { properties = new HashMap<>(); } - if (user instanceof UserSupportTextures) { - return new PlayerProfile(user.getUUID(), user.getUsername(), ((UserSupportTextures) user).getSkinTexture(), ((UserSupportTextures) user).getCloakTexture(), properties); + if (user instanceof UserSupportTextures userSupportTextures) { + return new PlayerProfile(user.getUUID(), user.getUsername(), userSupportTextures.getUserAssets(), properties); } if (pair.textureProvider == null) { throw new NullPointerException("TextureProvider not found"); @@ -272,10 +273,10 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, User user) { private PlayerProfile getPlayerProfile(UUID uuid, String username, String client, TextureProvider textureProvider, Map properties) { // Get skin texture - TextureProvider.SkinAndCloakTextures textures = textureProvider.getTextures(uuid, username, client); + var assets = textureProvider.getAssets(uuid, username, client); // Return combined profile - return new PlayerProfile(uuid, username, textures.skin, textures.cloak, properties); + return new PlayerProfile(uuid, username, assets, properties); } public AuthRequest.AuthPasswordInterface decryptPassword(AuthRequest.AuthPasswordInterface password) throws AuthException { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java index c3ca799b..018bb008 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/PlayerProfile.java @@ -12,28 +12,44 @@ public final class PlayerProfile { public final UUID uuid; public final String username; + @Deprecated public final Texture skin, cloak; + public final Map assets; public final Map properties; + @Deprecated public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak) { - this.uuid = Objects.requireNonNull(uuid, "uuid"); - this.username = username; - this.skin = skin; - this.cloak = cloak; - this.properties = new HashMap<>(); + this(uuid, username, skin, cloak, new HashMap<>()); } + @Deprecated public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Map properties) { this.uuid = Objects.requireNonNull(uuid, "uuid"); this.username = username; this.skin = skin; this.cloak = cloak; + this.assets = new HashMap<>(); + if(skin != null) { + this.assets.put("SKIN", skin); + } + if(cloak != null) { + this.assets.put("CAPE", cloak); + } this.properties = properties; } + public PlayerProfile(UUID uuid, String username, Map assets, Map properties) { + this.uuid = uuid; + this.username = username; + this.assets = assets; + this.properties = properties; + this.skin = assets.get("SKIN"); + this.cloak = assets.get("CAPE"); + } + public static PlayerProfile newOfflineProfile(String username) { - return new PlayerProfile(offlineUUID(username), username, null, null); + return new PlayerProfile(offlineUUID(username), username, new HashMap<>(), new HashMap<>()); } public static UUID offlineUUID(String username) {