Skip to content

Commit

Permalink
MixinLanguage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Feb 10, 2024
1 parent 3fd5fb3 commit 979f18f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ plugins {
}

preprocess {
def mc1152 = createNode("compat_1.15.2", 1_15_02, "mojang") // TODO Check MixinLanguage
def mc1152 = createNode("compat_1.15.2", 1_15_02, "mojang")
def mc1165 = createNode("compat_1.16.5", 1_16_05, "mojang")
def mc1171 = createNode("compat_1.17.1", 1_17_01, "mojang")
def mc1182 = createNode("compat_1.18.2", 1_18_02, "mojang") // TODO Check MixinLanguage
def mc1182 = createNode("compat_1.18.2", 1_18_02, "mojang")
def mc1190 = createNode("compat_1.19" , 1_19_00, "mojang")
def mc1192 = createNode("1.19.2", 1_19_02, "mojang")
def mc1193 = createNode("1.19.3", 1_19_03, "mojang")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//#if MC >= 11900
package com.xujiayao.mcdiscordchat.minecraft.mixins;

import net.minecraft.locale.Language;
import org.spongepowered.asm.mixin.Mixin;

/**
* @author Xujiayao
*/
@Mixin(Language.class)
public class MixinLanguage {
}
//#endif
1 change: 1 addition & 0 deletions src/main/resources/mcdiscordchat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "com.xujiayao.mcdiscordchat.minecraft.mixins",
"compatibilityLevel": "JAVA_17",
"mixins": [
"MixinLanguage",
"MixinPlayerAdvancements",
"MixinPlayerList",
"MixinServerGamePacketListenerImpl",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.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.locale.Language;
import net.minecraft.util.GsonHelper;
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.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static com.xujiayao.mcdiscordchat.Main.LOGGER;

/**
* @author Xujiayao
*/
@Mixin(Language.class)
public class MixinLanguage {

@Final
@Shadow
private static Pattern UNSUPPORTED_FORMAT_PATTERN;

@Final
@Shadow
private Map<String, String> storage;

@Inject(method = "<init>", at = @At(value = "RETURN"))
private void Language(CallbackInfo ci) {
FabricLoader.getInstance().getAllMods().forEach(modContainer -> {
Optional<Path> optional = modContainer.findPath("/assets/" + modContainer.getMetadata().getId() + "/lang/en_us.json");

if (optional.isPresent()) {
try (InputStream inputStream = Files.newInputStream(optional.get())) {
JsonObject json = new Gson().fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
String string = UNSUPPORTED_FORMAT_PATTERN.matcher(GsonHelper.convertToString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
storage.put(entry.getKey(), string);
}
} catch (Exception e) {
LOGGER.error("Couldn't read strings from /assets/{}", modContainer.getMetadata().getId() + "/lang/en_us.json", e);
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//#if MC >= 11600
package com.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.locale.Language;
import net.minecraft.util.GsonHelper;
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.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static com.xujiayao.mcdiscordchat.Main.LOGGER;

/**
* @author Xujiayao
*/
@Mixin(Language.class)
public class MixinLanguage {

@Final
@Shadow
private static Gson GSON;

@Final
@Shadow
private static Pattern UNSUPPORTED_FORMAT_PATTERN;

@ModifyVariable(method = "loadDefault()Lnet/minecraft/locale/Language;", at = @At("STORE"), ordinal = 0)
private static Map<String, String> mapInjected(Map<String, String> originalMap) {
Map<String, String> map = new HashMap<>(originalMap);

FabricLoader.getInstance().getAllMods().forEach(modContainer -> {
Optional<Path> 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<String, JsonElement> entry : json.entrySet()) {
String string = UNSUPPORTED_FORMAT_PATTERN.matcher(GsonHelper.convertToString(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

0 comments on commit 979f18f

Please sign in to comment.