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

94 lines
3.7 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.loader;
2016-08-28 20:55:54 +08:00
import java.util.HashMap;
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
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;
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
2016-12-25 17:05:30 +08:00
public static final MinecraftSessionService defaultSessionService=MinecraftUtil.getSessionService();
2016-02-27 15:22:35 +08:00
@Override
2016-09-10 21:00:53 +08:00
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
2016-08-28 20:55:54 +08:00
Map<MinecraftProfileTexture.Type,MinecraftProfileTexture> map=getTextures(gameProfile);
2016-07-31 22:00:46 +08:00
if(!map.isEmpty()){
CustomSkinLoader.logger.info("Default profile will be used.");
return ModelManager0.toUserProfile(map);
2016-02-27 15:22:35 +08:00
}
2016-07-31 22:00:46 +08:00
String username=gameProfile.getName();
GameProfile newGameProfile=loadGameProfile(username);
if(newGameProfile==null){
CustomSkinLoader.logger.info("Profile not found.("+username+"'s profile not found.)");
2016-02-27 15:22:35 +08:00
return null;
}
2016-08-28 21:00:57 +08:00
newGameProfile=defaultSessionService.fillProfileProperties(newGameProfile, false);
2016-08-28 20:55:54 +08:00
map=getTextures(newGameProfile);
2016-07-31 22:00:46 +08:00
if(!map.isEmpty()){
gameProfile.getProperties().putAll(newGameProfile.getProperties());
return ModelManager0.toUserProfile(map);
2016-02-27 15:22:35 +08:00
}
2016-07-31 22:00:46 +08:00
CustomSkinLoader.logger.info("Profile not found.("+username+" doesn't have skin/cape.)");
return null;
}
2016-08-28 20:55:54 +08:00
public static GameProfile loadGameProfile(String username) throws Exception{
2016-07-31 22:00:46 +08:00
//Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time)
2016-11-06 12:00:36 +08:00
HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/"+username).setCacheTime(0));
if(StringUtils.isEmpty(responce.content))
2016-02-27 15:22:35 +08:00
return null;
2016-08-28 20:55:54 +08:00
Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
2016-11-06 12:00:36 +08:00
GameProfile gameProfile=gson.fromJson(responce.content, GameProfile.class);
2016-07-31 22:00:46 +08:00
if(gameProfile.getId()==null)
2016-02-27 15:22:35 +08:00
return null;
2016-07-31 22:00:46 +08:00
return new GameProfile(gameProfile.getId(),gameProfile.getName());
2016-02-27 15:22:35 +08:00
}
2016-08-28 20:55:54 +08:00
public static Map<MinecraftProfileTexture.Type,MinecraftProfileTexture> getTextures(GameProfile gameProfile) throws Exception{
Property textureProperty=(Property)Iterables.getFirst(gameProfile.getProperties().get("textures"), null);
if (textureProperty==null)
return new HashMap();
String value=textureProperty.getValue();
2016-09-10 21:00:53 +08:00
if(StringUtils.isBlank(value))
2016-08-28 20:55:54 +08:00
return new HashMap();
String json=new String(Base64.decodeBase64(value),Charsets.UTF_8);
Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class,new UUIDTypeAdapter()).create();
2016-09-10 21:00:53 +08:00
MinecraftTexturesPayload result=gson.fromJson(json,MinecraftTexturesPayload.class);
2016-08-28 20:55:54 +08:00
if (result==null||result.getTextures()==null)
return new HashMap();
return result.getTextures();
}
2016-08-21 11:26:00 +08:00
@Override
public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) {
return true;
}
2016-08-28 19:40:18 +08:00
@Override
public String getName() {
return "MojangAPI";
}
2016-09-24 20:47:08 +08:00
@Override
public void initLocalFolder(SkinSiteProfile ssp) {
}
2016-02-27 15:22:35 +08:00
}