Skip to content

Commit 598ca10

Browse files
committed
Big Rewrite 1.5.0
1 parent e6ecc96 commit 598ca10

File tree

12 files changed

+64
-171
lines changed

12 files changed

+64
-171
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ ResourcePackName/
5353
└── diamond_sword.png # Replaced diamond sword texture
5454
```
5555

56-
## FTB Quests Support
57-
58-
This mod also provides optional support for [FTB Quests](https://github.com/FTBTeam/FTB-Quests).
59-
When FTB Quests is installed, images in the quest interface (`ChapterImage`) will
60-
also switch to the corresponding localized textures based on the current language.
61-
62-
* Any images or icons used in quests can be replaced via resource packs.
63-
* If no localized texture is found, it will fall back to the default texture.
64-
* Implemented via a **Mixin plugin**, so this mod does not have a hard dependency on FTB Quests.
65-
6656
## Performance
6757

6858
This mod is heavily optimized and should have negligible performance impact:

README_CN.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ Texture Locale Redirector 为 Minecraft 资源包提供了原生的多语言纹
5454
└── diamond_sword.png # 替换的钻石剑纹理
5555
```
5656

57-
## FTB Quests 支持
58-
59-
本模组同时对 [FTB Quests](https://github.com/FTBTeam/FTB-Quests) 提供了可选支持。
60-
当 FTB Quests 存在时,任务界面中的图片(`ChapterImage`)同样会根据当前语言切换到对应的本地化纹理。
61-
62-
* 任务中使用到的所有的图片和图标都可以在资源包中指定替换
63-
* 如果未找到对应语言的贴图,则会回退到默认的贴图
64-
* 通过 **Mixin 插件**实现,模组本体不会强制依赖 FTB Quests
65-
6657
## 性能
6758

6859
本模组进行了大量的优化,对性能不应造成明显影响

build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ allprojects {
3636
options.release = 21
3737
}
3838

39-
repositories {
40-
maven { url = "https://maven.fallenbreath.me/releases" }
41-
maven { url = "https://maven.ftb.dev/releases" }
42-
}
43-
4439
java {
4540
withSourcesJar()
4641
}

common/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@ dependencies {
66
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
77
// Do NOT use other classes from fabric loader
88
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
9-
modImplementation "dev.ftb.mods:ftb-quests:${rootProject.ftb_quests_version}"
10-
11-
modImplementation "me.fallenbreath:conditional-mixin-common:${rootProject.conditional_mixin_version}"
129
}

common/src/main/java/com/wulian/texturelocaleredirector/LangTextureCache.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,25 @@ public static void setCurrentLanguage(String lang) {
3131
public static String getCurrentLanguage() {
3232
return currentLanguage;
3333
}
34-
}
34+
35+
public static Identifier getLocalizedId(Identifier originalId) {
36+
String lang = currentLanguage;
37+
38+
String originalPath = originalId.getPath();
39+
40+
String texturePrefix = "textures/";
41+
int index = originalPath.indexOf(texturePrefix);
42+
if (index == -1) return null;
43+
44+
String before = originalPath.substring(0, index + texturePrefix.length());
45+
String after = originalPath.substring(index + texturePrefix.length());
46+
47+
// Avoid duplicate redirection textures/zh_cn/zh_cn/...
48+
if (after.startsWith(lang + "/")) {
49+
return null;
50+
}
51+
52+
String localizedPath = before + lang + "/" + after;
53+
return Identifier.of(originalId.getNamespace(), localizedPath);
54+
}
55+
}

common/src/main/java/com/wulian/texturelocaleredirector/TLRMixinPlugin.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

common/src/main/java/com/wulian/texturelocaleredirector/mixin/NamespaceResourceManagerMixin.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.resource.ResourceManager;
88
import net.minecraft.util.Identifier;
99
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Unique;
1011
import org.spongepowered.asm.mixin.injection.At;
1112
import org.spongepowered.asm.mixin.injection.Inject;
1213
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@@ -17,15 +18,13 @@
1718
import 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
}

common/src/main/java/com/wulian/texturelocaleredirector/mixin/ftbquests/ChapterImageMixin.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"required": true,
33
"package": "com.wulian.texturelocaleredirector.mixin",
4-
"plugin": "com.wulian.texturelocaleredirector.TLRMixinPlugin",
54
"compatibilityLevel": "JAVA_21",
65
"minVersion": "0.8",
76
"injectors": {
@@ -11,8 +10,5 @@
1110
"LanguageManagerMixin",
1211
"NamespaceResourceManagerMixin",
1312
"ReloadableResourceManagerImplMixin"
14-
],
15-
"mixins": [
16-
"ftbquests.ChapterImageMixin"
1713
]
1814
}

fabric/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ configurations {
2525
dependencies {
2626
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
2727
modApi "net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_api_version}"
28-
modApi "dev.ftb.mods:ftb-quests-fabric:${rootProject.ftb_quests_version}"
29-
30-
modImplementation(include("me.fallenbreath:conditional-mixin-fabric:${rootProject.conditional_mixin_version}"))
3128

3229
common(project(path: ":common", configuration: "namedElements")) {
3330
transitive = false

0 commit comments

Comments
 (0)