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

121 lines
5.3 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.loader;
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-07-31 22:00:46 +08:00
import com.mojang.authlib.minecraft.MinecraftSessionService;
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-12-25 17:05:30 +08:00
import customskinloader.utils.MinecraftUtil;
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
public static final MinecraftSessionService defaultSessionService=MinecraftUtil.getSessionService();
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
Map<MinecraftProfileTexture.Type,MinecraftProfileTexture> map=getTextures(gameProfile);
if(!map.isEmpty()){
CustomSkinLoader.logger.info("Default profile will be used.");
return ModelManager0.toUserProfile(map);
}
String username=gameProfile.getName();
GameProfile newGameProfile=loadGameProfile(username);
if(newGameProfile==null){
CustomSkinLoader.logger.info("Profile not found.("+username+"'s profile not found.)");
return null;
}
newGameProfile=fillGameProfile(newGameProfile);
map=getTextures(newGameProfile);
if(!map.isEmpty()){
gameProfile.getProperties().putAll(newGameProfile.getProperties());
return ModelManager0.toUserProfile(map);
}
CustomSkinLoader.logger.info("Profile not found.("+username+" doesn't have skin/cape.)");
return null;
}
//Username -> UUID
2018-06-25 00:34:32 +08:00
public static GameProfile loadGameProfile(String username) {
2018-02-12 12:20:28 +08:00
//Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time)
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/"+username).setCacheTime(0));
if(StringUtils.isEmpty(responce.content))
return null;
2018-02-12 15:02:12 +08:00
2018-02-12 12:20:28 +08:00
Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
GameProfile gameProfile=gson.fromJson(responce.content, GameProfile.class);
2018-02-12 15:02:12 +08:00
2018-02-12 12:20:28 +08:00
if(gameProfile.getId()==null)
return null;
return new GameProfile(gameProfile.getId(),gameProfile.getName());
}
//UUID -> Profile
2018-06-25 00:34:32 +08:00
public static GameProfile fillGameProfile(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(
new HttpRequest("https://sessionserver.mojang.com/session/minecraft/profile/"
+ UUIDTypeAdapter.fromUUID(profile.getId())).setCacheTime(90));
2018-02-12 12:20:28 +08:00
if(StringUtils.isEmpty(responce.content))
return profile;
2018-02-12 15:02:12 +08:00
Gson gson=new GsonBuilder()
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.create();
MinecraftProfilePropertiesResponse propertiesResponce=gson.fromJson(responce.content, MinecraftProfilePropertiesResponse.class);
GameProfile newGameProfile=new GameProfile(propertiesResponce.getId(),propertiesResponce.getName());
newGameProfile.getProperties().putAll(propertiesResponce.getProperties());
return newGameProfile;
2018-02-12 12:20:28 +08:00
}
2018-06-25 00:34:32 +08:00
public static Map<MinecraftProfileTexture.Type,MinecraftProfileTexture> getTextures(GameProfile gameProfile) {
2018-02-12 12:20:28 +08:00
if(gameProfile==null)
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
Property textureProperty=Iterables.getFirst(gameProfile.getProperties().get("textures"), null);
2018-02-12 12:20:28 +08:00
if (textureProperty==null)
2018-06-25 00:34:32 +08:00
return Maps.newHashMap();
2018-02-12 12:20:28 +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);
2018-02-12 12:20:28 +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();
}
@Override
public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) {
return true;
}
@Override
public String getName() {
return "MojangAPI";
}
2016-09-24 20:47:08 +08:00
2018-02-12 12:20:28 +08:00
@Override
public void initLocalFolder(SkinSiteProfile ssp) {
}
2016-02-27 15:22:35 +08:00
}