1+ package com .wulian .texturelocaleredirector .mixin ;
2+
3+ import com .wulian .texturelocaleredirector .LanguageTextureManager ;
4+ import net .minecraft .client .MinecraftClient ;
5+ import net .minecraft .resource .NamespaceResourceManager ;
6+ import net .minecraft .resource .Resource ;
7+ import net .minecraft .resource .ResourceManager ;
8+ import net .minecraft .util .Identifier ;
9+ import org .spongepowered .asm .mixin .Mixin ;
10+ import org .spongepowered .asm .mixin .injection .At ;
11+ import org .spongepowered .asm .mixin .injection .Inject ;
12+ import org .spongepowered .asm .mixin .injection .callback .CallbackInfoReturnable ;
13+
14+ import java .util .HashMap ;
15+ import java .util .Map ;
16+ import java .util .function .Predicate ;
17+
18+ @ Mixin (NamespaceResourceManager .class )
19+ public abstract class NamespaceResourceManagerMixin implements ResourceManager {
20+ @ Inject (method = "findResources" , at = @ At ("RETURN" ))
21+ private void onFindResources (String startingPath , Predicate <Identifier > allowedPathPredicate , CallbackInfoReturnable <Map <Identifier , Resource >> cir ) {
22+ if (LanguageTextureManager .isReplacingAtlasTextures ()) {
23+ return ;
24+ }
25+
26+ String currentLang = MinecraftClient .getInstance ().getLanguageManager ().getLanguage ();
27+ if ("en_us" .equals (currentLang )) {
28+ return ;
29+ }
30+
31+ Map <Identifier , Resource > originalMap = cir .getReturnValue ();
32+ Map <Identifier , Resource > replacements = new HashMap <>();
33+
34+ for (Identifier originalId : originalMap .keySet ()) {
35+ Identifier langId = LanguageTextureManager .getLanguageSpecificIdForAtlas (originalId , currentLang );
36+
37+ if (langId != null ) {
38+ LanguageTextureManager .setReplacingAtlasTextures (true );
39+ try {
40+ this .getResource (langId ).ifPresent (langResource -> {
41+ replacements .put (originalId , langResource );
42+ });
43+ } finally {
44+ LanguageTextureManager .setReplacingAtlasTextures (false );
45+ }
46+ }
47+ }
48+
49+ if (!replacements .isEmpty ()) {
50+ originalMap .putAll (replacements );
51+ }
52+ }
53+ }
0 commit comments