generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: structure part & rich i18n (#101)
Signed-off-by: Liyan Zhao <return65535@qq.com>
- Loading branch information
Showing
43 changed files
with
1,461 additions
and
1,087 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/zly2006/reden/access/TranslationStorageAccess.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.zly2006.reden.access | ||
|
||
import net.minecraft.text.Text | ||
|
||
interface TranslationStorageAccess { | ||
|
||
@Suppress("INAPPLICABLE_JVM_NAME") | ||
@get:JvmName("getTextMap\$reden") | ||
val textMap: Map<String, Text> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/main/java/com/github/zly2006/reden/mixin/debugger/MixinWorldTickScheduler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/github/zly2006/reden/mixin/richTranslation/MixinTranslatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.zly2006.reden.mixin.richTranslation; | ||
|
||
import com.github.zly2006.reden.utils.richTranslation.RichTranslationKt; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import net.minecraft.text.StringVisitable; | ||
import net.minecraft.text.TranslatableTextContent; | ||
import net.minecraft.util.Language; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
// overwrite owo | ||
@Mixin(value = TranslatableTextContent.class, priority = 10) | ||
public class MixinTranslatable { | ||
@Shadow | ||
@Final | ||
private String key; | ||
|
||
@Shadow | ||
@Final | ||
private Object[] args; | ||
|
||
@Shadow | ||
private List<StringVisitable> translations; | ||
|
||
@Inject( | ||
method = "updateTranslations", | ||
at = @At( | ||
value = "FIELD", | ||
target = "Lnet/minecraft/text/TranslatableTextContent;languageCache:Lnet/minecraft/util/Language;", | ||
ordinal = 1, | ||
shift = At.Shift.AFTER | ||
), | ||
cancellable = true | ||
) | ||
private void translate(CallbackInfo ci, @Local Language language) { | ||
var text = RichTranslationKt.processTranslate(language, key, args); | ||
if (text != null) { | ||
translations = Collections.singletonList(text); | ||
ci.cancel(); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/github/zly2006/reden/mixin/richTranslation/MixinTranslationStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.zly2006.reden.mixin.richTranslation; | ||
|
||
import com.github.zly2006.reden.access.TranslationStorageAccess; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import net.minecraft.client.resource.language.TranslationStorage; | ||
import net.minecraft.resource.ResourceManager; | ||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Mixin(TranslationStorage.class) | ||
public class MixinTranslationStorage implements com.github.zly2006.reden.access.TranslationStorageAccess { | ||
@Unique | ||
private final Map<String, Text> textMap = new HashMap<>(); | ||
@Unique | ||
private static final Map<String, Text> tempTextMap = new HashMap<>(); | ||
|
||
@Override | ||
public @NotNull Map<String, Text> getTextMap$reden() { | ||
return textMap; | ||
} | ||
|
||
@Inject( | ||
method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/client/resource/language/TranslationStorage;load(Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V" | ||
) | ||
) | ||
private static void loadCustomText(ResourceManager resourceManager, List<String> definitions, boolean rightToLeft, CallbackInfoReturnable<TranslationStorage> cir, @Local Identifier identifier) { | ||
Gson gson = new Gson(); | ||
resourceManager.getAllResources(identifier).forEach(resource -> { | ||
try { | ||
var jo = gson.fromJson(new InputStreamReader(resource.getInputStream()), JsonObject.class); | ||
jo.entrySet().stream().filter(it -> it.getValue() instanceof JsonArray).forEach(it -> { | ||
MutableText text = Text.Serialization.fromJsonTree(it.getValue()); | ||
tempTextMap.put(it.getKey(), text); | ||
}); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Inject( | ||
method = "load(Lnet/minecraft/resource/ResourceManager;Ljava/util/List;Z)Lnet/minecraft/client/resource/language/TranslationStorage;", | ||
at = @At("TAIL") | ||
) | ||
private static void finish(ResourceManager resourceManager, List<String> definitions, boolean rightToLeft, CallbackInfoReturnable<TranslationStorage> cir) { | ||
((TranslationStorageAccess) cir.getReturnValue()).getTextMap$reden().putAll(tempTextMap); | ||
tempTextMap.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.