Skip to content

Commit ed75a5f

Browse files
committed
Big Rewrite 1.5.0
1 parent 8227532 commit ed75a5f

File tree

11 files changed

+64
-153
lines changed

11 files changed

+64
-153
lines changed

build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ allprojects {
3030
version = rootProject.mod_version
3131
group = rootProject.maven_group
3232

33-
repositories {
34-
maven { url "https://maven.ftb.dev/releases" }
35-
}
36-
3733
tasks.withType(JavaCompile).configureEach {
3834
options.encoding = "UTF-8"
3935
options.release = 17

common/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ architectury {
22
common(rootProject.enabled_platforms.split(","))
33
}
44

5-
repositories {
6-
maven { url = "https://maven.ftb.dev/releases" }
7-
}
8-
95
dependencies {
106
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
117
// Do NOT use other classes from fabric loader
128
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
13-
modImplementation "dev.ftb.mods:ftb-quests:${rootProject.ftb_quests_version}"
149
}

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 & 42 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 = new Identifier(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 & 51 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_17",
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +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}"
2928

3029
common(project(path: ":common", configuration: "namedElements")) {
3130
transitive = false

forge/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ loom {
2929
}
3030
}
3131

32-
repositories {
33-
maven { url = "https://maven.ftb.dev/releases" }
34-
}
35-
3632
dependencies {
3733
forge "net.minecraftforge:forge:${rootProject.minecraft_version}-${rootProject.forge_version}"
38-
modApi "dev.ftb.mods:ftb-quests-forge:${rootProject.ftb_quests_version}"
3934

4035
common(project(path: ":common", configuration: "namedElements")) {
4136
transitive = false

gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ neoforge_version=20.4.251
1515
fabric_loader_version=0.18.1
1616
fabric_api_version=0.97.3+1.20.4
1717

18-
ftb_quests_version=2004.2.3

0 commit comments

Comments
 (0)