Files
MCCustomSkinLoader/Common/source/customskinloader/loader/MojangAPILoader.java
T

139 lines
5.8 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.loader;
2019-09-18 22:15:58 +08:00
import java.util.Collections;
2016-04-30 22:12:26 +08:00
import java.util.Map;
2016-07-31 22:00:46 +08:00
import java.util.UUID;
2016-02-27 15:22:35 +08:00
2018-06-25 00:34:32 +08:00
import com.google.common.collect.Maps;
2016-08-28 20:55:54 +08:00
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
2016-09-10 21:00:53 +08:00
import org.apache.commons.lang3.StringUtils;
2016-08-28 20:55:54 +08:00
import com.google.common.collect.Iterables;
2016-02-27 15:22:35 +08:00
import com.google.gson.Gson;
2016-07-31 22:00:46 +08:00
import com.google.gson.GsonBuilder;
import com.mojang.authlib.GameProfile;
2016-02-27 15:22:35 +08:00
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
2016-08-28 20:55:54 +08:00
import com.mojang.authlib.properties.Property;
2018-02-12 15:02:12 +08:00
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.authlib.yggdrasil.response.MinecraftProfilePropertiesResponse;
2016-08-28 20:55:54 +08:00
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload;
2016-07-31 22:00:46 +08:00
import com.mojang.util.UUIDTypeAdapter;
2016-02-27 15:22:35 +08:00
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
2016-07-31 22:00:46 +08:00
import customskinloader.profile.ModelManager0;
import customskinloader.profile.UserProfile;
2016-11-06 12:00:36 +08:00
import customskinloader.utils.HttpRequestUtil;
import customskinloader.utils.HttpRequestUtil.HttpRequest;
import customskinloader.utils.HttpRequestUtil.HttpResponce;
2016-02-27 15:22:35 +08:00
2016-08-21 11:26:00 +08:00
public class MojangAPILoader implements ProfileLoader.IProfileLoader {
2016-02-27 15:22:35 +08:00
2018-02-12 12:20:28 +08:00
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
2019-09-18 22:15:58 +08:00
Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> map = getTextures(gameProfile);
if (!map.isEmpty()) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Default profile will be used.");
return ModelManager0.toUserProfile(map);
}
2019-09-18 22:15:58 +08:00
String username = gameProfile.getName();
GameProfile newGameProfile = loadGameProfile(ssp.apiRoot, username);
if (newGameProfile == null) {
CustomSkinLoader.logger.info("Profile not found.(" + username + "'s profile not found.)");
2018-02-12 12:20:28 +08:00
return null;
}
2019-09-20 16:33:04 +08:00
newGameProfile = fillGameProfile(ssp.sessionRoot, newGameProfile);
2019-09-18 22:15:58 +08:00
map = getTextures(newGameProfile);
if (!map.isEmpty()) {
2018-02-12 12:20:28 +08:00
gameProfile.getProperties().putAll(newGameProfile.getProperties());
return ModelManager0.toUserProfile(map);
}
2019-09-18 22:15:58 +08:00
CustomSkinLoader.logger.info("Profile not found.(" + username + " doesn't have skin/cape.)");
2018-02-12 12:20:28 +08:00
return null;
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
//Username -> UUID
2019-09-18 22:15:58 +08:00
public static GameProfile loadGameProfile(String apiRoot, String username) {
//Doc (https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs)
Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
HttpResponce responce = HttpRequestUtil.makeHttpRequest(
new HttpRequest(apiRoot + "profiles/minecraft")
.setCacheTime(-1).setPayload(gson.toJson(Collections.singletonList(username)))
);
if (StringUtils.isEmpty(responce.content))
2018-02-12 12:20:28 +08:00
return null;
2019-09-18 22:15:58 +08:00
GameProfile[] profiles = gson.fromJson(responce.content, GameProfile[].class);
if (profiles.length == 0) return null;
GameProfile gameProfile = profiles[0];
if (gameProfile.getId() == null)
2018-02-12 12:20:28 +08:00
return null;
2019-09-18 22:15:58 +08:00
return new GameProfile(gameProfile.getId(), gameProfile.getName());
2018-02-12 12:20:28 +08:00
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
//UUID -> Profile
2019-09-18 22:15:58 +08:00
public static GameProfile fillGameProfile(String sessionRoot, GameProfile profile) {
2018-02-12 12:20:28 +08:00
//Doc (http://wiki.vg/Mojang_API#UUID_-.3E_Profile_.2B_Skin.2FCape)
2018-06-25 00:34:32 +08:00
HttpResponce responce = HttpRequestUtil.makeHttpRequest(
2019-09-18 22:15:58 +08:00
new HttpRequest(sessionRoot + "session/minecraft/profile/"
2018-06-25 00:34:32 +08:00
+ UUIDTypeAdapter.fromUUID(profile.getId())).setCacheTime(90));
2019-09-18 22:15:58 +08:00
if (StringUtils.isEmpty(responce.content))
2018-02-12 12:20:28 +08:00
return profile;
2019-09-18 22:15:58 +08:00
Gson gson = new GsonBuilder()
2018-02-12 15:02:12 +08:00
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.create();
2019-09-18 22:15:58 +08:00
MinecraftProfilePropertiesResponse propertiesResponce = gson.fromJson(responce.content, MinecraftProfilePropertiesResponse.class);
GameProfile newGameProfile = new GameProfile(propertiesResponce.getId(), propertiesResponce.getName());
2018-02-12 15:02:12 +08:00
newGameProfile.getProperties().putAll(propertiesResponce.getProperties());
2019-09-18 22:15:58 +08:00
2018-02-12 15:02:12 +08:00
return newGameProfile;
2018-02-12 12:20:28 +08:00
}
2019-09-18 22:15:58 +08:00
public static Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> getTextures(GameProfile gameProfile) {
if (gameProfile == null)
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
2019-09-18 22:15:58 +08:00
Property textureProperty = Iterables.getFirst(gameProfile.getProperties().get("textures"), null);
if (textureProperty == null)
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
2019-09-18 22:15:58 +08:00
String value = textureProperty.getValue();
if (StringUtils.isBlank(value))
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
@SuppressWarnings("deprecation") String json = new String(Base64.decodeBase64(value), Charsets.UTF_8);
2019-09-18 22:15:58 +08:00
Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
MinecraftTexturesPayload result = gson.fromJson(json, MinecraftTexturesPayload.class);
if (result == null || result.getTextures() == null)
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
2018-02-12 12:20:28 +08:00
return result.getTextures();
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
@Override
public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) {
return true;
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
@Override
public String getName() {
return "MojangAPI";
}
2016-09-24 20:47:08 +08:00
2018-02-12 12:20:28 +08:00
@Override
2019-09-18 22:15:58 +08:00
public void init(SkinSiteProfile ssp) {
2019-09-20 11:44:07 +08:00
//Init default api & session root for Mojang API
if (ssp.apiRoot == null)
2019-09-18 22:15:58 +08:00
ssp.apiRoot = "https://api.mojang.com/";
2019-09-20 16:33:04 +08:00
if (ssp.sessionRoot == null)
ssp.sessionRoot = "https://sessionserver.mojang.com/";
2019-10-03 15:30:46 +08:00
}
private static final String MOJANG_API_ROOT = "https://api{DO_NOT_MODIFY}.mojang.com/";
2019-10-03 15:30:46 +08:00
public static String getMojangApiRoot() {
return MOJANG_API_ROOT.replace("{DO_NOT_MODIFY}", "");
2018-02-12 12:20:28 +08:00
}
2016-02-27 15:22:35 +08:00
}