diff --git a/Common/source/customskinloader/loader/JsonAPILoader.java b/Common/source/customskinloader/loader/JsonAPILoader.java index 9258985..968dbd0 100644 --- a/Common/source/customskinloader/loader/JsonAPILoader.java +++ b/Common/source/customskinloader/loader/JsonAPILoader.java @@ -1,7 +1,8 @@ package customskinloader.loader; import java.io.File; -import java.io.FileInputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.List; import com.mojang.authlib.GameProfile; @@ -15,21 +16,27 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; public class JsonAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.IProfileLoader { - + public interface IJsonAPI { List getDefaultProfiles(JsonAPILoader loader); + String toJsonUrl(String root, String username); + String getPayload(SkinSiteProfile ssp); + UserProfile toUserProfile(String root, String json, boolean local); + String getName(); } - public static class ErrorProfile{ + + public static class ErrorProfile { public int errno; public String msg; } - + private final IJsonAPI jsonAPI; - public JsonAPILoader(IJsonAPI jsonAPI){ + + public JsonAPILoader(IJsonAPI jsonAPI) { this.jsonAPI = jsonAPI; } @@ -62,61 +69,69 @@ public class JsonAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.IPr } // === IProfileLoader === - + @Override public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception { - String username=gameProfile.getName(); + String username = gameProfile.getName(); if (StringUtils.isEmpty(ssp.root)) { CustomSkinLoader.logger.info("Root not defined."); return null; } boolean local = HttpUtil0.isLocal(ssp.root); String jsonUrl = this.jsonAPI.toJsonUrl(ssp.root, username); + //Some API like MinecraftCapesAPI won't load profile at sometime + if (jsonUrl == null) { + CustomSkinLoader.logger.info("Profile url not found."); + return null; + } String json; - if(local){ + if (local) { File jsonFile = new File(CustomSkinLoader.DATA_DIR, jsonUrl); - if(!jsonFile.exists()){ + if (!jsonFile.exists()) { CustomSkinLoader.logger.info("Profile File not found."); return null; } - json=IOUtils.toString(new FileInputStream(jsonFile), "UTF-8"); - }else{ - HttpRequestUtil.HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(jsonUrl).setCacheTime(90).setUserAgent(ssp.userAgent).setPayload(this.jsonAPI.getPayload(ssp))); - json=responce.content; + json = IOUtils.toString(Files.newInputStream(jsonFile.toPath()), StandardCharsets.UTF_8); + } else { + HttpRequestUtil.HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(jsonUrl).setCacheTime(90).setUserAgent(ssp.userAgent).setPayload(this.jsonAPI.getPayload(ssp))); + json = responce.content; } - if(json==null||json.equals("")){ + if (json == null || json.equals("")) { CustomSkinLoader.logger.info("Profile not found."); return null; } - - ErrorProfile profile=CustomSkinLoader.GSON.fromJson(json, ErrorProfile.class); - if(profile.errno!=0){ - CustomSkinLoader.logger.info("Error "+profile.errno+": "+profile.msg); + + ErrorProfile profile = CustomSkinLoader.GSON.fromJson(json, ErrorProfile.class); + if (profile.errno != 0) { + CustomSkinLoader.logger.info("Error " + profile.errno + ": " + profile.msg); return null; } - - UserProfile p=this.jsonAPI.toUserProfile(ssp.root, json, local); - if(p==null||p.isEmpty()){ + + UserProfile p = this.jsonAPI.toUserProfile(ssp.root, json, local); + if (p == null || p.isEmpty()) { CustomSkinLoader.logger.info("Both skin and cape not found."); return null; - }else{ + } else { return p; } } + @Override public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) { return !StringUtils.isNoneEmpty(ssp0.root) || ssp0.root.equalsIgnoreCase(ssp1.root); } + @Override public String getName() { return this.jsonAPI.getName(); } - @SuppressWarnings("ResultOfMethodCallIgnored") + @Override public void init(SkinSiteProfile ssp) { - if(HttpUtil0.isLocal(ssp.root)){ - File f=new File(ssp.root); - if(!f.exists()) + if (HttpUtil0.isLocal(ssp.root)) { + File f = new File(ssp.root); + if (!f.exists()) + //noinspection ResultOfMethodCallIgnored f.mkdirs(); } } diff --git a/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java b/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java index a3594f9..bc95e7a 100644 --- a/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java +++ b/Common/source/customskinloader/loader/jsonapi/MinecraftCapesAPI.java @@ -49,8 +49,13 @@ public class MinecraftCapesAPI implements JsonAPILoader.IJsonAPI { @Override public String toJsonUrl(String root, String username) { + String uuid = MojangAPILoader.getMojangUuidByUsername(username); + //If uuid cannot be found, we won't load profile in this API. + if (uuid == null) { + return null; + } //API url is `${root}${uuid}` - return root + MojangAPILoader.getMojangUuidByUsername(username); + return root + uuid; } @Override