[FEATURE] PlayerProfile properties, User assets

This commit is contained in:
Gravita 2022-03-17 23:28:16 +07:00
parent 2b117f6717
commit a54d7ba89a
5 changed files with 100 additions and 17 deletions

View file

@ -3,6 +3,9 @@
import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.Texture; import pro.gravit.launcher.profiles.Texture;
import java.util.HashMap;
import java.util.Map;
public interface UserSupportTextures { public interface UserSupportTextures {
Texture getSkinTexture(); Texture getSkinTexture();
@ -15,4 +18,17 @@ default Texture getSkinTexture(ClientProfile profile) {
default Texture getCloakTexture(ClientProfile profile) { default Texture getCloakTexture(ClientProfile profile) {
return getCloakTexture(); return getCloakTexture();
} }
default Map<String, Texture> getUserAssets() {
var skin = getSkinTexture();
var cape = getCloakTexture();
Map<String, Texture> map = new HashMap<>();
if(skin != null) {
map.put("SKIN", skin);
}
if(cape != null) {
map.put("CAPE", cape);
}
return map;
}
} }

View file

@ -1,5 +1,6 @@
package pro.gravit.launchserver.auth.texture; package pro.gravit.launchserver.auth.texture;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import pro.gravit.launcher.HTTPRequest; import pro.gravit.launcher.HTTPRequest;
@ -7,12 +8,16 @@
import pro.gravit.launcher.profiles.Texture; import pro.gravit.launcher.profiles.Texture;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class JsonTextureProvider extends TextureProvider { public class JsonTextureProvider extends TextureProvider {
public String url; public String url;
private transient final Logger logger = LogManager.getLogger(); private transient final Logger logger = LogManager.getLogger();
private transient static final Type MAP_TYPE = new TypeToken<Map<String, Texture>>() {}.getType();
@Override @Override
public void close() throws IOException { public void close() throws IOException {
@ -22,23 +27,36 @@ public void close() throws IOException {
@Override @Override
public Texture getCloakTexture(UUID uuid, String username, String client) throws IOException { public Texture getCloakTexture(UUID uuid, String username, String client) throws IOException {
logger.warn("Ineffective get cloak texture for {}", username); logger.warn("Ineffective get cloak texture for {}", username);
return getTextures(uuid, username, client).cloak; return getAssets(uuid, username, client).get("CAPE");
} }
@Override @Override
public Texture getSkinTexture(UUID uuid, String username, String client) throws IOException { public Texture getSkinTexture(UUID uuid, String username, String client) throws IOException {
logger.warn("Ineffective get skin texture for {}", username); logger.warn("Ineffective get skin texture for {}", username);
return getTextures(uuid, username, client).skin; return getAssets(uuid, username, client).get("SKIN");
} }
@Override @Override
public SkinAndCloakTextures getTextures(UUID uuid, String username, String client) { public Map<String, Texture> getAssets(UUID uuid, String username, String client) {
try { try {
var result = HTTPRequest.jsonRequest(null, "GET", new URL(RequestTextureProvider.getTextureURL(url, uuid, username, client))); var result = HTTPRequest.jsonRequest(null, "GET", new URL(RequestTextureProvider.getTextureURL(url, uuid, username, client)));
return Launcher.gsonManager.gson.fromJson(result, SkinAndCloakTextures.class);
Map<String, Texture> 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) { } catch (IOException e) {
logger.error("JsonTextureProvider", e); logger.error("JsonTextureProvider", e);
return new SkinAndCloakTextures(null, null); return new HashMap<>();
} }
} }
} }

View file

@ -4,6 +4,8 @@
import pro.gravit.utils.ProviderMap; import pro.gravit.utils.ProviderMap;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public abstract class TextureProvider implements AutoCloseable { 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; public abstract Texture getSkinTexture(UUID uuid, String username, String client) throws IOException;
@Deprecated
public static class SkinAndCloakTextures { public static class SkinAndCloakTextures {
public final Texture skin; public final Texture skin;
public final Texture cloak; public final Texture cloak;
@ -41,6 +44,7 @@ public SkinAndCloakTextures(Texture skin, Texture cloak) {
} }
} }
@Deprecated
public SkinAndCloakTextures getTextures(UUID uuid, String username, String client) { public SkinAndCloakTextures getTextures(UUID uuid, String username, String client) {
Texture skin; Texture skin;
@ -60,4 +64,32 @@ public SkinAndCloakTextures getTextures(UUID uuid, String username, String clien
return new SkinAndCloakTextures(skin, cloak); return new SkinAndCloakTextures(skin, cloak);
} }
public Map<String, Texture> 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<String, Texture> map = new HashMap<>();
if(skin != null) {
map.put("SKIN", skin);
}
if(cloak != null) {
map.put("CAPE", cloak);
}
return map;
}
} }

View file

@ -8,6 +8,7 @@
import pro.gravit.launcher.events.request.AuthRequestEvent; import pro.gravit.launcher.events.request.AuthRequestEvent;
import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.PlayerProfile; 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.AuthRequest;
import pro.gravit.launcher.request.auth.password.*; import pro.gravit.launcher.request.auth.password.*;
import pro.gravit.launchserver.LaunchServer; 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 getPlayerProfile(client.uuid, client.username, client.profile == null ? null : client.profile.getTitle(), client.auth.textureProvider, new HashMap<>());
} }
// Return combined profile // 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) { public PlayerProfile getPlayerProfile(AuthProviderPair pair, String username) {
@ -229,7 +230,7 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, String username, Cl
if (pair.textureProvider != null) { if (pair.textureProvider != null) {
return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider, new HashMap<>()); 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) { public PlayerProfile getPlayerProfile(AuthProviderPair pair, UUID uuid) {
@ -261,8 +262,8 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, User user) {
} else { } else {
properties = new HashMap<>(); properties = new HashMap<>();
} }
if (user instanceof UserSupportTextures) { if (user instanceof UserSupportTextures userSupportTextures) {
return new PlayerProfile(user.getUUID(), user.getUsername(), ((UserSupportTextures) user).getSkinTexture(), ((UserSupportTextures) user).getCloakTexture(), properties); return new PlayerProfile(user.getUUID(), user.getUsername(), userSupportTextures.getUserAssets(), properties);
} }
if (pair.textureProvider == null) { if (pair.textureProvider == null) {
throw new NullPointerException("TextureProvider not found"); 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<String, String> properties) { private PlayerProfile getPlayerProfile(UUID uuid, String username, String client, TextureProvider textureProvider, Map<String, String> properties) {
// Get skin texture // Get skin texture
TextureProvider.SkinAndCloakTextures textures = textureProvider.getTextures(uuid, username, client); var assets = textureProvider.getAssets(uuid, username, client);
// Return combined profile // 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 { public AuthRequest.AuthPasswordInterface decryptPassword(AuthRequest.AuthPasswordInterface password) throws AuthException {

View file

@ -12,28 +12,44 @@ public final class PlayerProfile {
public final UUID uuid; public final UUID uuid;
public final String username; public final String username;
@Deprecated
public final Texture skin, cloak; public final Texture skin, cloak;
public final Map<String, Texture> assets;
public final Map<String, String> properties; public final Map<String, String> properties;
@Deprecated
public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak) { public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak) {
this.uuid = Objects.requireNonNull(uuid, "uuid"); this(uuid, username, skin, cloak, new HashMap<>());
this.username = username;
this.skin = skin;
this.cloak = cloak;
this.properties = new HashMap<>();
} }
@Deprecated
public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Map<String, String> properties) { public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Map<String, String> properties) {
this.uuid = Objects.requireNonNull(uuid, "uuid"); this.uuid = Objects.requireNonNull(uuid, "uuid");
this.username = username; this.username = username;
this.skin = skin; this.skin = skin;
this.cloak = cloak; 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; this.properties = properties;
} }
public PlayerProfile(UUID uuid, String username, Map<String, Texture> assets, Map<String, String> 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) { 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) { public static UUID offlineUUID(String username) {