22
33import cam72cam .mod .ModCore ;
44import cam72cam .mod .event .platform .LoadDatapackEvent ;
5+ import com .mojang .logging .LogUtils ;
6+ import net .minecraft .FileUtil ;
57import net .minecraft .client .Minecraft ;
6- import net .minecraft .network .chat .TextComponent ;
8+ import net .minecraft .network .chat .Component ;
79import net .minecraft .resources .ResourceLocation ;
810import net .minecraft .server .packs .*;
911import net .minecraft .server .packs .metadata .MetadataSectionSerializer ;
1012import net .minecraft .server .packs .repository .Pack ;
11- import net .minecraft .server .packs .repository .PackCompatibility ;
1213import net .minecraft .server .packs .repository .PackSource ;
14+ import net .minecraft .server .packs .resources .IoSupplier ;
15+ import net .minecraft .world .flag .FeatureFlagSet ;
1316import net .minecraftforge .api .distmarker .Dist ;
1417import net .minecraftforge .api .distmarker .OnlyIn ;
1518import net .minecraftforge .eventbus .api .SubscribeEvent ;
2225import java .io .File ;
2326import java .io .IOException ;
2427import java .io .InputStream ;
28+ import java .nio .file .Files ;
29+ import java .nio .file .Path ;
2530import java .util .*;
2631import java .util .function .Function ;
27- import java .util .function .Predicate ;
2832import java .util .stream .Collectors ;
2933
3034/**
@@ -122,18 +126,18 @@ public static void loadClientResources() {
122126 packs .add (1 , pack );
123127 packs .add (pack );
124128
125- Minecraft .getInstance ().getResourcePackRepository ().addPackFinder ((consumer , packInfoFactory ) -> {
129+ Minecraft .getInstance ().getResourcePackRepository ().addPackFinder ((consumer ) -> {
126130 for (PackResources pack1 : packs ) {
127- consumer .accept (new Pack (pack1 .getName (),
128- true ,
129- () -> pack1 ,
130- new TextComponent ( "" ) ,
131- new TextComponent ( "" ),
132- PackCompatibility . COMPATIBLE ,
133- Pack .Position .TOP ,
134- true ,
135- PackSource .DEFAULT ,
136- true ));
131+ consumer .accept (Pack . create (pack1 .packId (),
132+ Component . literal ( "" ) ,
133+ true ,
134+ s -> pack1 ,
135+ new Pack . Info ( Component . literal ( "" ), 13 , FeatureFlagSet . of () ),
136+ PackType . CLIENT_RESOURCES ,
137+ Pack .Position .TOP ,
138+ true ,
139+ PackSource .DEFAULT
140+ ));
137141 }
138142 });
139143 }
@@ -186,39 +190,53 @@ public static void reload() {
186190 */
187191 private static class InternalResourcePack extends AbstractPackResources {
188192 public InternalResourcePack () {
189- super (ModList . get (). getModFileById ( "universalmodcore" ). getFile (). getFilePath (). toFile () );
193+ super ("UMC Generated Resources" , true );
190194 }
191195
192196 @ Override
193- public InputStream getResource (String resourcePath ) throws IOException {
194- if ("pack.mcmeta" .equals (resourcePath )) {
195- return new ByteArrayInputStream ("{}" .getBytes ());
197+ public @ org .jetbrains .annotations .Nullable IoSupplier <InputStream > getRootResource (String ... strs ) {
198+ String path = String .join ("/" , strs );
199+ if (hasResource (path )) {
200+ return () -> getResource (path );
196201 }
202+ return null ;
203+ }
197204
205+ @ Override
206+ public IoSupplier <InputStream > getResource (PackType type , ResourceLocation loc ) {
207+ String path = locationToName (type , loc );
208+ if (hasResource (path )) {
209+ return () -> getResource (path );
210+ }
211+ return null ;
212+ }
213+
214+ private InputStream getResource (String resourcePath ) {
198215 Identifier ident = nameToLocation (resourcePath );
199216
200- if (DIRECT_RESOURCES .containsKey (ident )) {
201- return new ByteArrayInputStream (DIRECT_RESOURCES .get (ident ));
202- }
217+ try {
218+ if (DIRECT_RESOURCES .containsKey (ident )) {
219+ return new ByteArrayInputStream (DIRECT_RESOURCES .get (ident ));
220+ }
203221
204- for (Map .Entry <String , String > entry : REDIRECTS .entrySet ()) {
205- String src = ident .toString ();
206- if (src .startsWith (entry .getKey ())) {
207- Identifier redirect = handleRedirect (ident , entry .getKey (), entry .getValue ());
208- return redirect .getResourceStream ();
222+ for (Map .Entry <String , String > entry : REDIRECTS .entrySet ()) {
223+ String src = ident .toString ();
224+ if (src .startsWith (entry .getKey ())) {
225+ Identifier redirect = handleRedirect (ident , entry .getKey (), entry .getValue ());
226+ return redirect .getResourceStream ();
227+ }
209228 }
210- }
211229
212- //It must already have been populated in hasResourceName if exists
213- if (CACHED_GENERATOR_RESULTS .containsKey (ident )) {
214- return new ByteArrayInputStream (CACHED_GENERATOR_RESULTS .get (ident ));
230+ //It must already have been populated in hasResourceName if exists
231+ if (CACHED_GENERATOR_RESULTS .containsKey (ident )) {
232+ return new ByteArrayInputStream (CACHED_GENERATOR_RESULTS .get (ident ));
233+ }
234+ } catch (Exception ignored ) {
215235 }
216-
217236 return null ;
218237 }
219238
220- @ Override
221- public boolean hasResource (String resourcePath ) {
239+ private boolean hasResource (String resourcePath ) {
222240 if (resourcePath .endsWith ("mcmeta" ) && !"pack.mcmeta" .equals (resourcePath )) {
223241 //We don't handle resource metadata
224242 return false ;
@@ -256,31 +274,22 @@ && handleRedirect(ident, entry.getKey(), entry.getValue()).canLoad()) {
256274 }
257275
258276 @ Override
259- public Collection < ResourceLocation > getResources (PackType type , String pathIn , String namespace , int maxDepth , Predicate < String > filter ) {
277+ public void listResources (PackType type , String pathIn , String namespace , PackResources . ResourceOutput output ) {
260278 //TODO list all redirect/conditional resources, may need new parameters in API?
261- List <ResourceLocation > result = new ArrayList <>();
262279 final String folder = pathIn + "/" ; // Ensure folders
263280 DIRECT_RESOURCES .forEach ((k , v ) -> {
264281 String path = k .getPath ();
265- if (k .getDomain ().equals (namespace ) && path .startsWith (folder ) && filter .test (path )) {
266- path = path .substring ((folder ).length ());
267- if (path .chars ().filter (ch -> ch == '/' ).count () < maxDepth ) {
268- result .add (k .internal );
269- }
282+ if (k .getDomain ().equals (namespace ) && path .startsWith (folder )) {
283+ output .accept (k .internal , () -> new ByteArrayInputStream (v ));
270284 }
271285 });
272286
273287 CACHED_GENERATOR_RESULTS .forEach ((k , v ) -> {
274288 String path = k .getPath ();
275- if (k .getDomain ().equals (namespace ) && path .startsWith (folder ) && filter .test (path )) {
276- path = path .substring ((folder ).length ());
277- if (path .chars ().filter (ch -> ch == '/' ).count () < maxDepth ) {
278- result .add (k .internal );
279- }
289+ if (k .getDomain ().equals (namespace ) && path .startsWith (folder )) {
290+ output .accept (k .internal , () -> new ByteArrayInputStream (v ));
280291 }
281292 });
282-
283- return result ;
284293 }
285294
286295 @ Override
@@ -290,11 +299,6 @@ public Set<String> getNamespaces(PackType type) {
290299 return collect ;
291300 }
292301
293- @ Override
294- public String getName () {
295- return "UMC Generated Resources" ;
296- }
297-
298302 @ Nullable
299303 @ Override
300304 public <T > T getMetadataSection (MetadataSectionSerializer <T > p_195760_1_ ) throws IOException {
@@ -312,11 +316,6 @@ public void close() {
312316 */
313317 @ OnlyIn (Dist .DEDICATED_SERVER )
314318 public static InputStream loadServerResource (Identifier ident ) throws IOException {
315- if (ident .getPath ().endsWith ("mcmeta" )) {
316- //We don't handle resource metadata
317- return null ;
318- }
319-
320319 if (DIRECT_RESOURCES .containsKey (ident )) {
321320 return new ByteArrayInputStream (DIRECT_RESOURCES .get (ident ));
322321 }
@@ -353,37 +352,49 @@ public static class InternalDataPack extends AbstractPackResources {
353352 static Map <ResourceLocation , byte []> data = new HashMap <>();
354353
355354 public InternalDataPack () {
356- super (ModList .get ().getModFileById ("universalmodcore" ).getFile ().getFilePath ().toFile ());
355+ super ("UMC Generated Resources" , true );
356+ }
357+
358+ @ Override
359+ public @ org .jetbrains .annotations .Nullable IoSupplier <InputStream > getRootResource (String ... strs ) {
360+ String path = String .join ("/" , strs );
361+ if (hasResource (path )) {
362+ return () -> getResource (path );
363+ }
364+ return null ;
357365 }
358366
359367 @ Override
360- public InputStream getResource (String resourcePath ) throws IOException {
368+ public IoSupplier <InputStream > getResource (PackType type , ResourceLocation loc ) {
369+ String path = locationToName (type , loc );
370+ if (hasResource (path )) {
371+ return () -> getResource (path );
372+ }
373+ return null ;
374+ }
375+
376+ private InputStream getResource (String resourcePath ) throws IOException {
361377 if ("pack.mcmeta" .equals (resourcePath )) {
362378 return new ByteArrayInputStream ("{}" .getBytes ());
363379 }
364380
365381 return new ByteArrayInputStream (data .get (nameToLocation (resourcePath ).internal ));
366382 }
367383
368- @ Override
369- public boolean hasResource (String resourcePath ) {
384+ private boolean hasResource (String resourcePath ) {
370385 return "pack.mcmeta" .equals (resourcePath ) || data .containsKey (nameToLocation (resourcePath ).internal );
371386 }
372387
373388 @ Override
374- public Collection < ResourceLocation > getResources (PackType type , String pathIn , String namespace , int maxDepth , Predicate < String > filter ) {
389+ public void listResources (PackType type , String pathIn , String namespace , PackResources . ResourceOutput output ) {
375390 List <ResourceLocation > result = new ArrayList <>();
376391 final String folder = pathIn + "/" ; // Ensure folders
377392 data .keySet ().forEach ((k ) -> {
378393 String path = k .getPath ();
379- if (k .getNamespace ().equals (namespace ) && path .startsWith (folder ) && filter .test (path )) {
380- path = path .substring ((folder ).length ());
381- if (path .chars ().filter (ch -> ch == '/' ).count () < maxDepth ) {
382- result .add (k );
383- }
394+ if (k .getNamespace ().equals (namespace ) && path .startsWith (folder ) && data .containsKey (k )) {
395+ output .accept (k , () -> new ByteArrayInputStream (data .get (k )));
384396 }
385397 });
386- return result ;
387398 }
388399
389400 @ Override
@@ -393,46 +404,49 @@ public Set<String> getNamespaces(PackType type) {
393404 return collect ;
394405 }
395406
396- @ Override
397- public String getName () {
398- return "UMC Generated Data" ;
399- }
400-
401407 @ Override
402408 public void close () {
403409
404410 }
405411 }
406412
407- private static class UMCFolderPack extends FolderPackResources {
413+ private static class UMCFolderPack extends PathPackResources {
414+ private final Path root ;
415+
408416 public UMCFolderPack (File folder ) {
409- super (folder );
417+ super (folder .getName (), folder .toPath (), false );
418+ this .root = folder .toPath ();
410419 }
411420
412421 @ Override
413- public InputStream getResource (String name ) throws IOException {
414- InputStream stream = super .getResource (name );
415- File file = this .getFile (name );
416- return new Identifier .InputStreamMod (stream , file .lastModified ());
422+ public IoSupplier <InputStream > getResource (PackType p_249352_ , ResourceLocation p_251715_ ) {
423+ Path path = this .root .resolve (p_249352_ .getDirectory ()).resolve (p_251715_ .getNamespace ());
424+ return getResource (p_251715_ , path );
417425 }
418426
419- @ Override
420- public boolean hasResource (String resourcePath ) {
421- return super .hasResource (resourcePath );
427+ public static IoSupplier <InputStream > getResource (ResourceLocation p_250145_ , Path p_251046_ ) {
428+ return FileUtil .decomposePath (p_250145_ .getPath ()).get ().map ((p_251647_ ) -> {
429+ Path path = FileUtil .resolvePath (p_251046_ , p_251647_ );
430+ return Files .exists (path ) ? new Identifier .IoInputStreamMod (() -> Files .newInputStream (path ), path .toFile ().lastModified ()) : null ;
431+ }, (p_248714_ ) -> {
432+ LogUtils .getLogger ().error ("Invalid path {}: {}" , p_250145_ , p_248714_ .message ());
433+ return null ;
434+ });
422435 }
423436 }
424437
425438 private static class UMCFilePack extends FilePackResources {
426439 private final File path ;
427440
428441 public UMCFilePack (File fileIn ) {
429- super (fileIn );
442+ super (fileIn . getName (), fileIn , false );
430443 this .path = fileIn ;
431444 }
432445
433446 @ Override
434- public InputStream getResource (String name ) throws IOException {
435- return new Identifier .InputStreamMod (super .getResource (name ), path .lastModified ());
447+ public IoSupplier <InputStream > getResource (PackType p_249605_ , ResourceLocation p_252147_ ) {
448+ IoSupplier <InputStream > found = super .getResource (p_249605_ , p_252147_ );
449+ return found != null ? new Identifier .IoInputStreamMod (found , path .lastModified ()) : null ;
436450 }
437451 }
438452
@@ -442,6 +456,11 @@ private static Identifier handleRedirect(Identifier src, String requestedPrefix,
442456 return new Identifier (actualPrefix + suffix );
443457 }
444458
459+ private static String locationToName (PackType type , ResourceLocation path ) {
460+ String pattern = type == PackType .CLIENT_RESOURCES ? "assets/%s/%s" : "data/%s/%s" ;
461+ return String .format (pattern , path .getNamespace (), path .getPath ());
462+ }
463+
445464 private static Identifier nameToLocation (String path ) {
446465 if (path .startsWith ("assets/" )) {
447466 //assets/[domain]/[path] -> domain:path
0 commit comments