From 8a4697b75bc4bfcaf51091bbb840e12c7cb60efb Mon Sep 17 00:00:00 2001 From: Xujiayao Date: Sat, 10 Dec 2022 16:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DMixinLanguage=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E6=8A=A5=E9=94=99=EF=BC=88=E6=9C=80=E6=96=B0?= =?UTF-8?q?Fabric=E7=89=88=E6=9C=AC=E5=B7=B2=E4=B8=8D=E5=86=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=AD=A4=E7=B1=BB=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #96 --- .../minecraft/mixins/MixinLanguage.java | 64 +----------------- .../minecraft/mixins/MixinLanguage.java | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 63 deletions(-) create mode 100644 versions/1.18.2/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java diff --git a/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java b/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java index 6027f73c..1e35c788 100644 --- a/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java +++ b/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java @@ -1,75 +1,13 @@ -//#if MC >= 11600 +//#if MC >= 11900 package top.xujiayao.mcdiscordchat.minecraft.mixins; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.util.JsonHelper; import net.minecraft.util.Language; -//#if MC >= 11800 -import org.slf4j.Logger; -//#else -//$$ import org.apache.logging.log4j.Logger; -//#endif -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.Redirect; - -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.regex.Pattern; /** * @author Xujiayao */ @Mixin(Language.class) public abstract class MixinLanguage { - - @Final - @Shadow - private static Logger LOGGER; - - @Final - @Shadow - private static Gson GSON; - - @Final - @Shadow - private static Pattern TOKEN_PATTERN; - - @SuppressWarnings({"unchecked", "mapping"}) - @Redirect(method = "create", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;build()Lcom/google/common/collect/ImmutableMap;")) - private static ImmutableMap build(Builder builder) { - LinkedHashMap map = new LinkedHashMap<>((ImmutableMap) builder.build()); - - FabricLoader.getInstance().getAllMods().forEach(modContainer -> { - Optional optional = modContainer.findPath("/assets/" + modContainer.getMetadata().getId() + "/lang/en_us.json"); - - if (optional.isPresent()) { - try (InputStream inputStream = Files.newInputStream(optional.get())) { - JsonObject json = GSON.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class); - for (Map.Entry entry : json.entrySet()) { - String string = TOKEN_PATTERN.matcher(JsonHelper.asString(entry.getValue(), entry.getKey())).replaceAll("%$1s"); - map.put(entry.getKey(), string); - } - } catch (Exception e) { - LOGGER.error("Couldn't read strings from /assets/{}", modContainer.getMetadata().getId() + "/lang/en_us.json", e); - } - } - }); - - return (ImmutableMap) ImmutableMap.builder().putAll(map).build(); - } } //#endif \ No newline at end of file diff --git a/versions/1.18.2/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java b/versions/1.18.2/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java new file mode 100644 index 00000000..322e4fc7 --- /dev/null +++ b/versions/1.18.2/src/main/java/top/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.java @@ -0,0 +1,65 @@ +//#if MC >= 11600 +package top.xujiayao.mcdiscordchat.minecraft.mixins; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.util.JsonHelper; +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.ModifyVariable; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +import static top.xujiayao.mcdiscordchat.Main.LOGGER; + +/** + * @author Xujiayao + */ +@Mixin(Language.class) +public abstract class MixinLanguage { + + @Final + @Shadow + private static Gson GSON; + + @Final + @Shadow + private static Pattern TOKEN_PATTERN; + + @ModifyVariable(method = "create", at = @At("STORE"), ordinal = 0) + private static Map mapInjected(Map originalMap) { + LinkedHashMap map = new LinkedHashMap<>(originalMap); + + FabricLoader.getInstance().getAllMods().forEach(modContainer -> { + Optional optional = modContainer.findPath("/assets/" + modContainer.getMetadata().getId() + "/lang/en_us.json"); + + if (optional.isPresent()) { + try (InputStream inputStream = Files.newInputStream(optional.get())) { + JsonObject json = GSON.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class); + for (Map.Entry entry : json.entrySet()) { + String string = TOKEN_PATTERN.matcher(JsonHelper.asString(entry.getValue(), entry.getKey())).replaceAll("%$1s"); + map.put(entry.getKey(), string); + } + } catch (Exception e) { + LOGGER.error("Couldn't read strings from /assets/{}", modContainer.getMetadata().getId() + "/lang/en_us.json", e); + } + } + }); + + return map; + } +} +//#endif \ No newline at end of file