22
33import com .google .common .hash .HashFunction ;
44import com .google .common .hash .Hashing ;
5+ import com .mojang .realmsclient .Unit ;
56import com .mojang .util .UndashedUuid ;
67import net .kyrptonaught .jankson .Jankson ;
78import net .kyrptonaught .lemclienthelper .LEMClientHelperMod ;
8- import net .minecraft .GameVersion ;
99import net .minecraft .SharedConstants ;
10- import net .minecraft .client . MinecraftClient ;
11- import net .minecraft .client .realms . SizeUnit ;
12- import net .minecraft .client .session . Session ;
13- import net .minecraft .client .toast .SystemToast ;
14- import net .minecraft .resource . ResourceType ;
15- import net .minecraft .text . Text ;
16- import net .minecraft .util . NetworkUtils ;
17- import net .minecraft .util .Util ;
18- import net .minecraft .util . path . CacheFiles ;
10+ import net .minecraft .WorldVersion ;
11+ import net .minecraft .client .Minecraft ;
12+ import net .minecraft .client .User ;
13+ import net .minecraft .client .gui . components . toasts .SystemToast ;
14+ import net .minecraft .network . chat . Component ;
15+ import net .minecraft .server . packs . DownloadCacheCleaner ;
16+ import net .minecraft .server . packs . PackType ;
17+ import net .minecraft .util .HttpUtil ;
18+ import net .minecraft .Util ;
1919
2020import java .io .InputStream ;
2121import java .net .Proxy ;
@@ -61,18 +61,18 @@ public static void getPackList() {
6161 }
6262
6363 public static void downloadPacks () {
64- Path downloadsDirectory = MinecraftClient .getInstance ().runDirectory .toPath ().resolve ("downloads" );
64+ Path downloadsDirectory = Minecraft .getInstance ().gameDirectory .toPath ().resolve ("downloads" );
6565 HashFunction SHA1 = Hashing .sha1 ();
66- Map <String , String > headers = getHeaders (MinecraftClient .getInstance ().getSession ());
67- Proxy proxy = MinecraftClient .getInstance ().getNetworkProxy ();
66+ Map <String , String > headers = getHeaders (Minecraft .getInstance ().getUser ());
67+ Proxy proxy = Minecraft .getInstance ().getProxy ();
6868
6969 AtomicInteger counter = new AtomicInteger (allPacks .packs .size ());
7070 for (AllPacks .RPOption rpOption : allPacks .packs ) {
7171 if (!checkPack (rpOption .uuid )) {
72- Util .getDownloadWorkerExecutor ().execute (() -> downloadPack (rpOption , downloadsDirectory , SHA1 , headers , proxy , () -> {
72+ Util .nonCriticalIoPool ().execute (() -> downloadPack (rpOption , downloadsDirectory , SHA1 , headers , proxy , () -> {
7373 counter .getAndDecrement ();
7474 if (counter .get () <= 0 && getConfig ().toastComplete ) {
75- SystemToast .add (MinecraftClient .getInstance ().getToastManager (), SystemToast .Type .PERIODIC_NOTIFICATION , Text .translatable ("key.lemclienthelper.alldownloadcomplete" ), null );
75+ SystemToast .add (Minecraft .getInstance ().getToasts (), SystemToast .SystemToastId .PERIODIC_NOTIFICATION , Component .translatable ("key.lemclienthelper.alldownloadcomplete" ), null );
7676 }
7777 }));
7878 }
@@ -81,29 +81,29 @@ public static void downloadPacks() {
8181
8282 public static void downloadPack (AllPacks .RPOption rpOption , Path downloadsDirectory , HashFunction SHA1 , Map <String , String > headers , Proxy proxy , Runnable onComplete ) {
8383 try {
84- NetworkUtils . download (downloadsDirectory .resolve (rpOption .uuid .toString ()), new URL (rpOption .url ), headers , SHA1 , null , 0xFA00000 , proxy , createListener (rpOption , onComplete ));
84+ HttpUtil . downloadFile (downloadsDirectory .resolve (rpOption .uuid .toString ()), new URL (rpOption .url ), headers , SHA1 , null , 0xFA00000 , proxy , createListener (rpOption , onComplete ));
8585 } catch (Exception e ) {
8686 e .printStackTrace ();
87- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloaderror" ), null );
87+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloaderror" ), null );
8888 }
8989 }
9090
9191 public static void deletePacks () {
92- CacheFiles . clear ( MinecraftClient .getInstance ().runDirectory .toPath ().resolve ("downloads" ), 0 );
92+ DownloadCacheCleaner . vacuumCacheDir ( Minecraft .getInstance ().gameDirectory .toPath ().resolve ("downloads" ), 0 );
9393 }
9494
9595 private static boolean checkPack (UUID packID ) {
96- Path downloadsDirectory = MinecraftClient .getInstance ().runDirectory .toPath ().resolve ("downloads" );
96+ Path downloadsDirectory = Minecraft .getInstance ().gameDirectory .toPath ().resolve ("downloads" );
9797 if (Files .exists (downloadsDirectory .resolve (packID .toString ()))) {
98- setStatus (packID , Text .translatable ("key.lemclienthelper.alreadydownloaded" ), null );
98+ setStatus (packID , Component .translatable ("key.lemclienthelper.alreadydownloaded" ), null );
9999 return true ;
100100 }
101101
102102 setStatus (packID , null , null );
103103 return false ;
104104 }
105105
106- public static void setStatus (UUID packID , Text status , Text status2 ) {
106+ public static void setStatus (UUID packID , Component status , Component status2 ) {
107107 for (AllPacks .RPOption rpOption : allPacks .packs ) {
108108 if (rpOption .uuid == packID ) {
109109 rpOption .status = status ;
@@ -112,41 +112,41 @@ public static void setStatus(UUID packID, Text status, Text status2) {
112112 }
113113 }
114114
115- private static Map <String , String > getHeaders (Session session ) {
116- GameVersion gameVersion = SharedConstants .getGameVersion ();
117- return Map .of ("X-Minecraft-Username" , session .getUsername (), "X-Minecraft-UUID" , UndashedUuid .toString (session .getUuidOrNull ()), "X-Minecraft-Version" , gameVersion .getName (), "X-Minecraft-Version-ID" , gameVersion .getId (), "X-Minecraft-Pack-Format" , String .valueOf (gameVersion .getResourceVersion ( ResourceType .CLIENT_RESOURCES )), "User-Agent" , "Minecraft Java/" + gameVersion .getName ());
115+ private static Map <String , String > getHeaders (User session ) {
116+ WorldVersion gameVersion = SharedConstants .getCurrentVersion ();
117+ return Map .of ("X-Minecraft-Username" , session .getName (), "X-Minecraft-UUID" , UndashedUuid .toString (session .getProfileId ()), "X-Minecraft-Version" , gameVersion .getName (), "X-Minecraft-Version-ID" , gameVersion .getId (), "X-Minecraft-Pack-Format" , String .valueOf (gameVersion .getPackVersion ( PackType .CLIENT_RESOURCES )), "User-Agent" , "Minecraft Java/" + gameVersion .getName ());
118118 }
119119
120- private static NetworkUtils . DownloadListener createListener (AllPacks .RPOption rpOption , Runnable onComplete ) {
121- return new NetworkUtils . DownloadListener () {
120+ private static HttpUtil . DownloadProgressListener createListener (AllPacks .RPOption rpOption , Runnable onComplete ) {
121+ return new HttpUtil . DownloadProgressListener () {
122122 private OptionalLong contentLength = OptionalLong .empty ();
123123
124- private Text getProgress (long writtenBytes ) {
125- return this .contentLength .isPresent () ? Text .translatable ("download.pack.progress.percent" , writtenBytes * 100L / this .contentLength .getAsLong ()) : Text .translatable ("download.pack.progress.bytes" , SizeUnit . getUserFriendlyString (writtenBytes ));
124+ private Component getProgress (long writtenBytes ) {
125+ return this .contentLength .isPresent () ? Component .translatable ("download.pack.progress.percent" , writtenBytes * 100L / this .contentLength .getAsLong ()) : Component .translatable ("download.pack.progress.bytes" , Unit . humanReadable (writtenBytes ));
126126 }
127127
128128 @ Override
129- public void onStart () {
130- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloading" ), null );
129+ public void requestStart () {
130+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloading" ), null );
131131 }
132132
133133 @ Override
134- public void onContentLength (OptionalLong contentLength ) {
134+ public void downloadStart (OptionalLong contentLength ) {
135135 this .contentLength = contentLength ;
136- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloading" ), getProgress (0L ));
136+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloading" ), getProgress (0L ));
137137 }
138138
139139 @ Override
140- public void onProgress (long writtenBytes ) {
141- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloading" ), getProgress (writtenBytes ));
140+ public void downloadedBytes (long writtenBytes ) {
141+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloading" ), getProgress (writtenBytes ));
142142 }
143143
144144 @ Override
145- public void onFinish (boolean success ) {
145+ public void requestFinished (boolean success ) {
146146 if (!success ) {
147- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloaderror" ), null );
147+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloaderror" ), null );
148148 } else {
149- setStatus (rpOption .uuid , Text .translatable ("key.lemclienthelper.downloadcomplete" ), null );
149+ setStatus (rpOption .uuid , Component .translatable ("key.lemclienthelper.downloadcomplete" ), null );
150150 }
151151 onComplete .run ();
152152 }
0 commit comments