1919
2020package org .maxgamer .quickshop .util .mojangapi ;
2121
22+ import com .google .common .cache .Cache ;
2223import com .google .common .cache .CacheBuilder ;
23- import com .google .common .cache .LoadingCache ;
2424import com .google .gson .Gson ;
2525import com .google .gson .JsonElement ;
2626import com .google .gson .JsonParser ;
2929import lombok .SneakyThrows ;
3030import org .jetbrains .annotations .NotNull ;
3131import org .jetbrains .annotations .Nullable ;
32- import org .maxgamer .quickshop .util .HttpCacheLoader ;
32+ import org .maxgamer .quickshop .util .HttpUtil ;
3333import org .maxgamer .quickshop .util .JsonUtil ;
34+ import org .maxgamer .quickshop .util .Util ;
3435
35- import java .net .MalformedURLException ;
36- import java .net .URL ;
3736import java .util .Objects ;
3837import java .util .Optional ;
39- import java .util .concurrent .ExecutionException ;
4038import java .util .concurrent .TimeUnit ;
4139
4240public class MojangAPI {
@@ -77,29 +75,40 @@ public static class AssetsFileData {
7775
7876 @ Data
7977 public static class ResourcesAPI {
80- private final LoadingCache <URL , Optional <String >> request = CacheBuilder .newBuilder ()
81- .expireAfterAccess (10 , TimeUnit .MINUTES )
82- .build (new HttpCacheLoader ());
78+ protected final Cache <String , String > requestCachePool = CacheBuilder .newBuilder ()
79+ .expireAfterWrite (7 , TimeUnit .DAYS )
80+ .build ();
81+ // private final LoadingCache<URL, Optional<String>> request = CacheBuilder.newBuilder()
82+ // .expireAfterAccess(10, TimeUnit.MINUTES)
83+ // .build(new HttpCacheLoader());
8384 private final MojangApiMirror apiMirror ;
8485
8586 public ResourcesAPI (MojangApiMirror mirror ) {
8687 this .apiMirror = mirror ;
8788 }
8889
8990 public Optional <String > get (@ NotNull String hash ) {
90- try {
91- return request .get (new URL (apiMirror .getResourcesDownloadRoot () + "/" + hash .substring (0 , 2 ) + "/" + hash ));
92- } catch (ExecutionException | MalformedURLException e ) {
93- return Optional .empty ();
94- }
91+ String url = apiMirror .getResourcesDownloadRoot () + "/" + hash .substring (0 , 2 ) + "/" + hash ;
92+ return Optional .ofNullable (HttpUtil .createGet (url ));
93+ // return data;
94+
95+
96+ // try {
97+ // return request.get(new URL(apiMirror.getResourcesDownloadRoot() + "/" + hash.substring(0, 2) + "/" + hash));
98+ // } catch (ExecutionException | MalformedURLException e) {
99+ // return Optional.empty();
100+ // }
95101 }
96102 }
97103
98104
99105 public static class AssetsAPI {
100- private final LoadingCache <URL , Optional <String >> request = CacheBuilder .newBuilder ()
101- .expireAfterAccess (10 , TimeUnit .MINUTES )
102- .build (new HttpCacheLoader ());
106+ protected final Cache <String , String > requestCachePool = CacheBuilder .newBuilder ()
107+ .expireAfterWrite (7 , TimeUnit .DAYS )
108+ .build ();
109+ // private final LoadingCache<URL, Optional<String>> request = CacheBuilder.newBuilder()
110+ // .expireAfterAccess(10, TimeUnit.MINUTES)
111+ // .build(new HttpCacheLoader());
103112 private final MetaAPI metaAPI ;
104113
105114 AssetsAPI (@ NotNull MojangApiMirror apiMirror , @ NotNull String version ) {
@@ -124,13 +133,15 @@ public Optional<AssetsFileData> getGameAssetsFile() {
124133 if (assetIndexBean == null || assetIndexBean .getUrl () == null || assetIndexBean .getId () == null ) {
125134 return Optional .empty ();
126135 }
136+ String data = HttpUtil .createGet (assetIndexBean .getUrl ());
137+ return Optional .of (new AssetsFileData (data , assetIndexBean .getSha1 (), assetIndexBean .getId ()));
127138
128- try {
129- Optional <String > fileContent = request .get (new URL (assetIndexBean .getUrl ()));
130- return fileContent .map (s -> new AssetsFileData (s , assetIndexBean .getSha1 (), assetIndexBean .getId ()));
131- } catch (ExecutionException | MalformedURLException e ) {
132- return Optional .empty ();
133- }
139+ // try {
140+ // Optional<String> fileContent = request.get(new URL(assetIndexBean.getUrl()));
141+ // return fileContent.map(s -> new AssetsFileData(s, assetIndexBean.getSha1(), assetIndexBean.getId()));
142+ // } catch (ExecutionException | MalformedURLException e) {
143+ // return Optional.empty();
144+ // }
134145
135146 }
136147
@@ -154,6 +165,9 @@ private Optional<GameInfoAPI.DataBean> getAssetsJson() {
154165 public static class GameInfoAPI {
155166 private final String json ;
156167 private final Gson gson = JsonUtil .getGson ();
168+ protected final Cache <String , String > requestCachePool = CacheBuilder .newBuilder ()
169+ .expireAfterWrite (7 , TimeUnit .DAYS )
170+ .build ();
157171
158172 public GameInfoAPI (@ NotNull String json ) {
159173 this .json = json ;
@@ -195,16 +209,18 @@ public static class AssetIndexBean {
195209
196210 public static class MetaAPI {
197211 //Cache with URL and Content(String)
198- private final LoadingCache <URL , Optional <String >> request = CacheBuilder .newBuilder ()
199- .expireAfterAccess (10 , TimeUnit .MINUTES )
200- .build (new HttpCacheLoader ());
201- private final URL metaEndpoint ;
212+ // private final LoadingCache<URL, Optional<String>> request = CacheBuilder.newBuilder()
213+ // .expireAfterAccess(10, TimeUnit.MINUTES)
214+ // .build(new HttpCacheLoader());
215+ protected final Cache <String , String > requestCachePool = CacheBuilder .newBuilder ()
216+ .expireAfterWrite (7 , TimeUnit .DAYS )
217+ .build ();
218+ private final String metaEndpoint ;
202219 private final String version ;
203220
204- @ SneakyThrows
205221 public MetaAPI (@ NotNull MojangApiMirror mirror , @ NotNull String version ) {
206222 this .version = version ;
207- this .metaEndpoint = new URL ( mirror .getLauncherMetaRoot () + "/mc/game/version_manifest.json" ) ;
223+ this .metaEndpoint = mirror .getLauncherMetaRoot () + "/mc/game/version_manifest.json" ;
208224 }
209225
210226 /**
@@ -214,12 +230,18 @@ public MetaAPI(@NotNull MojangApiMirror mirror, @NotNull String version) {
214230 */
215231 @ SneakyThrows
216232 public Optional <String > get () {
217- Optional <String > result = request .get (this .metaEndpoint );
218- if (!result .isPresent ()) {
233+
234+ String result = HttpUtil .createGet (this .metaEndpoint );
235+ // Optional<String> result = request.get(this.metaEndpoint);
236+ // if (!result.isPresent()) {
237+ // return Optional.empty();
238+ // }
239+ if (result == null ) {
240+ Util .debugLog ("Request Meta Endpoint failed." );
219241 return Optional .empty ();
220242 }
221243 try {
222- JsonElement index = new JsonParser ().parse (result . get () );
244+ JsonElement index = new JsonParser ().parse (result );
223245 if (!index .isJsonObject ()) {
224246 return Optional .empty ();
225247 }
@@ -232,7 +254,7 @@ public Optional<String> get() {
232254 JsonElement gameId = gameVersionData .getAsJsonObject ().get ("id" );
233255 JsonElement gameIndexUrl = gameVersionData .getAsJsonObject ().get ("url" );
234256 if (Objects .equals (gameId .getAsString (), version )) {
235- return request . get ( new URL (gameIndexUrl .getAsString ()));
257+ return Optional . ofNullable ( HttpUtil . createGet (gameIndexUrl .getAsString ()));
236258 }
237259 }
238260 }
0 commit comments