77import net .minecraft .resource .ResourceManager ;
88import net .minecraft .util .Identifier ;
99import org .spongepowered .asm .mixin .Mixin ;
10+ import org .spongepowered .asm .mixin .Unique ;
1011import org .spongepowered .asm .mixin .injection .At ;
1112import org .spongepowered .asm .mixin .injection .Inject ;
1213import org .spongepowered .asm .mixin .injection .callback .CallbackInfoReturnable ;
1718import java .util .function .Predicate ;
1819
1920@ Mixin (NamespaceResourceManager .class )
20- public abstract class NamespaceResourceManagerMixin implements ResourceManager {
21+ public abstract class NamespaceResourceManagerMixin implements ResourceManager {
2122
2223 @ Inject (method = "findResources" , at = @ At ("RETURN" ))
2324 private void onFindResources (String startingPath , Predicate <Identifier > allowedPathPredicate ,
2425 CallbackInfoReturnable <Map <Identifier , Resource >> cir ) {
2526
26- String currentLang = LangTextureCache .getCurrentLanguage ();
27-
28- if ("en_us" .equals (currentLang )) {
27+ if ("en_us" .equals (LangTextureCache .getCurrentLanguage ())) {
2928 return ;
3029 }
3130
@@ -38,48 +37,56 @@ private void onFindResources(String startingPath, Predicate<Identifier> allowedP
3837
3938 for (Map .Entry <Identifier , Resource > entry : originalResources .entrySet ()) {
4039 Identifier originalId = entry .getKey ();
41- String originalPath = originalId .getPath ();
42-
43- String [] parts = originalPath .split ("/" , 2 );
40+ Identifier langId = LangTextureCache .getLocalizedId (originalId );
4441
45- if (parts . length < 2 ) {
42+ if (langId == null ) {
4643 continue ;
4744 }
4845
49- String topLevelDir = parts [0 ];
50- String subPath = parts [1 ];
46+ Optional <Resource > langResource = this .checkResourceAndCache (langId , originalId );
47+ langResource .ifPresent (resource -> langSpecificResources .put (originalId , resource ));
48+ }
5149
52- // 避免重复,如zh_cn/zh_cn
53- if ( subPath . startsWith ( currentLang + "/" )) {
54- continue ;
55- }
50+ if (! langSpecificResources . isEmpty ()) {
51+ originalResources . putAll ( langSpecificResources );
52+ }
53+ }
5654
57- String langSpecificPath = topLevelDir + "/" + currentLang + "/" + subPath ;
58- Identifier langId = Identifier .of (originalId .getNamespace (), langSpecificPath );
59-
60- Boolean cache = LangTextureCache .get (langId );
61- if (cache != null ) {
62- if (cache ) {
63- this .getResource (langId ).ifPresent (resource -> {
64- langSpecificResources .put (originalId , resource );
65- TextureLocaleRedirector .LOGGER .info ("Using cached localized resource: {}" , langId );
66- });
67- }
68- continue ;
69- }
55+ @ Inject (method = "getResource" , at = @ At ("HEAD" ), cancellable = true )
56+ private void onGetResource (Identifier id , CallbackInfoReturnable <Optional <Resource >> cir ) {
7057
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 );
58+ Identifier langId = LangTextureCache .getLocalizedId (id );
59+ if (langId == null ) {
60+ return ;
61+ }
62+
63+ Optional <Resource > langResource = this .checkResourceAndCache (langId , id );
64+ if (langResource .isPresent ()) {
65+ cir .setReturnValue (langResource );
66+ }
67+ }
68+
69+ @ Unique
70+ public Optional <Resource > checkResourceAndCache (Identifier langId , Identifier originalId ) {
71+ Boolean cache = LangTextureCache .get (langId );
72+
73+ if (cache != null ) {
74+ if (cache ) {
75+ return this .getResource (langId );
7676 } else {
77- LangTextureCache . put ( langId , false );
77+ return Optional . empty ( );
7878 }
7979 }
8080
81- if (!langSpecificResources .isEmpty ()) {
82- originalResources .putAll (langSpecificResources );
81+ Optional <Resource > langResource = this .getResource (langId );
82+
83+ if (langResource .isPresent ()) {
84+ LangTextureCache .put (langId , true );
85+ TextureLocaleRedirector .LOGGER .info ("Redirected resource {} -> {}" , originalId , langId );
86+ return langResource ;
87+ } else {
88+ LangTextureCache .put (langId , false );
89+ return Optional .empty ();
8390 }
8491 }
8592}
0 commit comments