-
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.
修复MixinLanguage导致的报错(最新Fabric版本已不再需要此类)
Fix #96
- Loading branch information
Showing
2 changed files
with
66 additions
and
63 deletions.
There are no files selected for viewing
64 changes: 1 addition & 63 deletions
64
src/main/java/top/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 |
---|---|---|
@@ -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 <K, V> ImmutableMap<K, V> build(Builder<K, V> builder) { | ||
LinkedHashMap<String, String> map = new LinkedHashMap<>((ImmutableMap<String, String>) builder.build()); | ||
|
||
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 = 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<K, V>) ImmutableMap.builder().putAll(map).build(); | ||
} | ||
} | ||
//#endif |
65 changes: 65 additions & 0 deletions
65
versions/1.18.2/src/main/java/top/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 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<String, String> mapInjected(Map<String, String> originalMap) { | ||
LinkedHashMap<String, String> map = new LinkedHashMap<>(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 = 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 |