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

68 lines
2.7 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
import com.google.gson.Gson;
2016-07-31 22:00:46 +08:00
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.mojang.authlib.GameProfile;
2016-02-27 15:22:35 +08:00
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
2016-07-31 22:00:46 +08:00
import com.mojang.authlib.minecraft.MinecraftSessionService;
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-02-27 16:37:05 +08:00
import customskinloader.utils.HttpUtil0;
2016-07-31 22:00:46 +08:00
import net.minecraft.client.Minecraft;
2016-02-27 15:22:35 +08:00
public class MojangAPILoader implements IProfileLoader {
2016-07-31 22:00:46 +08:00
public static MinecraftSessionService defaultSessionService=null;
2016-02-27 15:22:35 +08:00
@Override
2016-07-31 22:00:46 +08:00
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
if(defaultSessionService==null){
CustomSkinLoader.logger.warning("Session Service Not Exist.");
2016-02-27 15:22:35 +08:00
return null;
}
2016-07-31 22:00:46 +08:00
Map<MinecraftProfileTexture.Type,MinecraftProfileTexture> map=defaultSessionService.getTextures(gameProfile, false);
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-07-31 22:00:46 +08:00
defaultSessionService.fillProfileProperties(newGameProfile, false);
map=defaultSessionService.getTextures(newGameProfile, false);
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;
}
public static GameProfile loadGameProfile(String username) throws JsonSyntaxException{
//Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time)
String json0=HttpUtil0.readHttp("https://api.mojang.com/users/profiles/minecraft/"+username,null);
if(json0==null||json0.equals(""))
2016-02-27 15:22:35 +08:00
return null;
2016-07-31 22:00:46 +08:00
Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
GameProfile gameProfile=gson.fromJson(json0, GameProfile.class);
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-07-31 22:00:46 +08:00
@Override
public UserProfile loadLocalProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
CustomSkinLoader.logger.warning("Local Skin couldn't load by MojangAPI.");
return loadProfile(ssp,gameProfile);
2016-02-27 15:22:35 +08:00
}
}