[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.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<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;
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<Map<String, Texture>>() {}.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<String, Texture> 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<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) {
logger.error("JsonTextureProvider", e);
return new SkinAndCloakTextures(null, null);
return new HashMap<>();
}
}
}

View file

@ -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<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.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<String, String> 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 {

View file

@ -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<String, Texture> assets;
public final Map<String, String> 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<String, String> 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<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) {
return new PlayerProfile(offlineUUID(username), username, null, null);
return new PlayerProfile(offlineUUID(username), username, new HashMap<>(), new HashMap<>());
}
public static UUID offlineUUID(String username) {