-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.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,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 |
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
59 changes: 59 additions & 0 deletions
59
...ompat_1.15.2/src/main/java/com/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.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,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); | ||
} | ||
} | ||
}); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ompat_1.18.2/src/main/java/com/xujiayao/mcdiscordchat/minecraft/mixins/MixinLanguage.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,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 |