1717import java .util .function .Predicate ;
1818
1919@ Mixin (NamespaceResourceManager .class )
20- public abstract class NamespaceResourceManagerMixin {
20+ public abstract class NamespaceResourceManagerMixin implements ResourceManager {
2121
2222 @ Inject (method = "findResources" , at = @ At ("RETURN" ))
2323 private void onFindResources (String startingPath , Predicate <Identifier > allowedPathPredicate ,
2424 CallbackInfoReturnable <Map <Identifier , Resource >> cir ) {
2525
2626 String currentLang = LangTextureCache .getCurrentLanguage ();
27+
2728 if ("en_us" .equals (currentLang )) {
2829 return ;
2930 }
3031
3132 Map <Identifier , Resource > originalResources = cir .getReturnValue ();
32- if (originalResources .isEmpty ()) return ;
33+ if (originalResources .isEmpty ()) {
34+ return ;
35+ }
3336
3437 Map <Identifier , Resource > langSpecificResources = new HashMap <>();
35- String textureKey = "textures/" ;
3638
3739 for (Map .Entry <Identifier , Resource > entry : originalResources .entrySet ()) {
3840 Identifier originalId = entry .getKey ();
41+ String originalPath = originalId .getPath ();
3942
40- if (!allowedPathPredicate .test (originalId )) {
41- continue ;
42- }
43+ String [] parts = originalPath .split ("/" , 2 );
4344
44- String path = originalId .getPath ();
45- int idx = path .indexOf (textureKey );
46- if (idx == -1 ) {
47- continue ; // 跳过不含 textures/ 的路径
45+ if (parts .length < 2 ) {
46+ continue ;
4847 }
4948
50- String before = path . substring ( 0 , idx + textureKey . length ()) ;
51- String after = path . substring ( idx + textureKey . length ()) ;
49+ String topLevelDir = parts [ 0 ] ;
50+ String subPath = parts [ 1 ] ;
5251
5352 // 避免重复,如zh_cn/zh_cn
54- if (after .startsWith (currentLang + "/" )) {
53+ if (subPath .startsWith (currentLang + "/" )) {
5554 continue ;
5655 }
5756
58- String langSpecificPath = before + currentLang + '/' + after ;
57+ String langSpecificPath = topLevelDir + "/" + currentLang + "/" + subPath ;
5958 Identifier langId = new Identifier (originalId .getNamespace (), langSpecificPath );
6059
6160 Boolean cache = LangTextureCache .get (langId );
6261 if (cache != null ) {
6362 if (cache ) {
64- try {
65- ((ResourceManager ) this ).getResource (langId ).ifPresent (resource -> {
66- langSpecificResources .put (originalId , resource );
67- TextureLocaleRedirector .LOGGER .info ("Using cached localized texture: {}" , langId );
68- });
69- } catch (Exception ignored ) {}
63+ this .getResource (langId ).ifPresent (resource -> {
64+ langSpecificResources .put (originalId , resource );
65+ TextureLocaleRedirector .LOGGER .info ("Using cached localized resource: {}" , langId );
66+ });
7067 }
7168 continue ;
7269 }
7370
74- try {
75- Optional <Resource > langResource = ((ResourceManager ) this ).getResource (langId );
76- if (langResource .isPresent ()) {
77- langSpecificResources .put (originalId , langResource .get ());
78- LangTextureCache .put (langId , true );
79- TextureLocaleRedirector .LOGGER .info ("Found and cached localized texture: {}" , langId );
80- } else {
81- LangTextureCache .put (langId , false );
82- }
83- } catch (Exception e ) {
71+ Optional <Resource > langResource = this .getResource (langId );
72+ if (langResource .isPresent ()) {
73+ langSpecificResources .put (originalId , langResource .get ());
74+ LangTextureCache .put (langId , true );
75+ TextureLocaleRedirector .LOGGER .info ("Found and cached localized resource: {}" , langId );
76+ } else {
8477 LangTextureCache .put (langId , false );
85- TextureLocaleRedirector .LOGGER .warn ("Failed to load localized texture: {}" , langId , e );
8678 }
8779 }
8880
8981 if (!langSpecificResources .isEmpty ()) {
9082 originalResources .putAll (langSpecificResources );
9183 }
9284 }
93- }
85+ }
0 commit comments